File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.4: download - view: text, annotated - select for diffs
Fri Jun 1 12:15:23 2012 UTC (11 years, 11 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11.
  - Backport 1.162.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.4 2012/06/01 12:15:23 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 affliated 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 (either quotas 
   90: or requestcourses).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course.
   98: 
   99: Outputs: 1
  100: 
  101: $datatable  - HTML containing form elements which allow settings to be changed. 
  102: 
  103: In the case of course requests, radio buttons are displayed for each institutional
  104: affiliate type (and also default, and _LC_adv) for each of the course types 
  105: (official, unofficial and community).  In each case the radio buttons allow the 
  106: selection of one of four values:
  107: 
  108: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  109: which have the following effects:
  110: 
  111: 0
  112: 
  113: =over
  114: 
  115: - course requests are not allowed for this course types/affiliation
  116: 
  117: =back
  118: 
  119: approval 
  120: 
  121: =over 
  122: 
  123: - course requests must be approved by a Doman Coordinator in the 
  124: course's domain
  125: 
  126: =back
  127: 
  128: validate 
  129: 
  130: =over
  131: 
  132: - an institutional validation (e.g., check requestor is instructor
  133: of record) needs to be passed before the course will be created.  The required
  134: validation is in localenroll.pm on the primary library server for the course 
  135: domain.
  136: 
  137: =back
  138: 
  139: autolimit 
  140: 
  141: =over
  142:  
  143: - course requests will be processed automatically up to a limit of
  144: N requests for the course type for the particular requestor.
  145: If N is undefined, there is no limit to the number of course requests
  146: which a course owner may submit and have processed automatically. 
  147: 
  148: =back
  149: 
  150: =item modify_quotas() 
  151: 
  152: =back
  153: 
  154: =cut
  155: 
  156: package Apache::domainprefs;
  157: 
  158: use strict;
  159: use Apache::Constants qw(:common :http);
  160: use Apache::lonnet;
  161: use Apache::loncommon();
  162: use Apache::lonhtmlcommon();
  163: use Apache::lonlocal;
  164: use Apache::lonmsg();
  165: use Apache::lonconfigsettings;
  166: use LONCAPA qw(:DEFAULT :match);
  167: use LONCAPA::Enrollment;
  168: use LONCAPA::lonauthcgi();
  169: use File::Copy;
  170: use Locale::Language;
  171: use DateTime::TimeZone;
  172: use DateTime::Locale;
  173: 
  174: my $registered_cleanup;
  175: my $modified_urls;
  176: 
  177: sub handler {
  178:     my $r=shift;
  179:     if ($r->header_only) {
  180:         &Apache::loncommon::content_type($r,'text/html');
  181:         $r->send_http_header;
  182:         return OK;
  183:     }
  184: 
  185:     my $context = 'domain';
  186:     my $dom = $env{'request.role.domain'};
  187:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  188:     if (&Apache::lonnet::allowed('mau',$dom)) {
  189:         &Apache::loncommon::content_type($r,'text/html');
  190:         $r->send_http_header;
  191:     } else {
  192:         $env{'user.error.msg'}=
  193:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  194:         return HTTP_NOT_ACCEPTABLE;
  195:     }
  196: 
  197:     $registered_cleanup=0;
  198:     @{$modified_urls}=();
  199: 
  200:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  201:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  202:                                             ['phase','actions']);
  203:     my $phase = 'pickactions';
  204:     if ( exists($env{'form.phase'}) ) {
  205:         $phase = $env{'form.phase'};
  206:     }
  207:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  208:     my %domconfig =
  209:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  210:                 'quotas','autoenroll','autoupdate','autocreate',
  211:                 'directorysrch','usercreation','usermodification',
  212:                 'contacts','defaults','scantron','coursecategories',
  213:                 'serverstatuses','requestcourses','usersessions',
  214:                 'loadbalancing'],$dom);
  215:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  216:                        'autoupdate','autocreate','directorysrch','contacts',
  217:                        'usercreation','usermodification','scantron',
  218:                        'requestcourses','coursecategories','serverstatuses',
  219:                        'usersessions');
  220:     if (keys(%servers) > 1) {
  221:         push(@prefs_order,'loadbalancing');
  222:     }
  223:     my %prefs = (
  224:         'rolecolors' =>
  225:                    { text => 'Default color schemes',
  226:                      help => 'Domain_Configuration_Color_Schemes',
  227:                      header => [{col1 => 'Student Settings',
  228:                                  col2 => '',},
  229:                                 {col1 => 'Coordinator Settings',
  230:                                  col2 => '',},
  231:                                 {col1 => 'Author Settings',
  232:                                  col2 => '',},
  233:                                 {col1 => 'Administrator Settings',
  234:                                  col2 => '',}],
  235:                     },
  236:         'login' =>
  237:                     { text => 'Log-in page options',
  238:                       help => 'Domain_Configuration_Login_Page',
  239:                       header => [{col1 => 'Item',
  240:                                   col2 => '',}],
  241:                     },
  242: 
  243:         'defaults' => 
  244:                     { text => 'Default authentication/language/timezone/portal',
  245:                       help => 'Domain_Configuration_LangTZAuth',
  246:                       header => [{col1 => 'Setting',
  247:                                   col2 => 'Value'}],
  248:                     },
  249:         'quotas' => 
  250:                     { text => 'Blogs, personal web pages, webDAV, portfolios',
  251:                       help => 'Domain_Configuration_Quotas',
  252:                       header => [{col1 => 'User affiliation',
  253:                                   col2 => 'Available tools',
  254:                                   col3 => 'Portfolio quota',}],
  255:                     },
  256:         'autoenroll' =>
  257:                    { text => 'Auto-enrollment settings',
  258:                      help => 'Domain_Configuration_Auto_Enrollment',
  259:                      header => [{col1 => 'Configuration setting',
  260:                                  col2 => 'Value(s)'}],
  261:                    },
  262:         'autoupdate' => 
  263:                    { text => 'Auto-update settings',
  264:                      help => 'Domain_Configuration_Auto_Updates',
  265:                      header => [{col1 => 'Setting',
  266:                                  col2 => 'Value',},
  267:                                 {col1 => 'Setting',
  268:                                  col2 => 'Affiliation'},
  269:                                 {col1 => 'User population',
  270:                                  col2 => 'Updateable user data'}],
  271:                   },
  272:         'autocreate' => 
  273:                   { text => 'Auto-course creation settings',
  274:                      help => 'Domain_Configuration_Auto_Creation',
  275:                      header => [{col1 => 'Configuration Setting',
  276:                                  col2 => 'Value',}],
  277:                   },
  278:         'directorysrch' => 
  279:                   { text => 'Institutional directory searches',
  280:                     help => 'Domain_Configuration_InstDirectory_Search',
  281:                     header => [{col1 => 'Setting',
  282:                                 col2 => 'Value',}],
  283:                   },
  284:         'contacts' =>
  285:                   { text => 'Contact Information',
  286:                     help => 'Domain_Configuration_Contact_Info',
  287:                     header => [{col1 => 'Setting',
  288:                                 col2 => 'Value',}],
  289:                   },
  290: 
  291:         'usercreation' => 
  292:                   { text => 'User creation',
  293:                     help => 'Domain_Configuration_User_Creation',
  294:                     header => [{col1 => 'Format rule type',
  295:                                 col2 => 'Format rules in force'},
  296:                                {col1 => 'User account creation',
  297:                                 col2 => 'Usernames which may be created',},
  298:                                {col1 => 'Context',
  299:                                 col2 => 'Assignable authentication types'}],
  300:                   },
  301:         'usermodification' =>
  302:                   { text => 'User modification',
  303:                     help => 'Domain_Configuration_User_Modification',
  304:                     header => [{col1 => 'Target user has role',
  305:                                 col2 => 'User information updateable in author context'},
  306:                                {col1 => 'Target user has role',
  307:                                 col2 => 'User information updateable in course context'},
  308:                                {col1 => "Status of user",
  309:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  310:                   },
  311:         'scantron' =>
  312:                   { text => 'Bubblesheet format file',
  313:                     help => 'Domain_Configuration_Scantron_Format',
  314:                     header => [ {col1 => 'Item',
  315:                                  col2 => '',
  316:                               }],
  317:                   },
  318:         'requestcourses' => 
  319:                  {text => 'Request creation of courses',
  320:                   help => 'Domain_Configuration_Request_Courses',
  321:                   header => [{col1 => 'User affiliation',
  322:                               col2 => 'Availability/Processing of requests',},
  323:                              {col1 => 'Setting',
  324:                               col2 => 'Value'}],
  325:                  },
  326:         'coursecategories' =>
  327:                   { text => 'Cataloging of courses/communities',
  328:                     help => 'Domain_Configuration_Cataloging_Courses',
  329:                     header => [{col1 => 'Category settings',
  330:                                 col2 => '',},
  331:                                {col1 => 'Categories',
  332:                                 col2 => '',
  333:                                }],
  334:                   },
  335:         'serverstatuses' =>
  336:                  {text   => 'Access to server status pages',
  337:                   help   => 'Domain_Configuration_Server_Status',
  338:                   header => [{col1 => 'Status Page',
  339:                               col2 => 'Other named users',
  340:                               col3 => 'Specific IPs',
  341:                             }],
  342:                  },
  343:         'usersessions' =>
  344:                  {text  => 'User session hosting/offloading',
  345:                   help  => 'Domain_Configuration_User_Sessions',
  346:                   header => [{col1 => 'Domain server',
  347:                               col2 => 'Servers to offload sessions to when busy'},
  348:                              {col1 => 'Hosting of users from other domains',
  349:                               col2 => 'Rules'},
  350:                              {col1 => "Hosting domain's own users elsewhere",
  351:                               col2 => 'Rules'}],
  352:                  },
  353:          'loadbalancing' =>
  354:                  {text  => 'Dedicated Load Balancer',
  355:                   help  => 'Domain_Configuration_Load_Balancing',
  356:                   header => [{col1 => 'Server',
  357:                               col2 => 'Default destinations',
  358:                               col3 => 'User affliation',
  359:                               col4 => 'Overrides'},
  360:                             ],
  361:                  },
  362:     );
  363:     if (keys(%servers) > 1) {
  364:         $prefs{'login'}  = { text   => 'Log-in page options',
  365:                              help   => 'Domain_Configuration_Login_Page',
  366:                             header => [{col1 => 'Log-in Service',
  367:                                         col2 => 'Server Setting',},
  368:                                        {col1 => 'Log-in Page Items',
  369:                                         col2 => ''}],
  370:                            };
  371:     }
  372:     my @roles = ('student','coordinator','author','admin');
  373:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  374:     &Apache::lonhtmlcommon::add_breadcrumb
  375:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  376:       text=>"Settings to display/modify"});
  377:     my $confname = $dom.'-domainconfig';
  378:     if ($phase eq 'process') {
  379:         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
  380:     } elsif ($phase eq 'display') {
  381:         my $js;
  382:         if (keys(%servers) > 1) {
  383:             my ($othertitle,$usertypes,$types) =
  384:                 &Apache::loncommon::sorted_inst_types($dom);
  385:             $js = &lonbalance_targets_js($dom,$types,\%servers).
  386:                   &new_spares_js().
  387:                   &common_domprefs_js().
  388:                   &Apache::loncommon::javascript_array_indexof();
  389:         }
  390:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  391:     } else {
  392:         if (keys(%domconfig) == 0) {
  393:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  394:             my @ids=&Apache::lonnet::current_machine_ids();
  395:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  396:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  397:                 my @loginimages = ('img','logo','domlogo','login');
  398:                 my $custom_img_count = 0;
  399:                 foreach my $img (@loginimages) {
  400:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  401:                         $custom_img_count ++;
  402:                     }
  403:                 }
  404:                 foreach my $role (@roles) {
  405:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  406:                         $custom_img_count ++;
  407:                     }
  408:                 }
  409:                 if ($custom_img_count > 0) {
  410:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  411:                     my $switch_server = &check_switchserver($dom,$confname);
  412:                     $r->print(
  413:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  414:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  415:     &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 />'.
  416:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  417:                     if ($switch_server) {
  418:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  419:                     }
  420:                     $r->print(&Apache::loncommon::end_page());
  421:                     return OK;
  422:                 }
  423:             }
  424:         }
  425:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  426:     }
  427:     return OK;
  428: }
  429: 
  430: sub process_changes {
  431:     my ($r,$dom,$confname,$action,$roles,$values) = @_;
  432:     my %domconfig;
  433:     if (ref($values) eq 'HASH') {
  434:         %domconfig = %{$values};
  435:     }
  436:     my $output;
  437:     if ($action eq 'login') {
  438:         $output = &modify_login($r,$dom,$confname,%domconfig);
  439:     } elsif ($action eq 'rolecolors') {
  440:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  441:                                      %domconfig);
  442:     } elsif ($action eq 'quotas') {
  443:         $output = &modify_quotas($dom,$action,%domconfig);
  444:     } elsif ($action eq 'autoenroll') {
  445:         $output = &modify_autoenroll($dom,%domconfig);
  446:     } elsif ($action eq 'autoupdate') {
  447:         $output = &modify_autoupdate($dom,%domconfig);
  448:     } elsif ($action eq 'autocreate') {
  449:         $output = &modify_autocreate($dom,%domconfig);
  450:     } elsif ($action eq 'directorysrch') {
  451:         $output = &modify_directorysrch($dom,%domconfig);
  452:     } elsif ($action eq 'usercreation') {
  453:         $output = &modify_usercreation($dom,%domconfig);
  454:     } elsif ($action eq 'usermodification') {
  455:         $output = &modify_usermodification($dom,%domconfig);
  456:     } elsif ($action eq 'contacts') {
  457:         $output = &modify_contacts($dom,%domconfig);
  458:     } elsif ($action eq 'defaults') {
  459:         $output = &modify_defaults($dom,$r);
  460:     } elsif ($action eq 'scantron') {
  461:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  462:     } elsif ($action eq 'coursecategories') {
  463:         $output = &modify_coursecategories($dom,%domconfig);
  464:     } elsif ($action eq 'serverstatuses') {
  465:         $output = &modify_serverstatuses($dom,%domconfig);
  466:     } elsif ($action eq 'requestcourses') {
  467:         $output = &modify_quotas($dom,$action,%domconfig);
  468:     } elsif ($action eq 'usersessions') {
  469:         $output = &modify_usersessions($dom,%domconfig);
  470:     } elsif ($action eq 'loadbalancing') {
  471:         $output = &modify_loadbalancing($dom,%domconfig);
  472:     }
  473:     return $output;
  474: }
  475: 
  476: sub print_config_box {
  477:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  478:     my $rowtotal = 0;
  479:     my $output;
  480:     if ($action eq 'coursecategories') {
  481:         $output = &coursecategories_javascript($settings);
  482:     }
  483:     $output .= 
  484:          '<table class="LC_nested_outer">
  485:           <tr>
  486:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  487:            &mt($item->{text}).'&nbsp;'.
  488:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  489:           '</tr>';
  490:     $rowtotal ++;
  491:     my $numheaders = 1;
  492:     if (ref($item->{'header'}) eq 'ARRAY') {
  493:         $numheaders = scalar(@{$item->{'header'}});
  494:     }
  495:     if ($numheaders > 1) {
  496:         my $colspan = '';
  497:         my $rightcolspan = '';
  498:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') || ($action eq 'helpsettings')) {
  499:             $colspan = ' colspan="2"';
  500:         }
  501:         if ($action eq 'usersessions') {
  502:             $rightcolspan = ' colspan="3"'; 
  503:         }
  504:         $output .= '
  505:           <tr>
  506:            <td>
  507:             <table class="LC_nested">
  508:              <tr class="LC_info_row">
  509:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  510:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  511:              </tr>';
  512:         $rowtotal ++;
  513:         if ($action eq 'autoupdate') {
  514:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  515:         } elsif ($action eq 'usercreation') {
  516:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  517:         } elsif ($action eq 'usermodification') {
  518:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  519:         } elsif ($action eq 'coursecategories') {
  520:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  521:         } elsif ($action eq 'login') {
  522:             $output .= &print_login('top',$dom,$confname,$phase,$settings,\$rowtotal);
  523:             $colspan = ' colspan="2"';
  524:         } elsif ($action eq 'requestcourses') {
  525:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  526:         } elsif ($action eq 'usersessions') {
  527:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  528:         } elsif ($action eq 'rolecolors') {
  529:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  530:         }
  531:         $output .= '
  532:            </table>
  533:           </td>
  534:          </tr>
  535:          <tr>
  536:            <td>
  537:             <table class="LC_nested">
  538:              <tr class="LC_info_row">
  539:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  540:         $output .= '
  541:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  542:              </tr>';
  543:             $rowtotal ++;
  544:         if ($action eq 'autoupdate') {
  545:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  546:            </table>
  547:           </td>
  548:          </tr>
  549:          <tr>
  550:            <td>
  551:             <table class="LC_nested">
  552:              <tr class="LC_info_row">
  553:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  554:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  555:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  556:             $rowtotal ++;
  557:         } elsif ($action eq 'usercreation') {
  558:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  559:            </table>
  560:           </td>
  561:          </tr>
  562:          <tr>
  563:            <td>
  564:             <table class="LC_nested">
  565:              <tr class="LC_info_row">
  566:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  567:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  568:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  569:             $rowtotal ++;
  570:         } elsif ($action eq 'usermodification') {
  571:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  572:            </table>
  573:           </td>
  574:          </tr>
  575:          <tr>
  576:            <td>
  577:             <table class="LC_nested">
  578:              <tr class="LC_info_row">
  579:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  580:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  581:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  582:             $rowtotal ++;
  583:         } elsif ($action eq 'coursecategories') {
  584:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  585:         } elsif ($action eq 'login') {
  586:             $output .= &print_login('bottom',$dom,$confname,$phase,$settings,\$rowtotal);
  587:         } elsif ($action eq 'requestcourses') {
  588:             $output .= &print_courserequestmail($dom,$settings,\$rowtotal);
  589:         } elsif ($action eq 'helpsettings') {
  590:             $output .= &print_helpsettings('bottom',$dom,$confname,$settings,\$rowtotal);
  591:         } elsif ($action eq 'usersessions') {
  592:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  593:            </table>
  594:           </td>
  595:          </tr>
  596:          <tr>
  597:            <td>
  598:             <table class="LC_nested">
  599:              <tr class="LC_info_row">
  600:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  601:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  602:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  603:             $rowtotal ++;
  604:         } elsif ($action eq 'coursedefaults') {
  605:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  606:         } elsif ($action eq 'rolecolors') {
  607:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  608:            </table>
  609:           </td>
  610:          </tr>
  611:          <tr>
  612:            <td>
  613:             <table class="LC_nested">
  614:              <tr class="LC_info_row">
  615:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  616:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  617:               <td class="LC_right_item" valign="top">'.
  618:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  619:              </tr>'.
  620:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  621:            </table>
  622:           </td>
  623:          </tr>
  624:          <tr>
  625:            <td>
  626:             <table class="LC_nested">
  627:              <tr class="LC_info_row">
  628:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  629:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  630:              </tr>'.
  631:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  632:             $rowtotal += 2;
  633:         }
  634:     } else {
  635:         $output .= '
  636:           <tr>
  637:            <td>
  638:             <table class="LC_nested">
  639:              <tr class="LC_info_row">';
  640:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  641:             $output .= '  
  642:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  643:         } elsif ($action eq 'serverstatuses') {
  644:             $output .= '
  645:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  646:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  647: 
  648:         } else {
  649:             $output .= '
  650:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  651:         }
  652:         if (defined($item->{'header'}->[0]->{'col3'})) {
  653:             $output .= '<td class="LC_left_item" valign="top">'.
  654:                        &mt($item->{'header'}->[0]->{'col2'});
  655:             if ($action eq 'serverstatuses') {
  656:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  657:             } 
  658:         } else {
  659:             $output .= '<td class="LC_right_item" valign="top">'.
  660:                        &mt($item->{'header'}->[0]->{'col2'});
  661:         }
  662:         $output .= '</td>';
  663:         if ($item->{'header'}->[0]->{'col3'}) {
  664:             if (defined($item->{'header'}->[0]->{'col4'})) {
  665:                 $output .= '<td class="LC_left_item" valign="top">'.
  666:                             &mt($item->{'header'}->[0]->{'col3'});
  667:             } else {
  668:                 $output .= '<td class="LC_right_item" valign="top">'.
  669:                            &mt($item->{'header'}->[0]->{'col3'});
  670:             }
  671:             if ($action eq 'serverstatuses') {
  672:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  673:             }
  674:             $output .= '</td>';
  675:         }
  676:         if ($item->{'header'}->[0]->{'col4'}) {
  677:             $output .= '<td class="LC_right_item" valign="top">'.
  678:                        &mt($item->{'header'}->[0]->{'col4'});
  679:         }
  680:         $output .= '</tr>';
  681:         $rowtotal ++;
  682:         if ($action eq 'login') {
  683:             $output .= &print_login('bottom',$dom,$confname,$phase,$settings,
  684:                                     \$rowtotal);
  685:         } elsif ($action eq 'quotas') {
  686:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  687:         } elsif ($action eq 'autoenroll') {
  688:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  689:         } elsif ($action eq 'autocreate') {
  690:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  691:         } elsif ($action eq 'directorysrch') {
  692:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  693:         } elsif ($action eq 'contacts') {
  694:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  695:         } elsif ($action eq 'defaults') {
  696:             $output .= &print_defaults($dom,\$rowtotal);
  697:         } elsif ($action eq 'scantron') {
  698:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  699:         } elsif ($action eq 'serverstatuses') {
  700:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  701:         } elsif ($action eq 'helpsettings') {
  702:             $output .= &print_helpsettings('top',$dom,$confname,$settings,\$rowtotal);
  703:         } elsif ($action eq 'loadbalancing') {
  704:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  705:         }
  706:     }
  707:     $output .= '
  708:    </table>
  709:   </td>
  710:  </tr>
  711: </table><br />';
  712:     return ($output,$rowtotal);
  713: }
  714: 
  715: sub print_login {
  716:     my ($position,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  717:     my ($css_class,$datatable);
  718:     my %choices = &login_choices();
  719: 
  720:     if ($position eq 'top') {
  721:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  722:         my $choice = $choices{'disallowlogin'};
  723:         $css_class = ' class="LC_odd_row"';
  724:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  725:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  726:                       '<th>'.$choices{'server'}.'</th>'.
  727:                       '<th>'.$choices{'serverpath'}.'</th>'.
  728:                       '<th>'.$choices{'custompath'}.'</th>'.
  729:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  730:         my %disallowed;
  731:         if (ref($settings) eq 'HASH') {
  732:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  733:                %disallowed = %{$settings->{'loginvia'}};
  734:             }
  735:         }
  736:         foreach my $lonhost (sort(keys(%servers))) {
  737:             my $direct = 'selected="selected"';
  738:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  739:                 if ($disallowed{$lonhost}{'server'} ne '') {
  740:                     $direct = '';
  741:                 }
  742:             }
  743:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  744:                           '<td><select name="'.$lonhost.'_server">'.
  745:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  746:                           '</option>';
  747:             foreach my $hostid (keys(%servers)) {
  748:                 next if ($servers{$hostid} eq $servers{$lonhost});
  749:                 my $selected = '';
  750:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  751:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  752:                         $selected = 'selected="selected"';
  753:                     }
  754:                 }
  755:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  756:                               $servers{$hostid}.'</option>';
  757:             }
  758:             $datatable .= '</select></td>'.
  759:                           '<td><select name="'.$lonhost.'_serverpath">';
  760:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  761:                 my $pathname = $path;
  762:                 if ($path eq 'custom') {
  763:                     $pathname = &mt('Custom Path').' ->';
  764:                 }
  765:                 my $selected = '';
  766:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  767:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  768:                         $selected = 'selected="selected"';
  769:                     }
  770:                 } elsif ($path eq '') {
  771:                     $selected = 'selected="selected"';
  772:                 }
  773:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  774:             }
  775:             $datatable .= '</select></td>';
  776:             my ($custom,$exempt);
  777:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  778:                 $custom = $disallowed{$lonhost}{'custompath'};
  779:                 $exempt = $disallowed{$lonhost}{'exempt'};
  780:             }
  781:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  782:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  783:                           '</tr>';
  784:         }
  785:         $datatable .= '</table></td></tr>';
  786:         return $datatable;
  787:     }
  788: 
  789:     my %defaultchecked = ( 
  790:                            'coursecatalog' => 'on',
  791:                            'adminmail'     => 'off',
  792:                            'newuser'       => 'off',
  793:                          );
  794:     my @toggles = ('coursecatalog','adminmail','newuser');
  795:     my (%checkedon,%checkedoff);
  796:     foreach my $item (@toggles) {
  797:         if ($defaultchecked{$item} eq 'on') { 
  798:             $checkedon{$item} = ' checked="checked" ';
  799:             $checkedoff{$item} = ' ';
  800:         } elsif ($defaultchecked{$item} eq 'off') {
  801:             $checkedoff{$item} = ' checked="checked" ';
  802:             $checkedon{$item} = ' ';
  803:         }
  804:     }
  805:     my @images = ('img','logo','domlogo','login');
  806:     my @logintext = ('textcol','bgcol');
  807:     my @bgs = ('pgbg','mainbg','sidebg');
  808:     my @links = ('link','alink','vlink');
  809:     my %designhash = &Apache::loncommon::get_domainconf($dom);
  810:     my %defaultdesign = %Apache::loncommon::defaultdesign;
  811:     my (%is_custom,%designs);
  812:     my %defaults = (
  813:                    font => $defaultdesign{'login.font'},
  814:                    );
  815:     foreach my $item (@images) {
  816:         $defaults{$item} = $defaultdesign{'login.'.$item};
  817:         $defaults{'showlogo'}{$item} = 1;
  818:     }
  819:     foreach my $item (@bgs) {
  820:         $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  821:     }
  822:     foreach my $item (@logintext) {
  823:         $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  824:     }
  825:     foreach my $item (@links) {
  826:         $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  827:     }
  828:     if (ref($settings) eq 'HASH') {
  829:         foreach my $item (@toggles) {
  830:             if ($settings->{$item} eq '1') {
  831:                 $checkedon{$item} =  ' checked="checked" ';
  832:                 $checkedoff{$item} = ' ';
  833:             } elsif ($settings->{$item} eq '0') {
  834:                 $checkedoff{$item} =  ' checked="checked" ';
  835:                 $checkedon{$item} = ' ';
  836:             }
  837:         }
  838:         foreach my $item (@images) {
  839:             if (defined($settings->{$item})) {
  840:                 $designs{$item} = $settings->{$item};
  841:                 $is_custom{$item} = 1;
  842:             }
  843:             if (defined($settings->{'showlogo'}{$item})) {
  844:                 $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  845:             }
  846:         }
  847:         foreach my $item (@logintext) {
  848:             if ($settings->{$item} ne '') {
  849:                 $designs{'logintext'}{$item} = $settings->{$item};
  850:                 $is_custom{$item} = 1;
  851:             }
  852:         }
  853:         if ($settings->{'font'} ne '') {
  854:             $designs{'font'} = $settings->{'font'};
  855:             $is_custom{'font'} = 1;
  856:         }
  857:         foreach my $item (@bgs) {
  858:             if ($settings->{$item} ne '') {
  859:                 $designs{'bgs'}{$item} = $settings->{$item};
  860:                 $is_custom{$item} = 1;
  861:             }
  862:         }
  863:         foreach my $item (@links) {
  864:             if ($settings->{$item} ne '') {
  865:                 $designs{'links'}{$item} = $settings->{$item};
  866:                 $is_custom{$item} = 1;
  867:             }
  868:         }
  869:     } else {
  870:         if ($designhash{$dom.'.login.font'} ne '') {
  871:             $designs{'font'} = $designhash{$dom.'.login.font'};
  872:             $is_custom{'font'} = 1;
  873:         }
  874:         foreach my $item (@images) {
  875:             if ($designhash{$dom.'.login.'.$item} ne '') {
  876:                 $designs{$item} = $designhash{$dom.'.login.'.$item};
  877:                 $is_custom{$item} = 1;
  878:             }
  879:         }
  880:         foreach my $item (@bgs) {
  881:             if ($designhash{$dom.'.login.'.$item} ne '') {
  882:                 $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  883:                 $is_custom{$item} = 1;
  884:             }
  885:         }
  886:         foreach my $item (@links) {
  887:             if ($designhash{$dom.'.login.'.$item} ne '') {
  888:                 $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  889:                 $is_custom{$item} = 1;
  890:             }
  891:         }
  892:     }
  893:     my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  894:                                                   logo => 'Institution Logo',
  895:                                                   domlogo => 'Domain Logo',
  896:                                                   login => 'Login box');
  897:     my $itemcount = 1;
  898:     foreach my $item (@toggles) {
  899:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
  900:         $datatable .=  
  901:             '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  902:             '</td><td>'.
  903:             '<span class="LC_nobreak"><label><input type="radio" name="'.
  904:             $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
  905:             '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
  906:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
  907:             '</tr>';
  908:         $itemcount ++;
  909:     }
  910:     $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
  911:     $datatable .= '</tr></table></td></tr>';
  912:     return $datatable;
  913: }
  914: 
  915: sub login_choices {
  916:     my %choices =
  917:         &Apache::lonlocal::texthash (
  918:             coursecatalog => 'Display Course/Community Catalog link?',
  919:             adminmail     => "Display Administrator's E-mail Address?",
  920:             disallowlogin => "Login page requests redirected",
  921:             hostid        => "Server",
  922:             server        => "Redirect to:",
  923:             serverpath    => "Path",
  924:             custompath    => "Custom", 
  925:             exempt        => "Exempt IP(s)",
  926:             directlogin   => "No redirect",
  927:             newuser       => "Link to create a user account",
  928:             img           => "Header",
  929:             logo          => "Main Logo",
  930:             domlogo       => "Domain Logo",
  931:             login         => "Log-in Header", 
  932:             textcol       => "Text color",
  933:             bgcol         => "Box color",
  934:             bgs           => "Background colors",
  935:             links         => "Link colors",
  936:             font          => "Font color",
  937:             pgbg          => "Header",
  938:             mainbg        => "Page",
  939:             sidebg        => "Login box",
  940:             link          => "Link",
  941:             alink         => "Active link",
  942:             vlink         => "Visited link",
  943:         );
  944:     return %choices;
  945: }
  946: 
  947: sub print_rolecolors {
  948:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
  949:     my %choices = &color_font_choices();
  950:     my @bgs = ('pgbg','tabbg','sidebg');
  951:     my @links = ('link','alink','vlink');
  952:     my @images = ('img');
  953:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
  954:     my %designhash = &Apache::loncommon::get_domainconf($dom);
  955:     my %defaultdesign = %Apache::loncommon::defaultdesign;
  956:     my (%is_custom,%designs);
  957:     my %defaults = (
  958:                    img => $defaultdesign{$role.'.img'},
  959:                    font => $defaultdesign{$role.'.font'},
  960: 		   fontmenu => $defaultdesign{$role.'.fontmenu'},
  961:                    );
  962:     foreach my $item (@bgs) {
  963:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
  964:     }
  965:     foreach my $item (@links) {
  966:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
  967:     }
  968:     if (ref($settings) eq 'HASH') {
  969:         if (ref($settings->{$role}) eq 'HASH') {
  970:             if ($settings->{$role}->{'img'} ne '') {
  971:                 $designs{'img'} = $settings->{$role}->{'img'};
  972:                 $is_custom{'img'} = 1;
  973:             }
  974:             if ($settings->{$role}->{'font'} ne '') {
  975:                 $designs{'font'} = $settings->{$role}->{'font'};
  976:                 $is_custom{'font'} = 1;
  977:             }
  978:             if ($settings->{$role}->{'fontmenu'} ne '') {
  979:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
  980:                 $is_custom{'fontmenu'} = 1;
  981:             }
  982:             foreach my $item (@bgs) {
  983:                 if ($settings->{$role}->{$item} ne '') {
  984:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
  985:                     $is_custom{$item} = 1;
  986:                 }
  987:             }
  988:             foreach my $item (@links) {
  989:                 if ($settings->{$role}->{$item} ne '') {
  990:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
  991:                     $is_custom{$item} = 1;
  992:                 }
  993:             }
  994:         }
  995:     } else {
  996:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  997:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
  998:             $is_custom{'img'} = 1;
  999:         }
 1000:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1001:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1002:             $is_custom{'fontmenu'} = 1; 
 1003:         }
 1004:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1005:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1006:             $is_custom{'font'} = 1;
 1007:         }
 1008:         foreach my $item (@bgs) {
 1009:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1010:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1011:                 $is_custom{$item} = 1;
 1012:             
 1013:             }
 1014:         }
 1015:         foreach my $item (@links) {
 1016:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1017:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1018:                 $is_custom{$item} = 1;
 1019:             }
 1020:         }
 1021:     }
 1022:     my $itemcount = 1;
 1023:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1024:     $datatable .= '</tr></table></td></tr>';
 1025:     return $datatable;
 1026: }
 1027: 
 1028: sub display_color_options {
 1029:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1030:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1031:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1032:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1033:     my $datatable = '<tr'.$css_class.'>'.
 1034:         '<td>'.$choices->{'font'}.'</td>';
 1035:     if (!$is_custom->{'font'}) {
 1036:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1037:     } else {
 1038:         $datatable .= '<td>&nbsp;</td>';
 1039:     }
 1040:     my $fontlink = &color_pick($phase,$role,'font',$choices->{'font'},$designs->{'font'});
 1041:     $datatable .= '<td><span class="LC_nobreak">'.
 1042:                   '<input type="text" size="10" name="'.$role.'_font"'.
 1043:                   ' value="'.$designs->{'font'}.'" />&nbsp;'.$fontlink.
 1044:                   '&nbsp;<span id="css_'.$role.'_font" style="background-color: '.
 1045:                   $designs->{'font'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1046:                   '</span></td></tr>';
 1047:     unless ($role eq 'login') { 
 1048:         $datatable .= '<tr'.$css_class.'>'.
 1049:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1050:         if (!$is_custom->{'fontmenu'}) {
 1051:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1052:         } else {
 1053:             $datatable .= '<td>&nbsp;</td>';
 1054:         }
 1055:         $fontlink = &color_pick($phase,$role,'fontmenu',$choices->{'fontmenu'},$designs->{'fontmenu'});
 1056:         $datatable .= '<td><span class="LC_nobreak">'.
 1057:                       '<input type="text" size="10" name="'.$role.'_fontmenu"'.
 1058:                       ' value="'.$designs->{'fontmenu'}.'" />&nbsp;'.$fontlink.
 1059:                       '&nbsp;<span id="css_'.$role.'_fontmenu" style="background-color: '.
 1060:                       $designs->{'fontmenu'}.';">&nbsp;&nbsp;&nbsp;</span>'.
 1061:                       '</span></td></tr>';
 1062:     }
 1063:     my $switchserver = &check_switchserver($dom,$confname);
 1064:     foreach my $img (@{$images}) {
 1065: 	$itemcount ++;
 1066:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1067:         $datatable .= '<tr'.$css_class.'>'.
 1068:                       '<td>'.$choices->{$img};
 1069:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1070:         if ($role eq 'login') {
 1071:             if ($img eq 'login') {
 1072:                 $login_hdr_pick =
 1073:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1074:                 $logincolors =
 1075:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1076:                                             $designs);
 1077:             } elsif ($img ne 'domlogo') {
 1078:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1079:             }
 1080:         }
 1081:         $datatable .= '</td>';
 1082:         if ($designs->{$img} ne '') {
 1083:             $imgfile = $designs->{$img};
 1084: 	    $img_import = ($imgfile =~ m{^/adm/});
 1085:         } else {
 1086:             $imgfile = $defaults->{$img};
 1087:         }
 1088:         if ($imgfile) {
 1089:             my ($showfile,$fullsize);
 1090:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1091:                 my $urldir = $1;
 1092:                 my $filename = $2;
 1093:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1094:                 if (@info) {
 1095:                     my $thumbfile = 'tn-'.$filename;
 1096:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1097:                     if (@thumb) {
 1098:                         $showfile = $urldir.'/'.$thumbfile;
 1099:                     } else {
 1100:                         $showfile = $imgfile;
 1101:                     }
 1102:                 } else {
 1103:                     $showfile = '';
 1104:                 }
 1105:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1106:                 $showfile = $imgfile;
 1107:                 my $imgdir = $1;
 1108:                 my $filename = $2;
 1109:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1110:                     $showfile = "/$imgdir/tn-".$filename;
 1111:                 } else {
 1112:                     my $input = $londocroot.$imgfile;
 1113:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1114:                     if (!-e $output) {
 1115:                         my ($width,$height) = &thumb_dimensions();
 1116:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1117:                         if ($fullwidth ne '' && $fullheight ne '') {
 1118:                             if ($fullwidth > $width && $fullheight > $height) { 
 1119:                                 my $size = $width.'x'.$height;
 1120:                                 system("convert -sample $size $input $output");
 1121:                                 $showfile = "/$imgdir/tn-".$filename;
 1122:                             }
 1123:                         }
 1124:                     }
 1125:                 }
 1126:             }
 1127:             if ($showfile) {
 1128:                 if ($showfile =~ m{^/(adm|res)/}) {
 1129:                     if ($showfile =~ m{^/res/}) {
 1130:                         my $local_showfile =
 1131:                             &Apache::lonnet::filelocation('',$showfile);
 1132:                         &Apache::lonnet::repcopy($local_showfile);
 1133:                     }
 1134:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1135:                 }
 1136:                 if ($imgfile) {
 1137:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1138:                         if ($imgfile =~ m{^/res/}) {
 1139:                             my $local_imgfile =
 1140:                                 &Apache::lonnet::filelocation('',$imgfile);
 1141:                             &Apache::lonnet::repcopy($local_imgfile);
 1142:                         }
 1143:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1144:                     } else {
 1145:                         $fullsize = $imgfile;
 1146:                     }
 1147:                 }
 1148:                 $datatable .= '<td>';
 1149:                 if ($img eq 'login') {
 1150:                     $datatable .= $login_hdr_pick;
 1151:                 } 
 1152:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1153:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1154:             } else {
 1155:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1156:                               &mt('Upload:');
 1157:             }
 1158:         } else {
 1159:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1160:                           &mt('Upload:');
 1161:         }
 1162:         if ($switchserver) {
 1163:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1164:         } else {
 1165:             if ($img ne 'login') { # suppress file selection for Log-in header
 1166:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1167:             }
 1168:         }
 1169:         $datatable .= '</td></tr>';
 1170:     }
 1171:     $itemcount ++;
 1172:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1173:     $datatable .= '<tr'.$css_class.'>'.
 1174:                   '<td>'.$choices->{'bgs'}.'</td>';
 1175:     my $bgs_def;
 1176:     foreach my $item (@{$bgs}) {
 1177:         if (!$is_custom->{$item}) {
 1178:             $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>';
 1179:         }
 1180:     }
 1181:     if ($bgs_def) {
 1182:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1183:     } else {
 1184:         $datatable .= '<td>&nbsp;</td>';
 1185:     }
 1186:     $datatable .= '<td class="LC_right_item">'.
 1187:                   '<table border="0"><tr>';
 1188:     foreach my $item (@{$bgs}) {
 1189:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'bgs'}{$item});
 1190:         $datatable .= '<td align="center">'.$link;
 1191:         if ($designs->{'bgs'}{$item}) {
 1192:             $datatable .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1193:         }
 1194:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'bgs'}{$item}.
 1195:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1196:     }
 1197:     $datatable .= '</tr></table></td></tr>';
 1198:     $itemcount ++;
 1199:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1200:     $datatable .= '<tr'.$css_class.'>'.
 1201:                   '<td>'.$choices->{'links'}.'</td>';
 1202:     my $links_def;
 1203:     foreach my $item (@{$links}) {
 1204:         if (!$is_custom->{$item}) {
 1205:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1206:         }
 1207:     }
 1208:     if ($links_def) {
 1209:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1210:     } else {
 1211:         $datatable .= '<td>&nbsp;</td>';
 1212:     }
 1213:     $datatable .= '<td class="LC_right_item">'.
 1214:                   '<table border="0"><tr>';
 1215:     foreach my $item (@{$links}) {
 1216:         $datatable .= '<td align="center">'."\n".
 1217:                       &color_pick($phase,$role,$item,$choices->{$item},
 1218:                                   $designs->{'links'}{$item});
 1219:         if ($designs->{'links'}{$item}) {
 1220:             $datatable.='&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'links'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1221:         }
 1222:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'links'}{$item}.
 1223:                       '" /></td>';
 1224:     }
 1225:     $$rowtotal += $itemcount;
 1226:     return $datatable;
 1227: }
 1228: 
 1229: sub logo_display_options {
 1230:     my ($img,$defaults,$designs) = @_;
 1231:     my $checkedon;
 1232:     if (ref($defaults) eq 'HASH') {
 1233:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1234:             if ($defaults->{'showlogo'}{$img}) {
 1235:                 $checkedon = 'checked="checked" ';     
 1236:             }
 1237:         } 
 1238:     }
 1239:     if (ref($designs) eq 'HASH') {
 1240:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1241:             if (defined($designs->{'showlogo'}{$img})) {
 1242:                 if ($designs->{'showlogo'}{$img} == 0) {
 1243:                     $checkedon = '';
 1244:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1245:                     $checkedon = 'checked="checked" ';
 1246:                 }
 1247:             }
 1248:         }
 1249:     }
 1250:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1251:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1252:            &mt('show').'</label>'."\n";
 1253: }
 1254: 
 1255: sub login_header_options  {
 1256:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1257:     my $output = '';
 1258:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1259:         $output .= &mt('Text default(s):').'<br />';
 1260:         if (!$is_custom->{'textcol'}) {
 1261:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1262:                        '&nbsp;&nbsp;&nbsp;';
 1263:         }
 1264:         if (!$is_custom->{'bgcol'}) {
 1265:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1266:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1267:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1268:         }
 1269:         $output .= '<br />';
 1270:     }
 1271:     $output .='<br />';
 1272:     return $output;
 1273: }
 1274: 
 1275: sub login_text_colors {
 1276:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1277:     my $color_menu = '<table border="0"><tr>';
 1278:     foreach my $item (@{$logintext}) {
 1279:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1280:         $color_menu .= '<td align="center">'.$link;
 1281:         if ($designs->{'logintext'}{$item}) {
 1282:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1283:         }
 1284:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1285:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1286:                        '<td>&nbsp;</td>';
 1287:     }
 1288:     $color_menu .= '</tr></table><br />';
 1289:     return $color_menu;
 1290: }
 1291: 
 1292: sub image_changes {
 1293:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1294:     my $output;
 1295:     if ($img eq 'login') {
 1296:             # suppress image for Log-in header
 1297:     } elsif (!$is_custom) {
 1298:         if ($img ne 'domlogo') {
 1299:             $output .= &mt('Default image:').'<br />';
 1300:         } else {
 1301:             $output .= &mt('Default in use:').'<br />';
 1302:         }
 1303:     }
 1304:     if ($img eq 'login') { # suppress image for Log-in header
 1305:         $output .= '<td>'.$logincolors;
 1306:     } else {
 1307:         if ($img_import) {
 1308:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1309:         }
 1310:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1311:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1312:         if ($is_custom) {
 1313:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1314:                        '<input type="checkbox" name="'.
 1315:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1316:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1317:         } else {
 1318:             $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1319:         }
 1320:     }
 1321:     return $output;
 1322: }
 1323: 
 1324: sub color_pick {
 1325:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1326:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1327:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1328:                ');">'.$desc.'</a>';
 1329:     return $link;
 1330: }
 1331: 
 1332: sub print_quotas {
 1333:     my ($dom,$settings,$rowtotal,$action) = @_;
 1334:     my $context;
 1335:     if ($action eq 'quotas') {
 1336:         $context = 'tools';
 1337:     } else {
 1338:         $context = $action;
 1339:     }
 1340:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 1341:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1342:     my $typecount = 0;
 1343:     my ($css_class,%titles);
 1344:     if ($context eq 'requestcourses') {
 1345:         @usertools = ('official','unofficial','community');
 1346:         @options =('norequest','approval','validate','autolimit');
 1347:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1348:         %titles = &courserequest_titles();
 1349:     } else {
 1350:         @usertools = ('aboutme','blog','webdav','portfolio');
 1351:         %titles = &tool_titles();
 1352:     }
 1353:     if (ref($types) eq 'ARRAY') {
 1354:         foreach my $type (@{$types}) {
 1355:             my $currdefquota;
 1356:             unless ($context eq 'requestcourses') {
 1357:                 if (ref($settings) eq 'HASH') {
 1358:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1359:                         $currdefquota = $settings->{defaultquota}->{$type}; 
 1360:                     } else {
 1361:                         $currdefquota = $settings->{$type};
 1362:                     }
 1363:                 }
 1364:             }
 1365:             if (defined($usertypes->{$type})) {
 1366:                 $typecount ++;
 1367:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1368:                 $datatable .= '<tr'.$css_class.'>'.
 1369:                               '<td>'.$usertypes->{$type}.'</td>'.
 1370:                               '<td class="LC_left_item">';
 1371:                 if ($context eq 'requestcourses') {
 1372:                     $datatable .= '<table><tr>';
 1373:                 }
 1374:                 my %cell;  
 1375:                 foreach my $item (@usertools) {
 1376:                     if ($context eq 'requestcourses') {
 1377:                         my ($curroption,$currlimit);
 1378:                         if (ref($settings) eq 'HASH') {
 1379:                             if (ref($settings->{$item}) eq 'HASH') {
 1380:                                 $curroption = $settings->{$item}->{$type};
 1381:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1382:                                     $currlimit = $1; 
 1383:                                 }
 1384:                             }
 1385:                         }
 1386:                         if (!$curroption) {
 1387:                             $curroption = 'norequest';
 1388:                         }
 1389:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1390:                         foreach my $option (@options) {
 1391:                             my $val = $option;
 1392:                             if ($option eq 'norequest') {
 1393:                                 $val = 0;  
 1394:                             }
 1395:                             if ($option eq 'validate') {
 1396:                                 my $canvalidate = 0;
 1397:                                 if (ref($validations{$item}) eq 'HASH') { 
 1398:                                     if ($validations{$item}{$type}) {
 1399:                                         $canvalidate = 1;
 1400:                                     }
 1401:                                 }
 1402:                                 next if (!$canvalidate);
 1403:                             }
 1404:                             my $checked = '';
 1405:                             if ($option eq $curroption) {
 1406:                                 $checked = ' checked="checked"';
 1407:                             } elsif ($option eq 'autolimit') {
 1408:                                 if ($curroption =~ /^autolimit/) {
 1409:                                     $checked = ' checked="checked"';
 1410:                                 }                       
 1411:                             } 
 1412:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1413:                                   '<input type="radio" name="crsreq_'.$item.
 1414:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1415:                                   $titles{$option}.'</label>';
 1416:                             if ($option eq 'autolimit') {
 1417:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1418:                                                 $item.'_limit_'.$type.'" size="1" '.
 1419:                                                 'value="'.$currlimit.'" />';
 1420:                             }
 1421:                             $cell{$item} .= '</span> ';
 1422:                             if ($option eq 'autolimit') {
 1423:                                 $cell{$item} .= $titles{'unlimited'};
 1424:                             }
 1425:                         }
 1426:                     } else {
 1427:                         my $checked = 'checked="checked" ';
 1428:                         if (ref($settings) eq 'HASH') {
 1429:                             if (ref($settings->{$item}) eq 'HASH') {
 1430:                                 if ($settings->{$item}->{$type} == 0) {
 1431:                                     $checked = '';
 1432:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1433:                                     $checked =  'checked="checked" ';
 1434:                                 }
 1435:                             }
 1436:                         }
 1437:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1438:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1439:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1440:                                       '</label></span>&nbsp; ';
 1441:                     }
 1442:                 }
 1443:                 if ($context eq 'requestcourses') {
 1444:                     $datatable .= '</tr><tr>';
 1445:                     foreach my $item (@usertools) {
 1446:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1447:                     }
 1448:                     $datatable .= '</tr></table>';
 1449:                 }
 1450:                 $datatable .= '</td>';
 1451:                 unless ($context eq 'requestcourses') {
 1452:                     $datatable .= 
 1453:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1454:                               '<input type="text" name="quota_'.$type.
 1455:                               '" value="'.$currdefquota.
 1456:                               '" size="5" /> Mb</span></td>';
 1457:                 }
 1458:                 $datatable .= '</tr>';
 1459:             }
 1460:         }
 1461:     }
 1462:     unless ($context eq 'requestcourses') {
 1463:         $defaultquota = '20';
 1464:         if (ref($settings) eq 'HASH') {
 1465:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1466:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1467:             } elsif (defined($settings->{'default'})) {
 1468:                 $defaultquota = $settings->{'default'};
 1469:             }
 1470:         }
 1471:     }
 1472:     $typecount ++;
 1473:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1474:     $datatable .= '<tr'.$css_class.'>'.
 1475:                   '<td>'.$othertitle.'</td>'.
 1476:                   '<td class="LC_left_item">';
 1477:     if ($context eq 'requestcourses') {
 1478:         $datatable .= '<table><tr>';
 1479:     }
 1480:     my %defcell;
 1481:     foreach my $item (@usertools) {
 1482:         if ($context eq 'requestcourses') {
 1483:             my ($curroption,$currlimit);
 1484:             if (ref($settings) eq 'HASH') {
 1485:                 if (ref($settings->{$item}) eq 'HASH') {
 1486:                     $curroption = $settings->{$item}->{'default'};
 1487:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1488:                         $currlimit = $1;
 1489:                     }
 1490:                 }
 1491:             }
 1492:             if (!$curroption) {
 1493:                 $curroption = 'norequest';
 1494:             }
 1495:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1496:             foreach my $option (@options) {
 1497:                 my $val = $option;
 1498:                 if ($option eq 'norequest') {
 1499:                     $val = 0;
 1500:                 }
 1501:                 if ($option eq 'validate') {
 1502:                     my $canvalidate = 0;
 1503:                     if (ref($validations{$item}) eq 'HASH') {
 1504:                         if ($validations{$item}{'default'}) {
 1505:                             $canvalidate = 1;
 1506:                         }
 1507:                     }
 1508:                     next if (!$canvalidate);
 1509:                 }
 1510:                 my $checked = '';
 1511:                 if ($option eq $curroption) {
 1512:                     $checked = ' checked="checked"';
 1513:                 } elsif ($option eq 'autolimit') {
 1514:                     if ($curroption =~ /^autolimit/) {
 1515:                         $checked = ' checked="checked"';
 1516:                     }
 1517:                 }
 1518:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1519:                                   '<input type="radio" name="crsreq_'.$item.
 1520:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1521:                                   $titles{$option}.'</label>';
 1522:                 if ($option eq 'autolimit') {
 1523:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1524:                                        $item.'_limit_default" size="1" '.
 1525:                                        'value="'.$currlimit.'" />';
 1526:                 }
 1527:                 $defcell{$item} .= '</span> ';
 1528:                 if ($option eq 'autolimit') {
 1529:                     $defcell{$item} .= $titles{'unlimited'};
 1530:                 }
 1531:             }
 1532:         } else {
 1533:             my $checked = 'checked="checked" ';
 1534:             if (ref($settings) eq 'HASH') {
 1535:                 if (ref($settings->{$item}) eq 'HASH') {
 1536:                     if ($settings->{$item}->{'default'} == 0) {
 1537:                         $checked = '';
 1538:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1539:                         $checked = 'checked="checked" ';
 1540:                     }
 1541:                 }
 1542:             }
 1543:             $datatable .= '<span class="LC_nobreak"><label>'.
 1544:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1545:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1546:                           '</label></span>&nbsp; ';
 1547:         }
 1548:     }
 1549:     if ($context eq 'requestcourses') {
 1550:         $datatable .= '</tr><tr>';
 1551:         foreach my $item (@usertools) {
 1552:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1553:         }
 1554:         $datatable .= '</tr></table>';
 1555:     }
 1556:     $datatable .= '</td>';
 1557:     unless ($context eq 'requestcourses') {
 1558:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1559:                       '<input type="text" name="defaultquota" value="'.
 1560:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1561:     }
 1562:     $datatable .= '</tr>';
 1563:     $typecount ++;
 1564:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1565:     $datatable .= '<tr'.$css_class.'>'.
 1566:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1567:     if ($context eq 'requestcourses') {
 1568:         $datatable .= &mt('(overrides affiliation, if set)').
 1569:                       '</td>'.
 1570:                       '<td class="LC_left_item">'.
 1571:                       '<table><tr>';
 1572:     } else {
 1573:         $datatable .= &mt('(overrides affiliation, if checked)').
 1574:                       '</td>'.
 1575:                       '<td class="LC_left_item" colspan="2">'.
 1576:                       '<br />';
 1577:     }
 1578:     my %advcell;
 1579:     foreach my $item (@usertools) {
 1580:         if ($context eq 'requestcourses') {
 1581:             my ($curroption,$currlimit);
 1582:             if (ref($settings) eq 'HASH') {
 1583:                 if (ref($settings->{$item}) eq 'HASH') {
 1584:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1585:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1586:                         $currlimit = $1;
 1587:                     }
 1588:                 }
 1589:             }
 1590:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1591:             my $checked = '';
 1592:             if ($curroption eq '') {
 1593:                 $checked = ' checked="checked"';
 1594:             }
 1595:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1596:                                '<input type="radio" name="crsreq_'.$item.
 1597:                                '__LC_adv" value=""'.$checked.' />'.
 1598:                                &mt('No override set').'</label></span>&nbsp; ';
 1599:             foreach my $option (@options) {
 1600:                 my $val = $option;
 1601:                 if ($option eq 'norequest') {
 1602:                     $val = 0;
 1603:                 }
 1604:                 if ($option eq 'validate') {
 1605:                     my $canvalidate = 0;
 1606:                     if (ref($validations{$item}) eq 'HASH') {
 1607:                         if ($validations{$item}{'_LC_adv'}) {
 1608:                             $canvalidate = 1;
 1609:                         }
 1610:                     }
 1611:                     next if (!$canvalidate);
 1612:                 }
 1613:                 my $checked = '';
 1614:                 if ($val eq $curroption) {
 1615:                     $checked = ' checked="checked"';
 1616:                 } elsif ($option eq 'autolimit') {
 1617:                     if ($curroption =~ /^autolimit/) {
 1618:                         $checked = ' checked="checked"';
 1619:                     }
 1620:                 }
 1621:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1622:                                   '<input type="radio" name="crsreq_'.$item.
 1623:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1624:                                   $titles{$option}.'</label>';
 1625:                 if ($option eq 'autolimit') {
 1626:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1627:                                        $item.'_limit__LC_adv" size="1" '.
 1628:                                        'value="'.$currlimit.'" />';
 1629:                 }
 1630:                 $advcell{$item} .= '</span> ';
 1631:                 if ($option eq 'autolimit') {
 1632:                     $advcell{$item} .= $titles{'unlimited'};
 1633:                 }
 1634:             }
 1635:         } else {
 1636:             my $checked = 'checked="checked" ';
 1637:             if (ref($settings) eq 'HASH') {
 1638:                 if (ref($settings->{$item}) eq 'HASH') {
 1639:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1640:                         $checked = '';
 1641:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1642:                         $checked = 'checked="checked" ';
 1643:                     }
 1644:                 }
 1645:             }
 1646:             $datatable .= '<span class="LC_nobreak"><label>'.
 1647:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1648:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1649:                           '</label></span>&nbsp; ';
 1650:         }
 1651:     }
 1652:     if ($context eq 'requestcourses') {
 1653:         $datatable .= '</tr><tr>';
 1654:         foreach my $item (@usertools) {
 1655:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1656:         }
 1657:         $datatable .= '</tr></table>';
 1658:     }
 1659:     $datatable .= '</td></tr>';
 1660:     $$rowtotal += $typecount;
 1661:     return $datatable;
 1662: }
 1663: 
 1664: sub print_courserequestmail {
 1665:     my ($dom,$settings,$rowtotal) = @_;
 1666:     my ($now,$datatable,%dompersonnel,@domcoord,@currapproval,$rows);
 1667:     $now = time;
 1668:     $rows = 0;
 1669:     %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 1670:     foreach my $server (keys(%dompersonnel)) {
 1671:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 1672:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 1673:             if (!grep(/^$uname:$udom$/,@domcoord)) {
 1674:                 push(@domcoord,$uname.':'.$udom);
 1675:             }
 1676:         }
 1677:     }
 1678:     if (ref($settings) eq 'HASH') {
 1679:         if (ref($settings->{'notify'}) eq 'HASH') {
 1680:             if ($settings->{'notify'}{'approval'} ne '') {
 1681:                @currapproval = split(',',$settings->{'notify'}{'approval'});
 1682:             }
 1683:         }
 1684:     }
 1685:     if (@currapproval) {
 1686:         foreach my $dc (@currapproval) {
 1687:             unless (grep(/^\Q$dc\E$/,@domcoord)) {
 1688:                 push(@domcoord,$dc);
 1689:             }
 1690:         }
 1691:     }
 1692:     @domcoord = sort(@domcoord);
 1693:     my $numinrow = 4;
 1694:     my $numdc = @domcoord;
 1695:     my $css_class = 'class="LC_odd_row"';
 1696:     $datatable = '<tr'.$css_class.'>'.
 1697:                  ' <td>'.&mt('Receive notification of course requests requiring approval.').
 1698:                  ' </td>'.
 1699:                  ' <td class="LC_left_item">';
 1700:     if (@domcoord > 0) {
 1701:         $datatable .= '<table>';
 1702:         for (my $i=0; $i<$numdc; $i++) {
 1703:             my $rem = $i%($numinrow);
 1704:             if ($rem == 0) {
 1705:                 if ($i > 0) {
 1706:                     $datatable .= '</tr>';
 1707:                 }
 1708:                 $datatable .= '<tr>';
 1709:                 $rows ++;
 1710:             }
 1711:             my $check = ' ';
 1712:             if (grep(/^\Q$domcoord[$i]\E$/,@currapproval)) {
 1713:                 $check = ' checked="checked" ';
 1714:             }
 1715:             my ($uname,$udom) = split(':',$domcoord[$i]);
 1716:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
 1717:             if ($i == $numdc-1) {
 1718:                 my $colsleft = $numinrow-$rem;
 1719:                 if ($colsleft > 1) {
 1720:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 1721:                 } else {
 1722:                     $datatable .= '<td class="LC_left_item">';
 1723:                 }
 1724:             } else {
 1725:                 $datatable .= '<td class="LC_left_item">';
 1726:             }
 1727:             $datatable .= '<span class="LC_nobreak"><label>'.
 1728:                           '<input type="checkbox" name="reqapprovalnotify" '.
 1729:                           'value="'.$domcoord[$i].'"'.$check.'/>'.
 1730:                           $fullname.'</label></span></td>';
 1731:         }
 1732:         $datatable .= '</tr></table>';
 1733:     } else {
 1734:         $datatable .= &mt('There are no active Domain Coordinators');
 1735:         $rows ++;
 1736:     }
 1737:     $datatable .='</td></tr>';
 1738:     $$rowtotal += $rows;
 1739:     return $datatable;
 1740: }
 1741: 
 1742: sub print_autoenroll {
 1743:     my ($dom,$settings,$rowtotal) = @_;
 1744:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1745:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1746:     if (ref($settings) eq 'HASH') {
 1747:         if (exists($settings->{'run'})) {
 1748:             if ($settings->{'run'} eq '0') {
 1749:                 $runoff = ' checked="checked" ';
 1750:                 $runon = ' ';
 1751:             } else {
 1752:                 $runon = ' checked="checked" ';
 1753:                 $runoff = ' ';
 1754:             }
 1755:         } else {
 1756:             if ($autorun) {
 1757:                 $runon = ' checked="checked" ';
 1758:                 $runoff = ' ';
 1759:             } else {
 1760:                 $runoff = ' checked="checked" ';
 1761:                 $runon = ' ';
 1762:             }
 1763:         }
 1764:         if (exists($settings->{'co-owners'})) {
 1765:             if ($settings->{'co-owners'} eq '0') {
 1766:                 $coownersoff = ' checked="checked" ';
 1767:                 $coownerson = ' ';
 1768:             } else {
 1769:                 $coownerson = ' checked="checked" ';
 1770:                 $coownersoff = ' ';
 1771:             }
 1772:         } else {
 1773:             $coownersoff = ' checked="checked" ';
 1774:             $coownerson = ' ';
 1775:         }
 1776:         if (exists($settings->{'sender_domain'})) {
 1777:             $defdom = $settings->{'sender_domain'};
 1778:         }
 1779:     } else {
 1780:         if ($autorun) {
 1781:             $runon = ' checked="checked" ';
 1782:             $runoff = ' ';
 1783:         } else {
 1784:             $runoff = ' checked="checked" ';
 1785:             $runon = ' ';
 1786:         }
 1787:     }
 1788:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 1789:     my $notif_sender;
 1790:     if (ref($settings) eq 'HASH') {
 1791:         $notif_sender = $settings->{'sender_uname'};
 1792:     }
 1793:     my $datatable='<tr class="LC_odd_row">'.
 1794:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 1795:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1796:                   '<input type="radio" name="autoenroll_run"'.
 1797:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1798:                   '<label><input type="radio" name="autoenroll_run"'.
 1799:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 1800:                   '</tr><tr>'.
 1801:                   '<td>'.&mt('Notification messages - sender').
 1802:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 1803:                   &mt('username').':&nbsp;'.
 1804:                   '<input type="text" name="sender_uname" value="'.
 1805:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 1806:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 1807:                   '<tr class="LC_odd_row">'.
 1808:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 1809:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1810:                   '<input type="radio" name="autoassign_coowners"'.
 1811:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1812:                   '<label><input type="radio" name="autoassign_coowners"'.
 1813:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 1814:                   '</tr>';
 1815:     $$rowtotal += 3;
 1816:     return $datatable;
 1817: }
 1818: 
 1819: sub print_autoupdate {
 1820:     my ($position,$dom,$settings,$rowtotal) = @_;
 1821:     my $datatable;
 1822:     if ($position eq 'top') {
 1823:         my $updateon = ' ';
 1824:         my $updateoff = ' checked="checked" ';
 1825:         my $classlistson = ' ';
 1826:         my $classlistsoff = ' checked="checked" ';
 1827:         if (ref($settings) eq 'HASH') {
 1828:             if ($settings->{'run'} eq '1') {
 1829:                 $updateon = $updateoff;
 1830:                 $updateoff = ' ';
 1831:             }
 1832:             if ($settings->{'classlists'} eq '1') {
 1833:                 $classlistson = $classlistsoff;
 1834:                 $classlistsoff = ' ';
 1835:             }
 1836:         }
 1837:         my %title = (
 1838:                    run => 'Auto-update active?',
 1839:                    classlists => 'Update information in classlists?',
 1840:                     );
 1841:         $datatable = '<tr class="LC_odd_row">'. 
 1842:                   '<td>'.&mt($title{'run'}).'</td>'.
 1843:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1844:                   '<input type="radio" name="autoupdate_run"'.
 1845:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1846:                   '<label><input type="radio" name="autoupdate_run"'.
 1847:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 1848:                   '</tr><tr>'.
 1849:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 1850:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 1851:                   '<label><input type="radio" name="classlists"'.
 1852:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1853:                   '<label><input type="radio" name="classlists"'.
 1854:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 1855:                   '</tr>';
 1856:         $$rowtotal += 2;
 1857:     } elsif ($position eq 'middle') {
 1858:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1859:         my $numinrow = 3;
 1860:         my $locknamesettings;
 1861:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 1862:                                      $dom,$numinrow,$othertitle,
 1863:                                     'lockablenames');
 1864:         $$rowtotal ++;
 1865:     } else {
 1866:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1867:         my @fields = ('lastname','firstname','middlename','generation',
 1868:                       'permanentemail','id');
 1869:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 1870:         my $numrows = 0;
 1871:         if (ref($types) eq 'ARRAY') {
 1872:             if (@{$types} > 0) {
 1873:                 $datatable = 
 1874:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 1875:                                          \@fields,$types,\$numrows);
 1876:                     $$rowtotal += @{$types}; 
 1877:             }
 1878:         }
 1879:         $datatable .= 
 1880:             &usertype_update_row($settings,{'default' => $othertitle},
 1881:                                  \%fieldtitles,\@fields,['default'],
 1882:                                  \$numrows);
 1883:         $$rowtotal ++;     
 1884:     }
 1885:     return $datatable;
 1886: }
 1887: 
 1888: sub print_autocreate {
 1889:     my ($dom,$settings,$rowtotal) = @_;
 1890:     my (%createon,%createoff);
 1891:     my $curr_dc;
 1892:     my @types = ('xml','req');
 1893:     if (ref($settings) eq 'HASH') {
 1894:         foreach my $item (@types) {
 1895:             $createoff{$item} = ' checked="checked" ';
 1896:             $createon{$item} = ' ';
 1897:             if (exists($settings->{$item})) {
 1898:                 if ($settings->{$item}) {
 1899:                     $createon{$item} = ' checked="checked" ';
 1900:                     $createoff{$item} = ' ';
 1901:                 }
 1902:             }
 1903:         }
 1904:         $curr_dc = $settings->{'xmldc'};
 1905:     } else {
 1906:         foreach my $item (@types) {
 1907:             $createoff{$item} = ' checked="checked" ';
 1908:             $createon{$item} = ' ';
 1909:         }
 1910:     }
 1911:     $$rowtotal += 2;
 1912:     my $datatable='<tr class="LC_odd_row">'.
 1913:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 1914:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1915:                   '<input type="radio" name="autocreate_xml"'.
 1916:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1917:                   '<label><input type="radio" name="autocreate_xml"'.
 1918:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 1919:                   '</td></tr><tr>'.
 1920:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 1921:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1922:                   '<input type="radio" name="autocreate_req"'.
 1923:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1924:                   '<label><input type="radio" name="autocreate_req"'.
 1925:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 1926:     my ($numdc,$dctable) = &active_dc_picker($dom,$curr_dc);
 1927:     if ($numdc > 1) {
 1928:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 1929:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 1930:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 1931:         $$rowtotal ++ ;
 1932:     } else {
 1933:         $datatable .= $dctable.'</td></tr>';
 1934:     }
 1935:     return $datatable;
 1936: }
 1937: 
 1938: sub print_directorysrch {
 1939:     my ($dom,$settings,$rowtotal) = @_;
 1940:     my $srchon = ' ';
 1941:     my $srchoff = ' checked="checked" ';
 1942:     my ($exacton,$containson,$beginson);
 1943:     my $localon = ' ';
 1944:     my $localoff = ' checked="checked" ';
 1945:     if (ref($settings) eq 'HASH') {
 1946:         if ($settings->{'available'} eq '1') {
 1947:             $srchon = $srchoff;
 1948:             $srchoff = ' ';
 1949:         }
 1950:         if ($settings->{'localonly'} eq '1') {
 1951:             $localon = $localoff;
 1952:             $localoff = ' ';
 1953:         }
 1954:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 1955:             foreach my $type (@{$settings->{'searchtypes'}}) {
 1956:                 if ($type eq 'exact') {
 1957:                     $exacton = ' checked="checked" ';
 1958:                 } elsif ($type eq 'contains') {
 1959:                     $containson = ' checked="checked" ';
 1960:                 } elsif ($type eq 'begins') {
 1961:                     $beginson = ' checked="checked" ';
 1962:                 }
 1963:             }
 1964:         } else {
 1965:             if ($settings->{'searchtypes'} eq 'exact') {
 1966:                 $exacton = ' checked="checked" ';
 1967:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 1968:                 $containson = ' checked="checked" ';
 1969:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 1970:                 $exacton = ' checked="checked" ';
 1971:                 $containson = ' checked="checked" ';
 1972:             }
 1973:         }
 1974:     }
 1975:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 1976:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1977: 
 1978:     my $numinrow = 4;
 1979:     my $cansrchrow = 0;
 1980:     my $datatable='<tr class="LC_odd_row">'.
 1981:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 1982:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1983:                   '<input type="radio" name="dirsrch_available"'.
 1984:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1985:                   '<label><input type="radio" name="dirsrch_available"'.
 1986:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 1987:                   '</tr><tr>'.
 1988:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 1989:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1990:                   '<input type="radio" name="dirsrch_localonly"'.
 1991:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 1992:                   '<label><input type="radio" name="dirsrch_localonly"'.
 1993:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 1994:                   '</tr>';
 1995:     $$rowtotal += 2;
 1996:     if (ref($usertypes) eq 'HASH') {
 1997:         if (keys(%{$usertypes}) > 0) {
 1998:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 1999:                                          $numinrow,$othertitle,'cansearch');
 2000:             $cansrchrow = 1;
 2001:         }
 2002:     }
 2003:     if ($cansrchrow) {
 2004:         $$rowtotal ++;
 2005:         $datatable .= '<tr>';
 2006:     } else {
 2007:         $datatable .= '<tr class="LC_odd_row">';
 2008:     }
 2009:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2010:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2011:     foreach my $title (@{$titleorder}) {
 2012:         if (defined($searchtitles->{$title})) {
 2013:             my $check = ' ';
 2014:             if (ref($settings) eq 'HASH') {
 2015:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2016:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2017:                         $check = ' checked="checked" ';
 2018:                     }
 2019:                 }
 2020:             }
 2021:             $datatable .= '<td class="LC_left_item">'.
 2022:                           '<span class="LC_nobreak"><label>'.
 2023:                           '<input type="checkbox" name="searchby" '.
 2024:                           'value="'.$title.'"'.$check.'/>'.
 2025:                           $searchtitles->{$title}.'</label></span></td>';
 2026:         }
 2027:     }
 2028:     $datatable .= '</tr></table></td></tr>';
 2029:     $$rowtotal ++;
 2030:     if ($cansrchrow) {
 2031:         $datatable .= '<tr class="LC_odd_row">';
 2032:     } else {
 2033:         $datatable .= '<tr>';
 2034:     }
 2035:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2036:                   '<td class="LC_left_item" colspan="2">'.
 2037:                   '<span class="LC_nobreak"><label>'.
 2038:                   '<input type="checkbox" name="searchtypes" '.
 2039:                   $exacton.' value="exact" />'.&mt('Exact match').
 2040:                   '</label>&nbsp;'.
 2041:                   '<label><input type="checkbox" name="searchtypes" '.
 2042:                   $beginson.' value="begins" />'.&mt('Begins with').
 2043:                   '</label>&nbsp;'.
 2044:                   '<label><input type="checkbox" name="searchtypes" '.
 2045:                   $containson.' value="contains" />'.&mt('Contains').
 2046:                   '</label></span></td></tr>';
 2047:     $$rowtotal ++;
 2048:     return $datatable;
 2049: }
 2050: 
 2051: sub print_contacts {
 2052:     my ($dom,$settings,$rowtotal) = @_;
 2053:     my $datatable;
 2054:     my @contacts = ('adminemail','supportemail');
 2055:     my (%checked,%to,%otheremails,%bccemails);
 2056:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2057:                     'requestsmail');
 2058:     foreach my $type (@mailings) {
 2059:         $otheremails{$type} = '';
 2060:     }
 2061:     $bccemails{'helpdeskmail'} = '';
 2062:     if (ref($settings) eq 'HASH') {
 2063:         foreach my $item (@contacts) {
 2064:             if (exists($settings->{$item})) {
 2065:                 $to{$item} = $settings->{$item};
 2066:             }
 2067:         }
 2068:         foreach my $type (@mailings) {
 2069:             if (exists($settings->{$type})) {
 2070:                 if (ref($settings->{$type}) eq 'HASH') {
 2071:                     foreach my $item (@contacts) {
 2072:                         if ($settings->{$type}{$item}) {
 2073:                             $checked{$type}{$item} = ' checked="checked" ';
 2074:                         }
 2075:                     }
 2076:                     $otheremails{$type} = $settings->{$type}{'others'};
 2077:                     if ($type eq 'helpdeskmail') {
 2078:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2079:                     }
 2080:                 }
 2081:             } elsif ($type eq 'lonstatusmail') {
 2082:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2083:             }
 2084:         }
 2085:     } else {
 2086:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2087:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2088:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2089:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2090:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2091:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2092:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2093:     }
 2094:     my ($titles,$short_titles) = &contact_titles();
 2095:     my $rownum = 0;
 2096:     my $css_class;
 2097:     foreach my $item (@contacts) {
 2098:         $rownum ++;
 2099:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2100:         $datatable .= '<tr'.$css_class.'>'. 
 2101:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2102:                   '</span></td><td class="LC_right_item">'.
 2103:                   '<input type="text" name="'.$item.'" value="'.
 2104:                   $to{$item}.'" /></td></tr>';
 2105:     }
 2106:     foreach my $type (@mailings) {
 2107:         $rownum ++;
 2108:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2109:         $datatable .= '<tr'.$css_class.'>'.
 2110:                       '<td><span class="LC_nobreak">'.
 2111:                       $titles->{$type}.': </span></td>'.
 2112:                       '<td class="LC_left_item">'.
 2113:                       '<span class="LC_nobreak">';
 2114:         foreach my $item (@contacts) {
 2115:             $datatable .= '<label>'.
 2116:                           '<input type="checkbox" name="'.$type.'"'.
 2117:                           $checked{$type}{$item}.
 2118:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2119:                           '</label>&nbsp;';
 2120:         }
 2121:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2122:                       '<input type="text" name="'.$type.'_others" '.
 2123:                       'value="'.$otheremails{$type}.'"  />';
 2124:         if ($type eq 'helpdeskmail') {
 2125:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2126:                           '<input type="text" name="'.$type.'_bcc" '.
 2127:                           'value="'.$bccemails{$type}.'"  />';
 2128:         }
 2129:         $datatable .= '</td></tr>'."\n";
 2130:     }
 2131:     $$rowtotal += $rownum;
 2132:     return $datatable;
 2133: }
 2134: 
 2135: sub print_helpsettings {
 2136: 
 2137: 	my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 2138: 	my ($css_class,$datatable);
 2139: 	
 2140: 	my $switchserver = &check_switchserver($dom,$confname);
 2141: 	
 2142: 	my $itemcount = 1;
 2143: 	
 2144: 	if ($position eq 'top') {
 2145: 		
 2146: 		my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2147: 		
 2148: 		%choices =
 2149: 			&Apache::lonlocal::texthash (
 2150: 				submitbugs => 'Display &quot;Submit a bug&quot; link?',
 2151: 		);
 2152: 		
 2153: 		%defaultchecked = ('submitbugs' => 'on');
 2154: 		
 2155: 		@toggles = ('submitbugs',);
 2156: 		
 2157: 		foreach my $item (@toggles) {
 2158: 			if ($defaultchecked{$item} eq 'on') { 
 2159: 				$checkedon{$item} = ' checked="checked" ';
 2160: 				$checkedoff{$item} = ' ';
 2161: 			} elsif ($defaultchecked{$item} eq 'off') {
 2162: 				$checkedoff{$item} = ' checked="checked" ';
 2163: 				$checkedon{$item} = ' ';
 2164: 			}
 2165: 		}
 2166: 		
 2167: 		if (ref($settings) eq 'HASH') {
 2168: 			foreach my $item (@toggles) {
 2169: 				if ($settings->{$item} eq '1') {
 2170: 					$checkedon{$item} =  ' checked="checked" ';
 2171: 					$checkedoff{$item} = ' ';
 2172: 				} elsif ($settings->{$item} eq '0') {
 2173: 					$checkedoff{$item} =  ' checked="checked" ';
 2174: 					$checkedon{$item} = ' ';
 2175: 				}
 2176: 			}
 2177: 		 }
 2178: 		
 2179: 		 foreach my $item (@toggles) {
 2180: 			$css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2181: 			$datatable .=  
 2182: 				'<tr'.$css_class.'>
 2183: 				<td><span class="LC_nobreak">'.$choices{$item}.'</span></td>
 2184: 				<td><span class="LC_nobreak">&nbsp;</span></td>
 2185: 				<td class="LC_right_item"><span class="LC_nobreak">
 2186: 				<label><input type="radio" name="'.$item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').'</label>&nbsp;
 2187: 				<label><input type="radio" name="'.$item.'" '.$checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2188: 				'</span></td>'.
 2189: 				'</tr>';
 2190: 			$itemcount ++;
 2191: 		 }
 2192:      
 2193:      } else {
 2194:      
 2195:      	$css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2196:      	
 2197:      	$datatable .= '<tr'.$css_class.'>';
 2198:      	
 2199:      	if (ref($settings) eq 'HASH') {
 2200: 			if ($settings->{'loginhelpurl'} ne '') {
 2201: 				my($directory, $filename) = $settings->{'loginhelpurl'} =~ m/(.*\/)(.*)$/;
 2202: 				$datatable .= '<td width="33%"><span class="LC_left_item"><label><a href="'.$settings->{'loginhelpurl'}.'" target="_blank">'.&mt('Custom Login Page Help File In Use').'</a></label></span></td>';
 2203: 				$datatable .= '<td width="33%"><span class="LC_right_item"><label><input type="checkbox" name="loginhelpurl_del" value="1" />'.&mt('Delete?').'</label></span></td>'
 2204: 			} else {
 2205: 				$datatable .= '<td width="33%"><span class="LC_left_item"><label>'.&mt('Default Login Page Help File In Use').'</label></span></td>';
 2206: 				$datatable .= '<td width="33%"><span class="LC_right_item">&nbsp;</span></td>';
 2207: 			}
 2208: 		} else {
 2209: 			$datatable .= '<td><span class="LC_left_item">&nbsp;</span></td>';
 2210: 			$datatable .= '<td><span class="LC_right_item">&nbsp;</span></td>';
 2211: 		}
 2212:     	
 2213:      	$datatable .= '<td width="33%"><span class="LC_right_item">';
 2214:      	if ($switchserver) {
 2215:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2216:         } else {
 2217:         	$datatable .= &mt('Upload Custom Login Page Help File:');
 2218:             $datatable .='<input type="file" name="loginhelpurl" />';
 2219:         }
 2220:         $datatable .= '</span></td></tr>';
 2221:         
 2222:      }
 2223:      
 2224:      return $datatable;
 2225: 	
 2226: }
 2227: 
 2228: 
 2229: sub radiobutton_prefs {
 2230:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount) = @_;
 2231:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2232:                    (ref($choices) eq 'HASH'));
 2233: 
 2234:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2235: 
 2236:     foreach my $item (@{$toggles}) {
 2237:         if ($defaultchecked->{$item} eq 'on') {
 2238:             $checkedon{$item} = ' checked="checked" ';
 2239:             $checkedoff{$item} = ' ';
 2240:         } elsif ($defaultchecked->{$item} eq 'off') {
 2241:             $checkedoff{$item} = ' checked="checked" ';
 2242:             $checkedon{$item} = ' ';
 2243:         }
 2244:     }
 2245:     if (ref($settings) eq 'HASH') {
 2246:         foreach my $item (@{$toggles}) {
 2247:             if ($settings->{$item} eq '1') {
 2248:                 $checkedon{$item} =  ' checked="checked" ';
 2249:                 $checkedoff{$item} = ' ';
 2250:             } elsif ($settings->{$item} eq '0') {
 2251:                 $checkedoff{$item} =  ' checked="checked" ';
 2252:                 $checkedon{$item} = ' ';
 2253:             }
 2254:         }
 2255:     }
 2256:     foreach my $item (@{$toggles}) {
 2257:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2258:         $datatable .=
 2259:             '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices->{$item}.
 2260:             '</span></td>'.
 2261:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2262:             '<label><input type="radio" name="'.
 2263:             $item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').
 2264:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2265:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2266:             '</span></td>'.
 2267:             '</tr>';
 2268:         $itemcount ++;
 2269:     }
 2270:     return ($datatable,$itemcount);
 2271: }
 2272: 
 2273: sub print_coursedefaults {
 2274:     my ($position,$dom,$settings,$rowtotal) = @_;
 2275:     my ($css_class,$datatable);
 2276:     my $itemcount = 1;
 2277:     if ($position eq 'top') {
 2278:         my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2279:         %choices =
 2280:             &Apache::lonlocal::texthash (
 2281:                 canuse_pdfforms => 'Course/Community users can create/upload PDF forms',
 2282:         );
 2283:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2284:         @toggles = ('canuse_pdfforms',);
 2285:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2286:                                                  \%choices,$itemcount);
 2287:         $$rowtotal += $itemcount;
 2288:     } else {
 2289:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2290:         my %choices =
 2291:             &Apache::lonlocal::texthash (
 2292:                 anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2293:         );
 2294:         my $currdefresponder;
 2295:         if (ref($settings) eq 'HASH') {
 2296:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2297:         }
 2298:         if (!$currdefresponder) {
 2299:             $currdefresponder = 10;
 2300:         } elsif ($currdefresponder < 1) {
 2301:             $currdefresponder = 1;
 2302:         }
 2303:         $datatable .=
 2304:                '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices{'anonsurvey_threshold'}.
 2305:                 '</span></td>'.
 2306:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2307:                 '<input type="text" name="anonsurvey_threshold"'.
 2308:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2309:                 '</td></tr>';
 2310:     }
 2311:     return $datatable;
 2312: }
 2313: 
 2314: sub print_usersessions {
 2315:     my ($position,$dom,$settings,$rowtotal) = @_;
 2316:     my ($css_class,$datatable,%checked,%choices);
 2317:     my (%by_ip,%by_location,@intdoms);
 2318:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2319: 
 2320:     my @alldoms = &Apache::lonnet::all_domains();
 2321:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2322:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2323:     my %altids = &id_for_thisdom(%servers);
 2324:     my $itemcount = 1;
 2325:     if ($position eq 'top') {
 2326:         if (keys(%serverhomes) > 1) {
 2327:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2328:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2329:         } else {
 2330:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2331:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2332:         }
 2333:     } else {
 2334:         if (keys(%by_location) == 0) {
 2335:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2336:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2337:         } else {
 2338:             my %lt = &usersession_titles();
 2339:             my $numinrow = 5;
 2340:             my $prefix;
 2341:             my @types;
 2342:             if ($position eq 'bottom') {
 2343:                 $prefix = 'remote';
 2344:                 @types = ('version','excludedomain','includedomain');
 2345:             } else {
 2346:                 $prefix = 'hosted';
 2347:                 @types = ('excludedomain','includedomain');
 2348:             }
 2349:             my (%current,%checkedon,%checkedoff);
 2350:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2351:             my @locations = sort(keys(%by_location));
 2352:             foreach my $type (@types) {
 2353:                 $checkedon{$type} = '';
 2354:                 $checkedoff{$type} = ' checked="checked"';
 2355:             }
 2356:             if (ref($settings) eq 'HASH') {
 2357:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2358:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2359:                         $current{$key} = $settings->{$prefix}{$key};
 2360:                         if ($key eq 'version') {
 2361:                             if ($current{$key} ne '') {
 2362:                                 $checkedon{$key} = ' checked="checked"';
 2363:                                 $checkedoff{$key} = '';
 2364:                             }
 2365:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2366:                             $checkedon{$key} = ' checked="checked"';
 2367:                             $checkedoff{$key} = '';
 2368:                         }
 2369:                     }
 2370:                 }
 2371:             }
 2372:             foreach my $type (@types) {
 2373:                 next if ($type ne 'version' && !@locations);
 2374:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2375:                 $datatable .= '<tr'.$css_class.'>
 2376:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2377:                                <span class="LC_nobreak">&nbsp;
 2378:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2379:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2380:                 if ($type eq 'version') {
 2381:                     my $selector = '<select name="'.$prefix.'_version">';
 2382:                     foreach my $version (@lcversions) {
 2383:                         my $selected = '';
 2384:                         if ($current{'version'} eq $version) {
 2385:                             $selected = ' selected="selected"';
 2386:                         }
 2387:                         $selector .= ' <option value="'.$version.'"'.
 2388:                                      $selected.'>'.$version.'</option>';
 2389:                     }
 2390:                     $selector .= '</select> ';
 2391:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2392:                 } else {
 2393:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2394:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2395:                                  ' />'.('&nbsp;'x2).
 2396:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2397:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2398:                                  "\n".
 2399:                                  '</div><div><table>';
 2400:                     my $rem;
 2401:                     for (my $i=0; $i<@locations; $i++) {
 2402:                         my ($showloc,$value,$checkedtype);
 2403:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2404:                             my $ip = $by_location{$locations[$i]}->[0];
 2405:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2406:                                  $value = join(':',@{$by_ip{$ip}});
 2407:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2408:                                 if (ref($current{$type}) eq 'ARRAY') {
 2409:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2410:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2411:                                             $checkedtype = ' checked="checked"';
 2412:                                             last;
 2413:                                         }
 2414:                                     }
 2415:                                 }
 2416:                             }
 2417:                         }
 2418:                         $rem = $i%($numinrow);
 2419:                         if ($rem == 0) {
 2420:                             if ($i > 0) {
 2421:                                 $datatable .= '</tr>';
 2422:                             }
 2423:                             $datatable .= '<tr>';
 2424:                         }
 2425:                         $datatable .= '<td class="LC_left_item">'.
 2426:                                       '<span class="LC_nobreak"><label>'.
 2427:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2428:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2429:                                       '</label></span></td>';
 2430:                     }
 2431:                     $rem = @locations%($numinrow);
 2432:                     my $colsleft = $numinrow - $rem;
 2433:                     if ($colsleft > 1 ) {
 2434:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2435:                                       '&nbsp;</td>';
 2436:                     } elsif ($colsleft == 1) {
 2437:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2438:                     }
 2439:                     $datatable .= '</tr></table>';
 2440:                 }
 2441:                 $datatable .= '</td></tr>';
 2442:                 $itemcount ++;
 2443:             }
 2444:         }
 2445:     }
 2446:     $$rowtotal += $itemcount;
 2447:     return $datatable;
 2448: }
 2449: 
 2450: sub build_location_hashes {
 2451:     my ($intdoms,$by_ip,$by_location) = @_;
 2452:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2453:                   (ref($by_location) eq 'HASH')); 
 2454:     my %iphost = &Apache::lonnet::get_iphost();
 2455:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2456:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2457:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2458:         foreach my $id (@{$iphost{$primary_ip}}) {
 2459:             my $intdom = &Apache::lonnet::internet_dom($id);
 2460:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2461:                 push(@{$intdoms},$intdom);
 2462:             }
 2463:         }
 2464:     }
 2465:     foreach my $ip (keys(%iphost)) {
 2466:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2467:             foreach my $id (@{$iphost{$ip}}) {
 2468:                 my $location = &Apache::lonnet::internet_dom($id);
 2469:                 if ($location) {
 2470:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2471:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2472:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2473:                             push(@{$by_ip->{$ip}},$location);
 2474:                         }
 2475:                     } else {
 2476:                         $by_ip->{$ip} = [$location];
 2477:                     }
 2478:                 }
 2479:             }
 2480:         }
 2481:     }
 2482:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2483:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2484:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2485:             my $first = $by_ip->{$ip}->[0];
 2486:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2487:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2488:                     push(@{$by_location->{$first}},$ip);
 2489:                 }
 2490:             } else {
 2491:                 $by_location->{$first} = [$ip];
 2492:             }
 2493:         }
 2494:     }
 2495:     return;
 2496: }
 2497: 
 2498: sub current_offloads_to {
 2499:     my ($dom,$settings,$servers) = @_;
 2500:     my (%spareid,%otherdomconfigs);
 2501:     if (ref($servers) eq 'HASH') {
 2502:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2503:             my $gotspares;
 2504:             if (ref($settings) eq 'HASH') {
 2505:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2506:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2507:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2508:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2509:                         $gotspares = 1;
 2510:                     }
 2511:                 }
 2512:             }
 2513:             unless ($gotspares) {
 2514:                 my $gotspares;
 2515:                 my $serverhomeID =
 2516:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2517:                 my $serverhomedom =
 2518:                     &Apache::lonnet::host_domain($serverhomeID);
 2519:                 if ($serverhomedom ne $dom) {
 2520:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2521:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2522:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2523:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2524:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2525:                                 $gotspares = 1;
 2526:                             }
 2527:                         }
 2528:                     } else {
 2529:                         $otherdomconfigs{$serverhomedom} =
 2530:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2531:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2532:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2533:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2534:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2535:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2536:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2537:                                         $gotspares = 1;
 2538:                                     }
 2539:                                 }
 2540:                             }
 2541:                         }
 2542:                     }
 2543:                 }
 2544:             }
 2545:             unless ($gotspares) {
 2546:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2547:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2548:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2549:                } else {
 2550:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2551:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2552:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2553:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2554:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2555:                     } else {
 2556:                         my %what = (
 2557:                              spareid => 1,
 2558:                         );
 2559:                         my ($result,$returnhash) = 
 2560:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2561:                         if ($result eq 'ok') { 
 2562:                             if (ref($returnhash) eq 'HASH') {
 2563:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2564:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2565:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2566:                                 }
 2567:                             }
 2568:                         }
 2569:                     }
 2570:                 }
 2571:             }
 2572:         }
 2573:     }
 2574:     return %spareid;
 2575: }
 2576: 
 2577: sub spares_row {
 2578:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2579:     my $css_class;
 2580:     my $numinrow = 4;
 2581:     my $itemcount = 1;
 2582:     my $datatable;
 2583:     my %typetitles = &sparestype_titles();
 2584:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2585:         foreach my $server (sort(keys(%{$servers}))) {
 2586:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2587:             my ($othercontrol,$serverdom);
 2588:             if ($serverhome ne $server) {
 2589:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2590:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2591:             } else {
 2592:                 $serverdom = &Apache::lonnet::host_domain($server);
 2593:                 if ($serverdom ne $dom) {
 2594:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2595:                 }
 2596:             }
 2597:             next unless (ref($spareid->{$server}) eq 'HASH');
 2598:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2599:             $datatable .= '<tr'.$css_class.'>
 2600:                            <td rowspan="2">
 2601:                             <span class="LC_nobreak"><b>'.$server.'</b> when busy, offloads to:</span></td>'."\n";
 2602:             my (%current,%canselect);
 2603:             my @choices = 
 2604:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2605:             foreach my $type ('primary','default') {
 2606:                 if (ref($spareid->{$server}) eq 'HASH') {
 2607:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2608:                         my @spares = @{$spareid->{$server}{$type}};
 2609:                         if (@spares > 0) {
 2610:                             if ($othercontrol) {
 2611:                                 $current{$type} = join(', ',@spares);
 2612:                             } else {
 2613:                                 $current{$type} .= '<table>';
 2614:                                 my $numspares = scalar(@spares);
 2615:                                 for (my $i=0;  $i<@spares; $i++) {
 2616:                                     my $rem = $i%($numinrow);
 2617:                                     if ($rem == 0) {
 2618:                                         if ($i > 0) {
 2619:                                             $current{$type} .= '</tr>';
 2620:                                         }
 2621:                                         $current{$type} .= '<tr>';
 2622:                                     }
 2623:                                     $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;'.
 2624:                                                        $spareid->{$server}{$type}[$i].
 2625:                                                        '</label></td>'."\n";
 2626:                                 }
 2627:                                 my $rem = @spares%($numinrow);
 2628:                                 my $colsleft = $numinrow - $rem;
 2629:                                 if ($colsleft > 1 ) {
 2630:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2631:                                                        '" class="LC_left_item">'.
 2632:                                                        '&nbsp;</td>';
 2633:                                 } elsif ($colsleft == 1) {
 2634:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2635:                                 }
 2636:                                 $current{$type} .= '</tr></table>';
 2637:                             }
 2638:                         }
 2639:                     }
 2640:                     if ($current{$type} eq '') {
 2641:                         $current{$type} = &mt('None specified');
 2642:                     }
 2643:                     if ($othercontrol) {
 2644:                         if ($type eq 'primary') {
 2645:                             $canselect{$type} = $othercontrol;
 2646:                         }
 2647:                     } else {
 2648:                         $canselect{$type} = 
 2649:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2650:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2651:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2652:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2653:                         if (@choices > 0) {
 2654:                             foreach my $lonhost (@choices) {
 2655:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2656:                             }
 2657:                         }
 2658:                         $canselect{$type} .= '</select>'."\n";
 2659:                     }
 2660:                 } else {
 2661:                     $current{$type} = &mt('Could not be determined');
 2662:                     if ($type eq 'primary') {
 2663:                         $canselect{$type} =  $othercontrol;
 2664:                     }
 2665:                 }
 2666:                 if ($type eq 'default') {
 2667:                     $datatable .= '<tr'.$css_class.'>';
 2668:                 }
 2669:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2670:                               '<td>'.$current{$type}.'</td>'."\n".
 2671:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2672:             }
 2673:             $itemcount ++;
 2674:         }
 2675:     }
 2676:     $$rowtotal += $itemcount;
 2677:     return $datatable;
 2678: }
 2679: 
 2680: sub possible_newspares {
 2681:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2682:     my $serverhostname = &Apache::lonnet::hostname($server);
 2683:     my %excluded;
 2684:     if ($serverhostname ne '') {
 2685:         %excluded = (
 2686:                        $serverhostname => 1,
 2687:                     );
 2688:     }
 2689:     if (ref($currspares) eq 'HASH') {
 2690:         foreach my $type (keys(%{$currspares})) {
 2691:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2692:                 if (@{$currspares->{$type}} > 0) {
 2693:                     foreach my $curr (@{$currspares->{$type}}) {
 2694:                         my $hostname = &Apache::lonnet::hostname($curr);
 2695:                         $excluded{$hostname} = 1;
 2696:                     }
 2697:                 }
 2698:             }
 2699:         }
 2700:     }
 2701:     my @choices;
 2702:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2703:         if (keys(%{$serverhomes}) > 1) {
 2704:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2705:                 unless ($excluded{$name}) {
 2706:                     if (exists($altids->{$serverhomes->{$name}})) {
 2707:                         push(@choices,$altids->{$serverhomes->{$name}});
 2708:                     } else {
 2709:                         push(@choices,$serverhomes->{$name});
 2710:                     }
 2711:                 }
 2712:             }
 2713:         }
 2714:     }
 2715:     return sort(@choices);
 2716: }
 2717: 
 2718: sub print_loadbalancing {
 2719:     my ($dom,$settings,$rowtotal) = @_;
 2720:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2721:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2722:     my $numinrow = 1;
 2723:     my $datatable;
 2724:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2725:     my ($currbalancer,$currtargets,$currrules);
 2726:     if (keys(%servers) > 1) {
 2727:         if (ref($settings) eq 'HASH') {
 2728:             $currbalancer = $settings->{'lonhost'};
 2729:             $currtargets = $settings->{'targets'};
 2730:             $currrules = $settings->{'rules'};
 2731:         } else {
 2732:             ($currbalancer,$currtargets) = 
 2733:                 &Apache::lonnet::get_lonbalancer_config(\%servers);
 2734:         }
 2735:     } else {
 2736:         return;
 2737:     }
 2738:     my ($othertitle,$usertypes,$types) =
 2739:         &Apache::loncommon::sorted_inst_types($dom);
 2740:     my $rownum = 6;
 2741:     if (ref($types) eq 'ARRAY') {
 2742:         $rownum += scalar(@{$types});
 2743:     }
 2744:     my $css_class = ' class="LC_odd_row"';
 2745:     my $targets_div_style = 'display: none';
 2746:     my $disabled_div_style = 'display: block';
 2747:     my $homedom_div_style = 'display: none';
 2748:     $datatable = '<tr'.$css_class.'>'.
 2749:                  '<td rowspan="'.$rownum.'" valign="top">'.
 2750:                  '<p><select name="loadbalancing_lonhost" onchange="toggleTargets();">'."\n".
 2751:                  '<option value=""';
 2752:     if (($currbalancer eq '') || (!grep(/^\Q$currbalancer\E$/,keys(%servers)))) {
 2753:         $datatable .= ' selected="selected"';
 2754:     } else {
 2755:         $targets_div_style = 'display: block';
 2756:         $disabled_div_style = 'display: none';
 2757:         if ($dom eq &Apache::lonnet::host_domain($currbalancer)) {
 2758:             $homedom_div_style = 'display: block'; 
 2759:         }
 2760:     }
 2761:     $datatable .= '>'.&mt('None').'</option>'."\n";
 2762:     foreach my $lonhost (sort(keys(%servers))) {
 2763:         my $selected;
 2764:         if ($lonhost eq $currbalancer) {
 2765:             $selected .= ' selected="selected"';
 2766:         }
 2767:         $datatable .= '<option value="'.$lonhost.'"'.$selected.'>'.$lonhost.'</option>'."\n";
 2768:     }
 2769:     $datatable .= '</select></p></td><td rowspan="'.$rownum.'" valign="top">'.
 2770:                   '<div id="loadbalancing_disabled" style="'.$disabled_div_style.'">'.&mt('No dedicated Load Balancer').'</div>'."\n".
 2771:                   '<div id="loadbalancing_targets" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2772:     my ($numspares,@spares) = &count_servers($currbalancer,%servers);
 2773:     my @sparestypes = ('primary','default');
 2774:     my %typetitles = &sparestype_titles();
 2775:     foreach my $sparetype (@sparestypes) {
 2776:         my $targettable;
 2777:         for (my $i=0; $i<$numspares; $i++) {
 2778:             my $checked;
 2779:             if (ref($currtargets) eq 'HASH') {
 2780:                 if (ref($currtargets->{$sparetype}) eq 'ARRAY') {
 2781:                     if (grep(/^\Q$spares[$i]\E$/,@{$currtargets->{$sparetype}})) {
 2782:                         $checked = ' checked="checked"';
 2783:                     }
 2784:                 }
 2785:             }
 2786:             my $chkboxval;
 2787:             if (($currbalancer ne '') && (grep((/^\Q$currbalancer\E$/,keys(%servers))))) {
 2788:                 $chkboxval = $spares[$i];
 2789:             }
 2790:             $targettable .= '<td><label><input type="checkbox" name="loadbalancing_target_'.$sparetype.'"'.
 2791:                       $checked.' value="'.$chkboxval.'" id="loadbalancing_target_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$sparetype'".');" /><span id="loadbalancing_targettxt_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 2792:                       '</span></label></td>';
 2793:             my $rem = $i%($numinrow);
 2794:             if ($rem == 0) {
 2795:                 if ($i > 0) {
 2796:                     $targettable .= '</tr>';
 2797:                 }
 2798:                 $targettable .= '<tr>';
 2799:             }
 2800:         }
 2801:         if ($targettable ne '') {
 2802:             my $rem = $numspares%($numinrow);
 2803:             my $colsleft = $numinrow - $rem;
 2804:             if ($colsleft > 1 ) {
 2805:                 $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2806:                                 '&nbsp;</td>';
 2807:             } elsif ($colsleft == 1) {
 2808:                 $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 2809:             }
 2810:             $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 2811:                            '<table><tr>'.$targettable.'</table><br />';
 2812:         }
 2813:     }
 2814:     $datatable .= '</div></td></tr>'.
 2815:                   &loadbalancing_rules($dom,$intdom,$currrules,$othertitle,
 2816:                                        $usertypes,$types,\%servers,$currbalancer,
 2817:                                        $targets_div_style,$homedom_div_style,$css_class);
 2818:     $$rowtotal += $rownum;
 2819:     return $datatable;
 2820: }
 2821: 
 2822: sub loadbalancing_rules {
 2823:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 2824:         $currbalancer,$targets_div_style,$homedom_div_style,$css_class) = @_;
 2825:     my $output;
 2826:     my ($alltypes,$othertypes,$titles) = 
 2827:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 2828:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 2829:         foreach my $type (@{$alltypes}) {
 2830:             my $current;
 2831:             if (ref($currrules) eq 'HASH') {
 2832:                 $current = $currrules->{$type};
 2833:             }
 2834:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 2835:                 if ($dom ne &Apache::lonnet::host_domain($currbalancer)) {
 2836:                     $current = '';
 2837:                 }
 2838:             }
 2839:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 2840:                                              $servers,$currbalancer,$dom,
 2841:                                              $targets_div_style,$homedom_div_style,$css_class);
 2842:         }
 2843:     }
 2844:     return $output;
 2845: }
 2846: 
 2847: sub loadbalancing_titles {
 2848:     my ($dom,$intdom,$usertypes,$types) = @_;
 2849:     my %othertypes = (
 2850:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 2851:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 2852:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 2853:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 2854:                      );
 2855:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 2856:     if (ref($types) eq 'ARRAY') {
 2857:         unshift(@alltypes,@{$types},'default');
 2858:     }
 2859:     my %titles;
 2860:     foreach my $type (@alltypes) {
 2861:         if ($type =~ /^_LC_/) {
 2862:             $titles{$type} = $othertypes{$type};
 2863:         } elsif ($type eq 'default') {
 2864:             $titles{$type} = &mt('All users from [_1]',$dom);
 2865:             if (ref($types) eq 'ARRAY') {
 2866:                 if (@{$types} > 0) {
 2867:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 2868:                 }
 2869:             }
 2870:         } elsif (ref($usertypes) eq 'HASH') {
 2871:             $titles{$type} = $usertypes->{$type};
 2872:         }
 2873:     }
 2874:     return (\@alltypes,\%othertypes,\%titles);
 2875: }
 2876: 
 2877: sub loadbalance_rule_row {
 2878:     my ($type,$title,$current,$servers,$currbalancer,$dom,$targets_div_style,
 2879:         $homedom_div_style,$css_class) = @_;
 2880:     my @rulenames = ('default','homeserver');
 2881:     my %ruletitles = &offloadtype_text();
 2882:     if ($type eq '_LC_external') {
 2883:         push(@rulenames,'externalbalancer');
 2884:     } else {
 2885:         push(@rulenames,'specific');
 2886:     }
 2887:     push(@rulenames,'none');
 2888:     my $style = $targets_div_style;
 2889:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 2890:         $style = $homedom_div_style;
 2891:     }
 2892:     my $output = 
 2893:         '<tr'.$css_class.'><td valign="top"><div id="balanceruletitle_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 2894:         '<td><div id="balancerule_'.$type.'" style="'.$style.'">'."\n";
 2895:     for (my $i=0; $i<@rulenames; $i++) {
 2896:         my $rule = $rulenames[$i];
 2897:         my ($checked,$extra);
 2898:         if ($rulenames[$i] eq 'default') {
 2899:             $rule = '';
 2900:         }
 2901:         if ($rulenames[$i] eq 'specific') {
 2902:             if (ref($servers) eq 'HASH') {
 2903:                 my $default;
 2904:                 if (($current ne '') && (exists($servers->{$current}))) {
 2905:                     $checked = ' checked="checked"';
 2906:                 }
 2907:                 unless ($checked) {
 2908:                     $default = ' selected="selected"';
 2909:                 }
 2910:                 $extra = ':&nbsp;<select name="loadbalancing_singleserver_'.$type.
 2911:                          '" id="loadbalancing_singleserver_'.$type.
 2912:                          '" onchange="singleServerToggle('."'$type'".')">'."\n".
 2913:                          '<option value=""'.$default.'></option>'."\n";
 2914:                 foreach my $lonhost (sort(keys(%{$servers}))) {
 2915:                     next if ($lonhost eq $currbalancer);
 2916:                     my $selected;
 2917:                     if ($lonhost eq $current) {
 2918:                         $selected = ' selected="selected"';
 2919:                     }
 2920:                     $extra .= '<option value="'.$lonhost.'"'.$selected.'>'.$lonhost.'</option>';
 2921:                 }
 2922:                 $extra .= '</select>';
 2923:             }
 2924:         } elsif ($rule eq $current) {
 2925:             $checked = ' checked="checked"';
 2926:         }
 2927:         $output .= '<span class="LC_nobreak"><label>'.
 2928:                    '<input type="radio" name="loadbalancing_rules_'.$type.
 2929:                    '" id="loadbalancing_rules_'.$type.'_'.$i.'" value="'.
 2930:                    $rule.'" onclick="balanceruleChange('."this.form,'$type'".
 2931:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 2932:                    '</label>'.$extra.'</span><br />'."\n";
 2933:     }
 2934:     $output .= '</div></td></tr>'."\n";
 2935:     return $output;
 2936: }
 2937: 
 2938: sub offloadtype_text {
 2939:     my %ruletitles = &Apache::lonlocal::texthash (
 2940:            'default'          => 'Offloads to default destinations',
 2941:            'homeserver'       => "Offloads to user's home server",
 2942:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 2943:            'specific'         => 'Offloads to specific server',
 2944:            'none'             => 'No offload',
 2945:     );
 2946:     return %ruletitles;
 2947: }
 2948: 
 2949: sub sparestype_titles {
 2950:     my %typestitles = &Apache::lonlocal::texthash (
 2951:                           'primary' => 'primary',
 2952:                           'default' => 'default',
 2953:                       );
 2954:     return %typestitles;
 2955: }
 2956: 
 2957: sub contact_titles {
 2958:     my %titles = &Apache::lonlocal::texthash (
 2959:                    'supportemail' => 'Support E-mail address',
 2960:                    'adminemail'   => 'Default Server Admin E-mail address',
 2961:                    'errormail'    => 'Error reports to be e-mailed to',
 2962:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 2963:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 2964:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 2965:                    'requestsmail' => 'E-mail from course requests requiring approval',
 2966:                  );
 2967:     my %short_titles = &Apache::lonlocal::texthash (
 2968:                            adminemail   => 'Admin E-mail address',
 2969:                            supportemail => 'Support E-mail',
 2970:                        );   
 2971:     return (\%titles,\%short_titles);
 2972: }
 2973: 
 2974: sub tool_titles {
 2975:     my %titles = &Apache::lonlocal::texthash (
 2976:                      aboutme    => 'Personal web page',
 2977:                      blog       => 'Blog',
 2978:                      webdav     => 'WebDAV',
 2979:                      portfolio  => 'Portfolio',
 2980:                      official   => 'Official courses (with institutional codes)',
 2981:                      unofficial => 'Unofficial courses',
 2982:                      community  => 'Communities',
 2983:                  );
 2984:     return %titles;
 2985: }
 2986: 
 2987: sub courserequest_titles {
 2988:     my %titles = &Apache::lonlocal::texthash (
 2989:                                    official   => 'Official',
 2990:                                    unofficial => 'Unofficial',
 2991:                                    community  => 'Communities',
 2992:                                    norequest  => 'Not allowed',
 2993:                                    approval   => 'Approval by Dom. Coord.',
 2994:                                    validate   => 'With validation',
 2995:                                    autolimit  => 'Numerical limit',
 2996:                                    unlimited  => '(blank for unlimited)',
 2997:                  );
 2998:     return %titles;
 2999: }
 3000: 
 3001: sub courserequest_conditions {
 3002:     my %conditions = &Apache::lonlocal::texthash (
 3003:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3004:        validate   => '(Processing of request subject to instittutional validation).',
 3005:                  );
 3006:     return %conditions;
 3007: }
 3008: 
 3009: 
 3010: sub print_usercreation {
 3011:     my ($position,$dom,$settings,$rowtotal) = @_;
 3012:     my $numinrow = 4;
 3013:     my $datatable;
 3014:     if ($position eq 'top') {
 3015:         $$rowtotal ++;
 3016:         my $rowcount = 0;
 3017:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3018:         if (ref($rules) eq 'HASH') {
 3019:             if (keys(%{$rules}) > 0) {
 3020:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3021:                                                 $ruleorder,$numinrow,$rowcount);
 3022:                 $$rowtotal ++;
 3023:                 $rowcount ++;
 3024:             }
 3025:         }
 3026:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3027:         if (ref($idrules) eq 'HASH') {
 3028:             if (keys(%{$idrules}) > 0) {
 3029:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3030:                                                 $idruleorder,$numinrow,$rowcount);
 3031:                 $$rowtotal ++;
 3032:                 $rowcount ++;
 3033:             }
 3034:         }
 3035:         my ($emailrules,$emailruleorder) = 
 3036:             &Apache::lonnet::inst_userrules($dom,'email');
 3037:         if (ref($emailrules) eq 'HASH') {
 3038:             if (keys(%{$emailrules}) > 0) {
 3039:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3040:                                                 $emailruleorder,$numinrow,$rowcount);
 3041:                 $$rowtotal ++;
 3042:                 $rowcount ++;
 3043:             }
 3044:         }
 3045:         if ($rowcount == 0) {
 3046:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3047:             $$rowtotal ++;
 3048:             $rowcount ++;
 3049:         }
 3050:     } elsif ($position eq 'middle') {
 3051:         my @creators = ('author','course','requestcrs','selfcreate');
 3052:         my ($rules,$ruleorder) =
 3053:             &Apache::lonnet::inst_userrules($dom,'username');
 3054:         my %lt = &usercreation_types();
 3055:         my %checked;
 3056:         my @selfcreate; 
 3057:         if (ref($settings) eq 'HASH') {
 3058:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3059:                 foreach my $item (@creators) {
 3060:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3061:                 }
 3062:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3063:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3064:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3065:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3066:                         @selfcreate = ('email','login','sso');
 3067:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3068:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3069:                     }
 3070:                 }
 3071:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3072:                 foreach my $item (@creators) {
 3073:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3074:                         $checked{$item} = 'none';
 3075:                     }
 3076:                 }
 3077:             }
 3078:         }
 3079:         my $rownum = 0;
 3080:         foreach my $item (@creators) {
 3081:             $rownum ++;
 3082:             if ($item ne 'selfcreate') {  
 3083:                 if ($checked{$item} eq '') {
 3084:                     $checked{$item} = 'any';
 3085:                 }
 3086:             }
 3087:             my $css_class;
 3088:             if ($rownum%2) {
 3089:                 $css_class = '';
 3090:             } else {
 3091:                 $css_class = ' class="LC_odd_row" ';
 3092:             }
 3093:             $datatable .= '<tr'.$css_class.'>'.
 3094:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3095:                          '</span></td><td align="right">';
 3096:             my @options;
 3097:             if ($item eq 'selfcreate') {
 3098:                 push(@options,('email','login','sso'));
 3099:             } else {
 3100:                 @options = ('any');
 3101:                 if (ref($rules) eq 'HASH') {
 3102:                     if (keys(%{$rules}) > 0) {
 3103:                         push(@options,('official','unofficial'));
 3104:                     }
 3105:                 }
 3106:                 push(@options,'none');
 3107:             }
 3108:             foreach my $option (@options) {
 3109:                 my $type = 'radio';
 3110:                 my $check = ' ';
 3111:                 if ($item eq 'selfcreate') {
 3112:                     $type = 'checkbox';
 3113:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3114:                         $check = ' checked="checked" ';
 3115:                     }
 3116:                 } else {
 3117:                     if ($checked{$item} eq $option) {
 3118:                         $check = ' checked="checked" ';
 3119:                     }
 3120:                 } 
 3121:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3122:                               '<input type="'.$type.'" name="can_createuser_'.
 3123:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3124:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3125:             }
 3126:             $datatable .= '</td></tr>';
 3127:         }
 3128:         my ($othertitle,$usertypes,$types) =
 3129:             &Apache::loncommon::sorted_inst_types($dom);
 3130:         if (ref($usertypes) eq 'HASH') {
 3131:             if (keys(%{$usertypes}) > 0) {
 3132:                 my $createsettings;
 3133:                 if (ref($settings) eq 'HASH') {
 3134:                     $createsettings = $settings->{cancreate};
 3135:                 }
 3136:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3137:                                              $dom,$numinrow,$othertitle,
 3138:                                              'statustocreate');
 3139:                 $$rowtotal ++;
 3140:             }
 3141:         }
 3142:     } else {
 3143:         my @contexts = ('author','course','domain');
 3144:         my @authtypes = ('int','krb4','krb5','loc');
 3145:         my %checked;
 3146:         if (ref($settings) eq 'HASH') {
 3147:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3148:                 foreach my $item (@contexts) {
 3149:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3150:                         foreach my $auth (@authtypes) {
 3151:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3152:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3153:                             }
 3154:                         }
 3155:                     }
 3156:                 }
 3157:             }
 3158:         } else {
 3159:             foreach my $item (@contexts) {
 3160:                 foreach my $auth (@authtypes) {
 3161:                     $checked{$item}{$auth} = ' checked="checked" ';
 3162:                 }
 3163:             }
 3164:         }
 3165:         my %title = &context_names();
 3166:         my %authname = &authtype_names();
 3167:         my $rownum = 0;
 3168:         my $css_class; 
 3169:         foreach my $item (@contexts) {
 3170:             if ($rownum%2) {
 3171:                 $css_class = '';
 3172:             } else {
 3173:                 $css_class = ' class="LC_odd_row" ';
 3174:             }
 3175:             $datatable .=   '<tr'.$css_class.'>'.
 3176:                             '<td>'.$title{$item}.
 3177:                             '</td><td class="LC_left_item">'.
 3178:                             '<span class="LC_nobreak">';
 3179:             foreach my $auth (@authtypes) {
 3180:                 $datatable .= '<label>'. 
 3181:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3182:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3183:                               $authname{$auth}.'</label>&nbsp;';
 3184:             }
 3185:             $datatable .= '</span></td></tr>';
 3186:             $rownum ++;
 3187:         }
 3188:         $$rowtotal += $rownum;
 3189:     }
 3190:     return $datatable;
 3191: }
 3192: 
 3193: sub user_formats_row {
 3194:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3195:     my $output;
 3196:     my %text = (
 3197:                    'username' => 'new usernames',
 3198:                    'id'       => 'IDs',
 3199:                    'email'    => 'self-created accounts (e-mail)',
 3200:                );
 3201:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3202:     $output = '<tr '.$css_class.'>'.
 3203:               '<td><span class="LC_nobreak">';
 3204:     if ($type eq 'email') {
 3205:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3206:     } else {
 3207:         $output .= &mt("Format rules to check for $text{$type}: ");
 3208:     }
 3209:     $output .= '</span></td>'.
 3210:                '<td class="LC_left_item" colspan="2"><table>';
 3211:     my $rem;
 3212:     if (ref($ruleorder) eq 'ARRAY') {
 3213:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3214:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3215:                 my $rem = $i%($numinrow);
 3216:                 if ($rem == 0) {
 3217:                     if ($i > 0) {
 3218:                         $output .= '</tr>';
 3219:                     }
 3220:                     $output .= '<tr>';
 3221:                 }
 3222:                 my $check = ' ';
 3223:                 if (ref($settings) eq 'HASH') {
 3224:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3225:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3226:                             $check = ' checked="checked" ';
 3227:                         }
 3228:                     }
 3229:                 }
 3230:                 $output .= '<td class="LC_left_item">'.
 3231:                            '<span class="LC_nobreak"><label>'.
 3232:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3233:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3234:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3235:             }
 3236:         }
 3237:         $rem = @{$ruleorder}%($numinrow);
 3238:     }
 3239:     my $colsleft = $numinrow - $rem;
 3240:     if ($colsleft > 1 ) {
 3241:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3242:                    '&nbsp;</td>';
 3243:     } elsif ($colsleft == 1) {
 3244:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3245:     }
 3246:     $output .= '</tr></table></td></tr>';
 3247:     return $output;
 3248: }
 3249: 
 3250: sub usercreation_types {
 3251:     my %lt = &Apache::lonlocal::texthash (
 3252:                     author     => 'When adding a co-author',
 3253:                     course     => 'When adding a user to a course',
 3254:                     requestcrs => 'When requesting a course',
 3255:                     selfcreate => 'User creates own account', 
 3256:                     any        => 'Any',
 3257:                     official   => 'Institutional only ',
 3258:                     unofficial => 'Non-institutional only',
 3259:                     email      => 'E-mail address',
 3260:                     login      => 'Institutional Login',
 3261:                     sso        => 'SSO', 
 3262:                     none       => 'None',
 3263:     );
 3264:     return %lt;
 3265: }
 3266: 
 3267: sub authtype_names {
 3268:     my %lt = &Apache::lonlocal::texthash(
 3269:                       int    => 'Internal',
 3270:                       krb4   => 'Kerberos 4',
 3271:                       krb5   => 'Kerberos 5',
 3272:                       loc    => 'Local',
 3273:                   );
 3274:     return %lt;
 3275: }
 3276: 
 3277: sub context_names {
 3278:     my %context_title = &Apache::lonlocal::texthash(
 3279:        author => 'Creating users when an Author',
 3280:        course => 'Creating users when in a course',
 3281:        domain => 'Creating users when a Domain Coordinator',
 3282:     );
 3283:     return %context_title;
 3284: }
 3285: 
 3286: sub print_usermodification {
 3287:     my ($position,$dom,$settings,$rowtotal) = @_;
 3288:     my $numinrow = 4;
 3289:     my ($context,$datatable,$rowcount);
 3290:     if ($position eq 'top') {
 3291:         $rowcount = 0;
 3292:         $context = 'author'; 
 3293:         foreach my $role ('ca','aa') {
 3294:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3295:                                                    $numinrow,$rowcount);
 3296:             $$rowtotal ++;
 3297:             $rowcount ++;
 3298:         }
 3299:     } elsif ($position eq 'middle') {
 3300:         $context = 'course';
 3301:         $rowcount = 0;
 3302:         foreach my $role ('st','ep','ta','in','cr') {
 3303:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3304:                                                    $numinrow,$rowcount);
 3305:             $$rowtotal ++;
 3306:             $rowcount ++;
 3307:         }
 3308:     } elsif ($position eq 'bottom') {
 3309:         $context = 'selfcreate';
 3310:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3311:         $usertypes->{'default'} = $othertitle;
 3312:         if (ref($types) eq 'ARRAY') {
 3313:             push(@{$types},'default');
 3314:             $usertypes->{'default'} = $othertitle;
 3315:             foreach my $status (@{$types}) {
 3316:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3317:                                                        $numinrow,$rowcount,$usertypes);
 3318:                 $$rowtotal ++;
 3319:                 $rowcount ++;
 3320:             }
 3321:         }
 3322:     }
 3323:     return $datatable;
 3324: }
 3325: 
 3326: sub print_defaults {
 3327:     my ($dom,$rowtotal) = @_;
 3328:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3329:                  'datelocale_def','portal_def');
 3330:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3331:     my $titles = &defaults_titles($dom);
 3332:     my $rownum = 0;
 3333:     my ($datatable,$css_class);
 3334:     foreach my $item (@items) {
 3335:         if ($rownum%2) {
 3336:             $css_class = '';
 3337:         } else {
 3338:             $css_class = ' class="LC_odd_row" ';
 3339:         }
 3340:         $datatable .= '<tr'.$css_class.'>'.
 3341:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3342:                   '</span></td><td class="LC_right_item">';
 3343:         if ($item eq 'auth_def') {
 3344:             my @authtypes = ('internal','krb4','krb5','localauth');
 3345:             my %shortauth = (
 3346:                              internal => 'int',
 3347:                              krb4 => 'krb4',
 3348:                              krb5 => 'krb5',
 3349:                              localauth  => 'loc'
 3350:                            );
 3351:             my %authnames = &authtype_names();
 3352:             foreach my $auth (@authtypes) {
 3353:                 my $checked = ' ';
 3354:                 if ($domdefaults{$item} eq $auth) {
 3355:                     $checked = ' checked="checked" ';
 3356:                 }
 3357:                 $datatable .= '<label><input type="radio" name="'.$item.
 3358:                               '" value="'.$auth.'"'.$checked.'/>'.
 3359:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3360:             }
 3361:         } elsif ($item eq 'timezone_def') {
 3362:             my $includeempty = 1;
 3363:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3364:         } elsif ($item eq 'datelocale_def') {
 3365:             my $includeempty = 1;
 3366:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3367:         } else {
 3368:             my $size;
 3369:             if ($item eq 'portal_def') {
 3370:                 $size = ' size="25"';
 3371:             }
 3372:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3373:                           $domdefaults{$item}.'"'.$size.' />';
 3374:         }
 3375:         $datatable .= '</td></tr>';
 3376:         $rownum ++;
 3377:     }
 3378:     $$rowtotal += $rownum;
 3379:     return $datatable;
 3380: }
 3381: 
 3382: sub defaults_titles {
 3383:     my ($dom) = @_;
 3384:     my %titles = &Apache::lonlocal::texthash (
 3385:                    'auth_def'      => 'Default authentication type',
 3386:                    'auth_arg_def'  => 'Default authentication argument',
 3387:                    'lang_def'      => 'Default language',
 3388:                    'timezone_def'  => 'Default timezone',
 3389:                    'datelocale_def' => 'Default locale for dates',
 3390:                    'portal_def'     => 'Portal/Default URL',
 3391:                  );
 3392:     if ($dom) {
 3393:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3394:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3395:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3396:         $protocol = 'http' if ($protocol ne 'https');
 3397:         if ($uint_dom) {
 3398:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3399:                                          $uint_dom);
 3400:         }
 3401:     }
 3402:     return (\%titles);
 3403: }
 3404: 
 3405: sub print_scantronformat {
 3406:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3407:     my $itemcount = 1;
 3408:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3409:         %confhash);
 3410:     my $switchserver = &check_switchserver($dom,$confname);
 3411:     my %lt = &Apache::lonlocal::texthash (
 3412:                 default => 'Default bubblesheet format file error',
 3413:                 custom  => 'Custom bubblesheet format file error',
 3414:              );
 3415:     my %scantronfiles = (
 3416:         default => 'default.tab',
 3417:         custom => 'custom.tab',
 3418:     );
 3419:     foreach my $key (keys(%scantronfiles)) {
 3420:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3421:                               .$scantronfiles{$key};
 3422:     }
 3423:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3424:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3425:         if (!$switchserver) {
 3426:             my $servadm = $r->dir_config('lonAdmEMail');
 3427:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3428:             if ($configuserok eq 'ok') {
 3429:                 if ($author_ok eq 'ok') {
 3430:                     my %legacyfile = (
 3431:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3432:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3433:                     );
 3434:                     my %md5chk;
 3435:                     foreach my $type (keys(%legacyfile)) {
 3436:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3437:                         chomp($md5chk{$type});
 3438:                     }
 3439:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3440:                         foreach my $type (keys(%legacyfile)) {
 3441:                             ($scantronurls{$type},my $error) = 
 3442:                                 &legacy_scantronformat($r,$dom,$confname,
 3443:                                                  $type,$legacyfile{$type},
 3444:                                                  $scantronurls{$type},
 3445:                                                  $scantronfiles{$type});
 3446:                             if ($error ne '') {
 3447:                                 $error{$type} = $error;
 3448:                             }
 3449:                         }
 3450:                         if (keys(%error) == 0) {
 3451:                             $is_custom = 1;
 3452:                             $confhash{'scantron'}{'scantronformat'} = 
 3453:                                 $scantronurls{'custom'};
 3454:                             my $putresult = 
 3455:                                 &Apache::lonnet::put_dom('configuration',
 3456:                                                          \%confhash,$dom);
 3457:                             if ($putresult ne 'ok') {
 3458:                                 $error{'custom'} = 
 3459:                                     '<span class="LC_error">'.
 3460:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3461:                             }
 3462:                         }
 3463:                     } else {
 3464:                         ($scantronurls{'default'},my $error) =
 3465:                             &legacy_scantronformat($r,$dom,$confname,
 3466:                                           'default',$legacyfile{'default'},
 3467:                                           $scantronurls{'default'},
 3468:                                           $scantronfiles{'default'});
 3469:                         if ($error eq '') {
 3470:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3471:                             my $putresult =
 3472:                                 &Apache::lonnet::put_dom('configuration',
 3473:                                                          \%confhash,$dom);
 3474:                             if ($putresult ne 'ok') {
 3475:                                 $error{'default'} =
 3476:                                     '<span class="LC_error">'.
 3477:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3478:                             }
 3479:                         } else {
 3480:                             $error{'default'} = $error;
 3481:                         }
 3482:                     }
 3483:                 }
 3484:             }
 3485:         } else {
 3486:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3487:         }
 3488:     }
 3489:     if (ref($settings) eq 'HASH') {
 3490:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3491:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3492:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3493:                 $scantronurl = '';
 3494:             } else {
 3495:                 $scantronurl = $settings->{'scantronformat'};
 3496:             }
 3497:             $is_custom = 1;
 3498:         } else {
 3499:             $scantronurl = $scantronurls{'default'};
 3500:         }
 3501:     } else {
 3502:         if ($is_custom) {
 3503:             $scantronurl = $scantronurls{'custom'};
 3504:         } else {
 3505:             $scantronurl = $scantronurls{'default'};
 3506:         }
 3507:     }
 3508:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3509:     $datatable .= '<tr'.$css_class.'>';
 3510:     if (!$is_custom) {
 3511:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3512:                       '<span class="LC_nobreak">';
 3513:         if ($scantronurl) {
 3514:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3515:                           &mt('Default bubblesheet format file').'</a>';
 3516:         } else {
 3517:             $datatable = &mt('File unavailable for display');
 3518:         }
 3519:         $datatable .= '</span></td>';
 3520:         if (keys(%error) == 0) { 
 3521:             $datatable .= '<td valign="bottom">';
 3522:             if (!$switchserver) {
 3523:                 $datatable .= &mt('Upload:').'<br />';
 3524:             }
 3525:         } else {
 3526:             my $errorstr;
 3527:             foreach my $key (sort(keys(%error))) {
 3528:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3529:             }
 3530:             $datatable .= '<td>'.$errorstr;
 3531:         }
 3532:     } else {
 3533:         if (keys(%error) > 0) {
 3534:             my $errorstr;
 3535:             foreach my $key (sort(keys(%error))) {
 3536:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3537:             } 
 3538:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3539:         } elsif ($scantronurl) {
 3540:             $datatable .= '<td><span class="LC_nobreak">'.
 3541:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3542:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3543:                           '<input type="checkbox" name="scantronformat_del"'.
 3544:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3545:                           '<td><span class="LC_nobreak">&nbsp;'.
 3546:                           &mt('Replace:').'</span><br />';
 3547:         }
 3548:     }
 3549:     if (keys(%error) == 0) {
 3550:         if ($switchserver) {
 3551:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3552:         } else {
 3553:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3554:                          '<input type="file" name="scantronformat" /></span>';
 3555:         }
 3556:     }
 3557:     $datatable .= '</td></tr>';
 3558:     $$rowtotal ++;
 3559:     return $datatable;
 3560: }
 3561: 
 3562: sub legacy_scantronformat {
 3563:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3564:     my ($url,$error);
 3565:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3566:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3567:         (my $result,$url) =
 3568:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3569:                          '','',$newfile);
 3570:         if ($result ne 'ok') {
 3571:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3572:         }
 3573:     }
 3574:     return ($url,$error);
 3575: }
 3576: 
 3577: sub print_coursecategories {
 3578:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3579:     my $datatable;
 3580:     if ($position eq 'top') {
 3581:         my $toggle_cats_crs = ' ';
 3582:         my $toggle_cats_dom = ' checked="checked" ';
 3583:         my $can_cat_crs = ' ';
 3584:         my $can_cat_dom = ' checked="checked" ';
 3585:         my $toggle_catscomm_comm = ' ';
 3586:         my $toggle_catscomm_dom = ' checked="checked" ';
 3587:         my $can_catcomm_comm = ' ';
 3588:         my $can_catcomm_dom = ' checked="checked" ';
 3589: 
 3590:         if (ref($settings) eq 'HASH') {
 3591:             if ($settings->{'togglecats'} eq 'crs') {
 3592:                 $toggle_cats_crs = $toggle_cats_dom;
 3593:                 $toggle_cats_dom = ' ';
 3594:             }
 3595:             if ($settings->{'categorize'} eq 'crs') {
 3596:                 $can_cat_crs = $can_cat_dom;
 3597:                 $can_cat_dom = ' ';
 3598:             }
 3599:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3600:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3601:                 $toggle_catscomm_dom = ' ';
 3602:             }
 3603:             if ($settings->{'categorizecomm'} eq 'comm') {
 3604:                 $can_catcomm_comm = $can_catcomm_dom;
 3605:                 $can_catcomm_dom = ' ';
 3606:             }
 3607:         }
 3608:         my %title = &Apache::lonlocal::texthash (
 3609:                      togglecats     => 'Show/Hide a course in catalog',
 3610:                      togglecatscomm => 'Show/Hide a community in catalog',
 3611:                      categorize     => 'Assign a category to a course',
 3612:                      categorizecomm => 'Assign a category to a community',
 3613:                     );
 3614:         my %level = &Apache::lonlocal::texthash (
 3615:                      dom  => 'Set in Domain',
 3616:                      crs  => 'Set in Course',
 3617:                      comm => 'Set in Community',
 3618:                     );
 3619:         $datatable = '<tr class="LC_odd_row">'.
 3620:                   '<td>'.$title{'togglecats'}.'</td>'.
 3621:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3622:                   '<input type="radio" name="togglecats"'.
 3623:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3624:                   '<label><input type="radio" name="togglecats"'.
 3625:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3626:                   '</tr><tr>'.
 3627:                   '<td>'.$title{'categorize'}.'</td>'.
 3628:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3629:                   '<label><input type="radio" name="categorize"'.
 3630:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3631:                   '<label><input type="radio" name="categorize"'.
 3632:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3633:                   '</tr><tr class="LC_odd_row">'.
 3634:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 3635:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3636:                   '<input type="radio" name="togglecatscomm"'.
 3637:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3638:                   '<label><input type="radio" name="togglecatscomm"'.
 3639:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3640:                   '</tr><tr>'.
 3641:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 3642:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3643:                   '<label><input type="radio" name="categorizecomm"'.
 3644:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3645:                   '<label><input type="radio" name="categorizecomm"'.
 3646:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 3647:                   '</tr>';
 3648:         $$rowtotal += 4;
 3649:     } else {
 3650:         my $css_class;
 3651:         my $itemcount = 1;
 3652:         my $cathash; 
 3653:         if (ref($settings) eq 'HASH') {
 3654:             $cathash = $settings->{'cats'};
 3655:         }
 3656:         if (ref($cathash) eq 'HASH') {
 3657:             my (@cats,@trails,%allitems,%idx,@jsarray);
 3658:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 3659:                                                    \%allitems,\%idx,\@jsarray);
 3660:             my $maxdepth = scalar(@cats);
 3661:             my $colattrib = '';
 3662:             if ($maxdepth > 2) {
 3663:                 $colattrib = ' colspan="2" ';
 3664:             }
 3665:             my @path;
 3666:             if (@cats > 0) {
 3667:                 if (ref($cats[0]) eq 'ARRAY') {
 3668:                     my $numtop = @{$cats[0]};
 3669:                     my $maxnum = $numtop;
 3670:                     my %default_names = (
 3671:                           instcode    => &mt('Official courses'),
 3672:                           communities => &mt('Communities'),
 3673:                     );
 3674: 
 3675:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 3676:                         ($cathash->{'instcode::0'} eq '') ||
 3677:                         (!grep(/^communities$/,@{$cats[0]})) || 
 3678:                         ($cathash->{'communities::0'} eq '')) {
 3679:                         $maxnum ++;
 3680:                     }
 3681:                     my $lastidx;
 3682:                     for (my $i=0; $i<$numtop; $i++) {
 3683:                         my $parent = $cats[0][$i];
 3684:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3685:                         my $item = &escape($parent).'::0';
 3686:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 3687:                         $lastidx = $idx{$item};
 3688:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3689:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 3690:                         for (my $k=0; $k<=$maxnum; $k++) {
 3691:                             my $vpos = $k+1;
 3692:                             my $selstr;
 3693:                             if ($k == $i) {
 3694:                                 $selstr = ' selected="selected" ';
 3695:                             }
 3696:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3697:                         }
 3698:                         $datatable .= '</select></td><td>';
 3699:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 3700:                             $datatable .=  '<span class="LC_nobreak">'
 3701:                                            .$default_names{$parent}.'</span>';
 3702:                             if ($parent eq 'instcode') {
 3703:                                 $datatable .= '<br /><span class="LC_nobreak">('
 3704:                                               .&mt('with institutional codes')
 3705:                                               .')</span></td><td'.$colattrib.'>';
 3706:                             } else {
 3707:                                 $datatable .= '<table><tr><td>';
 3708:                             }
 3709:                             $datatable .= '<span class="LC_nobreak">'
 3710:                                           .'<label><input type="radio" name="'
 3711:                                           .$parent.'" value="1" checked="checked" />'
 3712:                                           .&mt('Display').'</label>';
 3713:                             if ($parent eq 'instcode') {
 3714:                                 $datatable .= '&nbsp;';
 3715:                             } else {
 3716:                                 $datatable .= '</span></td></tr><tr><td>'
 3717:                                               .'<span class="LC_nobreak">';
 3718:                             }
 3719:                             $datatable .= '<label><input type="radio" name="'
 3720:                                           .$parent.'" value="0" />'
 3721:                                           .&mt('Do not display').'</label></span>';
 3722:                             if ($parent eq 'communities') {
 3723:                                 $datatable .= '</td></tr></table>';
 3724:                             }
 3725:                             $datatable .= '</td>';
 3726:                         } else {
 3727:                             $datatable .= $parent
 3728:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 3729:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 3730:                         }
 3731:                         my $depth = 1;
 3732:                         push(@path,$parent);
 3733:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 3734:                         pop(@path);
 3735:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 3736:                         $itemcount ++;
 3737:                     }
 3738:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3739:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 3740:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 3741:                     for (my $k=0; $k<=$maxnum; $k++) {
 3742:                         my $vpos = $k+1;
 3743:                         my $selstr;
 3744:                         if ($k == $numtop) {
 3745:                             $selstr = ' selected="selected" ';
 3746:                         }
 3747:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3748:                     }
 3749:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 3750:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 3751:                                   .'</tr>'."\n";
 3752:                     $itemcount ++;
 3753:                     foreach my $default ('instcode','communities') {
 3754:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 3755:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3756:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 3757:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 3758:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 3759:                             for (my $k=0; $k<=$maxnum; $k++) {
 3760:                                 my $vpos = $k+1;
 3761:                                 my $selstr;
 3762:                                 if ($k == $maxnum) {
 3763:                                     $selstr = ' selected="selected" ';
 3764:                                 }
 3765:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3766:                             }
 3767:                             $datatable .= '</select></span></td>'.
 3768:                                           '<td><span class="LC_nobreak">'.
 3769:                                           $default_names{$default}.'</span>';
 3770:                             if ($default eq 'instcode') {
 3771:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 3772:                                               .&mt('with institutional codes').')</span>';
 3773:                             }
 3774:                             $datatable .= '</td>'
 3775:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 3776:                                           .&mt('Display').'</label>&nbsp;'
 3777:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 3778:                                           .&mt('Do not display').'</label></span></td></tr>';
 3779:                         }
 3780:                     }
 3781:                 }
 3782:             } else {
 3783:                 $datatable .= &initialize_categories($itemcount);
 3784:             }
 3785:         } else {
 3786:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 3787:                           .&initialize_categories($itemcount);
 3788:         }
 3789:         $$rowtotal += $itemcount;
 3790:     }
 3791:     return $datatable;
 3792: }
 3793: 
 3794: sub print_serverstatuses {
 3795:     my ($dom,$settings,$rowtotal) = @_;
 3796:     my $datatable;
 3797:     my @pages = &serverstatus_pages();
 3798:     my (%namedaccess,%machineaccess);
 3799:     foreach my $type (@pages) {
 3800:         $namedaccess{$type} = '';
 3801:         $machineaccess{$type}= '';
 3802:     }
 3803:     if (ref($settings) eq 'HASH') {
 3804:         foreach my $type (@pages) {
 3805:             if (exists($settings->{$type})) {
 3806:                 if (ref($settings->{$type}) eq 'HASH') {
 3807:                     foreach my $key (keys(%{$settings->{$type}})) {
 3808:                         if ($key eq 'namedusers') {
 3809:                             $namedaccess{$type} = $settings->{$type}->{$key};
 3810:                         } elsif ($key eq 'machines') {
 3811:                             $machineaccess{$type} = $settings->{$type}->{$key};
 3812:                         }
 3813:                     }
 3814:                 }
 3815:             }
 3816:         }
 3817:     }
 3818:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 3819:     my $rownum = 0;
 3820:     my $css_class;
 3821:     foreach my $type (@pages) {
 3822:         $rownum ++;
 3823:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3824:         $datatable .= '<tr'.$css_class.'>'.
 3825:                       '<td><span class="LC_nobreak">'.
 3826:                       $titles->{$type}.'</span></td>'.
 3827:                       '<td class="LC_left_item">'.
 3828:                       '<input type="text" name="'.$type.'_namedusers" '.
 3829:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 3830:                       '<td class="LC_right_item">'.
 3831:                       '<span class="LC_nobreak">'.
 3832:                       '<input type="text" name="'.$type.'_machines" '.
 3833:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 3834:                       '</td></tr>'."\n";
 3835:     }
 3836:     $$rowtotal += $rownum;
 3837:     return $datatable;
 3838: }
 3839: 
 3840: sub serverstatus_pages {
 3841:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 3842:             'clusterstatus','metadata_keywords','metadata_harvest',
 3843:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 3844: }
 3845: 
 3846: sub coursecategories_javascript {
 3847:     my ($settings) = @_;
 3848:     my ($output,$jstext,$cathash);
 3849:     if (ref($settings) eq 'HASH') {
 3850:         $cathash = $settings->{'cats'};
 3851:     }
 3852:     if (ref($cathash) eq 'HASH') {
 3853:         my (@cats,@jsarray,%idx);
 3854:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 3855:         if (@jsarray > 0) {
 3856:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 3857:             for (my $i=0; $i<@jsarray; $i++) {
 3858:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 3859:                     my $catstr = join('","',@{$jsarray[$i]});
 3860:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 3861:                 }
 3862:             }
 3863:         }
 3864:     } else {
 3865:         $jstext  = '    var categories = Array(1);'."\n".
 3866:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 3867:     }
 3868:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 3869:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 3870:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 3871:     $output = <<"ENDSCRIPT";
 3872: <script type="text/javascript">
 3873: // <![CDATA[
 3874: function reorderCats(form,parent,item,idx) {
 3875:     var changedVal;
 3876: $jstext
 3877:     var newpos = 'addcategory_pos';
 3878:     var current = new Array;
 3879:     if (parent == '') {
 3880:         var has_instcode = 0;
 3881:         var maxtop = categories[idx].length;
 3882:         for (var j=0; j<maxtop; j++) {
 3883:             if (categories[idx][j] == 'instcode::0') {
 3884:                 has_instcode == 1;
 3885:             }
 3886:         }
 3887:         if (has_instcode == 0) {
 3888:             categories[idx][maxtop] = 'instcode_pos';
 3889:         }
 3890:     } else {
 3891:         newpos += '_'+parent;
 3892:     }
 3893:     var maxh = 1 + categories[idx].length;
 3894:     var current = new Array;
 3895:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3896:     if (item == newpos) {
 3897:         changedVal = newitemVal;
 3898:     } else {
 3899:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3900:         current[newitemVal] = newpos;
 3901:     }
 3902:     for (var i=0; i<categories[idx].length; i++) {
 3903:         var elementName = categories[idx][i];
 3904:         if (elementName != item) {
 3905:             if (form.elements[elementName]) {
 3906:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3907:                 current[currVal] = elementName;
 3908:             }
 3909:         }
 3910:     }
 3911:     var oldVal;
 3912:     for (var j=0; j<maxh; j++) {
 3913:         if (current[j] == undefined) {
 3914:             oldVal = j;
 3915:         }
 3916:     }
 3917:     if (oldVal < changedVal) {
 3918:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3919:            var elementName = current[k];
 3920:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3921:         }
 3922:     } else {
 3923:         for (var k=changedVal; k<oldVal; k++) {
 3924:             var elementName = current[k];
 3925:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3926:         }
 3927:     }
 3928:     return;
 3929: }
 3930: 
 3931: function categoryCheck(form) {
 3932:     if (form.elements['addcategory_name'].value == 'instcode') {
 3933:         alert('$instcode_reserved\\n$choose_again');
 3934:         return false;
 3935:     }
 3936:     if (form.elements['addcategory_name'].value == 'communities') {
 3937:         alert('$communities_reserved\\n$choose_again');
 3938:         return false;
 3939:     }
 3940:     return true;
 3941: }
 3942: 
 3943: // ]]>
 3944: </script>
 3945: 
 3946: ENDSCRIPT
 3947:     return $output;
 3948: }
 3949: 
 3950: sub initialize_categories {
 3951:     my ($itemcount) = @_;
 3952:     my ($datatable,$css_class,$chgstr);
 3953:     my %default_names = (
 3954:                       instcode    => 'Official courses (with institutional codes)',
 3955:                       communities => 'Communities',
 3956:                         );
 3957:     my $select0 = ' selected="selected"';
 3958:     my $select1 = '';
 3959:     foreach my $default ('instcode','communities') {
 3960:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3961:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 3962:         if ($default eq 'communities') {
 3963:             $select1 = $select0;
 3964:             $select0 = '';
 3965:         }
 3966:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3967:                      .'<select name="'.$default.'_pos">'
 3968:                      .'<option value="0"'.$select0.'>1</option>'
 3969:                      .'<option value="1"'.$select1.'>2</option>'
 3970:                      .'<option value="2">3</option></select>&nbsp;'
 3971:                      .$default_names{$default}
 3972:                      .'</span></td><td><span class="LC_nobreak">'
 3973:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 3974:                      .&mt('Display').'</label>&nbsp;<label>'
 3975:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 3976:                  .'</label></span></td></tr>';
 3977:         $itemcount ++;
 3978:     }
 3979:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3980:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 3981:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3982:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 3983:                   .'<option value="0">1</option>'
 3984:                   .'<option value="1">2</option>'
 3985:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 3986:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 3987:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 3988:     return $datatable;
 3989: }
 3990: 
 3991: sub build_category_rows {
 3992:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 3993:     my ($text,$name,$item,$chgstr);
 3994:     if (ref($cats) eq 'ARRAY') {
 3995:         my $maxdepth = scalar(@{$cats});
 3996:         if (ref($cats->[$depth]) eq 'HASH') {
 3997:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 3998:                 my $numchildren = @{$cats->[$depth]{$parent}};
 3999:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4000:                 $text .= '<td><table class="LC_datatable">';
 4001:                 my ($idxnum,$parent_name,$parent_item);
 4002:                 my $higher = $depth - 1;
 4003:                 if ($higher == 0) {
 4004:                     $parent_name = &escape($parent).'::'.$higher;
 4005:                 } else {
 4006:                     if (ref($path) eq 'ARRAY') {
 4007:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4008:                     }
 4009:                 }
 4010:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4011:                 for (my $j=0; $j<=$numchildren; $j++) {
 4012:                     if ($j < $numchildren) {
 4013:                         $name = $cats->[$depth]{$parent}[$j];
 4014:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4015:                         $idxnum = $idx->{$item};
 4016:                     } else {
 4017:                         $name = $parent_name;
 4018:                         $item = $parent_item;
 4019:                     }
 4020:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4021:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4022:                     for (my $i=0; $i<=$numchildren; $i++) {
 4023:                         my $vpos = $i+1;
 4024:                         my $selstr;
 4025:                         if ($j == $i) {
 4026:                             $selstr = ' selected="selected" ';
 4027:                         }
 4028:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4029:                     }
 4030:                     $text .= '</select>&nbsp;';
 4031:                     if ($j < $numchildren) {
 4032:                         my $deeper = $depth+1;
 4033:                         $text .= $name.'&nbsp;'
 4034:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4035:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4036:                         if(ref($path) eq 'ARRAY') {
 4037:                             push(@{$path},$name);
 4038:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4039:                             pop(@{$path});
 4040:                         }
 4041:                     } else {
 4042:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4043:                         if ($j == $numchildren) {
 4044:                             $text .= $name;
 4045:                         } else {
 4046:                             $text .= $item;
 4047:                         }
 4048:                         $text .= '" value="" />';
 4049:                     }
 4050:                     $text .= '</td></tr>';
 4051:                 }
 4052:                 $text .= '</table></td>';
 4053:             } else {
 4054:                 my $higher = $depth-1;
 4055:                 if ($higher == 0) {
 4056:                     $name = &escape($parent).'::'.$higher;
 4057:                 } else {
 4058:                     if (ref($path) eq 'ARRAY') {
 4059:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4060:                     }
 4061:                 }
 4062:                 my $colspan;
 4063:                 if ($parent ne 'instcode') {
 4064:                     $colspan = $maxdepth - $depth - 1;
 4065:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4066:                 }
 4067:             }
 4068:         }
 4069:     }
 4070:     return $text;
 4071: }
 4072: 
 4073: sub modifiable_userdata_row {
 4074:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4075:     my $rolename;
 4076:     if ($context eq 'selfcreate') {
 4077:         if (ref($usertypes) eq 'HASH') {
 4078:             $rolename = $usertypes->{$role};
 4079:         } else {
 4080:             $rolename = $role;
 4081:         }
 4082:     } else {
 4083:         if ($role eq 'cr') {
 4084:             $rolename = &mt('Custom role');
 4085:         } else {
 4086:             $rolename = &Apache::lonnet::plaintext($role);
 4087:         }
 4088:     }
 4089:     my @fields = ('lastname','firstname','middlename','generation',
 4090:                   'permanentemail','id');
 4091:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4092:     my $output;
 4093:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4094:     $output = '<tr '.$css_class.'>'.
 4095:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4096:               '<td class="LC_left_item" colspan="2"><table>';
 4097:     my $rem;
 4098:     my %checks;
 4099:     if (ref($settings) eq 'HASH') {
 4100:         if (ref($settings->{$context}) eq 'HASH') {
 4101:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4102:                 foreach my $field (@fields) {
 4103:                     if ($settings->{$context}->{$role}->{$field}) {
 4104:                         $checks{$field} = ' checked="checked" ';
 4105:                     }
 4106:                 }
 4107:             }
 4108:         }
 4109:     }
 4110:     for (my $i=0; $i<@fields; $i++) {
 4111:         my $rem = $i%($numinrow);
 4112:         if ($rem == 0) {
 4113:             if ($i > 0) {
 4114:                 $output .= '</tr>';
 4115:             }
 4116:             $output .= '<tr>';
 4117:         }
 4118:         my $check = ' ';
 4119:         if (exists($checks{$fields[$i]})) {
 4120:             $check = $checks{$fields[$i]}
 4121:         } else {
 4122:             if ($role eq 'st') {
 4123:                 if (ref($settings) ne 'HASH') {
 4124:                     $check = ' checked="checked" '; 
 4125:                 }
 4126:             }
 4127:         }
 4128:         $output .= '<td class="LC_left_item">'.
 4129:                    '<span class="LC_nobreak"><label>'.
 4130:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4131:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4132:                    '</label></span></td>';
 4133:         $rem = @fields%($numinrow);
 4134:     }
 4135:     my $colsleft = $numinrow - $rem;
 4136:     if ($colsleft > 1 ) {
 4137:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4138:                    '&nbsp;</td>';
 4139:     } elsif ($colsleft == 1) {
 4140:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4141:     }
 4142:     $output .= '</tr></table></td></tr>';
 4143:     return $output;
 4144: }
 4145: 
 4146: sub insttypes_row {
 4147:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4148:     my %lt = &Apache::lonlocal::texthash (
 4149:                       cansearch => 'Users allowed to search',
 4150:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4151:                       lockablenames => 'User preference to lock name',
 4152:              );
 4153:     my $showdom;
 4154:     if ($context eq 'cansearch') {
 4155:         $showdom = ' ('.$dom.')';
 4156:     }
 4157:     my $output =  '<tr class="LC_odd_row">'.
 4158:                   '<td>'.$lt{$context}.$showdom.
 4159:                   '</td><td class="LC_left_item" colspan="2"><table>';
 4160:     my $rem;
 4161:     if (ref($types) eq 'ARRAY') {
 4162:         for (my $i=0; $i<@{$types}; $i++) {
 4163:             if (defined($usertypes->{$types->[$i]})) {
 4164:                 my $rem = $i%($numinrow);
 4165:                 if ($rem == 0) {
 4166:                     if ($i > 0) {
 4167:                         $output .= '</tr>';
 4168:                     }
 4169:                     $output .= '<tr>';
 4170:                 }
 4171:                 my $check = ' ';
 4172:                 if (ref($settings) eq 'HASH') {
 4173:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4174:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4175:                             $check = ' checked="checked" ';
 4176:                         }
 4177:                     } elsif ($context eq 'statustocreate') {
 4178:                         $check = ' checked="checked" ';
 4179:                     }
 4180:                 }
 4181:                 $output .= '<td class="LC_left_item">'.
 4182:                            '<span class="LC_nobreak"><label>'.
 4183:                            '<input type="checkbox" name="'.$context.'" '.
 4184:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4185:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4186:             }
 4187:         }
 4188:         $rem = @{$types}%($numinrow);
 4189:     }
 4190:     my $colsleft = $numinrow - $rem;
 4191:     if (($rem == 0) && (@{$types} > 0)) {
 4192:         $output .= '<tr>';
 4193:     }
 4194:     if ($colsleft > 1) {
 4195:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4196:     } else {
 4197:         $output .= '<td class="LC_left_item">';
 4198:     }
 4199:     my $defcheck = ' ';
 4200:     if (ref($settings) eq 'HASH') {  
 4201:         if (ref($settings->{$context}) eq 'ARRAY') {
 4202:             if (grep(/^default$/,@{$settings->{$context}})) {
 4203:                 $defcheck = ' checked="checked" ';
 4204:             }
 4205:         } elsif ($context eq 'statustocreate') {
 4206:             $defcheck = ' checked="checked" ';
 4207:         }
 4208:     }
 4209:     $output .= '<span class="LC_nobreak"><label>'.
 4210:                '<input type="checkbox" name="'.$context.'" '.
 4211:                'value="default"'.$defcheck.'/>'.
 4212:                $othertitle.'</label></span></td>'.
 4213:                '</tr></table></td></tr>';
 4214:     return $output;
 4215: }
 4216: 
 4217: sub sorted_searchtitles {
 4218:     my %searchtitles = &Apache::lonlocal::texthash(
 4219:                          'uname' => 'username',
 4220:                          'lastname' => 'last name',
 4221:                          'lastfirst' => 'last name, first name',
 4222:                      );
 4223:     my @titleorder = ('uname','lastname','lastfirst');
 4224:     return (\%searchtitles,\@titleorder);
 4225: }
 4226: 
 4227: sub sorted_searchtypes {
 4228:     my %srchtypes_desc = (
 4229:                            exact    => 'is exact match',
 4230:                            contains => 'contains ..',
 4231:                            begins   => 'begins with ..',
 4232:                          );
 4233:     my @srchtypeorder = ('exact','begins','contains');
 4234:     return (\%srchtypes_desc,\@srchtypeorder);
 4235: }
 4236: 
 4237: sub usertype_update_row {
 4238:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4239:     my $datatable;
 4240:     my $numinrow = 4;
 4241:     foreach my $type (@{$types}) {
 4242:         if (defined($usertypes->{$type})) {
 4243:             $$rownums ++;
 4244:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4245:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4246:                           '</td><td class="LC_left_item"><table>';
 4247:             for (my $i=0; $i<@{$fields}; $i++) {
 4248:                 my $rem = $i%($numinrow);
 4249:                 if ($rem == 0) {
 4250:                     if ($i > 0) {
 4251:                         $datatable .= '</tr>';
 4252:                     }
 4253:                     $datatable .= '<tr>';
 4254:                 }
 4255:                 my $check = ' ';
 4256:                 if (ref($settings) eq 'HASH') {
 4257:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4258:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4259:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4260:                                 $check = ' checked="checked" ';
 4261:                             }
 4262:                         }
 4263:                     }
 4264:                 }
 4265: 
 4266:                 if ($i == @{$fields}-1) {
 4267:                     my $colsleft = $numinrow - $rem;
 4268:                     if ($colsleft > 1) {
 4269:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4270:                     } else {
 4271:                         $datatable .= '<td>';
 4272:                     }
 4273:                 } else {
 4274:                     $datatable .= '<td>';
 4275:                 }
 4276:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4277:                               '<input type="checkbox" name="updateable_'.$type.
 4278:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4279:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4280:             }
 4281:             $datatable .= '</tr></table></td></tr>';
 4282:         }
 4283:     }
 4284:     return $datatable;
 4285: }
 4286: 
 4287: sub modify_login {
 4288:     my ($r,$dom,$confname,%domconfig) = @_;
 4289:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges);
 4290:     my %title = ( coursecatalog => 'Display course catalog',
 4291:                   adminmail => 'Display administrator E-mail address',
 4292:                   newuser => 'Link for visitors to create a user account',
 4293:                   loginheader => 'Log-in box header');
 4294:     my @offon = ('off','on');
 4295:     my %curr_loginvia;
 4296:     if (ref($domconfig{login}) eq 'HASH') {
 4297:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4298:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4299:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4300:             }
 4301:         }
 4302:     }
 4303:     my %loginhash;
 4304:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4305:                                            \%domconfig,\%loginhash);
 4306:     my @toggles = ('coursecatalog','adminmail','newuser');
 4307:     foreach my $item (@toggles) {
 4308:         $loginhash{login}{$item} = $env{'form.'.$item};
 4309:     }
 4310:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4311:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4312:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4313:                                          \%loginhash);
 4314:     }
 4315: 
 4316:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4317:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4318:     if (keys(%servers) > 1) {
 4319:         foreach my $lonhost (keys(%servers)) {
 4320:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4321:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4322:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4323:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4324:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4325:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4326:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4327:                         $changes{'loginvia'}{$lonhost} = 1;
 4328:                     } else {
 4329:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4330:                         $changes{'loginvia'}{$lonhost} = 1;
 4331:                     }
 4332:                 } else {
 4333:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4334:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4335:                         $changes{'loginvia'}{$lonhost} = 1;
 4336:                     }
 4337:                 }
 4338:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4339:                     foreach my $item (@loginvia_attribs) {
 4340:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4341:                     }
 4342:                 } else {
 4343:                     foreach my $item (@loginvia_attribs) {
 4344:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4345:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4346:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4347:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4348:                                 $new = '/';
 4349:                             }
 4350:                         }
 4351:                         if (($item eq 'custompath') && 
 4352:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4353:                             $new = '';
 4354:                         }
 4355:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4356:                             $changes{'loginvia'}{$lonhost} = 1;
 4357:                         }
 4358:                         if ($item eq 'exempt') {
 4359:                             $new =~ s/^\s+//;
 4360:                             $new =~ s/\s+$//;
 4361:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4362:                             my @okips;
 4363:                             foreach my $ip (@poss_ips) {
 4364:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4365:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4366:                                         push(@okips,$ip); 
 4367:                                     }
 4368:                                 }
 4369:                             }
 4370:                             if (@okips > 0) {
 4371:                                 $new = join(',',@okips); 
 4372:                             } else {
 4373:                                 $new = ''; 
 4374:                             }
 4375:                         }
 4376: 
 4377:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4378:                     }
 4379:                 }
 4380:             } else {
 4381:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4382:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4383:                     $changes{'loginvia'}{$lonhost} = 1;
 4384:                     foreach my $item (@loginvia_attribs) {
 4385:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4386:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4387:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4388:                                 $new = '/';
 4389:                             }
 4390:                         }
 4391:                         if (($item eq 'custompath') && 
 4392:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4393:                             $new = '';
 4394:                         }
 4395:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4396:                     }
 4397:                 }
 4398:             }
 4399:         }
 4400:     }
 4401: 
 4402:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4403:                                              $dom);
 4404:     if ($putresult eq 'ok') {
 4405:         my @toggles = ('coursecatalog','adminmail','newuser');
 4406:         my %defaultchecked = (
 4407:                     'coursecatalog' => 'on',
 4408:                     'adminmail'     => 'off',
 4409:                     'newuser'       => 'off',
 4410:         );
 4411:         if (ref($domconfig{'login'}) eq 'HASH') {
 4412:             foreach my $item (@toggles) {
 4413:                 if ($defaultchecked{$item} eq 'on') { 
 4414:                     if (($domconfig{'login'}{$item} eq '0') &&
 4415:                         ($env{'form.'.$item} eq '1')) {
 4416:                         $changes{$item} = 1;
 4417:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4418:                               $domconfig{'login'}{$item} eq '1') &&
 4419:                              ($env{'form.'.$item} eq '0')) {
 4420:                         $changes{$item} = 1;
 4421:                     }
 4422:                 } elsif ($defaultchecked{$item} eq 'off') {
 4423:                     if (($domconfig{'login'}{$item} eq '1') &&
 4424:                         ($env{'form.'.$item} eq '0')) {
 4425:                         $changes{$item} = 1;
 4426:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4427:                               $domconfig{'login'}{$item} eq '0') &&
 4428:                              ($env{'form.'.$item} eq '1')) {
 4429:                         $changes{$item} = 1;
 4430:                     }
 4431:                 }
 4432:             }
 4433:         }
 4434:         if (keys(%changes) > 0 || $colchgtext) {
 4435:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4436:             $resulttext = &mt('Changes made:').'<ul>';
 4437:             foreach my $item (sort(keys(%changes))) {
 4438:                 if ($item eq 'loginvia') {
 4439:                     if (ref($changes{$item}) eq 'HASH') {
 4440:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4441:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4442:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4443:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4444:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4445:                                     $protocol = 'http' if ($protocol ne 'https');
 4446:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4447: 
 4448:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4449:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4450:                                     } else {
 4451:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4452:                                     }
 4453:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4454:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4455:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4456:                                     }
 4457:                                     $resulttext .= '</li>';
 4458:                                 } else {
 4459:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4460:                                 }
 4461:                             } else {
 4462:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4463:                             }
 4464:                         }
 4465:                         $resulttext .= '</ul></li>';
 4466:                     }
 4467:                 } else {
 4468:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4469:                 }
 4470:             }
 4471:             $resulttext .= $colchgtext.'</ul>';
 4472:         } else {
 4473:             $resulttext = &mt('No changes made to log-in page settings');
 4474:         }
 4475:     } else {
 4476:         $resulttext = '<span class="LC_error">'.
 4477: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4478:     }
 4479:     if ($errors) {
 4480:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 4481:                        $errors.'</ul>';
 4482:     }
 4483:     return $resulttext;
 4484: }
 4485: 
 4486: sub color_font_choices {
 4487:     my %choices =
 4488:         &Apache::lonlocal::texthash (
 4489:             img => "Header",
 4490:             bgs => "Background colors",
 4491:             links => "Link colors",
 4492:             images => "Images",
 4493:             font => "Font color",
 4494:             fontmenu => "Font Menu",
 4495:             pgbg => "Page",
 4496:             tabbg => "Header",
 4497:             sidebg => "Border",
 4498:             link => "Link",
 4499:             alink => "Active link",
 4500:             vlink => "Visited link",
 4501:         );
 4502:     return %choices;
 4503: }
 4504: 
 4505: sub modify_rolecolors {
 4506:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 4507:     my ($resulttext,%rolehash);
 4508:     $rolehash{'rolecolors'} = {};
 4509:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 4510:         if ($domconfig{'rolecolors'} eq '') {
 4511:             $domconfig{'rolecolors'} = {};
 4512:         }
 4513:     }
 4514:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 4515:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 4516:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 4517:                                              $dom);
 4518:     if ($putresult eq 'ok') {
 4519:         if (keys(%changes) > 0) {
 4520:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4521:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 4522:                                              $rolehash{'rolecolors'});
 4523:         } else {
 4524:             $resulttext = &mt('No changes made to default color schemes');
 4525:         }
 4526:     } else {
 4527:         $resulttext = '<span class="LC_error">'.
 4528: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4529:     }
 4530:     if ($errors) {
 4531:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 4532:                        $errors.'</ul>';
 4533:     }
 4534:     return $resulttext;
 4535: }
 4536: 
 4537: sub modify_colors {
 4538:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 4539:     my (%changes,%choices);
 4540:     my @bgs;
 4541:     my @links = ('link','alink','vlink');
 4542:     my @logintext;
 4543:     my @images;
 4544:     my $servadm = $r->dir_config('lonAdmEMail');
 4545:     my $errors;
 4546:     foreach my $role (@{$roles}) {
 4547:         if ($role eq 'login') {
 4548:             %choices = &login_choices();
 4549:             @logintext = ('textcol','bgcol');
 4550:         } else {
 4551:             %choices = &color_font_choices();
 4552:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 4553:         }
 4554:         if ($role eq 'login') {
 4555:             @images = ('img','logo','domlogo','login');
 4556:             @bgs = ('pgbg','mainbg','sidebg');
 4557:         } else {
 4558:             @images = ('img');
 4559:             @bgs = ('pgbg','tabbg','sidebg'); 
 4560:         }
 4561:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 4562:         foreach my $item (@bgs,@links,@logintext) {
 4563:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 4564:         }
 4565:         my ($configuserok,$author_ok,$switchserver) = 
 4566:             &config_check($dom,$confname,$servadm);
 4567:         my ($width,$height) = &thumb_dimensions();
 4568:         if (ref($domconfig->{$role}) ne 'HASH') {
 4569:             $domconfig->{$role} = {};
 4570:         }
 4571:         foreach my $img (@images) {
 4572:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 4573:                 if (defined($env{'form.login_showlogo_'.$img})) {
 4574:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 4575:                 } else { 
 4576:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 4577:                 }
 4578:             } 
 4579: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 4580: 		 && !defined($domconfig->{$role}{$img})
 4581: 		 && !$env{'form.'.$role.'_del_'.$img}
 4582: 		 && $env{'form.'.$role.'_import_'.$img}) {
 4583: 		# import the old configured image from the .tab setting
 4584: 		# if they haven't provided a new one 
 4585: 		$domconfig->{$role}{$img} = 
 4586: 		    $env{'form.'.$role.'_import_'.$img};
 4587: 	    }
 4588:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 4589:                 my $error;
 4590:                 if ($configuserok eq 'ok') {
 4591:                     if ($switchserver) {
 4592:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 4593:                     } else {
 4594:                         if ($author_ok eq 'ok') {
 4595:                             my ($result,$logourl) = 
 4596:                                 &publishlogo($r,'upload',$role.'_'.$img,
 4597:                                            $dom,$confname,$img,$width,$height);
 4598:                             if ($result eq 'ok') {
 4599:                                 $confhash->{$role}{$img} = $logourl;
 4600:                                 $changes{$role}{'images'}{$img} = 1;
 4601:                             } else {
 4602:                                 $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);
 4603:                             }
 4604:                         } else {
 4605:                             $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);
 4606:                         }
 4607:                     }
 4608:                 } else {
 4609:                     $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);
 4610:                 }
 4611:                 if ($error) {
 4612:                     &Apache::lonnet::logthis($error);
 4613:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4614:                 }
 4615:             } elsif ($domconfig->{$role}{$img} ne '') {
 4616:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 4617:                     my $error;
 4618:                     if ($configuserok eq 'ok') {
 4619: # is confname an author?
 4620:                         if ($switchserver eq '') {
 4621:                             if ($author_ok eq 'ok') {
 4622:                                 my ($result,$logourl) = 
 4623:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 4624:                                             $dom,$confname,$img,$width,$height);
 4625:                                 if ($result eq 'ok') {
 4626:                                     $confhash->{$role}{$img} = $logourl;
 4627: 				    $changes{$role}{'images'}{$img} = 1;
 4628:                                 }
 4629:                             }
 4630:                         }
 4631:                     }
 4632:                 }
 4633:             }
 4634:         }
 4635:         if (ref($domconfig) eq 'HASH') {
 4636:             if (ref($domconfig->{$role}) eq 'HASH') {
 4637:                 foreach my $img (@images) {
 4638:                     if ($domconfig->{$role}{$img} ne '') {
 4639:                         if ($env{'form.'.$role.'_del_'.$img}) {
 4640:                             $confhash->{$role}{$img} = '';
 4641:                             $changes{$role}{'images'}{$img} = 1;
 4642:                         } else {
 4643:                             if ($confhash->{$role}{$img} eq '') {
 4644:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 4645:                             }
 4646:                         }
 4647:                     } else {
 4648:                         if ($env{'form.'.$role.'_del_'.$img}) {
 4649:                             $confhash->{$role}{$img} = '';
 4650:                             $changes{$role}{'images'}{$img} = 1;
 4651:                         } 
 4652:                     }
 4653:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 4654:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 4655:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 4656:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 4657:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 4658:                             }
 4659:                         } else {
 4660:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 4661:                                 $changes{$role}{'showlogo'}{$img} = 1;
 4662:                             }
 4663:                         }
 4664:                     }
 4665:                 }
 4666:                 if ($domconfig->{$role}{'font'} ne '') {
 4667:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 4668:                         $changes{$role}{'font'} = 1;
 4669:                     }
 4670:                 } else {
 4671:                     if ($confhash->{$role}{'font'}) {
 4672:                         $changes{$role}{'font'} = 1;
 4673:                     }
 4674:                 }
 4675:                 if ($role ne 'login') {
 4676:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 4677:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 4678:                             $changes{$role}{'fontmenu'} = 1;
 4679:                         }
 4680:                     } else {
 4681:                         if ($confhash->{$role}{'fontmenu'}) {
 4682:                             $changes{$role}{'fontmenu'} = 1;
 4683:                         }
 4684:                     }
 4685:                 }
 4686:                 foreach my $item (@bgs) {
 4687:                     if ($domconfig->{$role}{$item} ne '') {
 4688:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 4689:                             $changes{$role}{'bgs'}{$item} = 1;
 4690:                         } 
 4691:                     } else {
 4692:                         if ($confhash->{$role}{$item}) {
 4693:                             $changes{$role}{'bgs'}{$item} = 1;
 4694:                         }
 4695:                     }
 4696:                 }
 4697:                 foreach my $item (@links) {
 4698:                     if ($domconfig->{$role}{$item} ne '') {
 4699:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 4700:                             $changes{$role}{'links'}{$item} = 1;
 4701:                         }
 4702:                     } else {
 4703:                         if ($confhash->{$role}{$item}) {
 4704:                             $changes{$role}{'links'}{$item} = 1;
 4705:                         }
 4706:                     }
 4707:                 }
 4708:                 foreach my $item (@logintext) {
 4709:                     if ($domconfig->{$role}{$item} ne '') {
 4710:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 4711:                             $changes{$role}{'logintext'}{$item} = 1;
 4712:                         }
 4713:                     } else {
 4714:                         if ($confhash->{$role}{$item}) {
 4715:                             $changes{$role}{'logintext'}{$item} = 1;
 4716:                         }
 4717:                     }
 4718:                 }
 4719:             } else {
 4720:                 &default_change_checker($role,\@images,\@links,\@bgs,
 4721:                                         \@logintext,$confhash,\%changes); 
 4722:             }
 4723:         } else {
 4724:             &default_change_checker($role,\@images,\@links,\@bgs,
 4725:                                     \@logintext,$confhash,\%changes); 
 4726:         }
 4727:     }
 4728:     return ($errors,%changes);
 4729: }
 4730: 
 4731: sub config_check {
 4732:     my ($dom,$confname,$servadm) = @_;
 4733:     my ($configuserok,$author_ok,$switchserver,%currroles);
 4734:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 4735:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 4736:                                                    $confname,$servadm);
 4737:     if ($configuserok eq 'ok') {
 4738:         $switchserver = &check_switchserver($dom,$confname);
 4739:         if ($switchserver eq '') {
 4740:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 4741:         }
 4742:     }
 4743:     return ($configuserok,$author_ok,$switchserver);
 4744: }
 4745: 
 4746: sub default_change_checker {
 4747:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 4748:     foreach my $item (@{$links}) {
 4749:         if ($confhash->{$role}{$item}) {
 4750:             $changes->{$role}{'links'}{$item} = 1;
 4751:         }
 4752:     }
 4753:     foreach my $item (@{$bgs}) {
 4754:         if ($confhash->{$role}{$item}) {
 4755:             $changes->{$role}{'bgs'}{$item} = 1;
 4756:         }
 4757:     }
 4758:     foreach my $item (@{$logintext}) {
 4759:         if ($confhash->{$role}{$item}) {
 4760:             $changes->{$role}{'logintext'}{$item} = 1;
 4761:         }
 4762:     }
 4763:     foreach my $img (@{$images}) {
 4764:         if ($env{'form.'.$role.'_del_'.$img}) {
 4765:             $confhash->{$role}{$img} = '';
 4766:             $changes->{$role}{'images'}{$img} = 1;
 4767:         }
 4768:         if ($role eq 'login') {
 4769:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 4770:                 $changes->{$role}{'showlogo'}{$img} = 1;
 4771:             }
 4772:         }
 4773:     }
 4774:     if ($confhash->{$role}{'font'}) {
 4775:         $changes->{$role}{'font'} = 1;
 4776:     }
 4777: }
 4778: 
 4779: sub display_colorchgs {
 4780:     my ($dom,$changes,$roles,$confhash) = @_;
 4781:     my (%choices,$resulttext);
 4782:     if (!grep(/^login$/,@{$roles})) {
 4783:         $resulttext = &mt('Changes made:').'<br />';
 4784:     }
 4785:     foreach my $role (@{$roles}) {
 4786:         if ($role eq 'login') {
 4787:             %choices = &login_choices();
 4788:         } else {
 4789:             %choices = &color_font_choices();
 4790:         }
 4791:         if (ref($changes->{$role}) eq 'HASH') {
 4792:             if ($role ne 'login') {
 4793:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 4794:             }
 4795:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 4796:                 if ($role ne 'login') {
 4797:                     $resulttext .= '<ul>';
 4798:                 }
 4799:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 4800:                     if ($role ne 'login') {
 4801:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 4802:                     }
 4803:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 4804:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 4805:                             if ($confhash->{$role}{$key}{$item}) {
 4806:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 4807:                             } else {
 4808:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 4809:                             }
 4810:                         } elsif ($confhash->{$role}{$item} eq '') {
 4811:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 4812:                         } else {
 4813:                             my $newitem = $confhash->{$role}{$item};
 4814:                             if ($key eq 'images') {
 4815:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 4816:                             }
 4817:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 4818:                         }
 4819:                     }
 4820:                     if ($role ne 'login') {
 4821:                         $resulttext .= '</ul></li>';
 4822:                     }
 4823:                 } else {
 4824:                     if ($confhash->{$role}{$key} eq '') {
 4825:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 4826:                     } else {
 4827:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 4828:                     }
 4829:                 }
 4830:                 if ($role ne 'login') {
 4831:                     $resulttext .= '</ul>';
 4832:                 }
 4833:             }
 4834:         }
 4835:     }
 4836:     return $resulttext;
 4837: }
 4838: 
 4839: sub thumb_dimensions {
 4840:     return ('200','50');
 4841: }
 4842: 
 4843: sub check_dimensions {
 4844:     my ($inputfile) = @_;
 4845:     my ($fullwidth,$fullheight);
 4846:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 4847:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 4848:             my $imageinfo = <PIPE>;
 4849:             if (!close(PIPE)) {
 4850:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 4851:             }
 4852:             chomp($imageinfo);
 4853:             my ($fullsize) = 
 4854:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 4855:             if ($fullsize) {
 4856:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 4857:             }
 4858:         }
 4859:     }
 4860:     return ($fullwidth,$fullheight);
 4861: }
 4862: 
 4863: sub check_configuser {
 4864:     my ($uhome,$dom,$confname,$servadm) = @_;
 4865:     my ($configuserok,%currroles);
 4866:     if ($uhome eq 'no_host') {
 4867:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 4868:         my $configpass = &LONCAPA::Enrollment::create_password();
 4869:         $configuserok = 
 4870:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 4871:                              $configpass,'','','','','',undef,$servadm);
 4872:     } else {
 4873:         $configuserok = 'ok';
 4874:         %currroles = 
 4875:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 4876:     }
 4877:     return ($configuserok,%currroles);
 4878: }
 4879: 
 4880: sub check_authorstatus {
 4881:     my ($dom,$confname,%currroles) = @_;
 4882:     my $author_ok;
 4883:     if (!$currroles{':'.$dom.':au'}) {
 4884:         my $start = time;
 4885:         my $end = 0;
 4886:         $author_ok = 
 4887:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 4888:                                         'au',$end,$start,'','','domconfig');
 4889:     } else {
 4890:         $author_ok = 'ok';
 4891:     }
 4892:     return $author_ok;
 4893: }
 4894: 
 4895: sub publishlogo {
 4896:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 4897:     my ($output,$fname,$logourl);
 4898:     if ($action eq 'upload') {
 4899:         $fname=$env{'form.'.$formname.'.filename'};
 4900:         chop($env{'form.'.$formname});
 4901:     } else {
 4902:         ($fname) = ($formname =~ /([^\/]+)$/);
 4903:     }
 4904:     if ($savefileas ne '') {
 4905:         $fname = $savefileas;
 4906:     }
 4907:     $fname=&Apache::lonnet::clean_filename($fname);
 4908: # See if there is anything left
 4909:     unless ($fname) { return ('error: no uploaded file'); }
 4910:     $fname="$subdir/$fname";
 4911:     my $filepath=$r->dir_config('lonDocRoot')."/priv/$dom/$confname";
 4912:     my ($fnamepath,$file,$fetchthumb);
 4913:     $file=$fname;
 4914:     if ($fname=~m|/|) {
 4915:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 4916:     }
 4917:     my @parts=split(/\//,$filepath.'/'.$fnamepath);
 4918:     my $count;
 4919:     for ($count=4;$count<=$#parts;$count++) {
 4920:         $filepath.="/$parts[$count]";
 4921:         if ((-e $filepath)!=1) {
 4922:             mkdir($filepath,02770);
 4923:         }
 4924:     }
 4925:     # Check for bad extension and disallow upload
 4926:     if ($file=~/\.(\w+)$/ &&
 4927:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 4928:         $output = 
 4929:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 4930:     } elsif ($file=~/\.(\w+)$/ &&
 4931:         !defined(&Apache::loncommon::fileembstyle($1))) {
 4932:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 4933:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 4934:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 4935:     } elsif (-d "$filepath/$file") {
 4936:         $output = &mt('File name is a directory name - rename the file and re-upload');
 4937:     } else {
 4938:         my $source = $filepath.'/'.$file;
 4939:         my $logfile;
 4940:         if (!open($logfile,">>$source".'.log')) {
 4941:             return (&mt('No write permission to Construction Space'));
 4942:         }
 4943:         print $logfile
 4944: "\n================= Publish ".localtime()." ================\n".
 4945: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 4946: # Save the file
 4947:         if (!open(FH,'>'.$source)) {
 4948:             &Apache::lonnet::logthis('Failed to create '.$source);
 4949:             return (&mt('Failed to create file'));
 4950:         }
 4951:         if ($action eq 'upload') {
 4952:             if (!print FH ($env{'form.'.$formname})) {
 4953:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 4954:                 return (&mt('Failed to write file'));
 4955:             }
 4956:         } else {
 4957:             my $original = &Apache::lonnet::filelocation('',$formname);
 4958:             if(!copy($original,$source)) {
 4959:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 4960:                 return (&mt('Failed to write file'));
 4961:             }
 4962:         }
 4963:         close(FH);
 4964:         chmod(0660, $source); # Permissions to rw-rw---.
 4965: 
 4966:         my $docroot=$r->dir_config('lonDocRoot');
 4967:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 4968:         my $copyfile=$targetdir.'/'.$file;
 4969: 
 4970:         my @parts=split(/\//,$targetdir);
 4971:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 4972:         for (my $count=5;$count<=$#parts;$count++) {
 4973:             $path.="/$parts[$count]";
 4974:             if (!-e $path) {
 4975:                 print $logfile "\nCreating directory ".$path;
 4976:                 mkdir($path,02770);
 4977:             }
 4978:         }
 4979:         my $versionresult;
 4980:         if (-e $copyfile) {
 4981:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 4982:         } else {
 4983:             $versionresult = 'ok';
 4984:         }
 4985:         if ($versionresult eq 'ok') {
 4986:             if (copy($source,$copyfile)) {
 4987:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 4988:                 $output = 'ok';
 4989:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 4990:                 push(@{$modified_urls},[$copyfile,$source]);
 4991:                 my $metaoutput = 
 4992:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 4993:                 unless ($registered_cleanup) {
 4994:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 4995:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 4996:                     $registered_cleanup=1;
 4997:                 }
 4998:             } else {
 4999:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5000:                 $output = &mt('Failed to copy file to RES space').", $!";
 5001:             }
 5002:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5003:                 my $inputfile = $filepath.'/'.$file;
 5004:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5005:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5006:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5007:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5008:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5009:                         system("convert -sample $thumbsize $inputfile $outfile");
 5010:                         chmod(0660, $filepath.'/tn-'.$file);
 5011:                         if (-e $outfile) {
 5012:                             my $copyfile=$targetdir.'/tn-'.$file;
 5013:                             if (copy($outfile,$copyfile)) {
 5014:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5015:                                 my $thumb_metaoutput = 
 5016:                                     &write_metadata($dom,$confname,$formname,
 5017:                                                     $targetdir,'tn-'.$file,$logfile);
 5018:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5019:                                 unless ($registered_cleanup) {
 5020:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5021:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5022:                                     $registered_cleanup=1;
 5023:                                 }
 5024:                             } else {
 5025:                                 print $logfile "\nUnable to write ".$copyfile.
 5026:                                                ':'.$!."\n";
 5027:                             }
 5028:                         }
 5029:                     }
 5030:                 }
 5031:             }
 5032:         } else {
 5033:             $output = $versionresult;
 5034:         }
 5035:     }
 5036:     return ($output,$logourl);
 5037: }
 5038: 
 5039: sub logo_versioning {
 5040:     my ($targetdir,$file,$logfile) = @_;
 5041:     my $target = $targetdir.'/'.$file;
 5042:     my ($maxversion,$fn,$extn,$output);
 5043:     $maxversion = 0;
 5044:     if ($file =~ /^(.+)\.(\w+)$/) {
 5045:         $fn=$1;
 5046:         $extn=$2;
 5047:     }
 5048:     opendir(DIR,$targetdir);
 5049:     while (my $filename=readdir(DIR)) {
 5050:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5051:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5052:         }
 5053:     }
 5054:     $maxversion++;
 5055:     print $logfile "\nCreating old version ".$maxversion."\n";
 5056:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5057:     if (copy($target,$copyfile)) {
 5058:         print $logfile "Copied old target to ".$copyfile."\n";
 5059:         $copyfile=$copyfile.'.meta';
 5060:         if (copy($target.'.meta',$copyfile)) {
 5061:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5062:             $output = 'ok';
 5063:         } else {
 5064:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5065:             $output = &mt('Failed to copy old meta').", $!, ";
 5066:         }
 5067:     } else {
 5068:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5069:         $output = &mt('Failed to copy old target').", $!, ";
 5070:     }
 5071:     return $output;
 5072: }
 5073: 
 5074: sub write_metadata {
 5075:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5076:     my (%metadatafields,%metadatakeys,$output);
 5077:     $metadatafields{'title'}=$formname;
 5078:     $metadatafields{'creationdate'}=time;
 5079:     $metadatafields{'lastrevisiondate'}=time;
 5080:     $metadatafields{'copyright'}='public';
 5081:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5082:                                          $env{'user.domain'};
 5083:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5084:     $metadatafields{'domain'}=$dom;
 5085:     {
 5086:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5087:         my $mfh;
 5088:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5089:             foreach (sort keys %metadatafields) {
 5090:                 unless ($_=~/\./) {
 5091:                     my $unikey=$_;
 5092:                     $unikey=~/^([A-Za-z]+)/;
 5093:                     my $tag=$1;
 5094:                     $tag=~tr/A-Z/a-z/;
 5095:                     print $mfh "\n\<$tag";
 5096:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5097:                         my $value=$metadatafields{$unikey.'.'.$_};
 5098:                         $value=~s/\"/\'\'/g;
 5099:                         print $mfh ' '.$_.'="'.$value.'"';
 5100:                     }
 5101:                     print $mfh '>'.
 5102:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5103:                             .'</'.$tag.'>';
 5104:                 }
 5105:             }
 5106:             $output = 'ok';
 5107:             print $logfile "\nWrote metadata";
 5108:             close($mfh);
 5109:         } else {
 5110:             print $logfile "\nFailed to open metadata file";
 5111:             $output = &mt('Could not write metadata');
 5112:         }
 5113:     }
 5114:     return $output;
 5115: }
 5116: 
 5117: sub notifysubscribed {
 5118:     foreach my $targetsource (@{$modified_urls}){
 5119:         next unless (ref($targetsource) eq 'ARRAY');
 5120:         my ($target,$source)=@{$targetsource};
 5121:         if ($source ne '') {
 5122:             if (open(my $logfh,'>>'.$source.'.log')) {
 5123:                 print $logfh "\nCleanup phase: Notifications\n";
 5124:                 my @subscribed=&subscribed_hosts($target);
 5125:                 foreach my $subhost (@subscribed) {
 5126:                     print $logfh "\nNotifying host ".$subhost.':';
 5127:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5128:                     print $logfh $reply;
 5129:                 }
 5130:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5131:                 foreach my $subhost (@subscribedmeta) {
 5132:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5133:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5134:                                                         $subhost);
 5135:                     print $logfh $reply;
 5136:                 }
 5137:                 print $logfh "\n============ Done ============\n";
 5138:                 close($logfh);
 5139:             }
 5140:         }
 5141:     }
 5142:     return OK;
 5143: }
 5144: 
 5145: sub subscribed_hosts {
 5146:     my ($target) = @_;
 5147:     my @subscribed;
 5148:     if (open(my $fh,"<$target.subscription")) {
 5149:         while (my $subline=<$fh>) {
 5150:             if ($subline =~ /^($match_lonid):/) {
 5151:                 my $host = $1;
 5152:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5153:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5154:                         push(@subscribed,$host);
 5155:                     }
 5156:                 }
 5157:             }
 5158:         }
 5159:     }
 5160:     return @subscribed;
 5161: }
 5162: 
 5163: sub check_switchserver {
 5164:     my ($dom,$confname) = @_;
 5165:     my ($allowed,$switchserver);
 5166:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5167:     if ($home eq 'no_host') {
 5168:         $home = &Apache::lonnet::domain($dom,'primary');
 5169:     }
 5170:     my @ids=&Apache::lonnet::current_machine_ids();
 5171:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5172:     if (!$allowed) {
 5173: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&role=dc./'.$dom.'/">'.&mt('Switch Server').'</a>';
 5174:     }
 5175:     return $switchserver;
 5176: }
 5177: 
 5178: sub modify_quotas {
 5179:     my ($dom,$action,%domconfig) = @_;
 5180:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5181:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5182:     if ($action eq 'quotas') {
 5183:         $context = 'tools'; 
 5184:     } else { 
 5185:         $context = $action;
 5186:     }
 5187:     if ($context eq 'requestcourses') {
 5188:         @usertools = ('official','unofficial','community');
 5189:         @options =('norequest','approval','validate','autolimit');
 5190:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5191:         %titles = &courserequest_titles();
 5192:         $toolregexp = join('|',@usertools);
 5193:         %conditions = &courserequest_conditions();
 5194:     } else {
 5195:         @usertools = ('aboutme','blog','webdav','portfolio');
 5196:         %titles = &tool_titles();
 5197:     }
 5198:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5199:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5200:     foreach my $key (keys(%env)) {
 5201:         if ($context eq 'requestcourses') {
 5202:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5203:                 my $item = $1;
 5204:                 my $type = $2;
 5205:                 if ($type =~ /^limit_(.+)/) {
 5206:                     $limithash{$item}{$1} = $env{$key};
 5207:                 } else {
 5208:                     $confhash{$item}{$type} = $env{$key};
 5209:                 }
 5210:             }
 5211:         } else {
 5212:             if ($key =~ /^form\.quota_(.+)$/) {
 5213:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5214:             }
 5215:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5216:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5217:             }
 5218:         }
 5219:     }
 5220:     if ($context eq 'requestcourses') {
 5221:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5222:         @approvalnotify = sort(@approvalnotify);
 5223:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5224:         if (ref($domconfig{$action}) eq 'HASH') {
 5225:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5226:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5227:                     $changes{'notify'}{'approval'} = 1;
 5228:                 }
 5229:             } else {
 5230:                 if ($confhash{'notify'}{'approval'}) {
 5231:                     $changes{'notify'}{'approval'} = 1;
 5232:                 }
 5233:             }
 5234:         } else {
 5235:             if ($confhash{'notify'}{'approval'}) {
 5236:                 $changes{'notify'}{'approval'} = 1;
 5237:             }
 5238:         }
 5239:     } else {
 5240:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5241:     }
 5242:     foreach my $item (@usertools) {
 5243:         foreach my $type (@{$types},'default','_LC_adv') {
 5244:             my $unset; 
 5245:             if ($context eq 'requestcourses') {
 5246:                 $unset = '0';
 5247:                 if ($type eq '_LC_adv') {
 5248:                     $unset = '';
 5249:                 }
 5250:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5251:                     $confhash{$item}{$type} .= '=';
 5252:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5253:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5254:                     }
 5255:                 }
 5256:             } else {
 5257:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5258:                     $confhash{$item}{$type} = 1;
 5259:                 } else {
 5260:                     $confhash{$item}{$type} = 0;
 5261:                 }
 5262:             }
 5263:             if (ref($domconfig{$action}) eq 'HASH') {
 5264:                 if (ref($domconfig{$action}{$item}) eq 'HASH') {
 5265:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5266:                         $changes{$item}{$type} = 1;
 5267:                     }
 5268:                 } else {
 5269:                     if ($context eq 'requestcourses') {
 5270:                         if ($confhash{$item}{$type} ne $unset) {
 5271:                             $changes{$item}{$type} = 1;
 5272:                         }
 5273:                     } else {
 5274:                         if (!$confhash{$item}{$type}) {
 5275:                             $changes{$item}{$type} = 1;
 5276:                         }
 5277:                     }
 5278:                 }
 5279:             } else {
 5280:                 if ($context eq 'requestcourses') {
 5281:                     if ($confhash{$item}{$type} ne $unset) {
 5282:                         $changes{$item}{$type} = 1;
 5283:                     }
 5284:                 } else {
 5285:                     if (!$confhash{$item}{$type}) {
 5286:                         $changes{$item}{$type} = 1;
 5287:                     }
 5288:                 }
 5289:             }
 5290:         }
 5291:     }
 5292:     unless ($context eq 'requestcourses') {
 5293:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5294:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5295:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5296:                     if (exists($confhash{'defaultquota'}{$key})) {
 5297:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5298:                             $changes{'defaultquota'}{$key} = 1;
 5299:                         }
 5300:                     } else {
 5301:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5302:                     }
 5303:                 }
 5304:             } else {
 5305:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5306:                     if (exists($confhash{'defaultquota'}{$key})) {
 5307:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5308:                             $changes{'defaultquota'}{$key} = 1;
 5309:                         }
 5310:                     } else {
 5311:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5312:                     }
 5313:                 }
 5314:             }
 5315:         }
 5316:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5317:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5318:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5319:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5320:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5321:                             $changes{'defaultquota'}{$key} = 1;
 5322:                         }
 5323:                     } else {
 5324:                         if (!exists($domconfig{'quotas'}{$key})) {
 5325:                             $changes{'defaultquota'}{$key} = 1;
 5326:                         }
 5327:                     }
 5328:                 } else {
 5329:                     $changes{'defaultquota'}{$key} = 1;
 5330:                 }
 5331:             }
 5332:         }
 5333:     }
 5334: 
 5335:     foreach my $key (keys(%confhash)) {
 5336:         $domdefaults{$key} = $confhash{$key};
 5337:     }
 5338:    
 5339:     my %quotahash = (
 5340:                       $action => { %confhash }
 5341:                     );
 5342:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5343:                                              $dom);
 5344:     if ($putresult eq 'ok') {
 5345:         if (keys(%changes) > 0) {
 5346:             my $cachetime = 24*60*60;
 5347:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5348: 
 5349:             $resulttext = &mt('Changes made:').'<ul>';
 5350:             unless ($context eq 'requestcourses') {
 5351:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5352:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5353:                     foreach my $type (@{$types},'default') {
 5354:                         if (defined($changes{'defaultquota'}{$type})) {
 5355:                             my $typetitle = $usertypes->{$type};
 5356:                             if ($type eq 'default') {
 5357:                                 $typetitle = $othertitle;
 5358:                             }
 5359:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5360:                         }
 5361:                     }
 5362:                     $resulttext .= '</ul></li>';
 5363:                 }
 5364:             }
 5365:             my %newenv;
 5366:             foreach my $item (@usertools) {
 5367:                 if (ref($changes{$item}) eq 'HASH') {
 5368:                     my $newacc = 
 5369:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5370:                                                           $env{'user.domain'},
 5371:                                                           $item,'reload',$context);
 5372:                     if ($context eq 'requestcourses') {
 5373:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5374:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5375:                         }
 5376:                     } else {
 5377:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5378:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5379:                         }
 5380:                     }
 5381:                     $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5382:                     foreach my $type (@{$types},'default','_LC_adv') {
 5383:                         if ($changes{$item}{$type}) {
 5384:                             my $typetitle = $usertypes->{$type};
 5385:                             if ($type eq 'default') {
 5386:                                 $typetitle = $othertitle;
 5387:                             } elsif ($type eq '_LC_adv') {
 5388:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5389:                             }
 5390:                             if ($confhash{$item}{$type}) {
 5391:                                 if ($context eq 'requestcourses') {
 5392:                                     my $cond;
 5393:                                     if ($confhash{$item}{$type} =~ /^autolimit=(\d*)$/) {
 5394:                                         if ($1 eq '') {
 5395:                                             $cond = &mt('(Automatic processing of any request).');
 5396:                                         } else {
 5397:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5398:                                         }
 5399:                                     } else { 
 5400:                                         $cond = $conditions{$confhash{$item}{$type}};
 5401:                                     }
 5402:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5403:                                 } else {
 5404:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5405:                                 }
 5406:                             } else {
 5407:                                 if ($type eq '_LC_adv') {
 5408:                                     if ($confhash{$item}{$type} eq '0') {
 5409:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5410:                                     } else { 
 5411:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5412:                                     }
 5413:                                 } else {
 5414:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5415:                                 }
 5416:                             }
 5417:                         }
 5418:                     }
 5419:                     $resulttext .= '</ul></li>';
 5420:                 }
 5421:             }
 5422:             if ($action eq 'requestcourses') {
 5423:                 if (ref($changes{'notify'}) eq 'HASH') {
 5424:                     if ($changes{'notify'}{'approval'}) {
 5425:                         if (ref($confhash{'notify'}) eq 'HASH') {
 5426:                             if ($confhash{'notify'}{'approval'}) {
 5427:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 5428:                             } else {
 5429:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of course requests requiring approval.').'</li>';
 5430:                             }
 5431:                         }
 5432:                     }
 5433:                 }
 5434:             }
 5435:             $resulttext .= '</ul>';
 5436:             if (keys(%newenv)) {
 5437:                 &Apache::lonnet::appenv(\%newenv);
 5438:             }
 5439:         } else {
 5440:             if ($context eq 'requestcourses') {
 5441:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 5442:             } else {
 5443:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 5444:             }
 5445:         }
 5446:     } else {
 5447:         $resulttext = '<span class="LC_error">'.
 5448: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5449:     }
 5450:     return $resulttext;
 5451: }
 5452: 
 5453: sub modify_autoenroll {
 5454:     my ($dom,%domconfig) = @_;
 5455:     my ($resulttext,%changes);
 5456:     my %currautoenroll;
 5457:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 5458:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 5459:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 5460:         }
 5461:     }
 5462:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 5463:     my %title = ( run => 'Auto-enrollment active',
 5464:                   sender => 'Sender for notification messages',
 5465:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 5466:     my @offon = ('off','on');
 5467:     my $sender_uname = $env{'form.sender_uname'};
 5468:     my $sender_domain = $env{'form.sender_domain'};
 5469:     if ($sender_domain eq '') {
 5470:         $sender_uname = '';
 5471:     } elsif ($sender_uname eq '') {
 5472:         $sender_domain = '';
 5473:     }
 5474:     my $coowners = $env{'form.autoassign_coowners'};
 5475:     my %autoenrollhash =  (
 5476:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 5477:                                        'sender_uname' => $sender_uname,
 5478:                                        'sender_domain' => $sender_domain,
 5479:                                        'co-owners' => $coowners,
 5480:                                 }
 5481:                      );
 5482:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 5483:                                              $dom);
 5484:     if ($putresult eq 'ok') {
 5485:         if (exists($currautoenroll{'run'})) {
 5486:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 5487:                  $changes{'run'} = 1;
 5488:              }
 5489:         } elsif ($autorun) {
 5490:             if ($env{'form.autoenroll_run'} ne '1') {
 5491:                  $changes{'run'} = 1;
 5492:             }
 5493:         }
 5494:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 5495:             $changes{'sender'} = 1;
 5496:         }
 5497:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 5498:             $changes{'sender'} = 1;
 5499:         }
 5500:         if ($currautoenroll{'co-owners'} ne '') {
 5501:             if ($currautoenroll{'co-owners'} ne $coowners) {
 5502:                 $changes{'coowners'} = 1;
 5503:             }
 5504:         } elsif ($coowners) {
 5505:             $changes{'coowners'} = 1;
 5506:         }      
 5507:         if (keys(%changes) > 0) {
 5508:             $resulttext = &mt('Changes made:').'<ul>';
 5509:             if ($changes{'run'}) {
 5510:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 5511:             }
 5512:             if ($changes{'sender'}) {
 5513:                 if ($sender_uname eq '' || $sender_domain eq '') {
 5514:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 5515:                 } else {
 5516:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 5517:                 }
 5518:             }
 5519:             if ($changes{'coowners'}) {
 5520:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 5521:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 5522:             }
 5523:             $resulttext .= '</ul>';
 5524:         } else {
 5525:             $resulttext = &mt('No changes made to auto-enrollment settings');
 5526:         }
 5527:     } else {
 5528:         $resulttext = '<span class="LC_error">'.
 5529: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5530:     }
 5531:     return $resulttext;
 5532: }
 5533: 
 5534: sub modify_autoupdate {
 5535:     my ($dom,%domconfig) = @_;
 5536:     my ($resulttext,%currautoupdate,%fields,%changes);
 5537:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 5538:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 5539:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 5540:         }
 5541:     }
 5542:     my @offon = ('off','on');
 5543:     my %title = &Apache::lonlocal::texthash (
 5544:                    run => 'Auto-update:',
 5545:                    classlists => 'Updates to user information in classlists?'
 5546:                 );
 5547:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5548:     my %fieldtitles = &Apache::lonlocal::texthash (
 5549:                         id => 'Student/Employee ID',
 5550:                         permanentemail => 'E-mail address',
 5551:                         lastname => 'Last Name',
 5552:                         firstname => 'First Name',
 5553:                         middlename => 'Middle Name',
 5554:                         generation => 'Generation',
 5555:                       );
 5556:     $othertitle = &mt('All users');
 5557:     if (keys(%{$usertypes}) >  0) {
 5558:         $othertitle = &mt('Other users');
 5559:     }
 5560:     foreach my $key (keys(%env)) {
 5561:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 5562:             my ($usertype,$item) = ($1,$2);
 5563:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 5564:                 if ($usertype eq 'default') {   
 5565:                     push(@{$fields{$1}},$2);
 5566:                 } elsif (ref($types) eq 'ARRAY') {
 5567:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 5568:                         push(@{$fields{$1}},$2);
 5569:                     }
 5570:                 }
 5571:             }
 5572:         }
 5573:     }
 5574:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 5575:     @lockablenames = sort(@lockablenames);
 5576:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 5577:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 5578:         if (@changed) {
 5579:             $changes{'lockablenames'} = 1;
 5580:         }
 5581:     } else {
 5582:         if (@lockablenames) {
 5583:             $changes{'lockablenames'} = 1;
 5584:         }
 5585:     }
 5586:     my %updatehash = (
 5587:                       autoupdate => { run => $env{'form.autoupdate_run'},
 5588:                                       classlists => $env{'form.classlists'},
 5589:                                       fields => {%fields},
 5590:                                       lockablenames => \@lockablenames,
 5591:                                     }
 5592:                      );
 5593:     foreach my $key (keys(%currautoupdate)) {
 5594:         if (($key eq 'run') || ($key eq 'classlists')) {
 5595:             if (exists($updatehash{autoupdate}{$key})) {
 5596:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 5597:                     $changes{$key} = 1;
 5598:                 }
 5599:             }
 5600:         } elsif ($key eq 'fields') {
 5601:             if (ref($currautoupdate{$key}) eq 'HASH') {
 5602:                 foreach my $item (@{$types},'default') {
 5603:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 5604:                         my $change = 0;
 5605:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 5606:                             if (!exists($fields{$item})) {
 5607:                                 $change = 1;
 5608:                                 last;
 5609:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 5610:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 5611:                                     $change = 1;
 5612:                                     last;
 5613:                                 }
 5614:                             }
 5615:                         }
 5616:                         if ($change) {
 5617:                             push(@{$changes{$key}},$item);
 5618:                         }
 5619:                     } 
 5620:                 }
 5621:             }
 5622:         } elsif ($key eq 'lockablenames') {
 5623:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 5624:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 5625:                 if (@changed) {
 5626:                     $changes{'lockablenames'} = 1;
 5627:                 }
 5628:             } else {
 5629:                 if (@lockablenames) {
 5630:                     $changes{'lockablenames'} = 1;
 5631:                 }
 5632:             }
 5633:         }
 5634:     }
 5635:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 5636:         if (@lockablenames) {
 5637:             $changes{'lockablenames'} = 1;
 5638:         }
 5639:     }
 5640:     foreach my $item (@{$types},'default') {
 5641:         if (defined($fields{$item})) {
 5642:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 5643:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 5644:                     my $change = 0;
 5645:                     if (ref($fields{$item}) eq 'ARRAY') {
 5646:                         foreach my $type (@{$fields{$item}}) {
 5647:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 5648:                                 $change = 1;
 5649:                                 last;
 5650:                             }
 5651:                         }
 5652:                     }
 5653:                     if ($change) {
 5654:                         push(@{$changes{'fields'}},$item);
 5655:                     }
 5656:                 } else {
 5657:                     push(@{$changes{'fields'}},$item);
 5658:                 }
 5659:             } else {
 5660:                 push(@{$changes{'fields'}},$item);
 5661:             }
 5662:         }
 5663:     }
 5664:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 5665:                                              $dom);
 5666:     if ($putresult eq 'ok') {
 5667:         if (keys(%changes) > 0) {
 5668:             $resulttext = &mt('Changes made:').'<ul>';
 5669:             foreach my $key (sort(keys(%changes))) {
 5670:                 if ($key eq 'lockablenames') {
 5671:                     $resulttext .= '<li>';
 5672:                     if (@lockablenames) {
 5673:                         $usertypes->{'default'} = $othertitle;
 5674:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 5675:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 5676:                     } else {
 5677:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 5678:                     }
 5679:                     $resulttext .= '</li>';
 5680:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 5681:                     foreach my $item (@{$changes{$key}}) {
 5682:                         my @newvalues;
 5683:                         foreach my $type (@{$fields{$item}}) {
 5684:                             push(@newvalues,$fieldtitles{$type});
 5685:                         }
 5686:                         my $newvaluestr;
 5687:                         if (@newvalues > 0) {
 5688:                             $newvaluestr = join(', ',@newvalues);
 5689:                         } else {
 5690:                             $newvaluestr = &mt('none');
 5691:                         }
 5692:                         if ($item eq 'default') {
 5693:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 5694:                         } else {
 5695:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 5696:                         }
 5697:                     }
 5698:                 } else {
 5699:                     my $newvalue;
 5700:                     if ($key eq 'run') {
 5701:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 5702:                     } else {
 5703:                         $newvalue = $offon[$env{'form.'.$key}];
 5704:                     }
 5705:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 5706:                 }
 5707:             }
 5708:             $resulttext .= '</ul>';
 5709:         } else {
 5710:             $resulttext = &mt('No changes made to autoupdates');
 5711:         }
 5712:     } else {
 5713:         $resulttext = '<span class="LC_error">'.
 5714: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5715:     }
 5716:     return $resulttext;
 5717: }
 5718: 
 5719: sub modify_autocreate {
 5720:     my ($dom,%domconfig) = @_;
 5721:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 5722:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 5723:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 5724:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 5725:         }
 5726:     }
 5727:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 5728:                  req => 'Auto-creation of validated requests for official courses',
 5729:                  xmldc => 'Identity of course creator of courses from XML files',
 5730:                );
 5731:     my @types = ('xml','req');
 5732:     foreach my $item (@types) {
 5733:         $newvals{$item} = $env{'form.autocreate_'.$item};
 5734:         $newvals{$item} =~ s/\D//g;
 5735:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 5736:     }
 5737:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 5738:     my %domcoords = &get_active_dcs($dom);
 5739:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 5740:         $newvals{'xmldc'} = '';
 5741:     } 
 5742:     %autocreatehash =  (
 5743:                         autocreate => { xml => $newvals{'xml'},
 5744:                                         req => $newvals{'req'},
 5745:                                       }
 5746:                        );
 5747:     if ($newvals{'xmldc'} ne '') {
 5748:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 5749:     }
 5750:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 5751:                                              $dom);
 5752:     if ($putresult eq 'ok') {
 5753:         my @items = @types;
 5754:         if ($newvals{'xml'}) {
 5755:             push(@items,'xmldc');
 5756:         }
 5757:         foreach my $item (@items) {
 5758:             if (exists($currautocreate{$item})) {
 5759:                 if ($currautocreate{$item} ne $newvals{$item}) {
 5760:                     $changes{$item} = 1;
 5761:                 }
 5762:             } elsif ($newvals{$item}) {
 5763:                 $changes{$item} = 1;
 5764:             }
 5765:         }
 5766:         if (keys(%changes) > 0) {
 5767:             my @offon = ('off','on'); 
 5768:             $resulttext = &mt('Changes made:').'<ul>';
 5769:             foreach my $item (@types) {
 5770:                 if ($changes{$item}) {
 5771:                     my $newtxt = $offon[$newvals{$item}];
 5772:                     $resulttext .= '<li>'.&mt("$title{$item} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>';
 5773:                 }
 5774:             }
 5775:             if ($changes{'xmldc'}) {
 5776:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 5777:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 5778:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>'; 
 5779:             }
 5780:             $resulttext .= '</ul>';
 5781:         } else {
 5782:             $resulttext = &mt('No changes made to auto-creation settings');
 5783:         }
 5784:     } else {
 5785:         $resulttext = '<span class="LC_error">'.
 5786:             &mt('An error occurred: [_1]',$putresult).'</span>';
 5787:     }
 5788:     return $resulttext;
 5789: }
 5790: 
 5791: sub modify_directorysrch {
 5792:     my ($dom,%domconfig) = @_;
 5793:     my ($resulttext,%changes);
 5794:     my %currdirsrch;
 5795:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 5796:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 5797:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 5798:         }
 5799:     }
 5800:     my %title = ( available => 'Directory search available',
 5801:                   localonly => 'Other domains can search',
 5802:                   searchby => 'Search types',
 5803:                   searchtypes => 'Search latitude');
 5804:     my @offon = ('off','on');
 5805:     my @otherdoms = ('Yes','No');
 5806: 
 5807:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 5808:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 5809:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 5810: 
 5811:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5812:     if (keys(%{$usertypes}) == 0) {
 5813:         @cansearch = ('default');
 5814:     } else {
 5815:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 5816:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 5817:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 5818:                     push(@{$changes{'cansearch'}},$type);
 5819:                 }
 5820:             }
 5821:             foreach my $type (@cansearch) {
 5822:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 5823:                     push(@{$changes{'cansearch'}},$type);
 5824:                 }
 5825:             }
 5826:         } else {
 5827:             push(@{$changes{'cansearch'}},@cansearch);
 5828:         }
 5829:     }
 5830: 
 5831:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 5832:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 5833:             if (!grep(/^\Q$by\E$/,@searchby)) {
 5834:                 push(@{$changes{'searchby'}},$by);
 5835:             }
 5836:         }
 5837:         foreach my $by (@searchby) {
 5838:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 5839:                 push(@{$changes{'searchby'}},$by);
 5840:             }
 5841:         }
 5842:     } else {
 5843:         push(@{$changes{'searchby'}},@searchby);
 5844:     }
 5845: 
 5846:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 5847:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 5848:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 5849:                 push(@{$changes{'searchtypes'}},$type);
 5850:             }
 5851:         }
 5852:         foreach my $type (@searchtypes) {
 5853:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 5854:                 push(@{$changes{'searchtypes'}},$type);
 5855:             }
 5856:         }
 5857:     } else {
 5858:         if (exists($currdirsrch{'searchtypes'})) {
 5859:             foreach my $type (@searchtypes) {  
 5860:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 5861:                     push(@{$changes{'searchtypes'}},$type);
 5862:                 }
 5863:             }
 5864:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 5865:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 5866:             }   
 5867:         } else {
 5868:             push(@{$changes{'searchtypes'}},@searchtypes); 
 5869:         }
 5870:     }
 5871: 
 5872:     my %dirsrch_hash =  (
 5873:             directorysrch => { available => $env{'form.dirsrch_available'},
 5874:                                cansearch => \@cansearch,
 5875:                                localonly => $env{'form.dirsrch_localonly'},
 5876:                                searchby => \@searchby,
 5877:                                searchtypes => \@searchtypes,
 5878:                              }
 5879:             );
 5880:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 5881:                                              $dom);
 5882:     if ($putresult eq 'ok') {
 5883:         if (exists($currdirsrch{'available'})) {
 5884:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 5885:                  $changes{'available'} = 1;
 5886:              }
 5887:         } else {
 5888:             if ($env{'form.dirsrch_available'} eq '1') {
 5889:                 $changes{'available'} = 1;
 5890:             }
 5891:         }
 5892:         if (exists($currdirsrch{'localonly'})) {
 5893:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 5894:                  $changes{'localonly'} = 1;
 5895:              }
 5896:         } else {
 5897:             if ($env{'form.dirsrch_localonly'} eq '1') {
 5898:                 $changes{'localonly'} = 1;
 5899:             }
 5900:         }
 5901:         if (keys(%changes) > 0) {
 5902:             $resulttext = &mt('Changes made:').'<ul>';
 5903:             if ($changes{'available'}) {
 5904:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 5905:             }
 5906:             if ($changes{'localonly'}) {
 5907:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 5908:             }
 5909: 
 5910:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 5911:                 my $chgtext;
 5912:                 if (ref($usertypes) eq 'HASH') {
 5913:                     if (keys(%{$usertypes}) > 0) {
 5914:                         foreach my $type (@{$types}) {
 5915:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 5916:                                 $chgtext .= $usertypes->{$type}.'; ';
 5917:                             }
 5918:                         }
 5919:                         if (grep(/^default$/,@cansearch)) {
 5920:                             $chgtext .= $othertitle;
 5921:                         } else {
 5922:                             $chgtext =~ s/\; $//;
 5923:                         }
 5924:                         $resulttext .= '<li>'.&mt("Users from domain '<span class=\"LC_cusr_emph\">[_1]</span>' permitted to search the institutional directory set to: [_2]",$dom,$chgtext).'</li>';
 5925:                     }
 5926:                 }
 5927:             }
 5928:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 5929:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 5930:                 my $chgtext;
 5931:                 foreach my $type (@{$titleorder}) {
 5932:                     if (grep(/^\Q$type\E$/,@searchby)) {
 5933:                         if (defined($searchtitles->{$type})) {
 5934:                             $chgtext .= $searchtitles->{$type}.'; ';
 5935:                         }
 5936:                     }
 5937:                 }
 5938:                 $chgtext =~ s/\; $//;
 5939:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 5940:             }
 5941:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 5942:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 5943:                 my $chgtext;
 5944:                 foreach my $type (@{$srchtypeorder}) {
 5945:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 5946:                         if (defined($srchtypes_desc->{$type})) {
 5947:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 5948:                         }
 5949:                     }
 5950:                 }
 5951:                 $chgtext =~ s/\; $//;
 5952:                 $resulttext .= '<li>'.&mt("$title{'searchtypes'} set to: \"[_1]\"",$chgtext).'</li>';
 5953:             }
 5954:             $resulttext .= '</ul>';
 5955:         } else {
 5956:             $resulttext = &mt('No changes made to institution directory search settings');
 5957:         }
 5958:     } else {
 5959:         $resulttext = '<span class="LC_error">'.
 5960:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 5961:     }
 5962:     return $resulttext;
 5963: }
 5964: 
 5965: sub modify_contacts {
 5966:     my ($dom,%domconfig) = @_;
 5967:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 5968:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 5969:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 5970:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 5971:         }
 5972:     }
 5973:     my (%others,%to,%bcc);
 5974:     my @contacts = ('supportemail','adminemail');
 5975:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 5976:                     'requestsmail');
 5977:     foreach my $type (@mailings) {
 5978:         @{$newsetting{$type}} = 
 5979:             &Apache::loncommon::get_env_multiple('form.'.$type);
 5980:         foreach my $item (@contacts) {
 5981:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 5982:                 $contacts_hash{contacts}{$type}{$item} = 1;
 5983:             } else {
 5984:                 $contacts_hash{contacts}{$type}{$item} = 0;
 5985:             }
 5986:         }  
 5987:         $others{$type} = $env{'form.'.$type.'_others'};
 5988:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 5989:         if ($type eq 'helpdeskmail') {
 5990:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 5991:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 5992:         }
 5993:     }
 5994:     foreach my $item (@contacts) {
 5995:         $to{$item} = $env{'form.'.$item};
 5996:         $contacts_hash{'contacts'}{$item} = $to{$item};
 5997:     }
 5998:     if (keys(%currsetting) > 0) {
 5999:         foreach my $item (@contacts) {
 6000:             if ($to{$item} ne $currsetting{$item}) {
 6001:                 $changes{$item} = 1;
 6002:             }
 6003:         }
 6004:         foreach my $type (@mailings) {
 6005:             foreach my $item (@contacts) {
 6006:                 if (ref($currsetting{$type}) eq 'HASH') {
 6007:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6008:                         push(@{$changes{$type}},$item);
 6009:                     }
 6010:                 } else {
 6011:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6012:                 }
 6013:             }
 6014:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6015:                 push(@{$changes{$type}},'others');
 6016:             }
 6017:             if ($type eq 'helpdeskmail') {   
 6018:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6019:                     push(@{$changes{$type}},'bcc'); 
 6020:                 }
 6021:             }
 6022:         }
 6023:     } else {
 6024:         my %default;
 6025:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6026:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6027:         $default{'errormail'} = 'adminemail';
 6028:         $default{'packagesmail'} = 'adminemail';
 6029:         $default{'helpdeskmail'} = 'supportemail';
 6030:         $default{'lonstatusmail'} = 'adminemail';
 6031:         $default{'requestsmail'} = 'adminemail';
 6032:         foreach my $item (@contacts) {
 6033:            if ($to{$item} ne $default{$item}) {
 6034:               $changes{$item} = 1;
 6035:            } 
 6036:         }
 6037:         foreach my $type (@mailings) {
 6038:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6039:                
 6040:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6041:             }
 6042:             if ($others{$type} ne '') {
 6043:                 push(@{$changes{$type}},'others');
 6044:             }
 6045:             if ($type eq 'helpdeskmail') {
 6046:                 if ($bcc{$type} ne '') {
 6047:                     push(@{$changes{$type}},'bcc');
 6048:                 }
 6049:             }
 6050:         }
 6051:     }
 6052:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6053:                                              $dom);
 6054:     if ($putresult eq 'ok') {
 6055:         if (keys(%changes) > 0) {
 6056:             my ($titles,$short_titles)  = &contact_titles();
 6057:             $resulttext = &mt('Changes made:').'<ul>';
 6058:             foreach my $item (@contacts) {
 6059:                 if ($changes{$item}) {
 6060:                     $resulttext .= '<li>'.$titles->{$item}.
 6061:                                     &mt(' set to: ').
 6062:                                     '<span class="LC_cusr_emph">'.
 6063:                                     $to{$item}.'</span></li>';
 6064:                 }
 6065:             }
 6066:             foreach my $type (@mailings) {
 6067:                 if (ref($changes{$type}) eq 'ARRAY') {
 6068:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6069:                     my @text;
 6070:                     foreach my $item (@{$newsetting{$type}}) {
 6071:                         push(@text,$short_titles->{$item});
 6072:                     }
 6073:                     if ($others{$type} ne '') {
 6074:                         push(@text,$others{$type});
 6075:                     }
 6076:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6077:                                    join(', ',@text).'</span>';
 6078:                     if ($type eq 'helpdeskmail') {
 6079:                         if ($bcc{$type} ne '') {
 6080:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6081:                         }
 6082:                     }
 6083:                     $resulttext .= '</li>';
 6084:                 }
 6085:             }
 6086:             $resulttext .= '</ul>';
 6087:         } else {
 6088:             $resulttext = &mt('No changes made to contact information');
 6089:         }
 6090:     } else {
 6091:         $resulttext = '<span class="LC_error">'.
 6092:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6093:     }
 6094:     return $resulttext;
 6095: }
 6096: 
 6097: sub modify_usercreation {
 6098:     my ($dom,%domconfig) = @_;
 6099:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6100:     my $warningmsg;
 6101:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6102:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6103:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6104:         }
 6105:     }
 6106:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6107:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6108:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6109:     my @contexts = ('author','course','requestcrs','selfcreate');
 6110:     foreach my $item(@contexts) {
 6111:         if ($item eq 'selfcreate') {
 6112:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6113:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6114:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6115:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6116:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6117:                         $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.');   
 6118:                     }
 6119:                 }
 6120:             }
 6121:         } else {
 6122:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6123:         }
 6124:     }
 6125:     my ($othertitle,$usertypes,$types) = 
 6126:         &Apache::loncommon::sorted_inst_types($dom);
 6127:     if (ref($types) eq 'ARRAY') {
 6128:         if (@{$types} > 0) {
 6129:             @{$cancreate{'statustocreate'}} = 
 6130:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6131:         } else {
 6132:             @{$cancreate{'statustocreate'}} = ();
 6133:         }
 6134:         push(@contexts,'statustocreate');
 6135:     }
 6136:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6137:         foreach my $item (@contexts) {
 6138:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6139:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6140:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6141:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6142:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6143:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6144:                                     push(@{$changes{'cancreate'}},$item);
 6145:                                 }
 6146:                             }
 6147:                         }
 6148:                     }
 6149:                 } else {
 6150:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6151:                         if (@{$cancreate{$item}} > 0) {
 6152:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6153:                                 push(@{$changes{'cancreate'}},$item);
 6154:                             }
 6155:                         }
 6156:                     } else {
 6157:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6158:                             if (@{$cancreate{$item}} < 3) {
 6159:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6160:                                     push(@{$changes{'cancreate'}},$item);
 6161:                                 }
 6162:                             }
 6163:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6164:                             if (@{$cancreate{$item}} > 0) {
 6165:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6166:                                     push(@{$changes{'cancreate'}},$item);
 6167:                                 }
 6168:                             }
 6169:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6170:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6171:                                 push(@{$changes{'cancreate'}},$item);
 6172:                             }
 6173:                         }
 6174:                     }
 6175:                 }
 6176:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6177:                     foreach my $type (@{$cancreate{$item}}) {
 6178:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6179:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6180:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6181:                                     push(@{$changes{'cancreate'}},$item);
 6182:                                 }
 6183:                             }
 6184:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6185:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6186:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6187:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6188:                                     push(@{$changes{'cancreate'}},$item);
 6189:                                 }
 6190:                             }
 6191:                         }
 6192:                     }
 6193:                 }
 6194:             } else {
 6195:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6196:                     push(@{$changes{'cancreate'}},$item);
 6197:                 }
 6198:             }
 6199:         }
 6200:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6201:         foreach my $item (@contexts) {
 6202:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6203:                 if ($cancreate{$item} ne 'any') {
 6204:                     push(@{$changes{'cancreate'}},$item);
 6205:                 }
 6206:             } else {
 6207:                 if ($cancreate{$item} ne 'none') {
 6208:                     push(@{$changes{'cancreate'}},$item);
 6209:                 }
 6210:             }
 6211:         }
 6212:     } else {
 6213:         foreach my $item (@contexts)  {
 6214:             push(@{$changes{'cancreate'}},$item);
 6215:         }
 6216:     }
 6217: 
 6218:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6219:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6220:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6221:                 push(@{$changes{'username_rule'}},$type);
 6222:             }
 6223:         }
 6224:         foreach my $type (@username_rule) {
 6225:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6226:                 push(@{$changes{'username_rule'}},$type);
 6227:             }
 6228:         }
 6229:     } else {
 6230:         push(@{$changes{'username_rule'}},@username_rule);
 6231:     }
 6232: 
 6233:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6234:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 6235:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 6236:                 push(@{$changes{'id_rule'}},$type);
 6237:             }
 6238:         }
 6239:         foreach my $type (@id_rule) {
 6240:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 6241:                 push(@{$changes{'id_rule'}},$type);
 6242:             }
 6243:         }
 6244:     } else {
 6245:         push(@{$changes{'id_rule'}},@id_rule);
 6246:     }
 6247: 
 6248:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 6249:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 6250:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 6251:                 push(@{$changes{'email_rule'}},$type);
 6252:             }
 6253:         }
 6254:         foreach my $type (@email_rule) {
 6255:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 6256:                 push(@{$changes{'email_rule'}},$type);
 6257:             }
 6258:         }
 6259:     } else {
 6260:         push(@{$changes{'email_rule'}},@email_rule);
 6261:     }
 6262: 
 6263:     my @authen_contexts = ('author','course','domain');
 6264:     my @authtypes = ('int','krb4','krb5','loc');
 6265:     my %authhash;
 6266:     foreach my $item (@authen_contexts) {
 6267:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 6268:         foreach my $auth (@authtypes) {
 6269:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 6270:                 $authhash{$item}{$auth} = 1;
 6271:             } else {
 6272:                 $authhash{$item}{$auth} = 0;
 6273:             }
 6274:         }
 6275:     }
 6276:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 6277:         foreach my $item (@authen_contexts) {
 6278:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 6279:                 foreach my $auth (@authtypes) {
 6280:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 6281:                         push(@{$changes{'authtypes'}},$item);
 6282:                         last;
 6283:                     }
 6284:                 }
 6285:             }
 6286:         }
 6287:     } else {
 6288:         foreach my $item (@authen_contexts) {
 6289:             push(@{$changes{'authtypes'}},$item);
 6290:         }
 6291:     }
 6292: 
 6293:     my %usercreation_hash =  (
 6294:             usercreation => {
 6295:                               cancreate     => \%cancreate,
 6296:                               username_rule => \@username_rule,
 6297:                               id_rule       => \@id_rule,
 6298:                               email_rule    => \@email_rule,
 6299:                               authtypes     => \%authhash,
 6300:                             }
 6301:             );
 6302: 
 6303:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 6304:                                              $dom);
 6305: 
 6306:     my %selfcreatetypes = (
 6307:                              sso   => 'users authenticated by institutional single sign on',
 6308:                              login => 'users authenticated by institutional log-in',
 6309:                              email => 'users who provide a valid e-mail address for use as the username',
 6310:                           );
 6311:     if ($putresult eq 'ok') {
 6312:         if (keys(%changes) > 0) {
 6313:             $resulttext = &mt('Changes made:').'<ul>';
 6314:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 6315:                 my %lt = &usercreation_types();
 6316:                 foreach my $type (@{$changes{'cancreate'}}) {
 6317:                     my $chgtext;
 6318:                     unless ($type eq 'statustocreate') {
 6319:                         $chgtext = $lt{$type}.', ';
 6320:                     }
 6321:                     if ($type eq 'selfcreate') {
 6322:                         if (@{$cancreate{$type}} == 0) {
 6323:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 6324:                         } else {
 6325:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 6326:                             foreach my $case (@{$cancreate{$type}}) {
 6327:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 6328:                             }
 6329:                             $chgtext .= '</ul>';
 6330:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 6331:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 6332:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 6333:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 6334:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6335:                                         }
 6336:                                     }
 6337:                                 }
 6338:                             }
 6339:                         }
 6340:                     } elsif ($type eq 'statustocreate') {
 6341:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 6342:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 6343:                             if (@{$cancreate{'selfcreate'}} > 0) {
 6344:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 6345: 
 6346:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 6347:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 6348:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6349:                                     } 
 6350:                                 } elsif (ref($usertypes) eq 'HASH') {
 6351:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6352:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 6353:                                     } else {
 6354:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 6355:                                     }
 6356:                                     $chgtext .= '<ul>';
 6357:                                     foreach my $case (@{$cancreate{$type}}) {
 6358:                                         if ($case eq 'default') {
 6359:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 6360:                                         } else {
 6361:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 6362:                                         }
 6363:                                     }
 6364:                                     $chgtext .= '</ul>';
 6365:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6366:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 6367:                                     }
 6368:                                 }
 6369:                             } else {
 6370:                                 if (@{$cancreate{$type}} == 0) {
 6371:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 6372:                                 } else {
 6373:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 6374:                                 }
 6375:                             }
 6376:                         }
 6377:                     } else {
 6378:                         if ($cancreate{$type} eq 'none') {
 6379:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 6380:                         } elsif ($cancreate{$type} eq 'any') {
 6381:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 6382:                         } elsif ($cancreate{$type} eq 'official') {
 6383:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 6384:                         } elsif ($cancreate{$type} eq 'unofficial') {
 6385:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 6386:                         }
 6387:                     }
 6388:                     $resulttext .= '<li>'.$chgtext.'</li>';
 6389:                 }
 6390:             }
 6391:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 6392:                 my ($rules,$ruleorder) = 
 6393:                     &Apache::lonnet::inst_userrules($dom,'username');
 6394:                 my $chgtext = '<ul>';
 6395:                 foreach my $type (@username_rule) {
 6396:                     if (ref($rules->{$type}) eq 'HASH') {
 6397:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 6398:                     }
 6399:                 }
 6400:                 $chgtext .= '</ul>';
 6401:                 if (@username_rule > 0) {
 6402:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 6403:                 } else {
 6404:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 6405:                 }
 6406:             }
 6407:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 6408:                 my ($idrules,$idruleorder) = 
 6409:                     &Apache::lonnet::inst_userrules($dom,'id');
 6410:                 my $chgtext = '<ul>';
 6411:                 foreach my $type (@id_rule) {
 6412:                     if (ref($idrules->{$type}) eq 'HASH') {
 6413:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 6414:                     }
 6415:                 }
 6416:                 $chgtext .= '</ul>';
 6417:                 if (@id_rule > 0) {
 6418:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 6419:                 } else {
 6420:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 6421:                 }
 6422:             }
 6423:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 6424:                 my ($emailrules,$emailruleorder) =
 6425:                     &Apache::lonnet::inst_userrules($dom,'email');
 6426:                 my $chgtext = '<ul>';
 6427:                 foreach my $type (@email_rule) {
 6428:                     if (ref($emailrules->{$type}) eq 'HASH') {
 6429:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 6430:                     }
 6431:                 }
 6432:                 $chgtext .= '</ul>';
 6433:                 if (@email_rule > 0) {
 6434:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 6435:                 } else {
 6436:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 6437:                 }
 6438:             }
 6439: 
 6440:             my %authname = &authtype_names();
 6441:             my %context_title = &context_names();
 6442:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 6443:                 my $chgtext = '<ul>';
 6444:                 foreach my $type (@{$changes{'authtypes'}}) {
 6445:                     my @allowed;
 6446:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 6447:                     foreach my $auth (@authtypes) {
 6448:                         if ($authhash{$type}{$auth}) {
 6449:                             push(@allowed,$authname{$auth});
 6450:                         }
 6451:                     }
 6452:                     if (@allowed > 0) {
 6453:                         $chgtext .= join(', ',@allowed).'</li>';
 6454:                     } else {
 6455:                         $chgtext .= &mt('none').'</li>';
 6456:                     }
 6457:                 }
 6458:                 $chgtext .= '</ul>';
 6459:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 6460:                 $resulttext .= '</li>';
 6461:             }
 6462:             $resulttext .= '</ul>';
 6463:         } else {
 6464:             $resulttext = &mt('No changes made to user creation settings');
 6465:         }
 6466:     } else {
 6467:         $resulttext = '<span class="LC_error">'.
 6468:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6469:     }
 6470:     if ($warningmsg ne '') {
 6471:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 6472:     }
 6473:     return $resulttext;
 6474: }
 6475: 
 6476: sub modify_usermodification {
 6477:     my ($dom,%domconfig) = @_;
 6478:     my ($resulttext,%curr_usermodification,%changes);
 6479:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 6480:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 6481:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 6482:         }
 6483:     }
 6484:     my @contexts = ('author','course','selfcreate');
 6485:     my %context_title = (
 6486:                            author => 'In author context',
 6487:                            course => 'In course context',
 6488:                            selfcreate => 'When self creating account', 
 6489:                         );
 6490:     my @fields = ('lastname','firstname','middlename','generation',
 6491:                   'permanentemail','id');
 6492:     my %roles = (
 6493:                   author => ['ca','aa'],
 6494:                   course => ['st','ep','ta','in','cr'],
 6495:                 );
 6496:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6497:     if (ref($types) eq 'ARRAY') {
 6498:         push(@{$types},'default');
 6499:         $usertypes->{'default'} = $othertitle;
 6500:     }
 6501:     $roles{'selfcreate'} = $types;  
 6502:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6503:     my %modifyhash;
 6504:     foreach my $context (@contexts) {
 6505:         foreach my $role (@{$roles{$context}}) {
 6506:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 6507:             foreach my $item (@fields) {
 6508:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 6509:                     $modifyhash{$context}{$role}{$item} = 1;
 6510:                 } else {
 6511:                     $modifyhash{$context}{$role}{$item} = 0;
 6512:                 }
 6513:             }
 6514:         }
 6515:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 6516:             foreach my $role (@{$roles{$context}}) {
 6517:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 6518:                     foreach my $field (@fields) {
 6519:                         if ($modifyhash{$context}{$role}{$field} ne 
 6520:                                 $curr_usermodification{$context}{$role}{$field}) {
 6521:                             push(@{$changes{$context}},$role);
 6522:                             last;
 6523:                         }
 6524:                     }
 6525:                 }
 6526:             }
 6527:         } else {
 6528:             foreach my $context (@contexts) {
 6529:                 foreach my $role (@{$roles{$context}}) {
 6530:                     push(@{$changes{$context}},$role);
 6531:                 }
 6532:             }
 6533:         }
 6534:     }
 6535:     my %usermodification_hash =  (
 6536:                                    usermodification => \%modifyhash,
 6537:                                  );
 6538:     my $putresult = &Apache::lonnet::put_dom('configuration',
 6539:                                              \%usermodification_hash,$dom);
 6540:     if ($putresult eq 'ok') {
 6541:         if (keys(%changes) > 0) {
 6542:             $resulttext = &mt('Changes made: ').'<ul>';
 6543:             foreach my $context (@contexts) {
 6544:                 if (ref($changes{$context}) eq 'ARRAY') {
 6545:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 6546:                     if (ref($changes{$context}) eq 'ARRAY') {
 6547:                         foreach my $role (@{$changes{$context}}) {
 6548:                             my $rolename;
 6549:                             if ($context eq 'selfcreate') {
 6550:                                 $rolename = $role;
 6551:                                 if (ref($usertypes) eq 'HASH') {
 6552:                                     if ($usertypes->{$role} ne '') {
 6553:                                         $rolename = $usertypes->{$role};
 6554:                                     }
 6555:                                 }
 6556:                             } else {
 6557:                                 if ($role eq 'cr') {
 6558:                                     $rolename = &mt('Custom');
 6559:                                 } else {
 6560:                                     $rolename = &Apache::lonnet::plaintext($role);
 6561:                                 }
 6562:                             }
 6563:                             my @modifiable;
 6564:                             if ($context eq 'selfcreate') {
 6565:                                 $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): ');
 6566:                             } else {
 6567:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 6568:                             }
 6569:                             foreach my $field (@fields) {
 6570:                                 if ($modifyhash{$context}{$role}{$field}) {
 6571:                                     push(@modifiable,$fieldtitles{$field});
 6572:                                 }
 6573:                             }
 6574:                             if (@modifiable > 0) {
 6575:                                 $resulttext .= join(', ',@modifiable);
 6576:                             } else {
 6577:                                 $resulttext .= &mt('none'); 
 6578:                             }
 6579:                             $resulttext .= '</li>';
 6580:                         }
 6581:                         $resulttext .= '</ul></li>';
 6582:                     }
 6583:                 }
 6584:             }
 6585:             $resulttext .= '</ul>';
 6586:         } else {
 6587:             $resulttext = &mt('No changes made to user modification settings');
 6588:         }
 6589:     } else {
 6590:         $resulttext = '<span class="LC_error">'.
 6591:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6592:     }
 6593:     return $resulttext;
 6594: }
 6595: 
 6596: sub modify_defaults {
 6597:     my ($dom,$r) = @_;
 6598:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 6599:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6600:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 6601:     my @authtypes = ('internal','krb4','krb5','localauth');
 6602:     foreach my $item (@items) {
 6603:         $newvalues{$item} = $env{'form.'.$item};
 6604:         if ($item eq 'auth_def') {
 6605:             if ($newvalues{$item} ne '') {
 6606:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 6607:                     push(@errors,$item);
 6608:                 }
 6609:             }
 6610:         } elsif ($item eq 'lang_def') {
 6611:             if ($newvalues{$item} ne '') {
 6612:                 if ($newvalues{$item} =~ /^(\w+)/) {
 6613:                     my $langcode = $1;
 6614:                     if ($langcode ne 'x_chef') {
 6615:                         if (code2language($langcode) eq '') {
 6616:                             push(@errors,$item);
 6617:                         }
 6618:                     }
 6619:                 } else {
 6620:                     push(@errors,$item);
 6621:                 }
 6622:             }
 6623:         } elsif ($item eq 'timezone_def') {
 6624:             if ($newvalues{$item} ne '') {
 6625:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 6626:                     push(@errors,$item);   
 6627:                 }
 6628:             }
 6629:         } elsif ($item eq 'datelocale_def') {
 6630:             if ($newvalues{$item} ne '') {
 6631:                 my @datelocale_ids = DateTime::Locale->ids();
 6632:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 6633:                     push(@errors,$item);
 6634:                 }
 6635:             }
 6636:         } elsif ($item eq 'portal_def') {
 6637:             if ($newvalues{$item} ne '') {
 6638:                 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])\/?$/) {
 6639:                     push(@errors,$item);
 6640:                 }
 6641:             }
 6642:         }
 6643:         if (grep(/^\Q$item\E$/,@errors)) {
 6644:             $newvalues{$item} = $domdefaults{$item};
 6645:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 6646:             $changes{$item} = 1;
 6647:         }
 6648:         $domdefaults{$item} = $newvalues{$item};
 6649:     }
 6650:     my %defaults_hash = (
 6651:                          defaults => \%newvalues,
 6652:                         );
 6653:     my $title = &defaults_titles();
 6654:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 6655:                                              $dom);
 6656:     if ($putresult eq 'ok') {
 6657:         if (keys(%changes) > 0) {
 6658:             $resulttext = &mt('Changes made:').'<ul>';
 6659:             my $version = $r->dir_config('lonVersion');
 6660:             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";
 6661:             foreach my $item (sort(keys(%changes))) {
 6662:                 my $value = $env{'form.'.$item};
 6663:                 if ($value eq '') {
 6664:                     $value = &mt('none');
 6665:                 } elsif ($item eq 'auth_def') {
 6666:                     my %authnames = &authtype_names();
 6667:                     my %shortauth = (
 6668:                              internal => 'int',
 6669:                              krb4 => 'krb4',
 6670:                              krb5 => 'krb5',
 6671:                              localauth  => 'loc',
 6672:                     );
 6673:                     $value = $authnames{$shortauth{$value}};
 6674:                 }
 6675:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 6676:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 6677:             }
 6678:             $resulttext .= '</ul>';
 6679:             $mailmsgtext .= "\n";
 6680:             my $cachetime = 24*60*60;
 6681:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6682:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 6683:                 my $sysmail = $r->dir_config('lonSysEMail');
 6684:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 6685:             }
 6686:         } else {
 6687:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 6688:         }
 6689:     } else {
 6690:         $resulttext = '<span class="LC_error">'.
 6691:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6692:     }
 6693:     if (@errors > 0) {
 6694:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 6695:         foreach my $item (@errors) {
 6696:             $resulttext .= ' "'.$title->{$item}.'",';
 6697:         }
 6698:         $resulttext =~ s/,$//;
 6699:     }
 6700:     return $resulttext;
 6701: }
 6702: 
 6703: sub modify_scantron {
 6704:     my ($r,$dom,$confname,%domconfig) = @_;
 6705:     my ($resulttext,%confhash,%changes,$errors);
 6706:     my $custom = 'custom.tab';
 6707:     my $default = 'default.tab';
 6708:     my $servadm = $r->dir_config('lonAdmEMail');
 6709:     my ($configuserok,$author_ok,$switchserver) = 
 6710:         &config_check($dom,$confname,$servadm);
 6711:     if ($env{'form.scantronformat.filename'} ne '') {
 6712:         my $error;
 6713:         if ($configuserok eq 'ok') {
 6714:             if ($switchserver) {
 6715:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 6716:             } else {
 6717:                 if ($author_ok eq 'ok') {
 6718:                     my ($result,$scantronurl) =
 6719:                         &publishlogo($r,'upload','scantronformat',$dom,
 6720:                                      $confname,'scantron','','',$custom);
 6721:                     if ($result eq 'ok') {
 6722:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 6723:                         $changes{'scantronformat'} = 1;
 6724:                     } else {
 6725:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 6726:                     }
 6727:                 } else {
 6728:                     $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);
 6729:                 }
 6730:             }
 6731:         } else {
 6732:             $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);
 6733:         }
 6734:         if ($error) {
 6735:             &Apache::lonnet::logthis($error);
 6736:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6737:         }
 6738:     }
 6739:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 6740:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 6741:             if ($env{'form.scantronformat_del'}) {
 6742:                 $confhash{'scantron'}{'scantronformat'} = '';
 6743:                 $changes{'scantronformat'} = 1;
 6744:             }
 6745:         }
 6746:     }
 6747:     if (keys(%confhash) > 0) {
 6748:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 6749:                                                  $dom);
 6750:         if ($putresult eq 'ok') {
 6751:             if (keys(%changes) > 0) {
 6752:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 6753:                     $resulttext = &mt('Changes made:').'<ul>';
 6754:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 6755:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 6756:                     } else {
 6757:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 6758:                     }
 6759:                     $resulttext .= '</ul>';
 6760:                 } else {
 6761:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 6762:                 }
 6763:                 $resulttext .= '</ul>';
 6764:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6765:             } else {
 6766:                 $resulttext = &mt('No changes made to bubblesheet format file');
 6767:             }
 6768:         } else {
 6769:             $resulttext = '<span class="LC_error">'.
 6770:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 6771:         }
 6772:     } else {
 6773:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 6774:     }
 6775:     if ($errors) {
 6776:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6777:                        $errors.'</ul>';
 6778:     }
 6779:     return $resulttext;
 6780: }
 6781: 
 6782: sub modify_coursecategories {
 6783:     my ($dom,%domconfig) = @_;
 6784:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 6785:         $cathash);
 6786:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 6787:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 6788:         $cathash = $domconfig{'coursecategories'}{'cats'};
 6789:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 6790:             $changes{'togglecats'} = 1;
 6791:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 6792:         }
 6793:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 6794:             $changes{'categorize'} = 1;
 6795:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 6796:         }
 6797:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 6798:             $changes{'togglecatscomm'} = 1;
 6799:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 6800:         }
 6801:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 6802:             $changes{'categorizecomm'} = 1;
 6803:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 6804:         }
 6805:     } else {
 6806:         $changes{'togglecats'} = 1;
 6807:         $changes{'categorize'} = 1;
 6808:         $changes{'togglecatscomm'} = 1;
 6809:         $changes{'categorizecomm'} = 1;
 6810:         $domconfig{'coursecategories'} = {
 6811:                                              togglecats => $env{'form.togglecats'},
 6812:                                              categorize => $env{'form.categorize'},
 6813:                                              togglecatscomm => $env{'form.togglecatscomm'},
 6814:                                              categorizecomm => $env{'form.categorizecomm'},
 6815:                                          };
 6816:     }
 6817:     if (ref($cathash) eq 'HASH') {
 6818:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 6819:             push (@deletecategory,'instcode::0');
 6820:         }
 6821:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 6822:             push(@deletecategory,'communities::0');
 6823:         }
 6824:     }
 6825:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 6826:     if (ref($cathash) eq 'HASH') {
 6827:         if (@deletecategory > 0) {
 6828:             #FIXME Need to remove category from all courses using a deleted category 
 6829:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 6830:             foreach my $item (@deletecategory) {
 6831:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 6832:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 6833:                     $deletions{$item} = 1;
 6834:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 6835:                 }
 6836:             }
 6837:         }
 6838:         foreach my $item (keys(%{$cathash})) {
 6839:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 6840:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 6841:                 $reorderings{$item} = 1;
 6842:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 6843:             }
 6844:             if ($env{'form.addcategory_name_'.$item} ne '') {
 6845:                 my $newcat = $env{'form.addcategory_name_'.$item};
 6846:                 my $newdepth = $depth+1;
 6847:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 6848:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 6849:                 $adds{$newitem} = 1; 
 6850:             }
 6851:             if ($env{'form.subcat_'.$item} ne '') {
 6852:                 my $newcat = $env{'form.subcat_'.$item};
 6853:                 my $newdepth = $depth+1;
 6854:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 6855:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 6856:                 $adds{$newitem} = 1;
 6857:             }
 6858:         }
 6859:     }
 6860:     if ($env{'form.instcode'} eq '1') {
 6861:         if (ref($cathash) eq 'HASH') {
 6862:             my $newitem = 'instcode::0';
 6863:             if ($cathash->{$newitem} eq '') {  
 6864:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 6865:                 $adds{$newitem} = 1;
 6866:             }
 6867:         } else {
 6868:             my $newitem = 'instcode::0';
 6869:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 6870:             $adds{$newitem} = 1;
 6871:         }
 6872:     }
 6873:     if ($env{'form.communities'} eq '1') {
 6874:         if (ref($cathash) eq 'HASH') {
 6875:             my $newitem = 'communities::0';
 6876:             if ($cathash->{$newitem} eq '') {
 6877:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 6878:                 $adds{$newitem} = 1;
 6879:             }
 6880:         } else {
 6881:             my $newitem = 'communities::0';
 6882:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 6883:             $adds{$newitem} = 1;
 6884:         }
 6885:     }
 6886:     if ($env{'form.addcategory_name'} ne '') {
 6887:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 6888:             ($env{'form.addcategory_name'} ne 'communities')) {
 6889:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 6890:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 6891:             $adds{$newitem} = 1;
 6892:         }
 6893:     }
 6894:     my $putresult;
 6895:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 6896:         if (keys(%deletions) > 0) {
 6897:             foreach my $key (keys(%deletions)) {
 6898:                 if ($predelallitems{$key} ne '') {
 6899:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 6900:                 }
 6901:             }
 6902:         }
 6903:         my (@chkcats,@chktrails,%chkallitems);
 6904:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 6905:         if (ref($chkcats[0]) eq 'ARRAY') {
 6906:             my $depth = 0;
 6907:             my $chg = 0;
 6908:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 6909:                 my $name = $chkcats[0][$i];
 6910:                 my $item;
 6911:                 if ($name eq '') {
 6912:                     $chg ++;
 6913:                 } else {
 6914:                     $item = &escape($name).'::0';
 6915:                     if ($chg) {
 6916:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 6917:                     }
 6918:                     $depth ++; 
 6919:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 6920:                     $depth --;
 6921:                 }
 6922:             }
 6923:         }
 6924:     }
 6925:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 6926:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 6927:         if ($putresult eq 'ok') {
 6928:             my %title = (
 6929:                          togglecats     => 'Show/Hide a course in catalog',
 6930:                          categorize     => 'Assign a category to a course',
 6931:                          togglecatscomm => 'Show/Hide a community in catalog',
 6932:                          categorizecomm => 'Assign a category to a community',
 6933:                         );
 6934:             my %level = (
 6935:                          dom  => 'set in Domain ("Modify Course/Community")',
 6936:                          crs  => 'set in Course ("Course Configuration")',
 6937:                          comm => 'set in Community ("Community Configuration")',
 6938:                         );
 6939:             $resulttext = &mt('Changes made:').'<ul>';
 6940:             if ($changes{'togglecats'}) {
 6941:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 6942:             }
 6943:             if ($changes{'categorize'}) {
 6944:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 6945:             }
 6946:             if ($changes{'togglecatscomm'}) {
 6947:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 6948:             }
 6949:             if ($changes{'categorizecomm'}) {
 6950:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 6951:             }
 6952:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 6953:                 my $cathash;
 6954:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 6955:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 6956:                 } else {
 6957:                     $cathash = {};
 6958:                 } 
 6959:                 my (@cats,@trails,%allitems);
 6960:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 6961:                 if (keys(%deletions) > 0) {
 6962:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 6963:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 6964:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 6965:                     }
 6966:                     $resulttext .= '</ul></li>';
 6967:                 }
 6968:                 if (keys(%reorderings) > 0) {
 6969:                     my %sort_by_trail;
 6970:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 6971:                     foreach my $key (keys(%reorderings)) {
 6972:                         if ($allitems{$key} ne '') {
 6973:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 6974:                         }
 6975:                     }
 6976:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 6977:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 6978:                     }
 6979:                     $resulttext .= '</ul></li>';
 6980:                 }
 6981:                 if (keys(%adds) > 0) {
 6982:                     my %sort_by_trail;
 6983:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 6984:                     foreach my $key (keys(%adds)) {
 6985:                         if ($allitems{$key} ne '') {
 6986:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 6987:                         }
 6988:                     }
 6989:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 6990:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 6991:                     }
 6992:                     $resulttext .= '</ul></li>';
 6993:                 }
 6994:             }
 6995:             $resulttext .= '</ul>';
 6996:         } else {
 6997:             $resulttext = '<span class="LC_error">'.
 6998:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 6999:         }
 7000:     } else {
 7001:         $resulttext = &mt('No changes made to course and community categories');
 7002:     }
 7003:     return $resulttext;
 7004: }
 7005: 
 7006: sub modify_serverstatuses {
 7007:     my ($dom,%domconfig) = @_;
 7008:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7009:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7010:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7011:     }
 7012:     my @pages = &serverstatus_pages();
 7013:     foreach my $type (@pages) {
 7014:         $newserverstatus{$type}{'namedusers'} = '';
 7015:         $newserverstatus{$type}{'machines'} = '';
 7016:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7017:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7018:             my @okusers;
 7019:             foreach my $user (@users) {
 7020:                 my ($uname,$udom) = split(/:/,$user);
 7021:                 if (($udom =~ /^$match_domain$/) &&   
 7022:                     (&Apache::lonnet::domain($udom)) &&
 7023:                     ($uname =~ /^$match_username$/)) {
 7024:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7025:                         push(@okusers,$user);
 7026:                     }
 7027:                 }
 7028:             }
 7029:             if (@okusers > 0) {
 7030:                  @okusers = sort(@okusers);
 7031:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7032:             }
 7033:         }
 7034:         if (defined($env{'form.'.$type.'_machines'})) {
 7035:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7036:             my @okmachines;
 7037:             foreach my $ip (@machines) {
 7038:                 my @parts = split(/\./,$ip);
 7039:                 next if (@parts < 4);
 7040:                 my $badip = 0;
 7041:                 for (my $i=0; $i<4; $i++) {
 7042:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7043:                         $badip = 1;
 7044:                         last;
 7045:                     }
 7046:                 }
 7047:                 if (!$badip) {
 7048:                     push(@okmachines,$ip);     
 7049:                 }
 7050:             }
 7051:             @okmachines = sort(@okmachines);
 7052:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7053:         }
 7054:     }
 7055:     my %serverstatushash =  (
 7056:                                 serverstatuses => \%newserverstatus,
 7057:                             );
 7058:     foreach my $type (@pages) {
 7059:         foreach my $setting ('namedusers','machines') {
 7060:             my (@current,@new);
 7061:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7062:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7063:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7064:                 }
 7065:             }
 7066:             if ($newserverstatus{$type}{$setting} ne '') {
 7067:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7068:             }
 7069:             if (@current > 0) {
 7070:                 if (@new > 0) {
 7071:                     foreach my $item (@current) {
 7072:                         if (!grep(/^\Q$item\E$/,@new)) {
 7073:                             $changes{$type}{$setting} = 1;
 7074:                             last;
 7075:                         }
 7076:                     }
 7077:                     foreach my $item (@new) {
 7078:                         if (!grep(/^\Q$item\E$/,@current)) {
 7079:                             $changes{$type}{$setting} = 1;
 7080:                             last;
 7081:                         }
 7082:                     }
 7083:                 } else {
 7084:                     $changes{$type}{$setting} = 1;
 7085:                 }
 7086:             } elsif (@new > 0) {
 7087:                 $changes{$type}{$setting} = 1;
 7088:             }
 7089:         }
 7090:     }
 7091:     if (keys(%changes) > 0) {
 7092:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7093:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7094:                                                  \%serverstatushash,$dom);
 7095:         if ($putresult eq 'ok') {
 7096:             $resulttext .= &mt('Changes made:').'<ul>';
 7097:             foreach my $type (@pages) {
 7098:                 if (ref($changes{$type}) eq 'HASH') {
 7099:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7100:                     if ($changes{$type}{'namedusers'}) {
 7101:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7102:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7103:                         } else {
 7104:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7105:                         }
 7106:                     }
 7107:                     if ($changes{$type}{'machines'}) {
 7108:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7109:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7110:                         } else {
 7111:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7112:                         }
 7113: 
 7114:                     }
 7115:                     $resulttext .= '</ul></li>';
 7116:                 }
 7117:             }
 7118:             $resulttext .= '</ul>';
 7119:         } else {
 7120:             $resulttext = '<span class="LC_error">'.
 7121:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7122: 
 7123:         }
 7124:     } else {
 7125:         $resulttext = &mt('No changes made to access to server status pages');
 7126:     }
 7127:     return $resulttext;
 7128: }
 7129: 
 7130: sub modify_helpsettings {
 7131:     my ($r,$dom,$confname,%domconfig) = @_;
 7132:  	my ($resulttext,$errors,%changes,%helphash);
 7133:  	
 7134:  	my $customhelpfile  = $env{'form.loginhelpurl.filename'};
 7135:     my $defaulthelpfile = 'defaulthelp.html';
 7136:  	my $servadm = $r->dir_config('lonAdmEMail');
 7137:     my ($configuserok,$author_ok,$switchserver) = 
 7138:         &config_check($dom,$confname,$servadm);
 7139:  	
 7140:  	my %defaultchecked = ('submitbugs'	=> 'on');
 7141:  	my @offon = ('off','on');
 7142:     my %title = ( submitbugs     => 'Display link for users to submit a bug', 
 7143:     			  loginhelpurl  => 'Unauthenticated login help page set to custom file');
 7144:     			  
 7145:     my @toggles = ('submitbugs');
 7146: 
 7147:     $helphash{'helpsettings'} = {};
 7148:     
 7149:     if (ref($domconfig{'helpsettings'}) ne 'HASH') {
 7150:         if ($domconfig{'helpsettings'} eq '') {
 7151:             $domconfig{'helpsettings'} = {};
 7152:         }
 7153:     }
 7154:     
 7155:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7156:     	
 7157:         foreach my $item (@toggles) {
 7158:         	
 7159: 			if ($defaultchecked{$item} eq 'on') { 
 7160: 				if (($domconfig{'helpsettings'}{$item} eq '') &&
 7161: 					 ($env{'form.'.$item} eq '0')) {
 7162: 					$changes{$item} = 1;
 7163: 				} elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7164: 					$changes{$item} = 1;
 7165: 				}
 7166: 			} elsif ($defaultchecked{$item} eq 'off') {
 7167: 				if (($domconfig{'helpsettings'}{$item} eq '') &&
 7168: 					 ($env{'form.'.$item} eq '1')) {
 7169: 					$changes{$item} = 1;
 7170: 				} elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7171: 					$changes{$item} = 1;
 7172: 				}
 7173: 			}
 7174: 			$helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7175: 		}
 7176: 		
 7177: 		if ($customhelpfile ne '') {
 7178: 			my $error;
 7179: 			if ($configuserok eq 'ok') {
 7180: 				if ($switchserver) {
 7181: 					$error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7182: 				} else {
 7183: 					if ($author_ok eq 'ok') {
 7184: 						my ($result,$loginhelpurl) =
 7185: 							&publishlogo($r,'upload','loginhelpurl',$dom,
 7186: 										 $confname,'help','','',$customhelpfile);
 7187: 						if ($result eq 'ok') {
 7188: 							$helphash{'helpsettings'}{'loginhelpurl'} = $loginhelpurl;
 7189: 							$changes{'loginhelpurl'} = 1;
 7190: 						} else {
 7191: 							$error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customhelpfile,$result);
 7192: 						}
 7193: 					} else {
 7194: 						$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].",$customhelpfile,$confname,$dom,$author_ok);
 7195: 					}
 7196: 				}
 7197: 			} else {
 7198: 				$error = &mt("Upload of [_1] failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$customhelpfile,$confname,$dom,$configuserok);
 7199: 			}
 7200: 			if ($error) {
 7201: 				&Apache::lonnet::logthis($error);
 7202: 				$errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7203: 			}
 7204: 		}
 7205: 		
 7206:         if ($domconfig{'helpsettings'}{'loginhelpurl'} ne '') {
 7207:             if ($env{'form.loginhelpurl_del'}) {
 7208:                 $helphash{'helpsettings'}{'loginhelpurl'} = '';
 7209:                 $changes{'loginhelpurl'} = 1;
 7210:             }
 7211:         }
 7212:     }
 7213:     
 7214:     
 7215:     my $putresult;
 7216:     
 7217:     if (keys(%changes) > 0) {
 7218:     	$putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7219:     } else {
 7220:     	$putresult = 'ok';
 7221:     }
 7222:                                              
 7223:     if ($putresult eq 'ok') {
 7224:         if (keys(%changes) > 0) {
 7225: 			$resulttext = &mt('Changes made:').'<ul>';
 7226: 			foreach my $item (sort(keys(%changes))) {
 7227: 				if ($item eq 'submitbugs') {
 7228: 					$resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7229: 				}
 7230: 				if ($item eq 'loginhelpurl') {
 7231: 					if ($helphash{'helpsettings'}{'loginhelpurl'} eq '') {
 7232:                         $resulttext .= '<li>'.&mt('[_1] help file removed; [_2] file will be used for the unathorized help page in this domain.',$customhelpfile,$defaulthelpfile).'</li>';
 7233:                     } else {
 7234:                         $resulttext .= '<li>'.&mt("$title{$item} [_1]",$customhelpfile).'</li>';
 7235:                     }
 7236: 				}
 7237: 			}
 7238: 			$resulttext .= '</ul>';
 7239: 		} else {
 7240: 			$resulttext = &mt('No changes made to help settings');
 7241: 		}
 7242:     } else {
 7243:         $resulttext = '<span class="LC_error">'.
 7244: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7245:     }
 7246:     if ($errors) {
 7247:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7248:                        $errors.'</ul>';
 7249:     }
 7250:     return $resulttext;
 7251: }
 7252: 
 7253: sub modify_coursedefaults {
 7254:     my ($dom,%domconfig) = @_;
 7255:     my ($resulttext,$errors,%changes,%defaultshash);
 7256:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7257:     my @offon = ('off','on');
 7258:     my @toggles = ('canuse_pdfforms');
 7259: 
 7260:     $defaultshash{'coursedefaults'} = {};
 7261: 
 7262:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7263:         if ($domconfig{'coursedefaults'} eq '') {
 7264:             $domconfig{'coursedefaults'} = {};
 7265:         }
 7266:     }
 7267: 
 7268:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7269:         foreach my $item (@toggles) {
 7270:             if ($defaultchecked{$item} eq 'on') {
 7271:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7272:                     ($env{'form.'.$item} eq '0')) {
 7273:                     $changes{$item} = 1;
 7274:                 } elsif ($domconfig{'coursdefaults'}{$item} ne $env{'form.'.$item}) {
 7275:                     $changes{$item} = 1;
 7276:                 }
 7277:             } elsif ($defaultchecked{$item} eq 'off') {
 7278:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7279:                     ($env{'form.'.$item} eq '1')) {
 7280:                     $changes{$item} = 1;
 7281:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7282:                     $changes{$item} = 1;
 7283:                 }
 7284:             }
 7285:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7286:         }
 7287:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7288:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7289:         $newdefresponder =~ s/\D//g;
 7290:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7291:             $newdefresponder = 1;
 7292:         }
 7293:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7294:         if ($currdefresponder ne $newdefresponder) {
 7295:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7296:                 $changes{'anonsurvey_threshold'} = 1;
 7297:             }
 7298:         }
 7299:     }
 7300:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7301:                                              $dom);
 7302:     if ($putresult eq 'ok') {
 7303:         if (keys(%changes) > 0) {
 7304:             if ($changes{'canuse_pdfforms'}) {
 7305:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7306:                 $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7307:                 my $cachetime = 24*60*60;
 7308:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7309:             }
 7310:             $resulttext = &mt('Changes made:').'<ul>';
 7311:             foreach my $item (sort(keys(%changes))) {
 7312:                 if ($item eq 'canuse_pdfforms') {
 7313:                     if ($env{'form.'.$item} eq '1') {
 7314:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7315:                     } else {
 7316:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7317:                     }
 7318:                 } elsif ($item eq 'anonsurvey_threshold') {
 7319:                         $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7320:                 }
 7321:             }
 7322:             $resulttext .= '</ul>';
 7323:         } else {
 7324:             $resulttext = &mt('No changes made to course defaults');
 7325:         }
 7326:     } else {
 7327:         $resulttext = '<span class="LC_error">'.
 7328:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7329:     }
 7330:     return $resulttext;
 7331: }
 7332: 
 7333: sub modify_usersessions {
 7334:     my ($dom,%domconfig) = @_;
 7335:     my @hostingtypes = ('version','excludedomain','includedomain');
 7336:     my @offloadtypes = ('primary','default');
 7337:     my %types = (
 7338:                   remote => \@hostingtypes,
 7339:                   hosted => \@hostingtypes,
 7340:                   spares => \@offloadtypes,
 7341:                 );
 7342:     my @prefixes = ('remote','hosted','spares');
 7343:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7344:     my (%by_ip,%by_location,@intdoms);
 7345:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7346:     my @locations = sort(keys(%by_location));
 7347:     my (%defaultshash,%changes);
 7348:     foreach my $prefix (@prefixes) {
 7349:         $defaultshash{'usersessions'}{$prefix} = {};
 7350:     }
 7351:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7352:     my $resulttext;
 7353:     my %iphost = &Apache::lonnet::get_iphost();
 7354:     foreach my $prefix (@prefixes) {
 7355:         next if ($prefix eq 'spares');
 7356:         foreach my $type (@{$types{$prefix}}) {
 7357:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7358:             if ($type eq 'version') {
 7359:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7360:                 my $okvalue;
 7361:                 if ($value ne '') {
 7362:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 7363:                         $okvalue = $value;
 7364:                     }
 7365:                 }
 7366:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7367:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7368:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 7369:                             if ($inuse == 0) {
 7370:                                 $changes{$prefix}{$type} = 1;
 7371:                             } else {
 7372:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 7373:                                     $changes{$prefix}{$type} = 1;
 7374:                                 }
 7375:                                 if ($okvalue ne '') {
 7376:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7377:                                 } 
 7378:                             }
 7379:                         } else {
 7380:                             if (($inuse == 1) && ($okvalue ne '')) {
 7381:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7382:                                 $changes{$prefix}{$type} = 1;
 7383:                             }
 7384:                         }
 7385:                     } else {
 7386:                         if (($inuse == 1) && ($okvalue ne '')) {
 7387:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7388:                             $changes{$prefix}{$type} = 1;
 7389:                         }
 7390:                     }
 7391:                 } else {
 7392:                     if (($inuse == 1) && ($okvalue ne '')) {
 7393:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 7394:                         $changes{$prefix}{$type} = 1;
 7395:                     }
 7396:                 }
 7397:             } else {
 7398:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 7399:                 my @okvals;
 7400:                 foreach my $val (@vals) {
 7401:                     if ($val =~ /:/) {
 7402:                         my @items = split(/:/,$val);
 7403:                         foreach my $item (@items) {
 7404:                             if (ref($by_location{$item}) eq 'ARRAY') {
 7405:                                 push(@okvals,$item);
 7406:                             }
 7407:                         }
 7408:                     } else {
 7409:                         if (ref($by_location{$val}) eq 'ARRAY') {
 7410:                             push(@okvals,$val);
 7411:                         }
 7412:                     }
 7413:                 }
 7414:                 @okvals = sort(@okvals);
 7415:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7416:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 7417:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7418:                             if ($inuse == 0) {
 7419:                                 $changes{$prefix}{$type} = 1; 
 7420:                             } else {
 7421:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7422:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 7423:                                 if (@changed > 0) {
 7424:                                     $changes{$prefix}{$type} = 1;
 7425:                                 }
 7426:                             }
 7427:                         } else {
 7428:                             if ($inuse == 1) {
 7429:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7430:                                 $changes{$prefix}{$type} = 1;
 7431:                             }
 7432:                         } 
 7433:                     } else {
 7434:                         if ($inuse == 1) {
 7435:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7436:                             $changes{$prefix}{$type} = 1;
 7437:                         }
 7438:                     }
 7439:                 } else {
 7440:                     if ($inuse == 1) {
 7441:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 7442:                         $changes{$prefix}{$type} = 1;
 7443:                     }
 7444:                 }
 7445:             }
 7446:         }
 7447:     }
 7448: 
 7449:     my @alldoms = &Apache::lonnet::all_domains();
 7450:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7451:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 7452:     my $savespares;
 7453: 
 7454:     foreach my $lonhost (sort(keys(%servers))) {
 7455:         my $serverhomeID =
 7456:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 7457:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 7458:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 7459:         my %spareschg;
 7460:         foreach my $type (@{$types{'spares'}}) {
 7461:             my @okspares;
 7462:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 7463:             foreach my $server (@checked) {
 7464:                 if (&Apache::lonnet::hostname($server) ne '') {
 7465:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 7466:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 7467:                             push(@okspares,$server);
 7468:                         }
 7469:                     }
 7470:                 }
 7471:             }
 7472:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 7473:             my $newspare;
 7474:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 7475:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 7476:                     $newspare = $new;
 7477:                 }
 7478:             }
 7479:             my @spares;
 7480:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 7481:                 @spares = sort(@okspares,$newspare);
 7482:             } else {
 7483:                 @spares = sort(@okspares);
 7484:             }
 7485:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 7486:             if (ref($spareid{$lonhost}) eq 'HASH') {
 7487:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 7488:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 7489:                     if (@diffs > 0) {
 7490:                         $spareschg{$type} = 1;
 7491:                     }
 7492:                 }
 7493:             }
 7494:         }
 7495:         if (keys(%spareschg) > 0) {
 7496:             $changes{'spares'}{$lonhost} = \%spareschg;
 7497:         }
 7498:     }
 7499: 
 7500:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 7501:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 7502:             if (ref($changes{'spares'}) eq 'HASH') {
 7503:                 if (keys(%{$changes{'spares'}}) > 0) {
 7504:                     $savespares = 1;
 7505:                 }
 7506:             }
 7507:         } else {
 7508:             $savespares = 1;
 7509:         }
 7510:     }
 7511: 
 7512:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 7513:     if ((keys(%changes) > 0) || ($savespares)) {
 7514:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7515:                                                  $dom);
 7516:         if ($putresult eq 'ok') {
 7517:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 7518:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 7519:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 7520:                 }
 7521:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 7522:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 7523:                 }
 7524:             }
 7525:             my $cachetime = 24*60*60;
 7526:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7527:             if (keys(%changes) > 0) {
 7528:                 my %lt = &usersession_titles();
 7529:                 $resulttext = &mt('Changes made:').'<ul>';
 7530:                 foreach my $prefix (@prefixes) {
 7531:                     if (ref($changes{$prefix}) eq 'HASH') {
 7532:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 7533:                         if ($prefix eq 'spares') {
 7534:                             if (ref($changes{$prefix}) eq 'HASH') {
 7535:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 7536:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 7537:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 7538:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 7539:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 7540:                                         foreach my $type (@{$types{$prefix}}) {
 7541:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 7542:                                                 my $offloadto = &mt('None');
 7543:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 7544:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 7545:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 7546:                                                     }
 7547:                                                 }
 7548:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 7549:                                             }
 7550:                                         }
 7551:                                     }
 7552:                                     $resulttext .= '</li>';
 7553:                                 }
 7554:                             }
 7555:                         } else {
 7556:                             foreach my $type (@{$types{$prefix}}) {
 7557:                                 if (defined($changes{$prefix}{$type})) {
 7558:                                     my $newvalue;
 7559:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 7560:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 7561:                                             if ($type eq 'version') {
 7562:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 7563:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 7564:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 7565:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 7566:                                                 }
 7567:                                             }
 7568:                                         }
 7569:                                     }
 7570:                                     if ($newvalue eq '') {
 7571:                                         if ($type eq 'version') {
 7572:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 7573:                                         } else {
 7574:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 7575:                                         }
 7576:                                     } else {
 7577:                                         if ($type eq 'version') {
 7578:                                             $newvalue .= ' '.&mt('(or later)'); 
 7579:                                         }
 7580:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 7581:                                     }
 7582:                                 }
 7583:                             }
 7584:                         }
 7585:                         $resulttext .= '</ul>';
 7586:                     }
 7587:                 }
 7588:                 $resulttext .= '</ul>';
 7589:             } else {
 7590:                 $resulttext = $nochgmsg;
 7591:             }
 7592:         } else {
 7593:             $resulttext = '<span class="LC_error">'.
 7594:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7595:         }
 7596:     } else {
 7597:         $resulttext = $nochgmsg;
 7598:     }
 7599:     return $resulttext;
 7600: }
 7601: 
 7602: sub modify_loadbalancing {
 7603:     my ($dom,%domconfig) = @_;
 7604:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7605:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7606:     my ($othertitle,$usertypes,$types) =
 7607:         &Apache::loncommon::sorted_inst_types($dom);
 7608:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7609:     my @sparestypes = ('primary','default');
 7610:     my %typetitles = &sparestype_titles();
 7611:     my $resulttext;
 7612:     if (keys(%servers) > 1) {
 7613:         my ($currbalancer,$currtargets,$currrules);
 7614:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 7615:             $currbalancer = $domconfig{'loadbalancing'}{'lonhost'};
 7616:             $currtargets = $domconfig{'loadbalancing'}{'targets'};
 7617:             $currrules = $domconfig{'loadbalancing'}{'rules'};
 7618:         } else {
 7619:             ($currbalancer,$currtargets) = 
 7620:                 &Apache::lonnet::get_lonbalancer_config(\%servers);
 7621:         }
 7622:         my ($saveloadbalancing,%defaultshash,%changes);
 7623:         my ($alltypes,$othertypes,$titles) =
 7624:             &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 7625:         my %ruletitles = &offloadtype_text();
 7626:         my $balancer = $env{'form.loadbalancing_lonhost'};
 7627:         if (!$servers{$balancer}) {
 7628:             undef($balancer);
 7629:         }
 7630:         if ($currbalancer ne $balancer) {
 7631:             $changes{'lonhost'} = 1;
 7632:         }
 7633:         $defaultshash{'loadbalancing'}{'lonhost'} = $balancer;
 7634:         if ($balancer ne '') {
 7635:             unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 7636:                 $saveloadbalancing = 1;
 7637:             }
 7638:             foreach my $sparetype (@sparestypes) {
 7639:                 my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$sparetype);
 7640:                 my @offloadto;
 7641:                 foreach my $target (@targets) {
 7642:                     if (($servers{$target}) && ($target ne $balancer)) {
 7643:                         if ($sparetype eq 'default') {
 7644:                             if (ref($defaultshash{'loadbalancing'}{'targets'}{'primary'}) eq 'ARRAY') {
 7645:                                 next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{'targets'}{'primary'}}));
 7646:                             }
 7647:                         }
 7648:                         unless(grep(/^\Q$target\E$/,@offloadto)) {
 7649:                             push(@offloadto,$target);
 7650:                         }
 7651:                     }
 7652:                     $defaultshash{'loadbalancing'}{'targets'}{$sparetype} = \@offloadto;
 7653:                 }
 7654:             }
 7655:         } else {
 7656:             foreach my $sparetype (@sparestypes) {
 7657:                 $defaultshash{'loadbalancing'}{'targets'}{$sparetype} = [];
 7658:             }
 7659:         }
 7660:         if (ref($currtargets) eq 'HASH') {
 7661:             foreach my $sparetype (@sparestypes) {
 7662:                 if (ref($currtargets->{$sparetype}) eq 'ARRAY') {
 7663:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets->{$sparetype},$defaultshash{'loadbalancing'}{'targets'}{$sparetype});
 7664:                     if (@targetdiffs > 0) {
 7665:                         $changes{'targets'} = 1;
 7666:                     }
 7667:                 } elsif (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 7668:                     if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 7669:                         $changes{'targets'} = 1;
 7670:                     }
 7671:                 }
 7672:             }
 7673:         } else {
 7674:             foreach my $sparetype (@sparestypes) {
 7675:                 if (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 7676:                     if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 7677:                         $changes{'targets'} = 1;  
 7678:                     }
 7679:                 }
 7680:             }  
 7681:         }
 7682:         my $ishomedom;
 7683:         if ($balancer ne '') {
 7684:             if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 7685:                 $ishomedom = 1;
 7686:             }
 7687:         }
 7688:         if (ref($alltypes) eq 'ARRAY') {
 7689:             foreach my $type (@{$alltypes}) {
 7690:                 my $rule;
 7691:                 if ($balancer ne '') {
 7692:                     unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) && 
 7693:                          (!$ishomedom)) {
 7694:                         $rule = $env{'form.loadbalancing_rules_'.$type};
 7695:                     }
 7696:                     if ($rule eq 'specific') {
 7697:                         $rule = $env{'form.loadbalancing_singleserver_'.$type};
 7698:                     }
 7699:                 }
 7700:                 $defaultshash{'loadbalancing'}{'rules'}{$type} = $rule;
 7701:                 if (ref($currrules) eq 'HASH') {
 7702:                     if ($rule ne $currrules->{$type}) {
 7703:                         $changes{'rules'}{$type} = 1;
 7704:                     }
 7705:                 } elsif ($rule ne '') {
 7706:                     $changes{'rules'}{$type} = 1;
 7707:                 }
 7708:             }
 7709:         }
 7710:         my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 7711:         if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 7712:             my $putresult = &Apache::lonnet::put_dom('configuration',
 7713:                                                      \%defaultshash,$dom);
 7714:             if ($putresult eq 'ok') {
 7715:                 if (keys(%changes) > 0) {
 7716:                     if ($changes{'lonhost'}) {
 7717:                         if ($currbalancer ne '') {
 7718:                             &Apache::lonnet::remote_devalidate_cache($currbalancer,'loadbalancing',$dom);
 7719:                         }
 7720:                         if ($balancer eq '') {
 7721:                             $resulttext .= '<li>'.&mt('Load Balancing with dedicated server discontinued').'</li>'; 
 7722:                         } else {
 7723:                             &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 7724:                             $resulttext .= '<li>'.&mt('Dedicated Load Balancer server set to [_1]',$balancer);
 7725:                         }
 7726:                     } else {
 7727:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 7728:                     }
 7729:                     if (($changes{'targets'}) && ($balancer ne '')) {
 7730:                         my %offloadstr;
 7731:                         foreach my $sparetype (@sparestypes) {
 7732:                             if (ref($defaultshash{'loadbalancing'}{'targets'}{$sparetype}) eq 'ARRAY') {
 7733:                                 if (@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}} > 0) {
 7734:                                     $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{'targets'}{$sparetype}});
 7735:                                 }
 7736:                             }
 7737:                         }
 7738:                         if (keys(%offloadstr) == 0) {
 7739:                             $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 7740:                         } else {
 7741:                             my $showoffload;
 7742:                             foreach my $sparetype (@sparestypes) {
 7743:                                 $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 7744:                                 if (defined($offloadstr{$sparetype})) {
 7745:                                     $showoffload .= $offloadstr{$sparetype};
 7746:                                 } else {
 7747:                                     $showoffload .= &mt('None');
 7748:                                 }
 7749:                                 $showoffload .= ('&nbsp;'x3);
 7750:                             }
 7751:                             $resulttext .= '<li>'.&mt('By default, Load Balancer server set to offload to: [_1]',$showoffload).'</li>';
 7752:                         }
 7753:                     }
 7754:                     if ((ref($changes{'rules'}) eq 'HASH') && ($balancer ne '')) {
 7755:                         if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 7756:                             foreach my $type (@{$alltypes}) {
 7757:                                 if ($changes{'rules'}{$type}) {
 7758:                                     my $rule = $defaultshash{'loadbalancing'}{'rules'}{$type};
 7759:                                     my $balancetext;
 7760:                                     if ($rule eq '') {
 7761:                                         $balancetext =  $ruletitles{'default'};
 7762:                                     } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 7763:                                         $balancetext =  $ruletitles{$rule};
 7764:                                     } else {
 7765:                                         $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{'rules'}{$type});
 7766:                                     }
 7767:                                     $resulttext .= '<li>'.&mt('Load Balancing for [_1] set to: [_2]',$titles->{$type},$balancetext).'</li>';     
 7768:                                 }
 7769:                             }
 7770:                         }
 7771:                     }
 7772:                     if ($resulttext ne '') {
 7773:                         $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 7774:                     } else {
 7775:                         $resulttext = $nochgmsg;
 7776:                     }
 7777:                 } else {
 7778:                     $resulttext = $nochgmsg;
 7779:                     if ($balancer ne '') {
 7780:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 7781:                     }
 7782:                 }
 7783:             } else {
 7784:                 $resulttext = '<span class="LC_error">'.
 7785:                               &mt('An error occurred: [_1]',$putresult).'</span>';
 7786:             }
 7787:         } else {
 7788:             $resulttext = $nochgmsg;
 7789:         }
 7790:     } else {
 7791:         $resulttext =  &mt('Load Balancing unavailable as this domain only has one server.');
 7792:     }
 7793:     return $resulttext;
 7794: }
 7795: 
 7796: sub recurse_check {
 7797:     my ($chkcats,$categories,$depth,$name) = @_;
 7798:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 7799:         my $chg = 0;
 7800:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 7801:             my $category = $chkcats->[$depth]{$name}[$j];
 7802:             my $item;
 7803:             if ($category eq '') {
 7804:                 $chg ++;
 7805:             } else {
 7806:                 my $deeper = $depth + 1;
 7807:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 7808:                 if ($chg) {
 7809:                     $categories->{$item} -= $chg;
 7810:                 }
 7811:                 &recurse_check($chkcats,$categories,$deeper,$category);
 7812:                 $deeper --;
 7813:             }
 7814:         }
 7815:     }
 7816:     return;
 7817: }
 7818: 
 7819: sub recurse_cat_deletes {
 7820:     my ($item,$coursecategories,$deletions) = @_;
 7821:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7822:     my $subdepth = $depth + 1;
 7823:     if (ref($coursecategories) eq 'HASH') {
 7824:         foreach my $subitem (keys(%{$coursecategories})) {
 7825:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 7826:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 7827:                 delete($coursecategories->{$subitem});
 7828:                 $deletions->{$subitem} = 1;
 7829:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 7830:             }  
 7831:         }
 7832:     }
 7833:     return;
 7834: }
 7835: 
 7836: sub get_active_dcs {
 7837:     my ($dom) = @_;
 7838:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc']);
 7839:     my %domcoords;
 7840:     my $numdcs = 0;
 7841:     my $now = time;
 7842:     foreach my $server (keys(%dompersonnel)) {
 7843:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 7844:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 7845:             my ($end,$start) = split(':',$dompersonnel{$server}{$user});
 7846:             if (($end eq '') || ($end == 0) || ($end > $now)) {
 7847:                 if ($start <= $now) {
 7848:                     $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 7849:                 }
 7850:             }
 7851:         }
 7852:     }
 7853:     return %domcoords;
 7854: }
 7855: 
 7856: sub active_dc_picker {
 7857:     my ($dom,$curr_dc) = @_;
 7858:     my %domcoords = &get_active_dcs($dom); 
 7859:     my @dcs = sort(keys(%domcoords));
 7860:     my $numdcs = scalar(@dcs); 
 7861:     my $datatable;
 7862:     my $numinrow = 2;
 7863:     if ($numdcs > 1) {
 7864:         $datatable = '<table>';
 7865:         for (my $i=0; $i<@dcs; $i++) {
 7866:             my $rem = $i%($numinrow);
 7867:             if ($rem == 0) {
 7868:                 if ($i > 0) {
 7869:                     $datatable .= '</tr>';
 7870:                 }
 7871:                 $datatable .= '<tr>';
 7872:             }
 7873:             my $check = ' ';
 7874:             if ($curr_dc eq '') {
 7875:                 if (!$i) { 
 7876:                     $check = ' checked="checked" ';
 7877:                 }
 7878:             } elsif ($dcs[$i] eq $curr_dc) {
 7879:                 $check = ' checked="checked" ';
 7880:             }
 7881:             if ($i == @dcs - 1) {
 7882:                 my $colsleft = $numinrow - $rem;
 7883:                 if ($colsleft > 1) {
 7884:                     $datatable .= '<td colspan="'.$colsleft.'">';
 7885:                 } else {
 7886:                     $datatable .= '<td>';
 7887:                 }
 7888:             } else {
 7889:                 $datatable .= '<td>';
 7890:             }
 7891:             my ($dcname,$dcdom) = split(':',$dcs[$i]);
 7892:             $datatable .= '<span class="LC_nobreak"><label>'.
 7893:                           '<input type="radio" name="autocreate_xmldc"'.
 7894:                           ' value="'.$dcs[$i].'"'.$check.'/>'.
 7895:                           &Apache::loncommon::plainname($dcname,$dcdom).
 7896:                           '</label></span></td>';
 7897:         }
 7898:         $datatable .= '</tr></table>';
 7899:     } elsif (@dcs) {
 7900:         $datatable .= '<input type="hidden" name="autocreate_dc" value="'.
 7901:                       $dcs[0].'" />';
 7902:     }
 7903:     return ($numdcs,$datatable);
 7904: }
 7905: 
 7906: sub usersession_titles {
 7907:     return &Apache::lonlocal::texthash(
 7908:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 7909:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 7910:                spares => 'Servers offloaded to, when busy',
 7911:                version => 'LON-CAPA version requirement',
 7912:                excludedomain => 'Allow all, but exclude specific domains',
 7913:                includedomain => 'Deny all, but include specific domains',
 7914:                primary => 'Primary (checked first)',
 7915:                default => 'Default',
 7916:            );
 7917: }
 7918: 
 7919: sub id_for_thisdom {
 7920:     my (%servers) = @_;
 7921:     my %altids;
 7922:     foreach my $server (keys(%servers)) {
 7923:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 7924:         if ($serverhome ne $server) {
 7925:             $altids{$serverhome} = $server;
 7926:         }
 7927:     }
 7928:     return %altids;
 7929: }
 7930: 
 7931: sub count_servers {
 7932:     my ($currbalancer,%servers) = @_;
 7933:     my (@spares,$numspares);
 7934:     foreach my $lonhost (sort(keys(%servers))) {
 7935:         next if ($currbalancer eq $lonhost);
 7936:         push(@spares,$lonhost);
 7937:     }
 7938:     if ($currbalancer) {
 7939:         $numspares = scalar(@spares);
 7940:     } else {
 7941:         $numspares = scalar(@spares) - 1;
 7942:     }
 7943:     return ($numspares,@spares);
 7944: }
 7945: 
 7946: sub lonbalance_targets_js {
 7947:     my ($dom,$types,$servers) = @_;
 7948:     my $select = &mt('Select');
 7949:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 7950:     if (ref($servers) eq 'HASH') {
 7951:         $alltargets = join("','",sort(keys(%{$servers})));
 7952:         my @homedoms;
 7953:         foreach my $server (sort(keys(%{$servers}))) {
 7954:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 7955:                 push(@homedoms,'1');
 7956:             } else {
 7957:                 push(@homedoms,'0');
 7958:             }
 7959:         }
 7960:         $allishome = join("','",@homedoms);
 7961:     }
 7962:     if (ref($types) eq 'ARRAY') {
 7963:         if (@{$types} > 0) {
 7964:             @alltypes = @{$types};
 7965:         }
 7966:     }
 7967:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 7968:     $allinsttypes = join("','",@alltypes);
 7969:     return <<"END";
 7970: 
 7971: <script type="text/javascript">
 7972: // <![CDATA[
 7973: 
 7974: function toggleTargets() {
 7975:     var balancer = document.display.loadbalancing_lonhost.options[document.display.loadbalancing_lonhost.selectedIndex].value;
 7976:     if (balancer == '') {
 7977:         hideSpares();
 7978:     } else {
 7979:         var homedoms = new Array('$allishome');
 7980:         var ishomedom = homedoms[document.display.loadbalancing_lonhost.selectedIndex];
 7981:         showSpares(balancer,ishomedom);
 7982:     }
 7983:     return;
 7984: }
 7985: 
 7986: function showSpares(balancer,ishomedom) {
 7987:     var alltargets = new Array('$alltargets');
 7988:     var insttypes = new Array('$allinsttypes');
 7989:     var offloadtypes = new Array('primary','default');
 7990: 
 7991:     document.getElementById('loadbalancing_targets').style.display='block';
 7992:     document.getElementById('loadbalancing_disabled').style.display='none';
 7993:  
 7994:     for (var i=0; i<offloadtypes.length; i++) {
 7995:         var count = 0;
 7996:         for (var j=0; j<alltargets.length; j++) {
 7997:             if (alltargets[j] != balancer) {
 7998:                 document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+count).value = alltargets[j];
 7999:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).style.textAlign='left';
 8000:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).style.textFace='normal';
 8001:                 document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8002:                 count ++;
 8003:             }
 8004:         }
 8005:     }
 8006:     for (var k=0; k<insttypes.length; k++) {
 8007:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8008:             if (ishomedom == 1) {
 8009:                 document.getElementById('balanceruletitle_'+insttypes[k]).style.display='block';
 8010:                 document.getElementById('balancerule_'+insttypes[k]).style.display='block';
 8011:             } else {
 8012:                 document.getElementById('balanceruletitle_'+insttypes[k]).style.display='none';
 8013:                 document.getElementById('balancerule_'+insttypes[k]).style.display='none';
 8014: 
 8015:             }
 8016:         } else {
 8017:             document.getElementById('balanceruletitle_'+insttypes[k]).style.display='block';
 8018:             document.getElementById('balancerule_'+insttypes[k]).style.display='block';
 8019:         }
 8020:         if ((insttypes[k] != '_LC_external') && 
 8021:             ((insttypes[k] != '_LC_internetdom') ||
 8022:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8023:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[0] = new Option("","",true,true);
 8024:             for (var m=0; m<alltargets.length; m++) {
 8025:                 var idx = m+1;
 8026:                 if (alltargets[m] != balancer) {
 8027:                     document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8028:                 }
 8029:             }
 8030:         }
 8031:     }
 8032:     return;
 8033: }
 8034: 
 8035: function hideSpares() {
 8036:     var alltargets = new Array('$alltargets');
 8037:     var insttypes = new Array('$allinsttypes');
 8038:     var offloadtypes = new Array('primary','default');
 8039: 
 8040:     document.getElementById('loadbalancing_targets').style.display='none';
 8041:     document.getElementById('loadbalancing_disabled').style.display='block';
 8042: 
 8043:     var total = alltargets.length - 1;
 8044:     for (var i=0; i<offloadtypes; i++) {
 8045:         for (var j=0; j<total; j++) {
 8046:            document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+j).checked = false;
 8047:            document.getElementById('loadbalancing_target_'+offloadtypes[i]+'_'+j).value = '';
 8048:            document.getElementById('loadbalancing_targettxt_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8049:         }
 8050:     }
 8051:     for (var k=0; k<insttypes.length; k++) {
 8052:         document.getElementById('balanceruletitle_'+insttypes[k]).style.display='none';
 8053:         document.getElementById('balancerule_'+insttypes[k]).style.display='none';
 8054:         if (insttypes[k] != '_LC_external') {
 8055:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).length = 0;
 8056:             document.getElementById('loadbalancing_singleserver_'+insttypes[k]).options[0] = new Option("","",true,true);
 8057:         }
 8058:     }
 8059:     return;
 8060: }
 8061: 
 8062: function checkOffloads(item,type) {
 8063:     var alltargets = new Array('$alltargets');
 8064:     var offloadtypes = new Array('primary','default');
 8065:     if (item.checked) {
 8066:         var total = alltargets.length - 1;
 8067:         var other;
 8068:         if (type == offloadtypes[0]) {
 8069:             other = offloadtypes[1];
 8070:         } else {
 8071:             other = offloadtypes[0];
 8072:         }
 8073:         for (var i=0; i<total; i++) {
 8074:             var server = document.getElementById('loadbalancing_target_'+other+'_'+i).value;
 8075:             if (server == item.value) {
 8076:                 if (document.getElementById('loadbalancing_target_'+other+'_'+i).checked) {
 8077:                     document.getElementById('loadbalancing_target_'+other+'_'+i).checked = false;
 8078:                 }
 8079:             }
 8080:         }
 8081:     }
 8082:     return;
 8083: }
 8084: 
 8085: function singleServerToggle(type) {
 8086:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+type).selectedIndex;
 8087:     if (offloadtoSelIdx == 0) {
 8088:         document.getElementById('loadbalancing_rules_'+type+'_0').checked = true;
 8089:         document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '';
 8090: 
 8091:     } else {
 8092:         document.getElementById('loadbalancing_rules_'+type+'_2').checked = true;
 8093:         document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '$select';
 8094:     }
 8095:     return;
 8096: }
 8097: 
 8098: function balanceruleChange(formname,type) {
 8099:     if (type == '_LC_external') {
 8100:         return; 
 8101:     }
 8102:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+type);
 8103:     for (var i=0; i<typesRules.length; i++) {
 8104:         if (formname.elements[typesRules[i]].checked) {
 8105:             if (formname.elements[typesRules[i]].value != 'specific') {
 8106:                 document.getElementById('loadbalancing_singleserver_'+type).selectedIndex = 0;
 8107:                 document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '';
 8108:             } else {
 8109:                 document.getElementById('loadbalancing_singleserver_'+type).options[0].text = '$select';
 8110:             }
 8111:         }
 8112:     }
 8113:     return;
 8114: }
 8115: 
 8116: // ]]>
 8117: </script>
 8118: 
 8119: END
 8120: }
 8121: 
 8122: sub new_spares_js {
 8123:     my @sparestypes = ('primary','default');
 8124:     my $types = join("','",@sparestypes);
 8125:     my $select = &mt('Select');
 8126:     return <<"END";
 8127: 
 8128: <script type="text/javascript">
 8129: // <![CDATA[
 8130: 
 8131: function updateNewSpares(formname,lonhost) {
 8132:     var types = new Array('$types');
 8133:     var include = new Array();
 8134:     var exclude = new Array();
 8135:     for (var i=0; i<types.length; i++) {
 8136:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8137:         for (var j=0; j<spareboxes.length; j++) {
 8138:             if (formname.elements[spareboxes[j]].checked) {
 8139:                 exclude.push(formname.elements[spareboxes[j]].value);
 8140:             } else {
 8141:                 include.push(formname.elements[spareboxes[j]].value);
 8142:             }
 8143:         }
 8144:     }
 8145:     for (var i=0; i<types.length; i++) {
 8146:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8147:         var selIdx = newSpare.selectedIndex;
 8148:         var currnew = newSpare.options[selIdx].value;
 8149:         var okSpares = new Array();
 8150:         for (var j=0; j<newSpare.options.length; j++) {
 8151:             var possible = newSpare.options[j].value;
 8152:             if (possible != '') {
 8153:                 if (exclude.indexOf(possible) == -1) {
 8154:                     okSpares.push(possible);
 8155:                 } else {
 8156:                     if (currnew == possible) {
 8157:                         selIdx = 0;
 8158:                     }
 8159:                 }
 8160:             }
 8161:         }
 8162:         for (var k=0; k<include.length; k++) {
 8163:             if (okSpares.indexOf(include[k]) == -1) {
 8164:                 okSpares.push(include[k]);
 8165:             }
 8166:         }
 8167:         okSpares.sort();
 8168:         newSpare.options.length = 0;
 8169:         if (selIdx == 0) {
 8170:             newSpare.options[0] = new Option("$select","",true,true);
 8171:         } else {
 8172:             newSpare.options[0] = new Option("$select","",false,false);
 8173:         }
 8174:         for (var m=0; m<okSpares.length; m++) {
 8175:             var idx = m+1;
 8176:             var selThis = 0;
 8177:             if (selIdx != 0) {
 8178:                 if (okSpares[m] == currnew) {
 8179:                     selThis = 1;
 8180:                 }
 8181:             }
 8182:             if (selThis == 1) {
 8183:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8184:             } else {
 8185:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8186:             }
 8187:         }
 8188:     }
 8189:     return;
 8190: }
 8191: 
 8192: function checkNewSpares(lonhost,type) {
 8193:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8194:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8195:     if (chosen != '') { 
 8196:         var othertype;
 8197:         var othernewSpare;
 8198:         if (type == 'primary') {
 8199:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 8200:         }
 8201:         if (type == 'default') {
 8202:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 8203:         }
 8204:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 8205:             othernewSpare.selectedIndex = 0;
 8206:         }
 8207:     }
 8208:     return;
 8209: }
 8210: 
 8211: // ]]>
 8212: </script>
 8213: 
 8214: END
 8215: 
 8216: }
 8217: 
 8218: sub common_domprefs_js {
 8219:     return <<"END";
 8220: 
 8221: <script type="text/javascript">
 8222: // <![CDATA[
 8223: 
 8224: function getIndicesByName(formname,item) {
 8225:     var group = new Array();
 8226:     for (var i=0;i<formname.elements.length;i++) {
 8227:         if (formname.elements[i].name == item) {
 8228:             group.push(formname.elements[i].id);
 8229:         }
 8230:     }
 8231:     return group;
 8232: }
 8233: 
 8234: // ]]>
 8235: </script>
 8236: 
 8237: END
 8238: 
 8239: }
 8240: 
 8241: 1;

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