File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.226: download - view: text, annotated - select for diffs
Thu Feb 13 01:52:29 2014 UTC (10 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- remove obsolete comment.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.226 2014/02/13 01:52:29 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affiliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas, 
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course, and to display/store
   98: default quota sizes for Authoring Spaces.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial, community, and textbook).  In each case the radio buttons 
  107: allow the selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use LONCAPA qw(:DEFAULT :match);
  168: use LONCAPA::Enrollment;
  169: use LONCAPA::lonauthcgi();
  170: use File::Copy;
  171: use Locale::Language;
  172: use DateTime::TimeZone;
  173: use DateTime::Locale;
  174: 
  175: my $registered_cleanup;
  176: my $modified_urls;
  177: 
  178: sub handler {
  179:     my $r=shift;
  180:     if ($r->header_only) {
  181:         &Apache::loncommon::content_type($r,'text/html');
  182:         $r->send_http_header;
  183:         return OK;
  184:     }
  185: 
  186:     my $context = 'domain';
  187:     my $dom = $env{'request.role.domain'};
  188:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  189:     if (&Apache::lonnet::allowed('mau',$dom)) {
  190:         &Apache::loncommon::content_type($r,'text/html');
  191:         $r->send_http_header;
  192:     } else {
  193:         $env{'user.error.msg'}=
  194:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  195:         return HTTP_NOT_ACCEPTABLE;
  196:     }
  197: 
  198:     $registered_cleanup=0;
  199:     @{$modified_urls}=();
  200: 
  201:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  202:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  203:                                             ['phase','actions']);
  204:     my $phase = 'pickactions';
  205:     if ( exists($env{'form.phase'}) ) {
  206:         $phase = $env{'form.phase'};
  207:     }
  208:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  209:     my %domconfig =
  210:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  211:                 'quotas','autoenroll','autoupdate','autocreate',
  212:                 'directorysrch','usercreation','usermodification',
  213:                 'contacts','defaults','scantron','coursecategories',
  214:                 'serverstatuses','requestcourses','helpsettings',
  215:                 'coursedefaults','usersessions','loadbalancing',
  216:                 'requestauthor'],$dom);
  217:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  218:                        'autoupdate','autocreate','directorysrch','contacts',
  219:                        'usercreation','selfcreation','usermodification','scantron',
  220:                        'requestcourses','requestauthor','coursecategories',
  221:                        'serverstatuses','helpsettings',
  222:                        'coursedefaults','usersessions');
  223:     my %existing;
  224:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  225:         %existing = %{$domconfig{'loadbalancing'}};
  226:     }
  227:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  228:         push(@prefs_order,'loadbalancing');
  229:     }
  230:     my %prefs = (
  231:         'rolecolors' =>
  232:                    { text => 'Default color schemes',
  233:                      help => 'Domain_Configuration_Color_Schemes',
  234:                      header => [{col1 => 'Student Settings',
  235:                                  col2 => '',},
  236:                                 {col1 => 'Coordinator Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Author Settings',
  239:                                  col2 => '',},
  240:                                 {col1 => 'Administrator Settings',
  241:                                  col2 => '',}],
  242:                     },
  243:         'login' =>
  244:                     { text => 'Log-in page options',
  245:                       help => 'Domain_Configuration_Login_Page',
  246:                       header => [{col1 => 'Log-in Page Items',
  247:                                   col2 => '',},
  248:                                  {col1 => 'Log-in Help',
  249:                                   col2 => 'Value'}],
  250:                     },
  251:         'defaults' => 
  252:                     { text => 'Default authentication/language/timezone/portal',
  253:                       help => 'Domain_Configuration_LangTZAuth',
  254:                       header => [{col1 => 'Setting',
  255:                                   col2 => 'Value'}],
  256:                     },
  257:         'quotas' => 
  258:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  259:                       help => 'Domain_Configuration_Quotas',
  260:                       header => [{col1 => 'User affiliation',
  261:                                   col2 => 'Available tools',
  262:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  263:                     },
  264:         'autoenroll' =>
  265:                    { text => 'Auto-enrollment settings',
  266:                      help => 'Domain_Configuration_Auto_Enrollment',
  267:                      header => [{col1 => 'Configuration setting',
  268:                                  col2 => 'Value(s)'}],
  269:                    },
  270:         'autoupdate' => 
  271:                    { text => 'Auto-update settings',
  272:                      help => 'Domain_Configuration_Auto_Updates',
  273:                      header => [{col1 => 'Setting',
  274:                                  col2 => 'Value',},
  275:                                 {col1 => 'Setting',
  276:                                  col2 => 'Affiliation'},
  277:                                 {col1 => 'User population',
  278:                                  col2 => 'Updateable user data'}],
  279:                   },
  280:         'autocreate' => 
  281:                   { text => 'Auto-course creation settings',
  282:                      help => 'Domain_Configuration_Auto_Creation',
  283:                      header => [{col1 => 'Configuration Setting',
  284:                                  col2 => 'Value',}],
  285:                   },
  286:         'directorysrch' => 
  287:                   { text => 'Institutional directory searches',
  288:                     help => 'Domain_Configuration_InstDirectory_Search',
  289:                     header => [{col1 => 'Setting',
  290:                                 col2 => 'Value',}],
  291:                   },
  292:         'contacts' =>
  293:                   { text => 'Contact Information',
  294:                     help => 'Domain_Configuration_Contact_Info',
  295:                     header => [{col1 => 'Setting',
  296:                                 col2 => 'Value',}],
  297:                   },
  298:         'usercreation' => 
  299:                   { text => 'User creation',
  300:                     help => 'Domain_Configuration_User_Creation',
  301:                     header => [{col1 => 'Format rule type',
  302:                                 col2 => 'Format rules in force'},
  303:                                {col1 => 'User account creation',
  304:                                 col2 => 'Usernames which may be created',},
  305:                                {col1 => 'Context',
  306:                                 col2 => 'Assignable authentication types'}],
  307:                   },
  308:         'selfcreation' => 
  309:                   { text => 'Users self-creating accounts',
  310:                     help => 'Domain_Configuration_Self_Creation', 
  311:                     header => [{col1 => 'Self-creation with institutional username',
  312:                                 col2 => 'Enabled?'},
  313:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  314:                                 col2 => 'Information user can enter'},
  315:                                {col1 => 'Self-creation with e-mail as username',
  316:                                 col2 => 'Settings'}],
  317:                   },
  318:         'usermodification' =>
  319:                   { text => 'User modification',
  320:                     help => 'Domain_Configuration_User_Modification',
  321:                     header => [{col1 => 'Target user has role',
  322:                                 col2 => 'User information updateable in author context'},
  323:                                {col1 => 'Target user has role',
  324:                                 col2 => 'User information updateable in course context'}],
  325:                   },
  326:         'scantron' =>
  327:                   { text => 'Bubblesheet format file',
  328:                     help => 'Domain_Configuration_Scantron_Format',
  329:                     header => [ {col1 => 'Item',
  330:                                  col2 => '',
  331:                               }],
  332:                   },
  333:         'requestcourses' => 
  334:                  {text => 'Request creation of courses',
  335:                   help => 'Domain_Configuration_Request_Courses',
  336:                   header => [{col1 => 'User affiliation',
  337:                               col2 => 'Availability/Processing of requests',},
  338:                              {col1 => 'Setting',
  339:                               col2 => 'Value'},
  340:                              {col1 => 'Available textbooks',
  341:                               col2 => ''}],
  342:                  },
  343:         'requestauthor' =>
  344:                  {text => 'Request Authoring Space',
  345:                   help => 'Domain_Configuration_Request_Author',
  346:                   header => [{col1 => 'User affiliation',
  347:                               col2 => 'Availability/Processing of requests',},
  348:                              {col1 => 'Setting',
  349:                               col2 => 'Value'}],
  350:                  },
  351:         'coursecategories' =>
  352:                   { text => 'Cataloging of courses/communities',
  353:                     help => 'Domain_Configuration_Cataloging_Courses',
  354:                     header => [{col1 => 'Category settings',
  355:                                 col2 => '',},
  356:                                {col1 => 'Categories',
  357:                                 col2 => '',
  358:                                }],
  359:                   },
  360:         'serverstatuses' =>
  361:                  {text   => 'Access to server status pages',
  362:                   help   => 'Domain_Configuration_Server_Status',
  363:                   header => [{col1 => 'Status Page',
  364:                               col2 => 'Other named users',
  365:                               col3 => 'Specific IPs',
  366:                             }],
  367:                  },
  368:         'helpsettings' =>
  369:                  {text   => 'Help page settings',
  370:                   help   => 'Domain_Configuration_Help_Settings',
  371:                   header => [{col1 => 'Help Settings (logged-in users)',
  372:                               col2 => 'Value'}],
  373:                  },
  374:         'coursedefaults' => 
  375:                  {text => 'Course/Community defaults',
  376:                   help => 'Domain_Configuration_Course_Defaults',
  377:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  378:                               col2 => 'Value',},
  379:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  380:                               col2 => 'Value',},],
  381:                  },
  382:         'privacy' => 
  383:                  {text   => 'User Privacy',
  384:                   help   => 'Domain_Configuration_User_Privacy',
  385:                   header => [{col1 => 'Setting',
  386:                               col2 => 'Value',}],
  387:                  },
  388:         'usersessions' =>
  389:                  {text  => 'User session hosting/offloading',
  390:                   help  => 'Domain_Configuration_User_Sessions',
  391:                   header => [{col1 => 'Domain server',
  392:                               col2 => 'Servers to offload sessions to when busy'},
  393:                              {col1 => 'Hosting of users from other domains',
  394:                               col2 => 'Rules'},
  395:                              {col1 => "Hosting domain's own users elsewhere",
  396:                               col2 => 'Rules'}],
  397:                  },
  398:          'loadbalancing' =>
  399:                  {text  => 'Dedicated Load Balancer(s)',
  400:                   help  => 'Domain_Configuration_Load_Balancing',
  401:                   header => [{col1 => 'Balancers',
  402:                               col2 => 'Default destinations',
  403:                               col3 => 'User affiliation',
  404:                               col4 => 'Overrides'},
  405:                             ],
  406:                  },
  407:     );
  408:     if (keys(%servers) > 1) {
  409:         $prefs{'login'}  = { text   => 'Log-in page options',
  410:                              help   => 'Domain_Configuration_Login_Page',
  411:                             header => [{col1 => 'Log-in Service',
  412:                                         col2 => 'Server Setting',},
  413:                                        {col1 => 'Log-in Page Items',
  414:                                         col2 => ''},
  415:                                        {col1 => 'Log-in Help',
  416:                                         col2 => 'Value'}],
  417:                            };
  418:     }
  419: 
  420:     my @roles = ('student','coordinator','author','admin');
  421:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  422:     &Apache::lonhtmlcommon::add_breadcrumb
  423:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  424:       text=>"Settings to display/modify"});
  425:     my $confname = $dom.'-domainconfig';
  426: 
  427:     if ($phase eq 'process') {
  428:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  429:                                                               \%prefs,\%domconfig,$confname,\@roles);
  430:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  431:             $r->rflush();
  432:             &devalidate_remote_domconfs($dom,$result);
  433:         }
  434:     } elsif ($phase eq 'display') {
  435:         my $js = &recaptcha_js().
  436:                  &credits_js();
  437:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  438:             my ($othertitle,$usertypes,$types) =
  439:                 &Apache::loncommon::sorted_inst_types($dom);
  440:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  441:                                           $domconfig{'loadbalancing'}).
  442:                    &new_spares_js().
  443:                    &common_domprefs_js().
  444:                    &Apache::loncommon::javascript_array_indexof();
  445:         }
  446:         if (grep(/^requestcourses$/,@actions)) {
  447:             my $javascript_validations;
  448:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  449:             $js .= <<END;
  450: <script type="text/javascript">
  451: $javascript_validations
  452: </script>
  453: $coursebrowserjs
  454: END
  455:         }
  456:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  457:     } else {
  458: # check if domconfig user exists for the domain.
  459:         my $servadm = $r->dir_config('lonAdmEMail');
  460:         my ($configuserok,$author_ok,$switchserver) =
  461:             &config_check($dom,$confname,$servadm);
  462:         unless ($configuserok eq 'ok') {
  463:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  464:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  465:                           $confname).
  466:                       '<br />'
  467:             );
  468:             if ($switchserver) {
  469:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  470:                           '<br />'.
  471:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  472:                           '<br />'.
  473:                           &mt('The "[_1]" user can be created automatically when a Domain Coordinator visits the web-based "Set domain configuration" screen, in a session hosted on the primary library server.',$confname).
  474:                           '<br />'.
  475:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  476:                 );
  477:             } else {
  478:                 $r->print(&mt('To create that user from the command line run the ./UPDATE script found in the top level directory of the extracted LON-CAPA tarball.').
  479:                           '<br />'.
  480:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  481:                 );
  482:             }
  483:             $r->print(&Apache::loncommon::end_page());
  484:             return OK;
  485:         }
  486:         if (keys(%domconfig) == 0) {
  487:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  488:             my @ids=&Apache::lonnet::current_machine_ids();
  489:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  490:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  491:                 my @loginimages = ('img','logo','domlogo','login');
  492:                 my $custom_img_count = 0;
  493:                 foreach my $img (@loginimages) {
  494:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  495:                         $custom_img_count ++;
  496:                     }
  497:                 }
  498:                 foreach my $role (@roles) {
  499:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  500:                         $custom_img_count ++;
  501:                     }
  502:                 }
  503:                 if ($custom_img_count > 0) {
  504:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  505:                     my $switch_server = &check_switchserver($dom,$confname);
  506:                     $r->print(
  507:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  508:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  509:     &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 />'.
  510:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  511:                     if ($switch_server) {
  512:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  513:                     }
  514:                     $r->print(&Apache::loncommon::end_page());
  515:                     return OK;
  516:                 }
  517:             }
  518:         }
  519:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  520:     }
  521:     return OK;
  522: }
  523: 
  524: sub process_changes {
  525:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  526:     my %domconfig;
  527:     if (ref($values) eq 'HASH') {
  528:         %domconfig = %{$values};
  529:     }
  530:     my $output;
  531:     if ($action eq 'login') {
  532:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  533:     } elsif ($action eq 'rolecolors') {
  534:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  535:                                      $lastactref,%domconfig);
  536:     } elsif ($action eq 'quotas') {
  537:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  538:     } elsif ($action eq 'autoenroll') {
  539:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  540:     } elsif ($action eq 'autoupdate') {
  541:         $output = &modify_autoupdate($dom,%domconfig);
  542:     } elsif ($action eq 'autocreate') {
  543:         $output = &modify_autocreate($dom,%domconfig);
  544:     } elsif ($action eq 'directorysrch') {
  545:         $output = &modify_directorysrch($dom,%domconfig);
  546:     } elsif ($action eq 'usercreation') {
  547:         $output = &modify_usercreation($dom,%domconfig);
  548:     } elsif ($action eq 'selfcreation') {
  549:         $output = &modify_selfcreation($dom,%domconfig);
  550:     } elsif ($action eq 'usermodification') {
  551:         $output = &modify_usermodification($dom,%domconfig);
  552:     } elsif ($action eq 'contacts') {
  553:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  554:     } elsif ($action eq 'defaults') {
  555:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  556:     } elsif ($action eq 'scantron') {
  557:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  558:     } elsif ($action eq 'coursecategories') {
  559:         $output = &modify_coursecategories($dom,%domconfig);
  560:     } elsif ($action eq 'serverstatuses') {
  561:         $output = &modify_serverstatuses($dom,%domconfig);
  562:     } elsif ($action eq 'requestcourses') {
  563:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  564:     } elsif ($action eq 'requestauthor') {
  565:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  566:     } elsif ($action eq 'helpsettings') {
  567:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  568:     } elsif ($action eq 'coursedefaults') {
  569:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  570:     } elsif ($action eq 'usersessions') {
  571:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  572:     } elsif ($action eq 'loadbalancing') {
  573:         $output = &modify_loadbalancing($dom,%domconfig);
  574:     }
  575:     return $output;
  576: }
  577: 
  578: sub print_config_box {
  579:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  580:     my $rowtotal = 0;
  581:     my $output;
  582:     if ($action eq 'coursecategories') {
  583:         $output = &coursecategories_javascript($settings);
  584:     }
  585:     $output .= 
  586:          '<table class="LC_nested_outer">
  587:           <tr>
  588:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  589:            &mt($item->{text}).'&nbsp;'.
  590:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  591:           '</tr>';
  592:     $rowtotal ++;
  593:     my $numheaders = 1;
  594:     if (ref($item->{'header'}) eq 'ARRAY') {
  595:         $numheaders = scalar(@{$item->{'header'}});
  596:     }
  597:     if ($numheaders > 1) {
  598:         my $colspan = '';
  599:         my $rightcolspan = '';
  600:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  601:             (($action eq 'login') && ($numheaders < 3))) {
  602:             $colspan = ' colspan="2"';
  603:         }
  604:         if ($action eq 'usersessions') {
  605:             $rightcolspan = ' colspan="3"'; 
  606:         }
  607:         $output .= '
  608:           <tr>
  609:            <td>
  610:             <table class="LC_nested">
  611:              <tr class="LC_info_row">
  612:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  613:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  614:              </tr>';
  615:         $rowtotal ++;
  616:         if ($action eq 'autoupdate') {
  617:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  618:         } elsif ($action eq 'usercreation') {
  619:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  620:         } elsif ($action eq 'selfcreation') {
  621:             $output .= &print_selfcreation('top',$dom,$settings,\$rowtotal);
  622:         } elsif ($action eq 'usermodification') {
  623:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  624:         } elsif ($action eq 'coursecategories') {
  625:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  626:         } elsif ($action eq 'login') {
  627:             if ($numheaders == 3) {
  628:                 $colspan = ' colspan="2"';
  629:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  630:             } else {
  631:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  632:             }
  633:         } elsif ($action eq 'requestcourses') {
  634:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  635:         } elsif ($action eq 'requestauthor') {
  636:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  637:         } elsif ($action eq 'usersessions') {
  638:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  639:         } elsif ($action eq 'rolecolors') {
  640:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  641:         } elsif ($action eq 'coursedefaults') {
  642:             $output .= &print_coursedefaults('top',$dom,$settings,\$rowtotal);
  643:         }
  644:         $output .= '
  645:            </table>
  646:           </td>
  647:          </tr>
  648:          <tr>
  649:            <td>
  650:             <table class="LC_nested">
  651:              <tr class="LC_info_row">
  652:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  653:         $output .= '
  654:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  655:              </tr>';
  656:             $rowtotal ++;
  657:         if ($action eq 'autoupdate') {
  658:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  659:            </table>
  660:           </td>
  661:          </tr>
  662:          <tr>
  663:            <td>
  664:             <table class="LC_nested">
  665:              <tr class="LC_info_row">
  666:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  667:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  668:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  669:             $rowtotal ++;
  670:         } elsif ($action eq 'usercreation') {
  671:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  672:            </table>
  673:           </td>
  674:          </tr>
  675:          <tr>
  676:            <td>
  677:             <table class="LC_nested">
  678:              <tr class="LC_info_row">
  679:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  680:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  681:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  682:             $rowtotal ++;
  683:         } elsif ($action eq 'selfcreation') {
  684:             $output .= &print_selfcreation('middle',$dom,$settings,\$rowtotal).'
  685:            </table>
  686:           </td>
  687:          </tr>
  688:          <tr>
  689:            <td>
  690:             <table class="LC_nested">
  691:              <tr class="LC_info_row">
  692:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  693:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  694:              </tr>'.
  695:             &print_selfcreation('bottom',$dom,$settings,\$rowtotal);
  696:             $rowtotal ++;
  697:         } elsif ($action eq 'usermodification') {
  698:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal);
  699:         } elsif ($action eq 'coursecategories') {
  700:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  701:         } elsif ($action eq 'login') {
  702:             if ($numheaders == 3) {
  703:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  704:            </table>
  705:           </td>
  706:          </tr>
  707:          <tr>
  708:            <td>
  709:             <table class="LC_nested">
  710:              <tr class="LC_info_row">
  711:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  712:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  713:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  714:                 $rowtotal ++;
  715:             } else {
  716:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  717:             }
  718:         } elsif ($action eq 'requestcourses') {
  719:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal).
  720:                        &print_studentcode($settings,\$rowtotal).'
  721:            </table>
  722:           </td>
  723:          </tr>
  724:          <tr>
  725:            <td>
  726:             <table class="LC_nested">
  727:              <tr class="LC_info_row">
  728:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  729:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  730:                        &print_textbookcourses($dom,$settings,\$rowtotal);
  731:         } elsif ($action eq 'requestauthor') {
  732:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  733:         } elsif ($action eq 'usersessions') {
  734:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  735:            </table>
  736:           </td>
  737:          </tr>
  738:          <tr>
  739:            <td>
  740:             <table class="LC_nested">
  741:              <tr class="LC_info_row">
  742:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  743:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  744:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  745:             $rowtotal ++;
  746:         } elsif ($action eq 'coursedefaults') {
  747:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  748:         } elsif ($action eq 'rolecolors') {
  749:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  750:            </table>
  751:           </td>
  752:          </tr>
  753:          <tr>
  754:            <td>
  755:             <table class="LC_nested">
  756:              <tr class="LC_info_row">
  757:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  758:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  759:               <td class="LC_right_item" valign="top">'.
  760:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  761:              </tr>'.
  762:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  763:            </table>
  764:           </td>
  765:          </tr>
  766:          <tr>
  767:            <td>
  768:             <table class="LC_nested">
  769:              <tr class="LC_info_row">
  770:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  771:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  772:              </tr>'.
  773:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  774:             $rowtotal += 2;
  775:         }
  776:     } else {
  777:         $output .= '
  778:           <tr>
  779:            <td>
  780:             <table class="LC_nested">
  781:              <tr class="LC_info_row">';
  782:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  783:             $output .= '  
  784:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  785:         } elsif ($action eq 'serverstatuses') {
  786:             $output .= '
  787:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  788:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  789: 
  790:         } else {
  791:             $output .= '
  792:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  793:         }
  794:         if (defined($item->{'header'}->[0]->{'col3'})) {
  795:             $output .= '<td class="LC_left_item" valign="top">'.
  796:                        &mt($item->{'header'}->[0]->{'col2'});
  797:             if ($action eq 'serverstatuses') {
  798:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  799:             } 
  800:         } else {
  801:             $output .= '<td class="LC_right_item" valign="top">'.
  802:                        &mt($item->{'header'}->[0]->{'col2'});
  803:         }
  804:         $output .= '</td>';
  805:         if ($item->{'header'}->[0]->{'col3'}) {
  806:             if (defined($item->{'header'}->[0]->{'col4'})) {
  807:                 $output .= '<td class="LC_left_item" valign="top">'.
  808:                             &mt($item->{'header'}->[0]->{'col3'});
  809:             } else {
  810:                 $output .= '<td class="LC_right_item" valign="top">'.
  811:                            &mt($item->{'header'}->[0]->{'col3'});
  812:             }
  813:             if ($action eq 'serverstatuses') {
  814:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  815:             }
  816:             $output .= '</td>';
  817:         }
  818:         if ($item->{'header'}->[0]->{'col4'}) {
  819:             $output .= '<td class="LC_right_item" valign="top">'.
  820:                        &mt($item->{'header'}->[0]->{'col4'});
  821:         }
  822:         $output .= '</tr>';
  823:         $rowtotal ++;
  824:         if ($action eq 'quotas') {
  825:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  826:         } elsif ($action eq 'autoenroll') {
  827:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  828:         } elsif ($action eq 'autocreate') {
  829:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  830:         } elsif ($action eq 'directorysrch') {
  831:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  832:         } elsif ($action eq 'contacts') {
  833:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  834:         } elsif ($action eq 'defaults') {
  835:             $output .= &print_defaults($dom,$settings,\$rowtotal);
  836:         } elsif ($action eq 'scantron') {
  837:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  838:         } elsif ($action eq 'serverstatuses') {
  839:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  840:         } elsif ($action eq 'helpsettings') {
  841:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  842:         } elsif ($action eq 'loadbalancing') {
  843:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  844:         }
  845:     }
  846:     $output .= '
  847:    </table>
  848:   </td>
  849:  </tr>
  850: </table><br />';
  851:     return ($output,$rowtotal);
  852: }
  853: 
  854: sub print_login {
  855:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  856:     my ($css_class,$datatable);
  857:     my %choices = &login_choices();
  858: 
  859:     if ($caller eq 'service') {
  860:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  861:         my $choice = $choices{'disallowlogin'};
  862:         $css_class = ' class="LC_odd_row"';
  863:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  864:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  865:                       '<th>'.$choices{'server'}.'</th>'.
  866:                       '<th>'.$choices{'serverpath'}.'</th>'.
  867:                       '<th>'.$choices{'custompath'}.'</th>'.
  868:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  869:         my %disallowed;
  870:         if (ref($settings) eq 'HASH') {
  871:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  872:                %disallowed = %{$settings->{'loginvia'}};
  873:             }
  874:         }
  875:         foreach my $lonhost (sort(keys(%servers))) {
  876:             my $direct = 'selected="selected"';
  877:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  878:                 if ($disallowed{$lonhost}{'server'} ne '') {
  879:                     $direct = '';
  880:                 }
  881:             }
  882:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  883:                           '<td><select name="'.$lonhost.'_server">'.
  884:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  885:                           '</option>';
  886:             foreach my $hostid (sort(keys(%servers))) {
  887:                 next if ($servers{$hostid} eq $servers{$lonhost});
  888:                 my $selected = '';
  889:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  890:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  891:                         $selected = 'selected="selected"';
  892:                     }
  893:                 }
  894:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  895:                               $servers{$hostid}.'</option>';
  896:             }
  897:             $datatable .= '</select></td>'.
  898:                           '<td><select name="'.$lonhost.'_serverpath">';
  899:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  900:                 my $pathname = $path;
  901:                 if ($path eq 'custom') {
  902:                     $pathname = &mt('Custom Path').' ->';
  903:                 }
  904:                 my $selected = '';
  905:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  906:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  907:                         $selected = 'selected="selected"';
  908:                     }
  909:                 } elsif ($path eq '') {
  910:                     $selected = 'selected="selected"';
  911:                 }
  912:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  913:             }
  914:             $datatable .= '</select></td>';
  915:             my ($custom,$exempt);
  916:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  917:                 $custom = $disallowed{$lonhost}{'custompath'};
  918:                 $exempt = $disallowed{$lonhost}{'exempt'};
  919:             }
  920:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  921:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  922:                           '</tr>';
  923:         }
  924:         $datatable .= '</table></td></tr>';
  925:         return $datatable;
  926:     } elsif ($caller eq 'page') {
  927:         my %defaultchecked = ( 
  928:                                'coursecatalog' => 'on',
  929:                                'helpdesk'      => 'on',
  930:                                'adminmail'     => 'off',
  931:                                'newuser'       => 'off',
  932:                              );
  933:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  934:         my (%checkedon,%checkedoff);
  935:         foreach my $item (@toggles) {
  936:             if ($defaultchecked{$item} eq 'on') { 
  937:                 $checkedon{$item} = ' checked="checked" ';
  938:                 $checkedoff{$item} = ' ';
  939:             } elsif ($defaultchecked{$item} eq 'off') {
  940:                 $checkedoff{$item} = ' checked="checked" ';
  941:                 $checkedon{$item} = ' ';
  942:             }
  943:         }
  944:         my @images = ('img','logo','domlogo','login');
  945:         my @logintext = ('textcol','bgcol');
  946:         my @bgs = ('pgbg','mainbg','sidebg');
  947:         my @links = ('link','alink','vlink');
  948:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  949:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  950:         my (%is_custom,%designs);
  951:         my %defaults = (
  952:                        font => $defaultdesign{'login.font'},
  953:                        );
  954:         foreach my $item (@images) {
  955:             $defaults{$item} = $defaultdesign{'login.'.$item};
  956:             $defaults{'showlogo'}{$item} = 1;
  957:         }
  958:         foreach my $item (@bgs) {
  959:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  960:         }
  961:         foreach my $item (@logintext) {
  962:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  963:         }
  964:         foreach my $item (@links) {
  965:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  966:         }
  967:         if (ref($settings) eq 'HASH') {
  968:             foreach my $item (@toggles) {
  969:                 if ($settings->{$item} eq '1') {
  970:                     $checkedon{$item} =  ' checked="checked" ';
  971:                     $checkedoff{$item} = ' ';
  972:                 } elsif ($settings->{$item} eq '0') {
  973:                     $checkedoff{$item} =  ' checked="checked" ';
  974:                     $checkedon{$item} = ' ';
  975:                 }
  976:             }
  977:             foreach my $item (@images) {
  978:                 if (defined($settings->{$item})) {
  979:                     $designs{$item} = $settings->{$item};
  980:                     $is_custom{$item} = 1;
  981:                 }
  982:                 if (defined($settings->{'showlogo'}{$item})) {
  983:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  984:                 }
  985:             }
  986:             foreach my $item (@logintext) {
  987:                 if ($settings->{$item} ne '') {
  988:                     $designs{'logintext'}{$item} = $settings->{$item};
  989:                     $is_custom{$item} = 1;
  990:                 }
  991:             }
  992:             if ($settings->{'font'} ne '') {
  993:                 $designs{'font'} = $settings->{'font'};
  994:                 $is_custom{'font'} = 1;
  995:             }
  996:             foreach my $item (@bgs) {
  997:                 if ($settings->{$item} ne '') {
  998:                     $designs{'bgs'}{$item} = $settings->{$item};
  999:                     $is_custom{$item} = 1;
 1000:                 }
 1001:             }
 1002:             foreach my $item (@links) {
 1003:                 if ($settings->{$item} ne '') {
 1004:                     $designs{'links'}{$item} = $settings->{$item};
 1005:                     $is_custom{$item} = 1;
 1006:                 }
 1007:             }
 1008:         } else {
 1009:             if ($designhash{$dom.'.login.font'} ne '') {
 1010:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1011:                 $is_custom{'font'} = 1;
 1012:             }
 1013:             foreach my $item (@images) {
 1014:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1015:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1016:                     $is_custom{$item} = 1;
 1017:                 }
 1018:             }
 1019:             foreach my $item (@bgs) {
 1020:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1021:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1022:                     $is_custom{$item} = 1;
 1023:                 }
 1024:             }
 1025:             foreach my $item (@links) {
 1026:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1027:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1028:                     $is_custom{$item} = 1;
 1029:                 }
 1030:             }
 1031:         }
 1032:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1033:                                                       logo => 'Institution Logo',
 1034:                                                       domlogo => 'Domain Logo',
 1035:                                                       login => 'Login box');
 1036:         my $itemcount = 1;
 1037:         foreach my $item (@toggles) {
 1038:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1039:             $datatable .=  
 1040:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1041:                 '</td><td>'.
 1042:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1043:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1044:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1045:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1046:                 '</tr>';
 1047:             $itemcount ++;
 1048:         }
 1049:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1050:         $datatable .= '</tr></table></td></tr>';
 1051:     } elsif ($caller eq 'help') {
 1052:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1053:         my $switchserver = &check_switchserver($dom,$confname);
 1054:         my $itemcount = 1;
 1055:         $defaulturl = '/adm/loginproblems.html';
 1056:         $defaulttype = 'default';
 1057:         %lt = &Apache::lonlocal::texthash (
 1058:                      del     => 'Delete?',
 1059:                      rep     => 'Replace:',
 1060:                      upl     => 'Upload:',
 1061:                      default => 'Default',
 1062:                      custom  => 'Custom',
 1063:                                              );
 1064:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1065:         my @currlangs;
 1066:         if (ref($settings) eq 'HASH') {
 1067:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1068:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1069:                     next if ($settings->{'helpurl'}{$key} eq '');
 1070:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1071:                     $type{$key} = 'custom';
 1072:                     unless ($key eq 'nolang') {
 1073:                         push(@currlangs,$key);
 1074:                     }
 1075:                 }
 1076:             } elsif ($settings->{'helpurl'} ne '') {
 1077:                 $type{'nolang'} = 'custom';
 1078:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1079:             }
 1080:         }
 1081:         foreach my $lang ('nolang',sort(@currlangs)) {
 1082:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1083:             $datatable .= '<tr'.$css_class.'>';
 1084:             if ($url{$lang} eq '') {
 1085:                 $url{$lang} = $defaulturl;
 1086:             }
 1087:             if ($type{$lang} eq '') {
 1088:                 $type{$lang} = $defaulttype;
 1089:             }
 1090:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1091:             if ($lang eq 'nolang') {
 1092:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1093:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1094:             } else {
 1095:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1096:                                   $langchoices{$lang},
 1097:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1098:             }
 1099:             $datatable .= '</span></td>'."\n".
 1100:                           '<td class="LC_left_item">';
 1101:             if ($type{$lang} eq 'custom') {
 1102:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1103:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1104:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1105:             } else {
 1106:                 $datatable .= $lt{'upl'};
 1107:             }
 1108:             $datatable .='<br />';
 1109:             if ($switchserver) {
 1110:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1111:             } else {
 1112:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1113:             }
 1114:             $datatable .= '</td></tr>';
 1115:             $itemcount ++;
 1116:         }
 1117:         my @addlangs;
 1118:         foreach my $lang (sort(keys(%langchoices))) {
 1119:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1120:             push(@addlangs,$lang);
 1121:         }
 1122:         if (@addlangs > 0) {
 1123:             my %toadd;
 1124:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1125:             $toadd{''} = &mt('Select');
 1126:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1127:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1128:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1129:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1130:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1131:             if ($switchserver) {
 1132:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1133:             } else {
 1134:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1135:             }
 1136:             $datatable .= '</td></tr>';
 1137:             $itemcount ++;
 1138:         }
 1139:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1140:     }
 1141:     return $datatable;
 1142: }
 1143: 
 1144: sub login_choices {
 1145:     my %choices =
 1146:         &Apache::lonlocal::texthash (
 1147:             coursecatalog => 'Display Course/Community Catalog link?',
 1148:             adminmail     => "Display Administrator's E-mail Address?",
 1149:             helpdesk      => 'Display "Contact Helpdesk" link',
 1150:             disallowlogin => "Login page requests redirected",
 1151:             hostid        => "Server",
 1152:             server        => "Redirect to:",
 1153:             serverpath    => "Path",
 1154:             custompath    => "Custom", 
 1155:             exempt        => "Exempt IP(s)",
 1156:             directlogin   => "No redirect",
 1157:             newuser       => "Link to create a user account",
 1158:             img           => "Header",
 1159:             logo          => "Main Logo",
 1160:             domlogo       => "Domain Logo",
 1161:             login         => "Log-in Header", 
 1162:             textcol       => "Text color",
 1163:             bgcol         => "Box color",
 1164:             bgs           => "Background colors",
 1165:             links         => "Link colors",
 1166:             font          => "Font color",
 1167:             pgbg          => "Header",
 1168:             mainbg        => "Page",
 1169:             sidebg        => "Login box",
 1170:             link          => "Link",
 1171:             alink         => "Active link",
 1172:             vlink         => "Visited link",
 1173:         );
 1174:     return %choices;
 1175: }
 1176: 
 1177: sub print_rolecolors {
 1178:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1179:     my %choices = &color_font_choices();
 1180:     my @bgs = ('pgbg','tabbg','sidebg');
 1181:     my @links = ('link','alink','vlink');
 1182:     my @images = ('img');
 1183:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1184:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1185:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1186:     my (%is_custom,%designs);
 1187:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1188:     if (ref($settings) eq 'HASH') {
 1189:         if (ref($settings->{$role}) eq 'HASH') {
 1190:             if ($settings->{$role}->{'img'} ne '') {
 1191:                 $designs{'img'} = $settings->{$role}->{'img'};
 1192:                 $is_custom{'img'} = 1;
 1193:             }
 1194:             if ($settings->{$role}->{'font'} ne '') {
 1195:                 $designs{'font'} = $settings->{$role}->{'font'};
 1196:                 $is_custom{'font'} = 1;
 1197:             }
 1198:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1199:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1200:                 $is_custom{'fontmenu'} = 1;
 1201:             }
 1202:             foreach my $item (@bgs) {
 1203:                 if ($settings->{$role}->{$item} ne '') {
 1204:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1205:                     $is_custom{$item} = 1;
 1206:                 }
 1207:             }
 1208:             foreach my $item (@links) {
 1209:                 if ($settings->{$role}->{$item} ne '') {
 1210:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1211:                     $is_custom{$item} = 1;
 1212:                 }
 1213:             }
 1214:         }
 1215:     } else {
 1216:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1217:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1218:             $is_custom{'img'} = 1;
 1219:         }
 1220:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1221:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1222:             $is_custom{'fontmenu'} = 1; 
 1223:         }
 1224:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1225:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1226:             $is_custom{'font'} = 1;
 1227:         }
 1228:         foreach my $item (@bgs) {
 1229:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1230:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1231:                 $is_custom{$item} = 1;
 1232:             
 1233:             }
 1234:         }
 1235:         foreach my $item (@links) {
 1236:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1237:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1238:                 $is_custom{$item} = 1;
 1239:             }
 1240:         }
 1241:     }
 1242:     my $itemcount = 1;
 1243:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1244:     $datatable .= '</tr></table></td></tr>';
 1245:     return $datatable;
 1246: }
 1247: 
 1248: sub role_defaults {
 1249:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1250:     my %defaults;
 1251:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1252:         return %defaults;
 1253:     }
 1254:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1255:     if ($role eq 'login') {
 1256:         %defaults = (
 1257:                        font => $defaultdesign{$role.'.font'},
 1258:                     );
 1259:         if (ref($logintext) eq 'ARRAY') {
 1260:             foreach my $item (@{$logintext}) {
 1261:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1262:             }
 1263:         }
 1264:         foreach my $item (@{$images}) {
 1265:             $defaults{'showlogo'}{$item} = 1;
 1266:         }
 1267:     } else {
 1268:         %defaults = (
 1269:                        img => $defaultdesign{$role.'.img'},
 1270:                        font => $defaultdesign{$role.'.font'},
 1271:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1272:                     );
 1273:     }
 1274:     foreach my $item (@{$bgs}) {
 1275:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1276:     }
 1277:     foreach my $item (@{$links}) {
 1278:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1279:     }
 1280:     foreach my $item (@{$images}) {
 1281:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1282:     }
 1283:     return %defaults;
 1284: }
 1285: 
 1286: sub display_color_options {
 1287:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1288:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1289:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1290:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1291:     my $datatable = '<tr'.$css_class.'>'.
 1292:         '<td>'.$choices->{'font'}.'</td>';
 1293:     if (!$is_custom->{'font'}) {
 1294:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1295:     } else {
 1296:         $datatable .= '<td>&nbsp;</td>';
 1297:     }
 1298:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1299: 
 1300:     $datatable .= '<td><span class="LC_nobreak">'.
 1301:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1302:                   ' value="'.$current_color.'" />&nbsp;'.
 1303:                   '&nbsp;</td></tr>';
 1304:     unless ($role eq 'login') { 
 1305:         $datatable .= '<tr'.$css_class.'>'.
 1306:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1307:         if (!$is_custom->{'fontmenu'}) {
 1308:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1309:         } else {
 1310:             $datatable .= '<td>&nbsp;</td>';
 1311:         }
 1312: 	$current_color = $designs->{'fontmenu'} ?
 1313: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1314:         $datatable .= '<td><span class="LC_nobreak">'.
 1315:                       '<input class="colorchooser" type="text" size="10" name="'
 1316: 		      .$role.'_fontmenu"'.
 1317:                       ' value="'.$current_color.'" />&nbsp;'.
 1318:                       '&nbsp;</td></tr>';
 1319:     }
 1320:     my $switchserver = &check_switchserver($dom,$confname);
 1321:     foreach my $img (@{$images}) {
 1322: 	$itemcount ++;
 1323:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1324:         $datatable .= '<tr'.$css_class.'>'.
 1325:                       '<td>'.$choices->{$img};
 1326:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1327:         if ($role eq 'login') {
 1328:             if ($img eq 'login') {
 1329:                 $login_hdr_pick =
 1330:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1331:                 $logincolors =
 1332:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1333:                                        $designs,$defaults);
 1334:             } elsif ($img ne 'domlogo') {
 1335:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1336:             }
 1337:         }
 1338:         $datatable .= '</td>';
 1339:         if ($designs->{$img} ne '') {
 1340:             $imgfile = $designs->{$img};
 1341: 	    $img_import = ($imgfile =~ m{^/adm/});
 1342:         } else {
 1343:             $imgfile = $defaults->{$img};
 1344:         }
 1345:         if ($imgfile) {
 1346:             my ($showfile,$fullsize);
 1347:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1348:                 my $urldir = $1;
 1349:                 my $filename = $2;
 1350:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1351:                 if (@info) {
 1352:                     my $thumbfile = 'tn-'.$filename;
 1353:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1354:                     if (@thumb) {
 1355:                         $showfile = $urldir.'/'.$thumbfile;
 1356:                     } else {
 1357:                         $showfile = $imgfile;
 1358:                     }
 1359:                 } else {
 1360:                     $showfile = '';
 1361:                 }
 1362:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1363:                 $showfile = $imgfile;
 1364:                 my $imgdir = $1;
 1365:                 my $filename = $2;
 1366:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1367:                     $showfile = "/$imgdir/tn-".$filename;
 1368:                 } else {
 1369:                     my $input = $londocroot.$imgfile;
 1370:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1371:                     if (!-e $output) {
 1372:                         my ($width,$height) = &thumb_dimensions();
 1373:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1374:                         if ($fullwidth ne '' && $fullheight ne '') {
 1375:                             if ($fullwidth > $width && $fullheight > $height) { 
 1376:                                 my $size = $width.'x'.$height;
 1377:                                 system("convert -sample $size $input $output");
 1378:                                 $showfile = "/$imgdir/tn-".$filename;
 1379:                             }
 1380:                         }
 1381:                     }
 1382:                 }
 1383:             }
 1384:             if ($showfile) {
 1385:                 if ($showfile =~ m{^/(adm|res)/}) {
 1386:                     if ($showfile =~ m{^/res/}) {
 1387:                         my $local_showfile =
 1388:                             &Apache::lonnet::filelocation('',$showfile);
 1389:                         &Apache::lonnet::repcopy($local_showfile);
 1390:                     }
 1391:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1392:                 }
 1393:                 if ($imgfile) {
 1394:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1395:                         if ($imgfile =~ m{^/res/}) {
 1396:                             my $local_imgfile =
 1397:                                 &Apache::lonnet::filelocation('',$imgfile);
 1398:                             &Apache::lonnet::repcopy($local_imgfile);
 1399:                         }
 1400:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1401:                     } else {
 1402:                         $fullsize = $imgfile;
 1403:                     }
 1404:                 }
 1405:                 $datatable .= '<td>';
 1406:                 if ($img eq 'login') {
 1407:                     $datatable .= $login_hdr_pick;
 1408:                 } 
 1409:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1410:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1411:             } else {
 1412:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1413:                               &mt('Upload:').'<br />';
 1414:             }
 1415:         } else {
 1416:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1417:                           &mt('Upload:').'<br />';
 1418:         }
 1419:         if ($switchserver) {
 1420:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1421:         } else {
 1422:             if ($img ne 'login') { # suppress file selection for Log-in header
 1423:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1424:             }
 1425:         }
 1426:         $datatable .= '</td></tr>';
 1427:     }
 1428:     $itemcount ++;
 1429:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1430:     $datatable .= '<tr'.$css_class.'>'.
 1431:                   '<td>'.$choices->{'bgs'}.'</td>';
 1432:     my $bgs_def;
 1433:     foreach my $item (@{$bgs}) {
 1434:         if (!$is_custom->{$item}) {
 1435:             $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>';
 1436:         }
 1437:     }
 1438:     if ($bgs_def) {
 1439:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1440:     } else {
 1441:         $datatable .= '<td>&nbsp;</td>';
 1442:     }
 1443:     $datatable .= '<td class="LC_right_item">'.
 1444:                   '<table border="0"><tr>';
 1445: 
 1446:     foreach my $item (@{$bgs}) {
 1447:         $datatable .= '<td align="center">'.$choices->{$item};
 1448: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1449:         if ($designs->{'bgs'}{$item}) {
 1450:             $datatable .= '&nbsp;';
 1451:         }
 1452:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1453:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1454:     }
 1455:     $datatable .= '</tr></table></td></tr>';
 1456:     $itemcount ++;
 1457:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1458:     $datatable .= '<tr'.$css_class.'>'.
 1459:                   '<td>'.$choices->{'links'}.'</td>';
 1460:     my $links_def;
 1461:     foreach my $item (@{$links}) {
 1462:         if (!$is_custom->{$item}) {
 1463:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1464:         }
 1465:     }
 1466:     if ($links_def) {
 1467:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1468:     } else {
 1469:         $datatable .= '<td>&nbsp;</td>';
 1470:     }
 1471:     $datatable .= '<td class="LC_right_item">'.
 1472:                   '<table border="0"><tr>';
 1473:     foreach my $item (@{$links}) {
 1474: 	my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1475:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1476:         if ($designs->{'links'}{$item}) {
 1477:             $datatable.='&nbsp;';
 1478:         }
 1479:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1480:                       '" /></td>';
 1481:     }
 1482:     $$rowtotal += $itemcount;
 1483:     return $datatable;
 1484: }
 1485: 
 1486: sub logo_display_options {
 1487:     my ($img,$defaults,$designs) = @_;
 1488:     my $checkedon;
 1489:     if (ref($defaults) eq 'HASH') {
 1490:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1491:             if ($defaults->{'showlogo'}{$img}) {
 1492:                 $checkedon = 'checked="checked" ';     
 1493:             }
 1494:         } 
 1495:     }
 1496:     if (ref($designs) eq 'HASH') {
 1497:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1498:             if (defined($designs->{'showlogo'}{$img})) {
 1499:                 if ($designs->{'showlogo'}{$img} == 0) {
 1500:                     $checkedon = '';
 1501:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1502:                     $checkedon = 'checked="checked" ';
 1503:                 }
 1504:             }
 1505:         }
 1506:     }
 1507:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1508:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1509:            &mt('show').'</label>'."\n";
 1510: }
 1511: 
 1512: sub login_header_options  {
 1513:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1514:     my $output = '';
 1515:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1516:         $output .= &mt('Text default(s):').'<br />';
 1517:         if (!$is_custom->{'textcol'}) {
 1518:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1519:                        '&nbsp;&nbsp;&nbsp;';
 1520:         }
 1521:         if (!$is_custom->{'bgcol'}) {
 1522:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1523:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1524:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1525:         }
 1526:         $output .= '<br />';
 1527:     }
 1528:     $output .='<br />';
 1529:     return $output;
 1530: }
 1531: 
 1532: sub login_text_colors {
 1533:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1534:     my $color_menu = '<table border="0"><tr>';
 1535:     foreach my $item (@{$logintext}) {
 1536:         $color_menu .= '<td align="center">'.$choices->{$item};
 1537:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1538:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1539:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1540:     }
 1541:     $color_menu .= '</tr></table><br />';
 1542:     return $color_menu;
 1543: }
 1544: 
 1545: sub image_changes {
 1546:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1547:     my $output;
 1548:     if ($img eq 'login') {
 1549:             # suppress image for Log-in header
 1550:     } elsif (!$is_custom) {
 1551:         if ($img ne 'domlogo') {
 1552:             $output .= &mt('Default image:').'<br />';
 1553:         } else {
 1554:             $output .= &mt('Default in use:').'<br />';
 1555:         }
 1556:     }
 1557:     if ($img eq 'login') { # suppress image for Log-in header
 1558:         $output .= '<td>'.$logincolors;
 1559:     } else {
 1560:         if ($img_import) {
 1561:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1562:         }
 1563:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1564:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1565:         if ($is_custom) {
 1566:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1567:                        '<input type="checkbox" name="'.
 1568:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1569:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1570:         } else {
 1571:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1572:         }
 1573:     }
 1574:     return $output;
 1575: }
 1576: 
 1577: sub print_quotas {
 1578:     my ($dom,$settings,$rowtotal,$action) = @_;
 1579:     my $context;
 1580:     if ($action eq 'quotas') {
 1581:         $context = 'tools';
 1582:     } else {
 1583:         $context = $action;
 1584:     }
 1585:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1586:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1587:     my $typecount = 0;
 1588:     my ($css_class,%titles);
 1589:     if ($context eq 'requestcourses') {
 1590:         @usertools = ('official','unofficial','community','textbook');
 1591:         @options =('norequest','approval','validate','autolimit');
 1592:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1593:         %titles = &courserequest_titles();
 1594:     } elsif ($context eq 'requestauthor') {
 1595:         @usertools = ('author');
 1596:         @options = ('norequest','approval','automatic');
 1597:         %titles = &authorrequest_titles();
 1598:     } else {
 1599:         @usertools = ('aboutme','blog','webdav','portfolio');
 1600:         %titles = &tool_titles();
 1601:     }
 1602:     if (ref($types) eq 'ARRAY') {
 1603:         foreach my $type (@{$types}) {
 1604:             my ($currdefquota,$currauthorquota);
 1605:             unless (($context eq 'requestcourses') ||
 1606:                     ($context eq 'requestauthor')) {
 1607:                 if (ref($settings) eq 'HASH') {
 1608:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1609:                         $currdefquota = $settings->{defaultquota}->{$type};
 1610:                     } else {
 1611:                         $currdefquota = $settings->{$type};
 1612:                     }
 1613:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1614:                         $currauthorquota = $settings->{authorquota}->{$type};
 1615:                     }
 1616:                 }
 1617:             }
 1618:             if (defined($usertypes->{$type})) {
 1619:                 $typecount ++;
 1620:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1621:                 $datatable .= '<tr'.$css_class.'>'.
 1622:                               '<td>'.$usertypes->{$type}.'</td>'.
 1623:                               '<td class="LC_left_item">';
 1624:                 if ($context eq 'requestcourses') {
 1625:                     $datatable .= '<table><tr>';
 1626:                 }
 1627:                 my %cell;  
 1628:                 foreach my $item (@usertools) {
 1629:                     if ($context eq 'requestcourses') {
 1630:                         my ($curroption,$currlimit);
 1631:                         if (ref($settings) eq 'HASH') {
 1632:                             if (ref($settings->{$item}) eq 'HASH') {
 1633:                                 $curroption = $settings->{$item}->{$type};
 1634:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1635:                                     $currlimit = $1; 
 1636:                                 }
 1637:                             }
 1638:                         }
 1639:                         if (!$curroption) {
 1640:                             $curroption = 'norequest';
 1641:                         }
 1642:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1643:                         foreach my $option (@options) {
 1644:                             my $val = $option;
 1645:                             if ($option eq 'norequest') {
 1646:                                 $val = 0;  
 1647:                             }
 1648:                             if ($option eq 'validate') {
 1649:                                 my $canvalidate = 0;
 1650:                                 if (ref($validations{$item}) eq 'HASH') { 
 1651:                                     if ($validations{$item}{$type}) {
 1652:                                         $canvalidate = 1;
 1653:                                     }
 1654:                                 }
 1655:                                 next if (!$canvalidate);
 1656:                             }
 1657:                             my $checked = '';
 1658:                             if ($option eq $curroption) {
 1659:                                 $checked = ' checked="checked"';
 1660:                             } elsif ($option eq 'autolimit') {
 1661:                                 if ($curroption =~ /^autolimit/) {
 1662:                                     $checked = ' checked="checked"';
 1663:                                 }                       
 1664:                             } 
 1665:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1666:                                   '<input type="radio" name="crsreq_'.$item.
 1667:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1668:                                   $titles{$option}.'</label>';
 1669:                             if ($option eq 'autolimit') {
 1670:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1671:                                                 $item.'_limit_'.$type.'" size="1" '.
 1672:                                                 'value="'.$currlimit.'" />';
 1673:                             }
 1674:                             $cell{$item} .= '</span> ';
 1675:                             if ($option eq 'autolimit') {
 1676:                                 $cell{$item} .= $titles{'unlimited'};
 1677:                             }
 1678:                         }
 1679:                     } elsif ($context eq 'requestauthor') {
 1680:                         my $curroption;
 1681:                         if (ref($settings) eq 'HASH') {
 1682:                             $curroption = $settings->{$type};
 1683:                         }
 1684:                         if (!$curroption) {
 1685:                             $curroption = 'norequest';
 1686:                         }
 1687:                         foreach my $option (@options) {
 1688:                             my $val = $option;
 1689:                             if ($option eq 'norequest') {
 1690:                                 $val = 0;
 1691:                             }
 1692:                             my $checked = '';
 1693:                             if ($option eq $curroption) {
 1694:                                 $checked = ' checked="checked"';
 1695:                             }
 1696:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1697:                                   '<input type="radio" name="authorreq_'.$type.
 1698:                                   '" value="'.$val.'"'.$checked.' />'.
 1699:                                   $titles{$option}.'</label></span>&nbsp; ';
 1700:                         }
 1701:                     } else {
 1702:                         my $checked = 'checked="checked" ';
 1703:                         if (ref($settings) eq 'HASH') {
 1704:                             if (ref($settings->{$item}) eq 'HASH') {
 1705:                                 if ($settings->{$item}->{$type} == 0) {
 1706:                                     $checked = '';
 1707:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1708:                                     $checked =  'checked="checked" ';
 1709:                                 }
 1710:                             }
 1711:                         }
 1712:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1713:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1714:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1715:                                       '</label></span>&nbsp; ';
 1716:                     }
 1717:                 }
 1718:                 if ($context eq 'requestcourses') {
 1719:                     $datatable .= '</tr><tr>';
 1720:                     foreach my $item (@usertools) {
 1721:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1722:                     }
 1723:                     $datatable .= '</tr></table>';
 1724:                 }
 1725:                 $datatable .= '</td>';
 1726:                 unless (($context eq 'requestcourses') ||
 1727:                         ($context eq 'requestauthor')) {
 1728:                     $datatable .= 
 1729:                               '<td class="LC_right_item">'.
 1730:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1731:                               '<input type="text" name="quota_'.$type.
 1732:                               '" value="'.$currdefquota.
 1733:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1734:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1735:                               '<input type="text" name="authorquota_'.$type.
 1736:                               '" value="'.$currauthorquota.
 1737:                               '" size="5" /></span></td>';
 1738:                 }
 1739:                 $datatable .= '</tr>';
 1740:             }
 1741:         }
 1742:     }
 1743:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1744:         $defaultquota = '20';
 1745:         $authorquota = '500';
 1746:         if (ref($settings) eq 'HASH') {
 1747:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1748:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1749:             } elsif (defined($settings->{'default'})) {
 1750:                 $defaultquota = $settings->{'default'};
 1751:             }
 1752:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1753:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1754:             }
 1755:         }
 1756:     }
 1757:     $typecount ++;
 1758:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1759:     $datatable .= '<tr'.$css_class.'>'.
 1760:                   '<td>'.$othertitle.'</td>'.
 1761:                   '<td class="LC_left_item">';
 1762:     if ($context eq 'requestcourses') {
 1763:         $datatable .= '<table><tr>';
 1764:     }
 1765:     my %defcell;
 1766:     foreach my $item (@usertools) {
 1767:         if ($context eq 'requestcourses') {
 1768:             my ($curroption,$currlimit);
 1769:             if (ref($settings) eq 'HASH') {
 1770:                 if (ref($settings->{$item}) eq 'HASH') {
 1771:                     $curroption = $settings->{$item}->{'default'};
 1772:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1773:                         $currlimit = $1;
 1774:                     }
 1775:                 }
 1776:             }
 1777:             if (!$curroption) {
 1778:                 $curroption = 'norequest';
 1779:             }
 1780:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1781:             foreach my $option (@options) {
 1782:                 my $val = $option;
 1783:                 if ($option eq 'norequest') {
 1784:                     $val = 0;
 1785:                 }
 1786:                 if ($option eq 'validate') {
 1787:                     my $canvalidate = 0;
 1788:                     if (ref($validations{$item}) eq 'HASH') {
 1789:                         if ($validations{$item}{'default'}) {
 1790:                             $canvalidate = 1;
 1791:                         }
 1792:                     }
 1793:                     next if (!$canvalidate);
 1794:                 }
 1795:                 my $checked = '';
 1796:                 if ($option eq $curroption) {
 1797:                     $checked = ' checked="checked"';
 1798:                 } elsif ($option eq 'autolimit') {
 1799:                     if ($curroption =~ /^autolimit/) {
 1800:                         $checked = ' checked="checked"';
 1801:                     }
 1802:                 }
 1803:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1804:                                   '<input type="radio" name="crsreq_'.$item.
 1805:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1806:                                   $titles{$option}.'</label>';
 1807:                 if ($option eq 'autolimit') {
 1808:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1809:                                        $item.'_limit_default" size="1" '.
 1810:                                        'value="'.$currlimit.'" />';
 1811:                 }
 1812:                 $defcell{$item} .= '</span> ';
 1813:                 if ($option eq 'autolimit') {
 1814:                     $defcell{$item} .= $titles{'unlimited'};
 1815:                 }
 1816:             }
 1817:         } elsif ($context eq 'requestauthor') {
 1818:             my $curroption;
 1819:             if (ref($settings) eq 'HASH') {
 1820:                 $curroption = $settings->{'default'};
 1821:             }
 1822:             if (!$curroption) {
 1823:                 $curroption = 'norequest';
 1824:             }
 1825:             foreach my $option (@options) {
 1826:                 my $val = $option;
 1827:                 if ($option eq 'norequest') {
 1828:                     $val = 0;
 1829:                 }
 1830:                 my $checked = '';
 1831:                 if ($option eq $curroption) {
 1832:                     $checked = ' checked="checked"';
 1833:                 }
 1834:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1835:                               '<input type="radio" name="authorreq_default"'.
 1836:                               ' value="'.$val.'"'.$checked.' />'.
 1837:                               $titles{$option}.'</label></span>&nbsp; ';
 1838:             }
 1839:         } else {
 1840:             my $checked = 'checked="checked" ';
 1841:             if (ref($settings) eq 'HASH') {
 1842:                 if (ref($settings->{$item}) eq 'HASH') {
 1843:                     if ($settings->{$item}->{'default'} == 0) {
 1844:                         $checked = '';
 1845:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1846:                         $checked = 'checked="checked" ';
 1847:                     }
 1848:                 }
 1849:             }
 1850:             $datatable .= '<span class="LC_nobreak"><label>'.
 1851:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1852:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1853:                           '</label></span>&nbsp; ';
 1854:         }
 1855:     }
 1856:     if ($context eq 'requestcourses') {
 1857:         $datatable .= '</tr><tr>';
 1858:         foreach my $item (@usertools) {
 1859:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1860:         }
 1861:         $datatable .= '</tr></table>';
 1862:     }
 1863:     $datatable .= '</td>';
 1864:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1865:         $datatable .= '<td class="LC_right_item">'.
 1866:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1867:                       '<input type="text" name="defaultquota" value="'.
 1868:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1869:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1870:                       '<input type="text" name="authorquota" value="'.
 1871:                       $authorquota.'" size="5" /></span></td>';
 1872:     }
 1873:     $datatable .= '</tr>';
 1874:     $typecount ++;
 1875:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1876:     $datatable .= '<tr'.$css_class.'>'.
 1877:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1878:     if ($context eq 'requestcourses') {
 1879:         $datatable .= &mt('(overrides affiliation, if set)').
 1880:                       '</td>'.
 1881:                       '<td class="LC_left_item">'.
 1882:                       '<table><tr>';
 1883:     } else {
 1884:         $datatable .= &mt('(overrides affiliation, if checked)').
 1885:                       '</td>'.
 1886:                       '<td class="LC_left_item" colspan="2">'.
 1887:                       '<br />';
 1888:     }
 1889:     my %advcell;
 1890:     foreach my $item (@usertools) {
 1891:         if ($context eq 'requestcourses') {
 1892:             my ($curroption,$currlimit);
 1893:             if (ref($settings) eq 'HASH') {
 1894:                 if (ref($settings->{$item}) eq 'HASH') {
 1895:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1896:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1897:                         $currlimit = $1;
 1898:                     }
 1899:                 }
 1900:             }
 1901:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1902:             my $checked = '';
 1903:             if ($curroption eq '') {
 1904:                 $checked = ' checked="checked"';
 1905:             }
 1906:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1907:                                '<input type="radio" name="crsreq_'.$item.
 1908:                                '__LC_adv" value=""'.$checked.' />'.
 1909:                                &mt('No override set').'</label></span>&nbsp; ';
 1910:             foreach my $option (@options) {
 1911:                 my $val = $option;
 1912:                 if ($option eq 'norequest') {
 1913:                     $val = 0;
 1914:                 }
 1915:                 if ($option eq 'validate') {
 1916:                     my $canvalidate = 0;
 1917:                     if (ref($validations{$item}) eq 'HASH') {
 1918:                         if ($validations{$item}{'_LC_adv'}) {
 1919:                             $canvalidate = 1;
 1920:                         }
 1921:                     }
 1922:                     next if (!$canvalidate);
 1923:                 }
 1924:                 my $checked = '';
 1925:                 if ($val eq $curroption) {
 1926:                     $checked = ' checked="checked"';
 1927:                 } elsif ($option eq 'autolimit') {
 1928:                     if ($curroption =~ /^autolimit/) {
 1929:                         $checked = ' checked="checked"';
 1930:                     }
 1931:                 }
 1932:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1933:                                   '<input type="radio" name="crsreq_'.$item.
 1934:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1935:                                   $titles{$option}.'</label>';
 1936:                 if ($option eq 'autolimit') {
 1937:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1938:                                        $item.'_limit__LC_adv" size="1" '.
 1939:                                        'value="'.$currlimit.'" />';
 1940:                 }
 1941:                 $advcell{$item} .= '</span> ';
 1942:                 if ($option eq 'autolimit') {
 1943:                     $advcell{$item} .= $titles{'unlimited'};
 1944:                 }
 1945:             }
 1946:         } elsif ($context eq 'requestauthor') {
 1947:             my $curroption;
 1948:             if (ref($settings) eq 'HASH') {
 1949:                 $curroption = $settings->{'_LC_adv'};
 1950:             }
 1951:             my $checked = '';
 1952:             if ($curroption eq '') {
 1953:                 $checked = ' checked="checked"';
 1954:             }
 1955:             $datatable .= '<span class="LC_nobreak"><label>'.
 1956:                           '<input type="radio" name="authorreq__LC_adv"'.
 1957:                           ' value=""'.$checked.' />'.
 1958:                           &mt('No override set').'</label></span>&nbsp; ';
 1959:             foreach my $option (@options) {
 1960:                 my $val = $option;
 1961:                 if ($option eq 'norequest') {
 1962:                     $val = 0;
 1963:                 }
 1964:                 my $checked = '';
 1965:                 if ($val eq $curroption) {
 1966:                     $checked = ' checked="checked"';
 1967:                 }
 1968:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1969:                               '<input type="radio" name="authorreq__LC_adv"'.
 1970:                               ' value="'.$val.'"'.$checked.' />'.
 1971:                               $titles{$option}.'</label></span>&nbsp; ';
 1972:             }
 1973:         } else {
 1974:             my $checked = 'checked="checked" ';
 1975:             if (ref($settings) eq 'HASH') {
 1976:                 if (ref($settings->{$item}) eq 'HASH') {
 1977:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1978:                         $checked = '';
 1979:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1980:                         $checked = 'checked="checked" ';
 1981:                     }
 1982:                 }
 1983:             }
 1984:             $datatable .= '<span class="LC_nobreak"><label>'.
 1985:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1986:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1987:                           '</label></span>&nbsp; ';
 1988:         }
 1989:     }
 1990:     if ($context eq 'requestcourses') {
 1991:         $datatable .= '</tr><tr>';
 1992:         foreach my $item (@usertools) {
 1993:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1994:         }
 1995:         $datatable .= '</tr></table>';
 1996:     }
 1997:     $datatable .= '</td></tr>';
 1998:     $$rowtotal += $typecount;
 1999:     return $datatable;
 2000: }
 2001: 
 2002: sub print_requestmail {
 2003:     my ($dom,$action,$settings,$rowtotal) = @_;
 2004:     my ($now,$datatable,%currapp);
 2005:     $now = time;
 2006:     if (ref($settings) eq 'HASH') {
 2007:         if (ref($settings->{'notify'}) eq 'HASH') {
 2008:             if ($settings->{'notify'}{'approval'} ne '') {
 2009:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2010:             }
 2011:         }
 2012:     }
 2013:     my $numinrow = 2;
 2014:     my $css_class;
 2015:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2016:     my $text;
 2017:     if ($action eq 'requestcourses') {
 2018:         $text = &mt('Receive notification of course requests requiring approval');
 2019:     } elsif ($action eq 'requestauthor') {
 2020:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2021:     } else {
 2022:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2023:     }
 2024:     $datatable = '<tr'.$css_class.'>'.
 2025:                  ' <td>'.$text.'</td>'.
 2026:                  ' <td class="LC_left_item">';
 2027:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2028:                                                  $action.'notifyapproval',%currapp);
 2029:     if ($numdc > 0) {
 2030:         $datatable .= $table;
 2031:     } else {
 2032:         $datatable .= &mt('There are no active Domain Coordinators');
 2033:     }
 2034:     $datatable .='</td></tr>';
 2035:     $$rowtotal += $rows;
 2036:     return $datatable;
 2037: }
 2038: 
 2039: sub print_studentcode {
 2040:     my ($settings,$rowtotal) = @_;
 2041:     my $rownum = 0; 
 2042:     my ($output,%current);
 2043:     my @crstypes = ('official','unofficial','community','textbook');
 2044:     if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2045:         foreach my $type (@crstypes) {
 2046:             $current{$type} = $settings->{'uniquecode'}{$type};
 2047:         }
 2048:     }
 2049:     $output .= '<tr>'.
 2050:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2051:                '<td class="LC_left_item">';
 2052:     foreach my $type (@crstypes) {
 2053:         my $check = ' ';
 2054:         if ($current{$type}) {
 2055:             $check = ' checked="checked" ';
 2056:         }
 2057:         $output .= '<span class="LC_nobreak"><label>'.
 2058:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2059:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2060:     }
 2061:     $output .= '</td></tr>';
 2062:     $$rowtotal ++;
 2063:     return $output;
 2064: }
 2065: 
 2066: sub print_textbookcourses {
 2067:     my ($dom,$settings,$rowtotal) = @_;
 2068:     my $rownum = 0;
 2069:     my $css_class;
 2070:     my $itemcount = 1;
 2071:     my $maxnum = 0;
 2072:     my $bookshash;
 2073:     if (ref($settings) eq 'HASH') {
 2074:         $bookshash = $settings->{'textbooks'};
 2075:     }
 2076:     my %ordered;
 2077:     if (ref($bookshash) eq 'HASH') {
 2078:         foreach my $item (keys(%{$bookshash})) {
 2079:             if (ref($bookshash->{$item}) eq 'HASH') {
 2080:                 my $num = $bookshash->{$item}{'order'};
 2081:                 $ordered{$num} = $item;
 2082:             }
 2083:         }
 2084:     }
 2085:     my $confname = $dom.'-domainconfig';
 2086:     my $switchserver = &check_switchserver($dom,$confname);
 2087:     my $maxnum = scalar(keys(%ordered));
 2088:     my $datatable = &textbookcourses_javascript(\%ordered);
 2089:     if (keys(%ordered)) {
 2090:         my @items = sort { $a <=> $b } keys(%ordered);
 2091:         for (my $i=0; $i<@items; $i++) {
 2092:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2093:             my $key = $ordered{$items[$i]};
 2094:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2095:             my $coursetitle = $coursehash{'description'};
 2096:             my ($subject,$title,$author,$image,$imgsrc,$cdom,$cnum);
 2097:             if (ref($bookshash->{$key}) eq 'HASH') {
 2098:                 $subject = $bookshash->{$key}->{'subject'};
 2099:                 $title = $bookshash->{$key}->{'title'};
 2100:                 $author = $bookshash->{$key}->{'author'};
 2101:                 $image = $bookshash->{$key}->{'image'};
 2102:                 if ($image ne '') {
 2103:                     my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2104:                     my $imagethumb = "$path/tn-".$imagefile;
 2105:                     $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2106:                 }
 2107:             }
 2108:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$key'".');"';
 2109:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2110:                          .'<select name="'.$key.'"'.$chgstr.'>';
 2111:             for (my $k=0; $k<=$maxnum; $k++) {
 2112:                 my $vpos = $k+1;
 2113:                 my $selstr;
 2114:                 if ($k == $i) {
 2115:                     $selstr = ' selected="selected" ';
 2116:                 }
 2117:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2118:             }
 2119:             $datatable .= '</select>'.('&nbsp;'x2).
 2120:                 '<label><input type="checkbox" name="book_del" value="'.$key.'" />'.
 2121:                 &mt('Delete?').'</label></span></td>'.
 2122:                 '<td colspan="2">'.
 2123:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="book_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2124:                 ('&nbsp;'x2).
 2125:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="book_title_'.$i.'" value="'.$title.'" /></span> '.
 2126:                 ('&nbsp;'x2).
 2127:                 '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="book_author_'.$i.'" value="'.$author.'" /></span> '.
 2128:                 ('&nbsp;'x2).
 2129:                 '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2130:             if ($image) {
 2131:                 $datatable .= '<span class="LC_nobreak">'.
 2132:                               $imgsrc.
 2133:                               '<label><input type="checkbox" name="book_image_del"'.
 2134:                               ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2135:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2136:             }
 2137:             if ($switchserver) {
 2138:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2139:             } else {
 2140:                 $datatable .= '<input type="file" name="book_image_'.$i.'" value="" />';
 2141:             }
 2142:             $datatable .= '<input type="hidden" name="book_id_'.$i.'" value="'.$key.'" /></span> '.
 2143:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2144:                           $coursetitle.'</span></td></tr>'."\n";
 2145:             $itemcount ++;
 2146:         }
 2147:     }
 2148:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2149:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'addbook_pos'".');"';
 2150:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2151:                   '<input type="hidden" name="book_maxnum" value="'.$maxnum.'" />'."\n".
 2152:                   '<select name="addbook_pos"'.$chgstr.'>';
 2153:     for (my $k=0; $k<$maxnum+1; $k++) {
 2154:         my $vpos = $k+1;
 2155:         my $selstr;
 2156:         if ($k == $maxnum) {
 2157:             $selstr = ' selected="selected" ';
 2158:         }
 2159:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2160:     }
 2161:     $datatable .= '</select>&nbsp;'."\n".
 2162:                   '<input type="checkbox" name="addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2163:                   '<td colspan="2">'.
 2164:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="addbook_subject" value="" /></span> '."\n".
 2165:                   ('&nbsp;'x2).
 2166:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="addbook_title" value="" /></span> '."\n".
 2167:                   ('&nbsp;'x2).
 2168:                   '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="addbook_author" value="" /></span> '."\n".
 2169:                   ('&nbsp;'x2).
 2170:                   '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2171:     if ($switchserver) {
 2172:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2173:     } else {
 2174:         $datatable .= '<input type="file" name="addbook_image" value="" />';
 2175:     }
 2176:     $datatable .= '</span>'."\n".
 2177:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2178:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},'addbook_cdom').
 2179:                   '<input type="text" size="25" name="addbook_cnum" value="" />'.
 2180:                   &Apache::loncommon::selectcourse_link
 2181:                       ('display','addbook_cnum','addbook_cdom',undef,undef,undef,'Course');
 2182:                   '</span></td>'."\n".
 2183:                   '</tr>'."\n";
 2184:     $itemcount ++;
 2185:     return $datatable;
 2186: }
 2187: 
 2188: sub textbookcourses_javascript {
 2189:     my ($textbooks) = @_;
 2190:     return unless(ref($textbooks) eq 'HASH');
 2191:     my $num = scalar(keys(%{$textbooks}));
 2192:     my @jsarray;
 2193:     foreach my $item (sort {$a <=> $b } (keys(%{$textbooks}))) {
 2194:         push(@jsarray,$textbooks->{$item});
 2195:     }
 2196:     my $jstext = '    var textbooks = Array('."'".join("','",@jsarray)."'".');'."\n";
 2197:     return <<"ENDSCRIPT";
 2198: <script type="text/javascript">
 2199: // <![CDATA[
 2200: function reorderBooks(form,item) {
 2201:     var changedVal;
 2202: $jstext 
 2203:     var newpos = 'addbook_pos';
 2204:     var current = new Array;
 2205:     var maxh = 1 + $num;
 2206:     var current = new Array;
 2207:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2208:     if (item == newpos) {
 2209:         changedVal = newitemVal;
 2210:     } else {
 2211:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2212:         current[newitemVal] = newpos;
 2213:     }
 2214:     for (var i=0; i<textbooks.length; i++) {
 2215:         var elementName = textbooks[i];
 2216:         if (elementName != item) {
 2217:             if (form.elements[elementName]) {
 2218:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2219:                 current[currVal] = elementName;
 2220:             }
 2221:         }
 2222:     }
 2223:     var oldVal;
 2224:     for (var j=0; j<maxh; j++) {
 2225:         if (current[j] == undefined) {
 2226:             oldVal = j;
 2227:         }
 2228:     }
 2229:     if (oldVal < changedVal) {
 2230:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2231:            var elementName = current[k];
 2232:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2233:         }
 2234:     } else {
 2235:         for (var k=changedVal; k<oldVal; k++) {
 2236:             var elementName = current[k];
 2237:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2238:         }
 2239:     }
 2240:     return;
 2241: }
 2242: 
 2243: // ]]>
 2244: </script>
 2245: 
 2246: ENDSCRIPT
 2247: }
 2248: 
 2249: sub print_autoenroll {
 2250:     my ($dom,$settings,$rowtotal) = @_;
 2251:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2252:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2253:     if (ref($settings) eq 'HASH') {
 2254:         if (exists($settings->{'run'})) {
 2255:             if ($settings->{'run'} eq '0') {
 2256:                 $runoff = ' checked="checked" ';
 2257:                 $runon = ' ';
 2258:             } else {
 2259:                 $runon = ' checked="checked" ';
 2260:                 $runoff = ' ';
 2261:             }
 2262:         } else {
 2263:             if ($autorun) {
 2264:                 $runon = ' checked="checked" ';
 2265:                 $runoff = ' ';
 2266:             } else {
 2267:                 $runoff = ' checked="checked" ';
 2268:                 $runon = ' ';
 2269:             }
 2270:         }
 2271:         if (exists($settings->{'co-owners'})) {
 2272:             if ($settings->{'co-owners'} eq '0') {
 2273:                 $coownersoff = ' checked="checked" ';
 2274:                 $coownerson = ' ';
 2275:             } else {
 2276:                 $coownerson = ' checked="checked" ';
 2277:                 $coownersoff = ' ';
 2278:             }
 2279:         } else {
 2280:             $coownersoff = ' checked="checked" ';
 2281:             $coownerson = ' ';
 2282:         }
 2283:         if (exists($settings->{'sender_domain'})) {
 2284:             $defdom = $settings->{'sender_domain'};
 2285:         }
 2286:     } else {
 2287:         if ($autorun) {
 2288:             $runon = ' checked="checked" ';
 2289:             $runoff = ' ';
 2290:         } else {
 2291:             $runoff = ' checked="checked" ';
 2292:             $runon = ' ';
 2293:         }
 2294:     }
 2295:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2296:     my $notif_sender;
 2297:     if (ref($settings) eq 'HASH') {
 2298:         $notif_sender = $settings->{'sender_uname'};
 2299:     }
 2300:     my $datatable='<tr class="LC_odd_row">'.
 2301:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2302:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2303:                   '<input type="radio" name="autoenroll_run"'.
 2304:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2305:                   '<label><input type="radio" name="autoenroll_run"'.
 2306:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2307:                   '</tr><tr>'.
 2308:                   '<td>'.&mt('Notification messages - sender').
 2309:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2310:                   &mt('username').':&nbsp;'.
 2311:                   '<input type="text" name="sender_uname" value="'.
 2312:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2313:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2314:                   '<tr class="LC_odd_row">'.
 2315:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2316:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2317:                   '<input type="radio" name="autoassign_coowners"'.
 2318:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2319:                   '<label><input type="radio" name="autoassign_coowners"'.
 2320:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2321:                   '</tr>';
 2322:     $$rowtotal += 3;
 2323:     return $datatable;
 2324: }
 2325: 
 2326: sub print_autoupdate {
 2327:     my ($position,$dom,$settings,$rowtotal) = @_;
 2328:     my $datatable;
 2329:     if ($position eq 'top') {
 2330:         my $updateon = ' ';
 2331:         my $updateoff = ' checked="checked" ';
 2332:         my $classlistson = ' ';
 2333:         my $classlistsoff = ' checked="checked" ';
 2334:         if (ref($settings) eq 'HASH') {
 2335:             if ($settings->{'run'} eq '1') {
 2336:                 $updateon = $updateoff;
 2337:                 $updateoff = ' ';
 2338:             }
 2339:             if ($settings->{'classlists'} eq '1') {
 2340:                 $classlistson = $classlistsoff;
 2341:                 $classlistsoff = ' ';
 2342:             }
 2343:         }
 2344:         my %title = (
 2345:                    run => 'Auto-update active?',
 2346:                    classlists => 'Update information in classlists?',
 2347:                     );
 2348:         $datatable = '<tr class="LC_odd_row">'. 
 2349:                   '<td>'.&mt($title{'run'}).'</td>'.
 2350:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2351:                   '<input type="radio" name="autoupdate_run"'.
 2352:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2353:                   '<label><input type="radio" name="autoupdate_run"'.
 2354:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2355:                   '</tr><tr>'.
 2356:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2357:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2358:                   '<label><input type="radio" name="classlists"'.
 2359:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2360:                   '<label><input type="radio" name="classlists"'.
 2361:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2362:                   '</tr>';
 2363:         $$rowtotal += 2;
 2364:     } elsif ($position eq 'middle') {
 2365:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2366:         my $numinrow = 3;
 2367:         my $locknamesettings;
 2368:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2369:                                      $dom,$numinrow,$othertitle,
 2370:                                     'lockablenames');
 2371:         $$rowtotal ++;
 2372:     } else {
 2373:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2374:         my @fields = ('lastname','firstname','middlename','generation',
 2375:                       'permanentemail','id');
 2376:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2377:         my $numrows = 0;
 2378:         if (ref($types) eq 'ARRAY') {
 2379:             if (@{$types} > 0) {
 2380:                 $datatable = 
 2381:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2382:                                          \@fields,$types,\$numrows);
 2383:                     $$rowtotal += @{$types}; 
 2384:             }
 2385:         }
 2386:         $datatable .= 
 2387:             &usertype_update_row($settings,{'default' => $othertitle},
 2388:                                  \%fieldtitles,\@fields,['default'],
 2389:                                  \$numrows);
 2390:         $$rowtotal ++;     
 2391:     }
 2392:     return $datatable;
 2393: }
 2394: 
 2395: sub print_autocreate {
 2396:     my ($dom,$settings,$rowtotal) = @_;
 2397:     my (%createon,%createoff,%currhash);
 2398:     my @types = ('xml','req');
 2399:     if (ref($settings) eq 'HASH') {
 2400:         foreach my $item (@types) {
 2401:             $createoff{$item} = ' checked="checked" ';
 2402:             $createon{$item} = ' ';
 2403:             if (exists($settings->{$item})) {
 2404:                 if ($settings->{$item}) {
 2405:                     $createon{$item} = ' checked="checked" ';
 2406:                     $createoff{$item} = ' ';
 2407:                 }
 2408:             }
 2409:         }
 2410:         if ($settings->{'xmldc'} ne '') {
 2411:             $currhash{$settings->{'xmldc'}} = 1;
 2412:         }
 2413:     } else {
 2414:         foreach my $item (@types) {
 2415:             $createoff{$item} = ' checked="checked" ';
 2416:             $createon{$item} = ' ';
 2417:         }
 2418:     }
 2419:     $$rowtotal += 2;
 2420:     my $numinrow = 2;
 2421:     my $datatable='<tr class="LC_odd_row">'.
 2422:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2423:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2424:                   '<input type="radio" name="autocreate_xml"'.
 2425:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2426:                   '<label><input type="radio" name="autocreate_xml"'.
 2427:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2428:                   '</td></tr><tr>'.
 2429:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2430:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2431:                   '<input type="radio" name="autocreate_req"'.
 2432:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2433:                   '<label><input type="radio" name="autocreate_req"'.
 2434:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2435:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2436:                                                    'autocreate_xmldc',%currhash);
 2437:     if ($numdc > 1) {
 2438:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2439:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2440:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2441:     } else {
 2442:         $datatable .= $dctable.'</td></tr>';
 2443:     }
 2444:     $$rowtotal += $rows;
 2445:     return $datatable;
 2446: }
 2447: 
 2448: sub print_directorysrch {
 2449:     my ($dom,$settings,$rowtotal) = @_;
 2450:     my $srchon = ' ';
 2451:     my $srchoff = ' checked="checked" ';
 2452:     my ($exacton,$containson,$beginson);
 2453:     my $localon = ' ';
 2454:     my $localoff = ' checked="checked" ';
 2455:     if (ref($settings) eq 'HASH') {
 2456:         if ($settings->{'available'} eq '1') {
 2457:             $srchon = $srchoff;
 2458:             $srchoff = ' ';
 2459:         }
 2460:         if ($settings->{'localonly'} eq '1') {
 2461:             $localon = $localoff;
 2462:             $localoff = ' ';
 2463:         }
 2464:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2465:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2466:                 if ($type eq 'exact') {
 2467:                     $exacton = ' checked="checked" ';
 2468:                 } elsif ($type eq 'contains') {
 2469:                     $containson = ' checked="checked" ';
 2470:                 } elsif ($type eq 'begins') {
 2471:                     $beginson = ' checked="checked" ';
 2472:                 }
 2473:             }
 2474:         } else {
 2475:             if ($settings->{'searchtypes'} eq 'exact') {
 2476:                 $exacton = ' checked="checked" ';
 2477:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2478:                 $containson = ' checked="checked" ';
 2479:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2480:                 $exacton = ' checked="checked" ';
 2481:                 $containson = ' checked="checked" ';
 2482:             }
 2483:         }
 2484:     }
 2485:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2486:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2487: 
 2488:     my $numinrow = 4;
 2489:     my $cansrchrow = 0;
 2490:     my $datatable='<tr class="LC_odd_row">'.
 2491:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2492:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2493:                   '<input type="radio" name="dirsrch_available"'.
 2494:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2495:                   '<label><input type="radio" name="dirsrch_available"'.
 2496:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2497:                   '</tr><tr>'.
 2498:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2499:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2500:                   '<input type="radio" name="dirsrch_localonly"'.
 2501:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2502:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2503:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2504:                   '</tr>';
 2505:     $$rowtotal += 2;
 2506:     if (ref($usertypes) eq 'HASH') {
 2507:         if (keys(%{$usertypes}) > 0) {
 2508:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2509:                                          $numinrow,$othertitle,'cansearch');
 2510:             $cansrchrow = 1;
 2511:         }
 2512:     }
 2513:     if ($cansrchrow) {
 2514:         $$rowtotal ++;
 2515:         $datatable .= '<tr>';
 2516:     } else {
 2517:         $datatable .= '<tr class="LC_odd_row">';
 2518:     }
 2519:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2520:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2521:     foreach my $title (@{$titleorder}) {
 2522:         if (defined($searchtitles->{$title})) {
 2523:             my $check = ' ';
 2524:             if (ref($settings) eq 'HASH') {
 2525:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2526:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2527:                         $check = ' checked="checked" ';
 2528:                     }
 2529:                 }
 2530:             }
 2531:             $datatable .= '<td class="LC_left_item">'.
 2532:                           '<span class="LC_nobreak"><label>'.
 2533:                           '<input type="checkbox" name="searchby" '.
 2534:                           'value="'.$title.'"'.$check.'/>'.
 2535:                           $searchtitles->{$title}.'</label></span></td>';
 2536:         }
 2537:     }
 2538:     $datatable .= '</tr></table></td></tr>';
 2539:     $$rowtotal ++;
 2540:     if ($cansrchrow) {
 2541:         $datatable .= '<tr class="LC_odd_row">';
 2542:     } else {
 2543:         $datatable .= '<tr>';
 2544:     }
 2545:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2546:                   '<td class="LC_left_item" colspan="2">'.
 2547:                   '<span class="LC_nobreak"><label>'.
 2548:                   '<input type="checkbox" name="searchtypes" '.
 2549:                   $exacton.' value="exact" />'.&mt('Exact match').
 2550:                   '</label>&nbsp;'.
 2551:                   '<label><input type="checkbox" name="searchtypes" '.
 2552:                   $beginson.' value="begins" />'.&mt('Begins with').
 2553:                   '</label>&nbsp;'.
 2554:                   '<label><input type="checkbox" name="searchtypes" '.
 2555:                   $containson.' value="contains" />'.&mt('Contains').
 2556:                   '</label></span></td></tr>';
 2557:     $$rowtotal ++;
 2558:     return $datatable;
 2559: }
 2560: 
 2561: sub print_contacts {
 2562:     my ($dom,$settings,$rowtotal) = @_;
 2563:     my $datatable;
 2564:     my @contacts = ('adminemail','supportemail');
 2565:     my (%checked,%to,%otheremails,%bccemails);
 2566:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2567:                     'requestsmail','updatesmail','idconflictsmail');
 2568:     foreach my $type (@mailings) {
 2569:         $otheremails{$type} = '';
 2570:     }
 2571:     $bccemails{'helpdeskmail'} = '';
 2572:     if (ref($settings) eq 'HASH') {
 2573:         foreach my $item (@contacts) {
 2574:             if (exists($settings->{$item})) {
 2575:                 $to{$item} = $settings->{$item};
 2576:             }
 2577:         }
 2578:         foreach my $type (@mailings) {
 2579:             if (exists($settings->{$type})) {
 2580:                 if (ref($settings->{$type}) eq 'HASH') {
 2581:                     foreach my $item (@contacts) {
 2582:                         if ($settings->{$type}{$item}) {
 2583:                             $checked{$type}{$item} = ' checked="checked" ';
 2584:                         }
 2585:                     }
 2586:                     $otheremails{$type} = $settings->{$type}{'others'};
 2587:                     if ($type eq 'helpdeskmail') {
 2588:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2589:                     }
 2590:                 }
 2591:             } elsif ($type eq 'lonstatusmail') {
 2592:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2593:             }
 2594:         }
 2595:     } else {
 2596:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2597:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2598:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2599:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2600:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2601:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2602:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2603:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2604:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2605:     }
 2606:     my ($titles,$short_titles) = &contact_titles();
 2607:     my $rownum = 0;
 2608:     my $css_class;
 2609:     foreach my $item (@contacts) {
 2610:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2611:         $datatable .= '<tr'.$css_class.'>'. 
 2612:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2613:                   '</span></td><td class="LC_right_item">'.
 2614:                   '<input type="text" name="'.$item.'" value="'.
 2615:                   $to{$item}.'" /></td></tr>';
 2616:         $rownum ++;
 2617:     }
 2618:     foreach my $type (@mailings) {
 2619:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2620:         $datatable .= '<tr'.$css_class.'>'.
 2621:                       '<td><span class="LC_nobreak">'.
 2622:                       $titles->{$type}.': </span></td>'.
 2623:                       '<td class="LC_left_item">'.
 2624:                       '<span class="LC_nobreak">';
 2625:         foreach my $item (@contacts) {
 2626:             $datatable .= '<label>'.
 2627:                           '<input type="checkbox" name="'.$type.'"'.
 2628:                           $checked{$type}{$item}.
 2629:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2630:                           '</label>&nbsp;';
 2631:         }
 2632:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2633:                       '<input type="text" name="'.$type.'_others" '.
 2634:                       'value="'.$otheremails{$type}.'"  />';
 2635:         if ($type eq 'helpdeskmail') {
 2636:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2637:                           '<input type="text" name="'.$type.'_bcc" '.
 2638:                           'value="'.$bccemails{$type}.'"  />';
 2639:         }
 2640:         $datatable .= '</td></tr>'."\n";
 2641:         $rownum ++;
 2642:     }
 2643:     my %choices;
 2644:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2645:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2646:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2647:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2648:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2649:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2650:     my @toggles = ('reporterrors','reportupdates');
 2651:     my %defaultchecked = ('reporterrors'  => 'on',
 2652:                           'reportupdates' => 'on');
 2653:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2654:                                                \%choices,$rownum);
 2655:     $datatable .= $reports;
 2656:     $$rowtotal += $rownum;
 2657:     return $datatable;
 2658: }
 2659: 
 2660: sub print_helpsettings {
 2661:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2662:     my ($datatable,$itemcount);
 2663:     $itemcount = 1;
 2664:     my (%choices,%defaultchecked,@toggles);
 2665:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2666:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2667:                                  &mt('LON-CAPA bug tracker'),600,500));
 2668:     %defaultchecked = ('submitbugs' => 'on');
 2669:     @toggles = ('submitbugs',);
 2670: 
 2671:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2672:                                                  \%choices,$itemcount);
 2673:     return $datatable;
 2674: }
 2675: 
 2676: sub radiobutton_prefs {
 2677:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2678:         $additional) = @_;
 2679:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2680:                    (ref($choices) eq 'HASH'));
 2681: 
 2682:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2683: 
 2684:     foreach my $item (@{$toggles}) {
 2685:         if ($defaultchecked->{$item} eq 'on') {
 2686:             $checkedon{$item} = ' checked="checked" ';
 2687:             $checkedoff{$item} = ' ';
 2688:         } elsif ($defaultchecked->{$item} eq 'off') {
 2689:             $checkedoff{$item} = ' checked="checked" ';
 2690:             $checkedon{$item} = ' ';
 2691:         }
 2692:     }
 2693:     if (ref($settings) eq 'HASH') {
 2694:         foreach my $item (@{$toggles}) {
 2695:             if ($settings->{$item} eq '1') {
 2696:                 $checkedon{$item} =  ' checked="checked" ';
 2697:                 $checkedoff{$item} = ' ';
 2698:             } elsif ($settings->{$item} eq '0') {
 2699:                 $checkedoff{$item} =  ' checked="checked" ';
 2700:                 $checkedon{$item} = ' ';
 2701:             }
 2702:         }
 2703:     }
 2704:     if ($onclick) {
 2705:         $onclick = ' onclick="'.$onclick.'"';
 2706:     }
 2707:     foreach my $item (@{$toggles}) {
 2708:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2709:         $datatable .=
 2710:             '<tr'.$css_class.'><td valign="top">'.
 2711:             '<span class="LC_nobreak">'.$choices->{$item}.
 2712:             '</span></td>'.
 2713:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2714:             '<label><input type="radio" name="'.
 2715:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2716:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2717:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2718:             '</span>'.$additional.
 2719:             '</td>'.
 2720:             '</tr>';
 2721:         $itemcount ++;
 2722:     }
 2723:     return ($datatable,$itemcount);
 2724: }
 2725: 
 2726: sub print_coursedefaults {
 2727:     my ($position,$dom,$settings,$rowtotal) = @_;
 2728:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2729:     my $itemcount = 1;
 2730:     my %choices =  &Apache::lonlocal::texthash (
 2731:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2732:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2733:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2734:         coursecredits        => 'Credits can be specified for courses',
 2735:     );
 2736:     my %staticdefaults = (
 2737:                            anonsurvey_threshold => 10,
 2738:                            uploadquota          => 500,
 2739:                          );
 2740:     if ($position eq 'top') {
 2741:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2742:         @toggles = ('canuse_pdfforms');
 2743:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2744:                                                  \%choices,$itemcount);
 2745:     } else {
 2746:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2747:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2748:             %curruploadquota);
 2749:         my $currusecredits = 0;
 2750:         my @types = ('official','unofficial','community','textbook');
 2751:         if (ref($settings) eq 'HASH') {
 2752:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2753:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2754:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2755:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2756:                 }
 2757:             }
 2758:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2759:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2760:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2761:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2762:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2763:                     ($def_textbook_credits ne '')) {
 2764:                     $currusecredits = 1;
 2765:                 }
 2766:             }
 2767:         }
 2768:         if (!$currdefresponder) {
 2769:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2770:         } elsif ($currdefresponder < 1) {
 2771:             $currdefresponder = 1;
 2772:         }
 2773:         foreach my $type (@types) {
 2774:             if ($curruploadquota{$type} eq '') {
 2775:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2776:             }
 2777:         }
 2778:         $datatable .=
 2779:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2780:                 $choices{'anonsurvey_threshold'}.
 2781:                 '</span></td>'.
 2782:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2783:                 '<input type="text" name="anonsurvey_threshold"'.
 2784:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2785:                 '</td></tr>'."\n".
 2786:                 '<tr><td><span class="LC_nobreak">'.
 2787:                 $choices{'uploadquota'}.
 2788:                 '</span></td>'.
 2789:                 '<td align="right" class="LC_right_item">'.
 2790:                 '<table><tr>';
 2791:         foreach my $type (@types) {
 2792:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2793:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2794:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2795:         }
 2796:         $datatable .= '</tr></table></td></tr>'."\n";
 2797:         $itemcount += 2;
 2798:         my $onclick = 'toggleCredits(this.form);';
 2799:         my $display = 'none';
 2800:         if ($currusecredits) {
 2801:             $display = 'block';
 2802:         }
 2803:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2804:                          '<span class="LC_nobreak">'.
 2805:                          &mt('Default credits for official courses [_1]',
 2806:                          '<input type="text" name="official_credits" value="'.
 2807:                          $def_official_credits.'" size="3" />').
 2808:                          '</span><br />'.
 2809:                          '<span class="LC_nobreak">'.
 2810:                          &mt('Default credits for unofficial courses [_1]',
 2811:                          '<input type="text" name="unofficial_credits" value="'.
 2812:                          $def_unofficial_credits.'" size="3" />').
 2813:                          '</span><br />'.
 2814:                          '<span class="LC_nobreak">'.
 2815:                          &mt('Default credits for textbook courses [_1]',
 2816:                          '<input type="text" name="textbook_credits" value="'.
 2817:                          $def_textbook_credits.'" size="3" />').
 2818:                          '</span></div>'."\n";
 2819:         %defaultchecked = ('coursecredits' => 'off');
 2820:         @toggles = ('coursecredits');
 2821:         my $current = {
 2822:                         'coursecredits' => $currusecredits,
 2823:                       };
 2824:         (my $table,$itemcount) =
 2825:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2826:                                \%choices,$itemcount,$onclick,$additional);
 2827:         $datatable .= $table;
 2828:     }
 2829:     $$rowtotal += $itemcount;
 2830:     return $datatable;
 2831: }
 2832: 
 2833: sub print_usersessions {
 2834:     my ($position,$dom,$settings,$rowtotal) = @_;
 2835:     my ($css_class,$datatable,%checked,%choices);
 2836:     my (%by_ip,%by_location,@intdoms);
 2837:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2838: 
 2839:     my @alldoms = &Apache::lonnet::all_domains();
 2840:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2841:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2842:     my %altids = &id_for_thisdom(%servers);
 2843:     my $itemcount = 1;
 2844:     if ($position eq 'top') {
 2845:         if (keys(%serverhomes) > 1) {
 2846:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2847:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2848:         } else {
 2849:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2850:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2851:         }
 2852:     } else {
 2853:         if (keys(%by_location) == 0) {
 2854:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2855:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2856:         } else {
 2857:             my %lt = &usersession_titles();
 2858:             my $numinrow = 5;
 2859:             my $prefix;
 2860:             my @types;
 2861:             if ($position eq 'bottom') {
 2862:                 $prefix = 'remote';
 2863:                 @types = ('version','excludedomain','includedomain');
 2864:             } else {
 2865:                 $prefix = 'hosted';
 2866:                 @types = ('excludedomain','includedomain');
 2867:             }
 2868:             my (%current,%checkedon,%checkedoff);
 2869:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2870:             my @locations = sort(keys(%by_location));
 2871:             foreach my $type (@types) {
 2872:                 $checkedon{$type} = '';
 2873:                 $checkedoff{$type} = ' checked="checked"';
 2874:             }
 2875:             if (ref($settings) eq 'HASH') {
 2876:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2877:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2878:                         $current{$key} = $settings->{$prefix}{$key};
 2879:                         if ($key eq 'version') {
 2880:                             if ($current{$key} ne '') {
 2881:                                 $checkedon{$key} = ' checked="checked"';
 2882:                                 $checkedoff{$key} = '';
 2883:                             }
 2884:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2885:                             $checkedon{$key} = ' checked="checked"';
 2886:                             $checkedoff{$key} = '';
 2887:                         }
 2888:                     }
 2889:                 }
 2890:             }
 2891:             foreach my $type (@types) {
 2892:                 next if ($type ne 'version' && !@locations);
 2893:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2894:                 $datatable .= '<tr'.$css_class.'>
 2895:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2896:                                <span class="LC_nobreak">&nbsp;
 2897:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2898:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2899:                 if ($type eq 'version') {
 2900:                     my $selector = '<select name="'.$prefix.'_version">';
 2901:                     foreach my $version (@lcversions) {
 2902:                         my $selected = '';
 2903:                         if ($current{'version'} eq $version) {
 2904:                             $selected = ' selected="selected"';
 2905:                         }
 2906:                         $selector .= ' <option value="'.$version.'"'.
 2907:                                      $selected.'>'.$version.'</option>';
 2908:                     }
 2909:                     $selector .= '</select> ';
 2910:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2911:                 } else {
 2912:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2913:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2914:                                  ' />'.('&nbsp;'x2).
 2915:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2916:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2917:                                  "\n".
 2918:                                  '</div><div><table>';
 2919:                     my $rem;
 2920:                     for (my $i=0; $i<@locations; $i++) {
 2921:                         my ($showloc,$value,$checkedtype);
 2922:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2923:                             my $ip = $by_location{$locations[$i]}->[0];
 2924:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2925:                                  $value = join(':',@{$by_ip{$ip}});
 2926:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2927:                                 if (ref($current{$type}) eq 'ARRAY') {
 2928:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2929:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2930:                                             $checkedtype = ' checked="checked"';
 2931:                                             last;
 2932:                                         }
 2933:                                     }
 2934:                                 }
 2935:                             }
 2936:                         }
 2937:                         $rem = $i%($numinrow);
 2938:                         if ($rem == 0) {
 2939:                             if ($i > 0) {
 2940:                                 $datatable .= '</tr>';
 2941:                             }
 2942:                             $datatable .= '<tr>';
 2943:                         }
 2944:                         $datatable .= '<td class="LC_left_item">'.
 2945:                                       '<span class="LC_nobreak"><label>'.
 2946:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2947:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2948:                                       '</label></span></td>';
 2949:                     }
 2950:                     $rem = @locations%($numinrow);
 2951:                     my $colsleft = $numinrow - $rem;
 2952:                     if ($colsleft > 1 ) {
 2953:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2954:                                       '&nbsp;</td>';
 2955:                     } elsif ($colsleft == 1) {
 2956:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2957:                     }
 2958:                     $datatable .= '</tr></table>';
 2959:                 }
 2960:                 $datatable .= '</td></tr>';
 2961:                 $itemcount ++;
 2962:             }
 2963:         }
 2964:     }
 2965:     $$rowtotal += $itemcount;
 2966:     return $datatable;
 2967: }
 2968: 
 2969: sub build_location_hashes {
 2970:     my ($intdoms,$by_ip,$by_location) = @_;
 2971:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2972:                   (ref($by_location) eq 'HASH')); 
 2973:     my %iphost = &Apache::lonnet::get_iphost();
 2974:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2975:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2976:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2977:         foreach my $id (@{$iphost{$primary_ip}}) {
 2978:             my $intdom = &Apache::lonnet::internet_dom($id);
 2979:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2980:                 push(@{$intdoms},$intdom);
 2981:             }
 2982:         }
 2983:     }
 2984:     foreach my $ip (keys(%iphost)) {
 2985:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2986:             foreach my $id (@{$iphost{$ip}}) {
 2987:                 my $location = &Apache::lonnet::internet_dom($id);
 2988:                 if ($location) {
 2989:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2990:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2991:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2992:                             push(@{$by_ip->{$ip}},$location);
 2993:                         }
 2994:                     } else {
 2995:                         $by_ip->{$ip} = [$location];
 2996:                     }
 2997:                 }
 2998:             }
 2999:         }
 3000:     }
 3001:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3002:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3003:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3004:             my $first = $by_ip->{$ip}->[0];
 3005:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3006:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3007:                     push(@{$by_location->{$first}},$ip);
 3008:                 }
 3009:             } else {
 3010:                 $by_location->{$first} = [$ip];
 3011:             }
 3012:         }
 3013:     }
 3014:     return;
 3015: }
 3016: 
 3017: sub current_offloads_to {
 3018:     my ($dom,$settings,$servers) = @_;
 3019:     my (%spareid,%otherdomconfigs);
 3020:     if (ref($servers) eq 'HASH') {
 3021:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3022:             my $gotspares;
 3023:             if (ref($settings) eq 'HASH') {
 3024:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3025:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3026:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3027:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3028:                         $gotspares = 1;
 3029:                     }
 3030:                 }
 3031:             }
 3032:             unless ($gotspares) {
 3033:                 my $gotspares;
 3034:                 my $serverhomeID =
 3035:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3036:                 my $serverhomedom =
 3037:                     &Apache::lonnet::host_domain($serverhomeID);
 3038:                 if ($serverhomedom ne $dom) {
 3039:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3040:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3041:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3042:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3043:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3044:                                 $gotspares = 1;
 3045:                             }
 3046:                         }
 3047:                     } else {
 3048:                         $otherdomconfigs{$serverhomedom} =
 3049:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3050:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3051:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3052:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3053:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3054:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3055:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3056:                                         $gotspares = 1;
 3057:                                     }
 3058:                                 }
 3059:                             }
 3060:                         }
 3061:                     }
 3062:                 }
 3063:             }
 3064:             unless ($gotspares) {
 3065:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3066:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3067:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3068:                } else {
 3069:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3070:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3071:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3072:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3073:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3074:                     } else {
 3075:                         my %what = (
 3076:                              spareid => 1,
 3077:                         );
 3078:                         my ($result,$returnhash) = 
 3079:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3080:                         if ($result eq 'ok') { 
 3081:                             if (ref($returnhash) eq 'HASH') {
 3082:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3083:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3084:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3085:                                 }
 3086:                             }
 3087:                         }
 3088:                     }
 3089:                 }
 3090:             }
 3091:         }
 3092:     }
 3093:     return %spareid;
 3094: }
 3095: 
 3096: sub spares_row {
 3097:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3098:     my $css_class;
 3099:     my $numinrow = 4;
 3100:     my $itemcount = 1;
 3101:     my $datatable;
 3102:     my %typetitles = &sparestype_titles();
 3103:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3104:         foreach my $server (sort(keys(%{$servers}))) {
 3105:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3106:             my ($othercontrol,$serverdom);
 3107:             if ($serverhome ne $server) {
 3108:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3109:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3110:             } else {
 3111:                 $serverdom = &Apache::lonnet::host_domain($server);
 3112:                 if ($serverdom ne $dom) {
 3113:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3114:                 }
 3115:             }
 3116:             next unless (ref($spareid->{$server}) eq 'HASH');
 3117:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3118:             $datatable .= '<tr'.$css_class.'>
 3119:                            <td rowspan="2">
 3120:                             <span class="LC_nobreak">'.
 3121:                           &mt('[_1] when busy, offloads to:'
 3122:                               ,'<b>'.$server.'</b>').
 3123:                           "\n";
 3124:             my (%current,%canselect);
 3125:             my @choices = 
 3126:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3127:             foreach my $type ('primary','default') {
 3128:                 if (ref($spareid->{$server}) eq 'HASH') {
 3129:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3130:                         my @spares = @{$spareid->{$server}{$type}};
 3131:                         if (@spares > 0) {
 3132:                             if ($othercontrol) {
 3133:                                 $current{$type} = join(', ',@spares);
 3134:                             } else {
 3135:                                 $current{$type} .= '<table>';
 3136:                                 my $numspares = scalar(@spares);
 3137:                                 for (my $i=0;  $i<@spares; $i++) {
 3138:                                     my $rem = $i%($numinrow);
 3139:                                     if ($rem == 0) {
 3140:                                         if ($i > 0) {
 3141:                                             $current{$type} .= '</tr>';
 3142:                                         }
 3143:                                         $current{$type} .= '<tr>';
 3144:                                     }
 3145:                                     $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;'.
 3146:                                                        $spareid->{$server}{$type}[$i].
 3147:                                                        '</label></td>'."\n";
 3148:                                 }
 3149:                                 my $rem = @spares%($numinrow);
 3150:                                 my $colsleft = $numinrow - $rem;
 3151:                                 if ($colsleft > 1 ) {
 3152:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3153:                                                        '" class="LC_left_item">'.
 3154:                                                        '&nbsp;</td>';
 3155:                                 } elsif ($colsleft == 1) {
 3156:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3157:                                 }
 3158:                                 $current{$type} .= '</tr></table>';
 3159:                             }
 3160:                         }
 3161:                     }
 3162:                     if ($current{$type} eq '') {
 3163:                         $current{$type} = &mt('None specified');
 3164:                     }
 3165:                     if ($othercontrol) {
 3166:                         if ($type eq 'primary') {
 3167:                             $canselect{$type} = $othercontrol;
 3168:                         }
 3169:                     } else {
 3170:                         $canselect{$type} = 
 3171:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3172:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3173:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3174:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3175:                         if (@choices > 0) {
 3176:                             foreach my $lonhost (@choices) {
 3177:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3178:                             }
 3179:                         }
 3180:                         $canselect{$type} .= '</select>'."\n";
 3181:                     }
 3182:                 } else {
 3183:                     $current{$type} = &mt('Could not be determined');
 3184:                     if ($type eq 'primary') {
 3185:                         $canselect{$type} =  $othercontrol;
 3186:                     }
 3187:                 }
 3188:                 if ($type eq 'default') {
 3189:                     $datatable .= '<tr'.$css_class.'>';
 3190:                 }
 3191:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3192:                               '<td>'.$current{$type}.'</td>'."\n".
 3193:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3194:             }
 3195:             $itemcount ++;
 3196:         }
 3197:     }
 3198:     $$rowtotal += $itemcount;
 3199:     return $datatable;
 3200: }
 3201: 
 3202: sub possible_newspares {
 3203:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3204:     my $serverhostname = &Apache::lonnet::hostname($server);
 3205:     my %excluded;
 3206:     if ($serverhostname ne '') {
 3207:         %excluded = (
 3208:                        $serverhostname => 1,
 3209:                     );
 3210:     }
 3211:     if (ref($currspares) eq 'HASH') {
 3212:         foreach my $type (keys(%{$currspares})) {
 3213:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3214:                 if (@{$currspares->{$type}} > 0) {
 3215:                     foreach my $curr (@{$currspares->{$type}}) {
 3216:                         my $hostname = &Apache::lonnet::hostname($curr);
 3217:                         $excluded{$hostname} = 1;
 3218:                     }
 3219:                 }
 3220:             }
 3221:         }
 3222:     }
 3223:     my @choices;
 3224:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3225:         if (keys(%{$serverhomes}) > 1) {
 3226:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3227:                 unless ($excluded{$name}) {
 3228:                     if (exists($altids->{$serverhomes->{$name}})) {
 3229:                         push(@choices,$altids->{$serverhomes->{$name}});
 3230:                     } else {
 3231:                         push(@choices,$serverhomes->{$name});
 3232:                     }
 3233:                 }
 3234:             }
 3235:         }
 3236:     }
 3237:     return sort(@choices);
 3238: }
 3239: 
 3240: sub print_loadbalancing {
 3241:     my ($dom,$settings,$rowtotal) = @_;
 3242:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3243:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3244:     my $numinrow = 1;
 3245:     my $datatable;
 3246:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3247:     my (%currbalancer,%currtargets,%currrules,%existing);
 3248:     if (ref($settings) eq 'HASH') {
 3249:         %existing = %{$settings};
 3250:     }
 3251:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3252:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3253:                                   \%currtargets,\%currrules);
 3254:     } else {
 3255:         return;
 3256:     }
 3257:     my ($othertitle,$usertypes,$types) =
 3258:         &Apache::loncommon::sorted_inst_types($dom);
 3259:     my $rownum = 8;
 3260:     if (ref($types) eq 'ARRAY') {
 3261:         $rownum += scalar(@{$types});
 3262:     }
 3263:     my @css_class = ('LC_odd_row','LC_even_row');
 3264:     my $balnum = 0;
 3265:     my $islast;
 3266:     my (@toshow,$disabledtext);
 3267:     if (keys(%currbalancer) > 0) {
 3268:         @toshow = sort(keys(%currbalancer));
 3269:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3270:             push(@toshow,'');
 3271:         }
 3272:     } else {
 3273:         @toshow = ('');
 3274:         $disabledtext = &mt('No existing load balancer');
 3275:     }
 3276:     foreach my $lonhost (@toshow) {
 3277:         if ($balnum == scalar(@toshow)-1) {
 3278:             $islast = 1;
 3279:         } else {
 3280:             $islast = 0;
 3281:         }
 3282:         my $cssidx = $balnum%2;
 3283:         my $targets_div_style = 'display: none';
 3284:         my $disabled_div_style = 'display: block';
 3285:         my $homedom_div_style = 'display: none';
 3286:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3287:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3288:                       '<p>';
 3289:         if ($lonhost eq '') {
 3290:             $datatable .= '<span class="LC_nobreak">';
 3291:             if (keys(%currbalancer) > 0) {
 3292:                 $datatable .= &mt('Add balancer:');
 3293:             } else {
 3294:                 $datatable .= &mt('Enable balancer:');
 3295:             }
 3296:             $datatable .= '&nbsp;'.
 3297:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3298:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3299:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3300:                           '<option value="" selected="selected">'.&mt('None').
 3301:                           '</option>'."\n";
 3302:             foreach my $server (sort(keys(%servers))) {
 3303:                 next if ($currbalancer{$server});
 3304:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3305:             }
 3306:             $datatable .=
 3307:                 '</select>'."\n".
 3308:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3309:         } else {
 3310:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3311:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3312:                            &mt('Stop balancing').'</label>'.
 3313:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3314:             $targets_div_style = 'display: block';
 3315:             $disabled_div_style = 'display: none';
 3316:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3317:                 $homedom_div_style = 'display: block';
 3318:             }
 3319:         }
 3320:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3321:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3322:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3323:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3324:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3325:         my @sparestypes = ('primary','default');
 3326:         my %typetitles = &sparestype_titles();
 3327:         foreach my $sparetype (@sparestypes) {
 3328:             my $targettable;
 3329:             for (my $i=0; $i<$numspares; $i++) {
 3330:                 my $checked;
 3331:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3332:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3333:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3334:                             $checked = ' checked="checked"';
 3335:                         }
 3336:                     }
 3337:                 }
 3338:                 my ($chkboxval,$disabled);
 3339:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3340:                     $chkboxval = $spares[$i];
 3341:                 }
 3342:                 if (exists($currbalancer{$spares[$i]})) {
 3343:                     $disabled = ' disabled="disabled"';
 3344:                 }
 3345:                 $targettable .=
 3346:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3347:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3348:                     '</span></label></td>';
 3349:                 my $rem = $i%($numinrow);
 3350:                 if ($rem == 0) {
 3351:                     if (($i > 0) && ($i < $numspares-1)) {
 3352:                         $targettable .= '</tr>';
 3353:                     }
 3354:                     if ($i < $numspares-1) {
 3355:                         $targettable .= '<tr>';
 3356:                     }
 3357:                 }
 3358:             }
 3359:             if ($targettable ne '') {
 3360:                 my $rem = $numspares%($numinrow);
 3361:                 my $colsleft = $numinrow - $rem;
 3362:                 if ($colsleft > 1 ) {
 3363:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3364:                                     '&nbsp;</td>';
 3365:                 } elsif ($colsleft == 1) {
 3366:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3367:                 }
 3368:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3369:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3370:             }
 3371:         }
 3372:         $datatable .= '</div></td></tr>'.
 3373:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3374:                                            $othertitle,$usertypes,$types,\%servers,
 3375:                                            \%currbalancer,$lonhost,
 3376:                                            $targets_div_style,$homedom_div_style,
 3377:                                            $css_class[$cssidx],$balnum,$islast);
 3378:         $$rowtotal += $rownum;
 3379:         $balnum ++;
 3380:     }
 3381:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3382:     return $datatable;
 3383: }
 3384: 
 3385: sub get_loadbalancers_config {
 3386:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3387:     return unless ((ref($servers) eq 'HASH') &&
 3388:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3389:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3390:     if (keys(%{$existing}) > 0) {
 3391:         my $oldlonhost;
 3392:         foreach my $key (sort(keys(%{$existing}))) {
 3393:             if ($key eq 'lonhost') {
 3394:                 $oldlonhost = $existing->{'lonhost'};
 3395:                 $currbalancer->{$oldlonhost} = 1;
 3396:             } elsif ($key eq 'targets') {
 3397:                 if ($oldlonhost) {
 3398:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3399:                 }
 3400:             } elsif ($key eq 'rules') {
 3401:                 if ($oldlonhost) {
 3402:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3403:                 }
 3404:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3405:                 $currbalancer->{$key} = 1;
 3406:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3407:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3408:             }
 3409:         }
 3410:     } else {
 3411:         my ($balancerref,$targetsref) =
 3412:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3413:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3414:             foreach my $server (sort(keys(%{$balancerref}))) {
 3415:                 $currbalancer->{$server} = 1;
 3416:                 $currtargets->{$server} = $targetsref->{$server};
 3417:             }
 3418:         }
 3419:     }
 3420:     return;
 3421: }
 3422: 
 3423: sub loadbalancing_rules {
 3424:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3425:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3426:         $css_class,$balnum,$islast) = @_;
 3427:     my $output;
 3428:     my $num = 0;
 3429:     my ($alltypes,$othertypes,$titles) =
 3430:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3431:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3432:         foreach my $type (@{$alltypes}) {
 3433:             $num ++;
 3434:             my $current;
 3435:             if (ref($currrules) eq 'HASH') {
 3436:                 $current = $currrules->{$type};
 3437:             }
 3438:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3439:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3440:                     $current = '';
 3441:                 }
 3442:             }
 3443:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3444:                                              $servers,$currbalancer,$lonhost,$dom,
 3445:                                              $targets_div_style,$homedom_div_style,
 3446:                                              $css_class,$balnum,$num,$islast);
 3447:         }
 3448:     }
 3449:     return $output;
 3450: }
 3451: 
 3452: sub loadbalancing_titles {
 3453:     my ($dom,$intdom,$usertypes,$types) = @_;
 3454:     my %othertypes = (
 3455:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3456:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3457:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3458:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3459:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3460:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3461:                      );
 3462:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3463:     if (ref($types) eq 'ARRAY') {
 3464:         unshift(@alltypes,@{$types},'default');
 3465:     }
 3466:     my %titles;
 3467:     foreach my $type (@alltypes) {
 3468:         if ($type =~ /^_LC_/) {
 3469:             $titles{$type} = $othertypes{$type};
 3470:         } elsif ($type eq 'default') {
 3471:             $titles{$type} = &mt('All users from [_1]',$dom);
 3472:             if (ref($types) eq 'ARRAY') {
 3473:                 if (@{$types} > 0) {
 3474:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3475:                 }
 3476:             }
 3477:         } elsif (ref($usertypes) eq 'HASH') {
 3478:             $titles{$type} = $usertypes->{$type};
 3479:         }
 3480:     }
 3481:     return (\@alltypes,\%othertypes,\%titles);
 3482: }
 3483: 
 3484: sub loadbalance_rule_row {
 3485:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3486:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3487:     my @rulenames;
 3488:     my %ruletitles = &offloadtype_text();
 3489:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3490:         @rulenames = ('balancer','offloadedto');
 3491:     } else {
 3492:         @rulenames = ('default','homeserver');
 3493:         if ($type eq '_LC_external') {
 3494:             push(@rulenames,'externalbalancer');
 3495:         } else {
 3496:             push(@rulenames,'specific');
 3497:         }
 3498:         push(@rulenames,'none');
 3499:     }
 3500:     my $style = $targets_div_style;
 3501:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3502:         $style = $homedom_div_style;
 3503:     }
 3504:     my $space;
 3505:     if ($islast && $num == 1) {
 3506:         $space = '<div display="inline-block">&nbsp;</div>';
 3507:     }
 3508:     my $output =
 3509:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3510:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3511:         '<td valaign="top">'.$space.
 3512:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3513:     for (my $i=0; $i<@rulenames; $i++) {
 3514:         my $rule = $rulenames[$i];
 3515:         my ($checked,$extra);
 3516:         if ($rulenames[$i] eq 'default') {
 3517:             $rule = '';
 3518:         }
 3519:         if ($rulenames[$i] eq 'specific') {
 3520:             if (ref($servers) eq 'HASH') {
 3521:                 my $default;
 3522:                 if (($current ne '') && (exists($servers->{$current}))) {
 3523:                     $checked = ' checked="checked"';
 3524:                 }
 3525:                 unless ($checked) {
 3526:                     $default = ' selected="selected"';
 3527:                 }
 3528:                 $extra =
 3529:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3530:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3531:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3532:                     '<option value=""'.$default.'></option>'."\n";
 3533:                 foreach my $server (sort(keys(%{$servers}))) {
 3534:                     if (ref($currbalancer) eq 'HASH') {
 3535:                         next if (exists($currbalancer->{$server}));
 3536:                     }
 3537:                     my $selected;
 3538:                     if ($server eq $current) {
 3539:                         $selected = ' selected="selected"';
 3540:                     }
 3541:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3542:                 }
 3543:                 $extra .= '</select>';
 3544:             }
 3545:         } elsif ($rule eq $current) {
 3546:             $checked = ' checked="checked"';
 3547:         }
 3548:         $output .= '<span class="LC_nobreak"><label>'.
 3549:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3550:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3551:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3552:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3553:                    '</label>'.$extra.'</span><br />'."\n";
 3554:     }
 3555:     $output .= '</div></td></tr>'."\n";
 3556:     return $output;
 3557: }
 3558: 
 3559: sub offloadtype_text {
 3560:     my %ruletitles = &Apache::lonlocal::texthash (
 3561:            'default'          => 'Offloads to default destinations',
 3562:            'homeserver'       => "Offloads to user's home server",
 3563:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3564:            'specific'         => 'Offloads to specific server',
 3565:            'none'             => 'No offload',
 3566:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3567:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3568:     );
 3569:     return %ruletitles;
 3570: }
 3571: 
 3572: sub sparestype_titles {
 3573:     my %typestitles = &Apache::lonlocal::texthash (
 3574:                           'primary' => 'primary',
 3575:                           'default' => 'default',
 3576:                       );
 3577:     return %typestitles;
 3578: }
 3579: 
 3580: sub contact_titles {
 3581:     my %titles = &Apache::lonlocal::texthash (
 3582:                    'supportemail' => 'Support E-mail address',
 3583:                    'adminemail'   => 'Default Server Admin E-mail address',
 3584:                    'errormail'    => 'Error reports to be e-mailed to',
 3585:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3586:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3587:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3588:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3589:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3590:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3591:                  );
 3592:     my %short_titles = &Apache::lonlocal::texthash (
 3593:                            adminemail   => 'Admin E-mail address',
 3594:                            supportemail => 'Support E-mail',
 3595:                        );   
 3596:     return (\%titles,\%short_titles);
 3597: }
 3598: 
 3599: sub tool_titles {
 3600:     my %titles = &Apache::lonlocal::texthash (
 3601:                      aboutme    => 'Personal web page',
 3602:                      blog       => 'Blog',
 3603:                      webdav     => 'WebDAV',
 3604:                      portfolio  => 'Portfolio',
 3605:                      official   => 'Official courses (with institutional codes)',
 3606:                      unofficial => 'Unofficial courses',
 3607:                      community  => 'Communities',
 3608:                      textbook   => 'Textbook courses',
 3609:                  );
 3610:     return %titles;
 3611: }
 3612: 
 3613: sub courserequest_titles {
 3614:     my %titles = &Apache::lonlocal::texthash (
 3615:                                    official   => 'Official',
 3616:                                    unofficial => 'Unofficial',
 3617:                                    community  => 'Communities',
 3618:                                    textbook   => 'Textbook',
 3619:                                    norequest  => 'Not allowed',
 3620:                                    approval   => 'Approval by Dom. Coord.',
 3621:                                    validate   => 'With validation',
 3622:                                    autolimit  => 'Numerical limit',
 3623:                                    unlimited  => '(blank for unlimited)',
 3624:                  );
 3625:     return %titles;
 3626: }
 3627: 
 3628: sub authorrequest_titles {
 3629:     my %titles = &Apache::lonlocal::texthash (
 3630:                                    norequest  => 'Not allowed',
 3631:                                    approval   => 'Approval by Dom. Coord.',
 3632:                                    automatic  => 'Automatic approval',
 3633:                  );
 3634:     return %titles;
 3635: }
 3636: 
 3637: sub courserequest_conditions {
 3638:     my %conditions = &Apache::lonlocal::texthash (
 3639:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3640:        validate   => '(Processing of request subject to institutional validation).',
 3641:                  );
 3642:     return %conditions;
 3643: }
 3644: 
 3645: 
 3646: sub print_usercreation {
 3647:     my ($position,$dom,$settings,$rowtotal) = @_;
 3648:     my $numinrow = 4;
 3649:     my $datatable;
 3650:     if ($position eq 'top') {
 3651:         $$rowtotal ++;
 3652:         my $rowcount = 0;
 3653:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3654:         if (ref($rules) eq 'HASH') {
 3655:             if (keys(%{$rules}) > 0) {
 3656:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3657:                                                 $ruleorder,$numinrow,$rowcount);
 3658:                 $$rowtotal ++;
 3659:                 $rowcount ++;
 3660:             }
 3661:         }
 3662:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3663:         if (ref($idrules) eq 'HASH') {
 3664:             if (keys(%{$idrules}) > 0) {
 3665:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3666:                                                 $idruleorder,$numinrow,$rowcount);
 3667:                 $$rowtotal ++;
 3668:                 $rowcount ++;
 3669:             }
 3670:         }
 3671:         if ($rowcount == 0) {
 3672:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3673:             $$rowtotal ++;
 3674:             $rowcount ++;
 3675:         }
 3676:     } elsif ($position eq 'middle') {
 3677:         my @creators = ('author','course','requestcrs');
 3678:         my ($rules,$ruleorder) =
 3679:             &Apache::lonnet::inst_userrules($dom,'username');
 3680:         my %lt = &usercreation_types();
 3681:         my %checked;
 3682:         if (ref($settings) eq 'HASH') {
 3683:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3684:                 foreach my $item (@creators) {
 3685:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3686:                 }
 3687:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3688:                 foreach my $item (@creators) {
 3689:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3690:                         $checked{$item} = 'none';
 3691:                     }
 3692:                 }
 3693:             }
 3694:         }
 3695:         my $rownum = 0;
 3696:         foreach my $item (@creators) {
 3697:             $rownum ++;
 3698:             if ($checked{$item} eq '') {
 3699:                 $checked{$item} = 'any';
 3700:             }
 3701:             my $css_class;
 3702:             if ($rownum%2) {
 3703:                 $css_class = '';
 3704:             } else {
 3705:                 $css_class = ' class="LC_odd_row" ';
 3706:             }
 3707:             $datatable .= '<tr'.$css_class.'>'.
 3708:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3709:                          '</span></td><td align="right">';
 3710:             my @options = ('any');
 3711:             if (ref($rules) eq 'HASH') {
 3712:                 if (keys(%{$rules}) > 0) {
 3713:                     push(@options,('official','unofficial'));
 3714:                 }
 3715:             }
 3716:             push(@options,'none');
 3717:             foreach my $option (@options) {
 3718:                 my $type = 'radio';
 3719:                 my $check = ' ';
 3720:                 if ($checked{$item} eq $option) {
 3721:                     $check = ' checked="checked" ';
 3722:                 } 
 3723:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3724:                               '<input type="'.$type.'" name="can_createuser_'.
 3725:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3726:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3727:             }
 3728:             $datatable .= '</td></tr>';
 3729:         }
 3730:     } else {
 3731:         my @contexts = ('author','course','domain');
 3732:         my @authtypes = ('int','krb4','krb5','loc');
 3733:         my %checked;
 3734:         if (ref($settings) eq 'HASH') {
 3735:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3736:                 foreach my $item (@contexts) {
 3737:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3738:                         foreach my $auth (@authtypes) {
 3739:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3740:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3741:                             }
 3742:                         }
 3743:                     }
 3744:                 }
 3745:             }
 3746:         } else {
 3747:             foreach my $item (@contexts) {
 3748:                 foreach my $auth (@authtypes) {
 3749:                     $checked{$item}{$auth} = ' checked="checked" ';
 3750:                 }
 3751:             }
 3752:         }
 3753:         my %title = &context_names();
 3754:         my %authname = &authtype_names();
 3755:         my $rownum = 0;
 3756:         my $css_class; 
 3757:         foreach my $item (@contexts) {
 3758:             if ($rownum%2) {
 3759:                 $css_class = '';
 3760:             } else {
 3761:                 $css_class = ' class="LC_odd_row" ';
 3762:             }
 3763:             $datatable .=   '<tr'.$css_class.'>'.
 3764:                             '<td>'.$title{$item}.
 3765:                             '</td><td class="LC_left_item">'.
 3766:                             '<span class="LC_nobreak">';
 3767:             foreach my $auth (@authtypes) {
 3768:                 $datatable .= '<label>'. 
 3769:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3770:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3771:                               $authname{$auth}.'</label>&nbsp;';
 3772:             }
 3773:             $datatable .= '</span></td></tr>';
 3774:             $rownum ++;
 3775:         }
 3776:         $$rowtotal += $rownum;
 3777:     }
 3778:     return $datatable;
 3779: }
 3780: 
 3781: sub print_selfcreation {
 3782:     my ($position,$dom,$settings,$rowtotal) = @_;
 3783:     my (@selfcreate,$createsettings,$datatable);
 3784:     if (ref($settings) eq 'HASH') {
 3785:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 3786:             $createsettings = $settings->{'cancreate'};
 3787:             if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3788:                 @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3789:             } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3790:                 if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3791:                     @selfcreate = ('email','login','sso');
 3792:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3793:                     @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3794:                 }
 3795:             }
 3796:         }
 3797:     }
 3798:     my %radiohash;
 3799:     my $rownum = 0;
 3800:     my $numinrow = 4;
 3801:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 3802:     if ($position eq 'top') {
 3803:         my %choices = &Apache::lonlocal::texthash (
 3804:                                                       cancreate_login      => 'Institutional Login',
 3805:                                                       cancreate_sso        => 'Institutional Single Sign On',
 3806:                                                   );
 3807:         my @toggles = sort(keys(%choices));
 3808:         my %defaultchecked = (
 3809:                                'cancreate_login' => 'off',
 3810:                                'cancreate_sso'   => 'off',
 3811:                              );
 3812:         my $itemcount = 1;
 3813:         my $onclick;
 3814:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 3815:                                                      \%choices,$itemcount,$onclick);
 3816:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3817: 
 3818:         if (ref($usertypes) eq 'HASH') {
 3819:             if (keys(%{$usertypes}) > 0) {
 3820:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3821:                                              $dom,$numinrow,$othertitle,
 3822:                                              'statustocreate',$rownum);
 3823:                 $rownum ++;
 3824:                 $$rowtotal ++;
 3825:             }
 3826:         }
 3827:     } elsif ($position eq 'middle') {
 3828:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 3829:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3830:         $usertypes->{'default'} = $othertitle;
 3831:         if (ref($types) eq 'ARRAY') {
 3832:             push(@{$types},'default');
 3833:             $usertypes->{'default'} = $othertitle;
 3834:             foreach my $status (@{$types}) {
 3835:                 $rownum ++;
 3836:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 3837:                                                        $numinrow,$rownum,$usertypes);
 3838:                 $$rowtotal ++;
 3839:             }
 3840:         }
 3841:     } else {
 3842:         my $css_class = $rownum%2?' class="LC_odd_row"':'';
 3843:         my %choices =
 3844:             &Apache::lonlocal::texthash(
 3845:                                           email         => 'Approved automatically',
 3846:                                           emailapproval => 'Queued for approval by DC',
 3847:                                           off           => 'Not enabled',
 3848:                                        );
 3849:         $datatable .= '<tr'.$css_class.'>'.
 3850:                       '<td>'.&mt('E-mail address as username').
 3851:                       '</td><td class="LC_left_item">'.
 3852:                       '<span class="LC_nobreak">';
 3853:         foreach my $option ('email','emailapproval','off') {
 3854:             my $checked;
 3855:             if ($option eq 'email') {
 3856:                 if ($radiohash{'cancreate_email'}) {
 3857:                     $checked = 'checked="checked"';
 3858:                 }
 3859:             } elsif ($option eq 'emailapproval') {
 3860:                 if ($radiohash{'cancreate_emailapproval'}) {
 3861:                     $checked = 'checked="checked"';
 3862:                 }
 3863:             } else {
 3864:                 if ((!$radiohash{'cancreate_email'}) && (!$radiohash{'cancreate_emailapproval'})) {
 3865:                     $checked = 'checked="checked"';  
 3866:                 }
 3867:             }
 3868:             $datatable .= '<label>'.
 3869:                           '<input type="radio" name="cancreate_email" '.
 3870:                           $checked.' value="'.$option.'" />'.
 3871:                           $choices{$option}.'</label>&nbsp;';
 3872:         }
 3873:         $$rowtotal ++;
 3874:         $rownum ++;
 3875:         $datatable .= '</span></td></tr>'.
 3876:                       &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 3877:         $rownum ++;
 3878:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 3879:         $numinrow = 2;
 3880:         $datatable .= &modifiable_userdata_row('cancreate','emailusername',$settings,
 3881:                                                $numinrow,$rownum,undef,$infofields,$infotitles);
 3882:         $$rowtotal ++;
 3883:         $rownum ++;
 3884:         my ($emailrules,$emailruleorder) =
 3885:             &Apache::lonnet::inst_userrules($dom,'email');
 3886:         if (ref($emailrules) eq 'HASH') {
 3887:             if (keys(%{$emailrules}) > 0) {
 3888:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3889:                                                 $emailruleorder,$numinrow,$rownum);
 3890:                 $rownum ++;
 3891:                 $$rowtotal ++;
 3892:             }
 3893:         }
 3894:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3895:     }
 3896:     return $datatable;
 3897: }
 3898: 
 3899: sub captcha_choice {
 3900:     my ($context,$settings,$itemcount) = @_;
 3901:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3902:     my %lt = &captcha_phrases();
 3903:     $keyentry = 'hidden';
 3904:     if ($context eq 'cancreate') {
 3905:         $rowname = &mt('CAPTCHA validation');
 3906:     } elsif ($context eq 'login') {
 3907:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3908:     }
 3909:     if (ref($settings) eq 'HASH') {
 3910:         if ($settings->{'captcha'}) {
 3911:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3912:         } else {
 3913:             $checked{'original'} = ' checked="checked"';
 3914:         }
 3915:         if ($settings->{'captcha'} eq 'recaptcha') {
 3916:             $pubtext = $lt{'pub'};
 3917:             $privtext = $lt{'priv'};
 3918:             $keyentry = 'text';
 3919:         }
 3920:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3921:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3922:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3923:         }
 3924:     } else {
 3925:         $checked{'original'} = ' checked="checked"';
 3926:     }
 3927:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3928:     my $output = '<tr'.$css_class.'>'.
 3929:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3930:                  '<table><tr><td>'."\n";
 3931:     foreach my $option ('original','recaptcha','notused') {
 3932:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3933:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3934:                    $lt{$option}.'</label></span>';
 3935:         unless ($option eq 'notused') {
 3936:             $output .= ('&nbsp;'x2)."\n";
 3937:         }
 3938:     }
 3939: #
 3940: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3941: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3942: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 3943: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3944: #
 3945:     $output .= '</td></tr>'."\n".
 3946:                '<tr><td>'."\n".
 3947:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3948:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3949:                $currpub.'" size="40" /></span><br />'."\n".
 3950:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3951:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3952:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3953:                '</td></tr>';
 3954:     return $output;
 3955: }
 3956: 
 3957: sub user_formats_row {
 3958:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3959:     my $output;
 3960:     my %text = (
 3961:                    'username' => 'new usernames',
 3962:                    'id'       => 'IDs',
 3963:                    'email'    => 'self-created accounts (e-mail)',
 3964:                );
 3965:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3966:     $output = '<tr '.$css_class.'>'.
 3967:               '<td><span class="LC_nobreak">';
 3968:     if ($type eq 'email') {
 3969:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3970:     } else {
 3971:         $output .= &mt("Format rules to check for $text{$type}: ");
 3972:     }
 3973:     $output .= '</span></td>'.
 3974:                '<td class="LC_left_item" colspan="2"><table>';
 3975:     my $rem;
 3976:     if (ref($ruleorder) eq 'ARRAY') {
 3977:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3978:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3979:                 my $rem = $i%($numinrow);
 3980:                 if ($rem == 0) {
 3981:                     if ($i > 0) {
 3982:                         $output .= '</tr>';
 3983:                     }
 3984:                     $output .= '<tr>';
 3985:                 }
 3986:                 my $check = ' ';
 3987:                 if (ref($settings) eq 'HASH') {
 3988:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3989:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3990:                             $check = ' checked="checked" ';
 3991:                         }
 3992:                     }
 3993:                 }
 3994:                 $output .= '<td class="LC_left_item">'.
 3995:                            '<span class="LC_nobreak"><label>'.
 3996:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3997:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3998:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3999:             }
 4000:         }
 4001:         $rem = @{$ruleorder}%($numinrow);
 4002:     }
 4003:     my $colsleft = $numinrow - $rem;
 4004:     if ($colsleft > 1 ) {
 4005:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4006:                    '&nbsp;</td>';
 4007:     } elsif ($colsleft == 1) {
 4008:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4009:     }
 4010:     $output .= '</tr></table></td></tr>';
 4011:     return $output;
 4012: }
 4013: 
 4014: sub usercreation_types {
 4015:     my %lt = &Apache::lonlocal::texthash (
 4016:                     author     => 'When adding a co-author',
 4017:                     course     => 'When adding a user to a course',
 4018:                     requestcrs => 'When requesting a course',
 4019:                     any        => 'Any',
 4020:                     official   => 'Institutional only ',
 4021:                     unofficial => 'Non-institutional only',
 4022:                     none       => 'None',
 4023:     );
 4024:     return %lt;
 4025: }
 4026: 
 4027: sub selfcreation_types {
 4028:     my %lt = &Apache::lonlocal::texthash (
 4029:                     selfcreate => 'User creates own account',
 4030:                     any        => 'Any',
 4031:                     official   => 'Institutional only ',
 4032:                     unofficial => 'Non-institutional only',
 4033:                     email      => 'E-mail address',
 4034:                     login      => 'Institutional Login',
 4035:                     sso        => 'SSO',
 4036:              );
 4037: }
 4038: 
 4039: sub authtype_names {
 4040:     my %lt = &Apache::lonlocal::texthash(
 4041:                       int    => 'Internal',
 4042:                       krb4   => 'Kerberos 4',
 4043:                       krb5   => 'Kerberos 5',
 4044:                       loc    => 'Local',
 4045:                   );
 4046:     return %lt;
 4047: }
 4048: 
 4049: sub context_names {
 4050:     my %context_title = &Apache::lonlocal::texthash(
 4051:        author => 'Creating users when an Author',
 4052:        course => 'Creating users when in a course',
 4053:        domain => 'Creating users when a Domain Coordinator',
 4054:     );
 4055:     return %context_title;
 4056: }
 4057: 
 4058: sub print_usermodification {
 4059:     my ($position,$dom,$settings,$rowtotal) = @_;
 4060:     my $numinrow = 4;
 4061:     my ($context,$datatable,$rowcount);
 4062:     if ($position eq 'top') {
 4063:         $rowcount = 0;
 4064:         $context = 'author'; 
 4065:         foreach my $role ('ca','aa') {
 4066:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4067:                                                    $numinrow,$rowcount);
 4068:             $$rowtotal ++;
 4069:             $rowcount ++;
 4070:         }
 4071:     } elsif ($position eq 'middle') {
 4072:         $context = 'course';
 4073:         $rowcount = 0;
 4074:         foreach my $role ('st','ep','ta','in','cr') {
 4075:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4076:                                                    $numinrow,$rowcount);
 4077:             $$rowtotal ++;
 4078:             $rowcount ++;
 4079:         }
 4080:     }
 4081:     return $datatable;
 4082: }
 4083: 
 4084: sub print_defaults {
 4085:     my ($dom,$settings,$rowtotal) = @_;
 4086:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4087:                  'datelocale_def','portal_def');
 4088:     my %defaults;
 4089:     if (ref($settings) eq 'HASH') {
 4090:         %defaults = %{$settings};
 4091:     } else {
 4092:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4093:         foreach my $item (@items) {
 4094:             $defaults{$item} = $domdefaults{$item};
 4095:         }
 4096:     }
 4097:     my $titles = &defaults_titles($dom);
 4098:     my $rownum = 0;
 4099:     my ($datatable,$css_class);
 4100:     foreach my $item (@items) {
 4101:         if ($rownum%2) {
 4102:             $css_class = '';
 4103:         } else {
 4104:             $css_class = ' class="LC_odd_row" ';
 4105:         }
 4106:         $datatable .= '<tr'.$css_class.'>'.
 4107:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 4108:                   '</span></td><td class="LC_right_item">';
 4109:         if ($item eq 'auth_def') {
 4110:             my @authtypes = ('internal','krb4','krb5','localauth');
 4111:             my %shortauth = (
 4112:                              internal => 'int',
 4113:                              krb4 => 'krb4',
 4114:                              krb5 => 'krb5',
 4115:                              localauth  => 'loc'
 4116:                            );
 4117:             my %authnames = &authtype_names();
 4118:             foreach my $auth (@authtypes) {
 4119:                 my $checked = ' ';
 4120:                 if ($defaults{$item} eq $auth) {
 4121:                     $checked = ' checked="checked" ';
 4122:                 }
 4123:                 $datatable .= '<label><input type="radio" name="'.$item.
 4124:                               '" value="'.$auth.'"'.$checked.'/>'.
 4125:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4126:             }
 4127:         } elsif ($item eq 'timezone_def') {
 4128:             my $includeempty = 1;
 4129:             $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4130:         } elsif ($item eq 'datelocale_def') {
 4131:             my $includeempty = 1;
 4132:             $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4133:         } elsif ($item eq 'lang_def') {
 4134:             my %langchoices = &get_languages_hash();
 4135:             $langchoices{''} = 'No language preference';
 4136:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4137:             $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4138:                                                           \%langchoices);
 4139:         } else {
 4140:             my $size;
 4141:             if ($item eq 'portal_def') {
 4142:                 $size = ' size="25"';
 4143:             }
 4144:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 4145:                           $defaults{$item}.'"'.$size.' />';
 4146:         }
 4147:         $datatable .= '</td></tr>';
 4148:         $rownum ++;
 4149:     }
 4150:     $$rowtotal += $rownum;
 4151:     return $datatable;
 4152: }
 4153: 
 4154: sub get_languages_hash {
 4155:     my %langchoices;
 4156:     foreach my $id (&Apache::loncommon::languageids()) {
 4157:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4158:         if ($code ne '') {
 4159:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4160:         }
 4161:     }
 4162:     return %langchoices;
 4163: }
 4164: 
 4165: sub defaults_titles {
 4166:     my ($dom) = @_;
 4167:     my %titles = &Apache::lonlocal::texthash (
 4168:                    'auth_def'      => 'Default authentication type',
 4169:                    'auth_arg_def'  => 'Default authentication argument',
 4170:                    'lang_def'      => 'Default language',
 4171:                    'timezone_def'  => 'Default timezone',
 4172:                    'datelocale_def' => 'Default locale for dates',
 4173:                    'portal_def'     => 'Portal/Default URL',
 4174:                  );
 4175:     if ($dom) {
 4176:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4177:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4178:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4179:         $protocol = 'http' if ($protocol ne 'https');
 4180:         if ($uint_dom) {
 4181:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4182:                                          $uint_dom);
 4183:         }
 4184:     }
 4185:     return (\%titles);
 4186: }
 4187: 
 4188: sub print_scantronformat {
 4189:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4190:     my $itemcount = 1;
 4191:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4192:         %confhash);
 4193:     my $switchserver = &check_switchserver($dom,$confname);
 4194:     my %lt = &Apache::lonlocal::texthash (
 4195:                 default => 'Default bubblesheet format file error',
 4196:                 custom  => 'Custom bubblesheet format file error',
 4197:              );
 4198:     my %scantronfiles = (
 4199:         default => 'default.tab',
 4200:         custom => 'custom.tab',
 4201:     );
 4202:     foreach my $key (keys(%scantronfiles)) {
 4203:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4204:                               .$scantronfiles{$key};
 4205:     }
 4206:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4207:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4208:         if (!$switchserver) {
 4209:             my $servadm = $r->dir_config('lonAdmEMail');
 4210:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4211:             if ($configuserok eq 'ok') {
 4212:                 if ($author_ok eq 'ok') {
 4213:                     my %legacyfile = (
 4214:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4215:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4216:                     );
 4217:                     my %md5chk;
 4218:                     foreach my $type (keys(%legacyfile)) {
 4219:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4220:                         chomp($md5chk{$type});
 4221:                     }
 4222:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4223:                         foreach my $type (keys(%legacyfile)) {
 4224:                             ($scantronurls{$type},my $error) = 
 4225:                                 &legacy_scantronformat($r,$dom,$confname,
 4226:                                                  $type,$legacyfile{$type},
 4227:                                                  $scantronurls{$type},
 4228:                                                  $scantronfiles{$type});
 4229:                             if ($error ne '') {
 4230:                                 $error{$type} = $error;
 4231:                             }
 4232:                         }
 4233:                         if (keys(%error) == 0) {
 4234:                             $is_custom = 1;
 4235:                             $confhash{'scantron'}{'scantronformat'} = 
 4236:                                 $scantronurls{'custom'};
 4237:                             my $putresult = 
 4238:                                 &Apache::lonnet::put_dom('configuration',
 4239:                                                          \%confhash,$dom);
 4240:                             if ($putresult ne 'ok') {
 4241:                                 $error{'custom'} = 
 4242:                                     '<span class="LC_error">'.
 4243:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4244:                             }
 4245:                         }
 4246:                     } else {
 4247:                         ($scantronurls{'default'},my $error) =
 4248:                             &legacy_scantronformat($r,$dom,$confname,
 4249:                                           'default',$legacyfile{'default'},
 4250:                                           $scantronurls{'default'},
 4251:                                           $scantronfiles{'default'});
 4252:                         if ($error eq '') {
 4253:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4254:                             my $putresult =
 4255:                                 &Apache::lonnet::put_dom('configuration',
 4256:                                                          \%confhash,$dom);
 4257:                             if ($putresult ne 'ok') {
 4258:                                 $error{'default'} =
 4259:                                     '<span class="LC_error">'.
 4260:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4261:                             }
 4262:                         } else {
 4263:                             $error{'default'} = $error;
 4264:                         }
 4265:                     }
 4266:                 }
 4267:             }
 4268:         } else {
 4269:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4270:         }
 4271:     }
 4272:     if (ref($settings) eq 'HASH') {
 4273:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4274:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4275:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4276:                 $scantronurl = '';
 4277:             } else {
 4278:                 $scantronurl = $settings->{'scantronformat'};
 4279:             }
 4280:             $is_custom = 1;
 4281:         } else {
 4282:             $scantronurl = $scantronurls{'default'};
 4283:         }
 4284:     } else {
 4285:         if ($is_custom) {
 4286:             $scantronurl = $scantronurls{'custom'};
 4287:         } else {
 4288:             $scantronurl = $scantronurls{'default'};
 4289:         }
 4290:     }
 4291:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4292:     $datatable .= '<tr'.$css_class.'>';
 4293:     if (!$is_custom) {
 4294:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4295:                       '<span class="LC_nobreak">';
 4296:         if ($scantronurl) {
 4297:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4298:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4299:         } else {
 4300:             $datatable = &mt('File unavailable for display');
 4301:         }
 4302:         $datatable .= '</span></td>';
 4303:         if (keys(%error) == 0) { 
 4304:             $datatable .= '<td valign="bottom">';
 4305:             if (!$switchserver) {
 4306:                 $datatable .= &mt('Upload:').'<br />';
 4307:             }
 4308:         } else {
 4309:             my $errorstr;
 4310:             foreach my $key (sort(keys(%error))) {
 4311:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4312:             }
 4313:             $datatable .= '<td>'.$errorstr;
 4314:         }
 4315:     } else {
 4316:         if (keys(%error) > 0) {
 4317:             my $errorstr;
 4318:             foreach my $key (sort(keys(%error))) {
 4319:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4320:             } 
 4321:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4322:         } elsif ($scantronurl) {
 4323:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4324:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4325:             $datatable .= '<td><span class="LC_nobreak">'.
 4326:                           $link.
 4327:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4328:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4329:                           '<td><span class="LC_nobreak">&nbsp;'.
 4330:                           &mt('Replace:').'</span><br />';
 4331:         }
 4332:     }
 4333:     if (keys(%error) == 0) {
 4334:         if ($switchserver) {
 4335:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4336:         } else {
 4337:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4338:                          '<input type="file" name="scantronformat" /></span>';
 4339:         }
 4340:     }
 4341:     $datatable .= '</td></tr>';
 4342:     $$rowtotal ++;
 4343:     return $datatable;
 4344: }
 4345: 
 4346: sub legacy_scantronformat {
 4347:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4348:     my ($url,$error);
 4349:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4350:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4351:         (my $result,$url) =
 4352:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4353:                          '','',$newfile);
 4354:         if ($result ne 'ok') {
 4355:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4356:         }
 4357:     }
 4358:     return ($url,$error);
 4359: }
 4360: 
 4361: sub print_coursecategories {
 4362:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4363:     my $datatable;
 4364:     if ($position eq 'top') {
 4365:         my $toggle_cats_crs = ' ';
 4366:         my $toggle_cats_dom = ' checked="checked" ';
 4367:         my $can_cat_crs = ' ';
 4368:         my $can_cat_dom = ' checked="checked" ';
 4369:         my $toggle_catscomm_comm = ' ';
 4370:         my $toggle_catscomm_dom = ' checked="checked" ';
 4371:         my $can_catcomm_comm = ' ';
 4372:         my $can_catcomm_dom = ' checked="checked" ';
 4373: 
 4374:         if (ref($settings) eq 'HASH') {
 4375:             if ($settings->{'togglecats'} eq 'crs') {
 4376:                 $toggle_cats_crs = $toggle_cats_dom;
 4377:                 $toggle_cats_dom = ' ';
 4378:             }
 4379:             if ($settings->{'categorize'} eq 'crs') {
 4380:                 $can_cat_crs = $can_cat_dom;
 4381:                 $can_cat_dom = ' ';
 4382:             }
 4383:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4384:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4385:                 $toggle_catscomm_dom = ' ';
 4386:             }
 4387:             if ($settings->{'categorizecomm'} eq 'comm') {
 4388:                 $can_catcomm_comm = $can_catcomm_dom;
 4389:                 $can_catcomm_dom = ' ';
 4390:             }
 4391:         }
 4392:         my %title = &Apache::lonlocal::texthash (
 4393:                      togglecats     => 'Show/Hide a course in catalog',
 4394:                      togglecatscomm => 'Show/Hide a community in catalog',
 4395:                      categorize     => 'Assign a category to a course',
 4396:                      categorizecomm => 'Assign a category to a community',
 4397:                     );
 4398:         my %level = &Apache::lonlocal::texthash (
 4399:                      dom  => 'Set in Domain',
 4400:                      crs  => 'Set in Course',
 4401:                      comm => 'Set in Community',
 4402:                     );
 4403:         $datatable = '<tr class="LC_odd_row">'.
 4404:                   '<td>'.$title{'togglecats'}.'</td>'.
 4405:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4406:                   '<input type="radio" name="togglecats"'.
 4407:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4408:                   '<label><input type="radio" name="togglecats"'.
 4409:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4410:                   '</tr><tr>'.
 4411:                   '<td>'.$title{'categorize'}.'</td>'.
 4412:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4413:                   '<label><input type="radio" name="categorize"'.
 4414:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4415:                   '<label><input type="radio" name="categorize"'.
 4416:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4417:                   '</tr><tr class="LC_odd_row">'.
 4418:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4419:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4420:                   '<input type="radio" name="togglecatscomm"'.
 4421:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4422:                   '<label><input type="radio" name="togglecatscomm"'.
 4423:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4424:                   '</tr><tr>'.
 4425:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4426:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4427:                   '<label><input type="radio" name="categorizecomm"'.
 4428:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4429:                   '<label><input type="radio" name="categorizecomm"'.
 4430:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4431:                   '</tr>';
 4432:         $$rowtotal += 4;
 4433:     } else {
 4434:         my $css_class;
 4435:         my $itemcount = 1;
 4436:         my $cathash; 
 4437:         if (ref($settings) eq 'HASH') {
 4438:             $cathash = $settings->{'cats'};
 4439:         }
 4440:         if (ref($cathash) eq 'HASH') {
 4441:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4442:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4443:                                                    \%allitems,\%idx,\@jsarray);
 4444:             my $maxdepth = scalar(@cats);
 4445:             my $colattrib = '';
 4446:             if ($maxdepth > 2) {
 4447:                 $colattrib = ' colspan="2" ';
 4448:             }
 4449:             my @path;
 4450:             if (@cats > 0) {
 4451:                 if (ref($cats[0]) eq 'ARRAY') {
 4452:                     my $numtop = @{$cats[0]};
 4453:                     my $maxnum = $numtop;
 4454:                     my %default_names = (
 4455:                           instcode    => &mt('Official courses'),
 4456:                           communities => &mt('Communities'),
 4457:                     );
 4458: 
 4459:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4460:                         ($cathash->{'instcode::0'} eq '') ||
 4461:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4462:                         ($cathash->{'communities::0'} eq '')) {
 4463:                         $maxnum ++;
 4464:                     }
 4465:                     my $lastidx;
 4466:                     for (my $i=0; $i<$numtop; $i++) {
 4467:                         my $parent = $cats[0][$i];
 4468:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4469:                         my $item = &escape($parent).'::0';
 4470:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4471:                         $lastidx = $idx{$item};
 4472:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4473:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4474:                         for (my $k=0; $k<=$maxnum; $k++) {
 4475:                             my $vpos = $k+1;
 4476:                             my $selstr;
 4477:                             if ($k == $i) {
 4478:                                 $selstr = ' selected="selected" ';
 4479:                             }
 4480:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4481:                         }
 4482:                         $datatable .= '</select></span></td><td>';
 4483:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4484:                             $datatable .=  '<span class="LC_nobreak">'
 4485:                                            .$default_names{$parent}.'</span>';
 4486:                             if ($parent eq 'instcode') {
 4487:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4488:                                               .&mt('with institutional codes')
 4489:                                               .')</span></td><td'.$colattrib.'>';
 4490:                             } else {
 4491:                                 $datatable .= '<table><tr><td>';
 4492:                             }
 4493:                             $datatable .= '<span class="LC_nobreak">'
 4494:                                           .'<label><input type="radio" name="'
 4495:                                           .$parent.'" value="1" checked="checked" />'
 4496:                                           .&mt('Display').'</label>';
 4497:                             if ($parent eq 'instcode') {
 4498:                                 $datatable .= '&nbsp;';
 4499:                             } else {
 4500:                                 $datatable .= '</span></td></tr><tr><td>'
 4501:                                               .'<span class="LC_nobreak">';
 4502:                             }
 4503:                             $datatable .= '<label><input type="radio" name="'
 4504:                                           .$parent.'" value="0" />'
 4505:                                           .&mt('Do not display').'</label></span>';
 4506:                             if ($parent eq 'communities') {
 4507:                                 $datatable .= '</td></tr></table>';
 4508:                             }
 4509:                             $datatable .= '</td>';
 4510:                         } else {
 4511:                             $datatable .= $parent
 4512:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 4513:                                           .'<input type="checkbox" name="deletecategory" '
 4514:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4515:                         }
 4516:                         my $depth = 1;
 4517:                         push(@path,$parent);
 4518:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4519:                         pop(@path);
 4520:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4521:                         $itemcount ++;
 4522:                     }
 4523:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4524:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4525:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4526:                     for (my $k=0; $k<=$maxnum; $k++) {
 4527:                         my $vpos = $k+1;
 4528:                         my $selstr;
 4529:                         if ($k == $numtop) {
 4530:                             $selstr = ' selected="selected" ';
 4531:                         }
 4532:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4533:                     }
 4534:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4535:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4536:                                   .'</tr>'."\n";
 4537:                     $itemcount ++;
 4538:                     foreach my $default ('instcode','communities') {
 4539:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4540:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4541:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4542:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4543:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4544:                             for (my $k=0; $k<=$maxnum; $k++) {
 4545:                                 my $vpos = $k+1;
 4546:                                 my $selstr;
 4547:                                 if ($k == $maxnum) {
 4548:                                     $selstr = ' selected="selected" ';
 4549:                                 }
 4550:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4551:                             }
 4552:                             $datatable .= '</select></span></td>'.
 4553:                                           '<td><span class="LC_nobreak">'.
 4554:                                           $default_names{$default}.'</span>';
 4555:                             if ($default eq 'instcode') {
 4556:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4557:                                               .&mt('with institutional codes').')</span>';
 4558:                             }
 4559:                             $datatable .= '</td>'
 4560:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4561:                                           .&mt('Display').'</label>&nbsp;'
 4562:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4563:                                           .&mt('Do not display').'</label></span></td></tr>';
 4564:                         }
 4565:                     }
 4566:                 }
 4567:             } else {
 4568:                 $datatable .= &initialize_categories($itemcount);
 4569:             }
 4570:         } else {
 4571:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4572:                           .&initialize_categories($itemcount);
 4573:         }
 4574:         $$rowtotal += $itemcount;
 4575:     }
 4576:     return $datatable;
 4577: }
 4578: 
 4579: sub print_serverstatuses {
 4580:     my ($dom,$settings,$rowtotal) = @_;
 4581:     my $datatable;
 4582:     my @pages = &serverstatus_pages();
 4583:     my (%namedaccess,%machineaccess);
 4584:     foreach my $type (@pages) {
 4585:         $namedaccess{$type} = '';
 4586:         $machineaccess{$type}= '';
 4587:     }
 4588:     if (ref($settings) eq 'HASH') {
 4589:         foreach my $type (@pages) {
 4590:             if (exists($settings->{$type})) {
 4591:                 if (ref($settings->{$type}) eq 'HASH') {
 4592:                     foreach my $key (keys(%{$settings->{$type}})) {
 4593:                         if ($key eq 'namedusers') {
 4594:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4595:                         } elsif ($key eq 'machines') {
 4596:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4597:                         }
 4598:                     }
 4599:                 }
 4600:             }
 4601:         }
 4602:     }
 4603:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4604:     my $rownum = 0;
 4605:     my $css_class;
 4606:     foreach my $type (@pages) {
 4607:         $rownum ++;
 4608:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4609:         $datatable .= '<tr'.$css_class.'>'.
 4610:                       '<td><span class="LC_nobreak">'.
 4611:                       $titles->{$type}.'</span></td>'.
 4612:                       '<td class="LC_left_item">'.
 4613:                       '<input type="text" name="'.$type.'_namedusers" '.
 4614:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4615:                       '<td class="LC_right_item">'.
 4616:                       '<span class="LC_nobreak">'.
 4617:                       '<input type="text" name="'.$type.'_machines" '.
 4618:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4619:                       '</td></tr>'."\n";
 4620:     }
 4621:     $$rowtotal += $rownum;
 4622:     return $datatable;
 4623: }
 4624: 
 4625: sub serverstatus_pages {
 4626:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4627:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4628:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 4629:             'uniquecodes');
 4630: }
 4631: 
 4632: sub coursecategories_javascript {
 4633:     my ($settings) = @_;
 4634:     my ($output,$jstext,$cathash);
 4635:     if (ref($settings) eq 'HASH') {
 4636:         $cathash = $settings->{'cats'};
 4637:     }
 4638:     if (ref($cathash) eq 'HASH') {
 4639:         my (@cats,@jsarray,%idx);
 4640:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4641:         if (@jsarray > 0) {
 4642:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4643:             for (my $i=0; $i<@jsarray; $i++) {
 4644:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4645:                     my $catstr = join('","',@{$jsarray[$i]});
 4646:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4647:                 }
 4648:             }
 4649:         }
 4650:     } else {
 4651:         $jstext  = '    var categories = Array(1);'."\n".
 4652:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4653:     }
 4654:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4655:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4656:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4657:     $output = <<"ENDSCRIPT";
 4658: <script type="text/javascript">
 4659: // <![CDATA[
 4660: function reorderCats(form,parent,item,idx) {
 4661:     var changedVal;
 4662: $jstext
 4663:     var newpos = 'addcategory_pos';
 4664:     var current = new Array;
 4665:     if (parent == '') {
 4666:         var has_instcode = 0;
 4667:         var maxtop = categories[idx].length;
 4668:         for (var j=0; j<maxtop; j++) {
 4669:             if (categories[idx][j] == 'instcode::0') {
 4670:                 has_instcode == 1;
 4671:             }
 4672:         }
 4673:         if (has_instcode == 0) {
 4674:             categories[idx][maxtop] = 'instcode_pos';
 4675:         }
 4676:     } else {
 4677:         newpos += '_'+parent;
 4678:     }
 4679:     var maxh = 1 + categories[idx].length;
 4680:     var current = new Array;
 4681:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4682:     if (item == newpos) {
 4683:         changedVal = newitemVal;
 4684:     } else {
 4685:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4686:         current[newitemVal] = newpos;
 4687:     }
 4688:     for (var i=0; i<categories[idx].length; i++) {
 4689:         var elementName = categories[idx][i];
 4690:         if (elementName != item) {
 4691:             if (form.elements[elementName]) {
 4692:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4693:                 current[currVal] = elementName;
 4694:             }
 4695:         }
 4696:     }
 4697:     var oldVal;
 4698:     for (var j=0; j<maxh; j++) {
 4699:         if (current[j] == undefined) {
 4700:             oldVal = j;
 4701:         }
 4702:     }
 4703:     if (oldVal < changedVal) {
 4704:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4705:            var elementName = current[k];
 4706:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4707:         }
 4708:     } else {
 4709:         for (var k=changedVal; k<oldVal; k++) {
 4710:             var elementName = current[k];
 4711:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4712:         }
 4713:     }
 4714:     return;
 4715: }
 4716: 
 4717: function categoryCheck(form) {
 4718:     if (form.elements['addcategory_name'].value == 'instcode') {
 4719:         alert('$instcode_reserved\\n$choose_again');
 4720:         return false;
 4721:     }
 4722:     if (form.elements['addcategory_name'].value == 'communities') {
 4723:         alert('$communities_reserved\\n$choose_again');
 4724:         return false;
 4725:     }
 4726:     return true;
 4727: }
 4728: 
 4729: // ]]>
 4730: </script>
 4731: 
 4732: ENDSCRIPT
 4733:     return $output;
 4734: }
 4735: 
 4736: sub initialize_categories {
 4737:     my ($itemcount) = @_;
 4738:     my ($datatable,$css_class,$chgstr);
 4739:     my %default_names = (
 4740:                       instcode    => 'Official courses (with institutional codes)',
 4741:                       communities => 'Communities',
 4742:                         );
 4743:     my $select0 = ' selected="selected"';
 4744:     my $select1 = '';
 4745:     foreach my $default ('instcode','communities') {
 4746:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4747:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4748:         if ($default eq 'communities') {
 4749:             $select1 = $select0;
 4750:             $select0 = '';
 4751:         }
 4752:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4753:                      .'<select name="'.$default.'_pos">'
 4754:                      .'<option value="0"'.$select0.'>1</option>'
 4755:                      .'<option value="1"'.$select1.'>2</option>'
 4756:                      .'<option value="2">3</option></select>&nbsp;'
 4757:                      .$default_names{$default}
 4758:                      .'</span></td><td><span class="LC_nobreak">'
 4759:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4760:                      .&mt('Display').'</label>&nbsp;<label>'
 4761:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4762:                  .'</label></span></td></tr>';
 4763:         $itemcount ++;
 4764:     }
 4765:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4766:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4767:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4768:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4769:                   .'<option value="0">1</option>'
 4770:                   .'<option value="1">2</option>'
 4771:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4772:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4773:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4774:     return $datatable;
 4775: }
 4776: 
 4777: sub build_category_rows {
 4778:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4779:     my ($text,$name,$item,$chgstr);
 4780:     if (ref($cats) eq 'ARRAY') {
 4781:         my $maxdepth = scalar(@{$cats});
 4782:         if (ref($cats->[$depth]) eq 'HASH') {
 4783:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4784:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4785:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4786:                 $text .= '<td><table class="LC_data_table">';
 4787:                 my ($idxnum,$parent_name,$parent_item);
 4788:                 my $higher = $depth - 1;
 4789:                 if ($higher == 0) {
 4790:                     $parent_name = &escape($parent).'::'.$higher;
 4791:                 } else {
 4792:                     if (ref($path) eq 'ARRAY') {
 4793:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4794:                     }
 4795:                 }
 4796:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4797:                 for (my $j=0; $j<=$numchildren; $j++) {
 4798:                     if ($j < $numchildren) {
 4799:                         $name = $cats->[$depth]{$parent}[$j];
 4800:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4801:                         $idxnum = $idx->{$item};
 4802:                     } else {
 4803:                         $name = $parent_name;
 4804:                         $item = $parent_item;
 4805:                     }
 4806:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4807:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4808:                     for (my $i=0; $i<=$numchildren; $i++) {
 4809:                         my $vpos = $i+1;
 4810:                         my $selstr;
 4811:                         if ($j == $i) {
 4812:                             $selstr = ' selected="selected" ';
 4813:                         }
 4814:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4815:                     }
 4816:                     $text .= '</select>&nbsp;';
 4817:                     if ($j < $numchildren) {
 4818:                         my $deeper = $depth+1;
 4819:                         $text .= $name.'&nbsp;'
 4820:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4821:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4822:                         if(ref($path) eq 'ARRAY') {
 4823:                             push(@{$path},$name);
 4824:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4825:                             pop(@{$path});
 4826:                         }
 4827:                     } else {
 4828:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4829:                         if ($j == $numchildren) {
 4830:                             $text .= $name;
 4831:                         } else {
 4832:                             $text .= $item;
 4833:                         }
 4834:                         $text .= '" value="" />';
 4835:                     }
 4836:                     $text .= '</td></tr>';
 4837:                 }
 4838:                 $text .= '</table></td>';
 4839:             } else {
 4840:                 my $higher = $depth-1;
 4841:                 if ($higher == 0) {
 4842:                     $name = &escape($parent).'::'.$higher;
 4843:                 } else {
 4844:                     if (ref($path) eq 'ARRAY') {
 4845:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4846:                     }
 4847:                 }
 4848:                 my $colspan;
 4849:                 if ($parent ne 'instcode') {
 4850:                     $colspan = $maxdepth - $depth - 1;
 4851:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4852:                 }
 4853:             }
 4854:         }
 4855:     }
 4856:     return $text;
 4857: }
 4858: 
 4859: sub modifiable_userdata_row {
 4860:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 4861:     my $rolename;
 4862:     if ($context eq 'cancreate') {
 4863:         if ($role eq 'emailusername') {
 4864:             $rolename = &mt('Data user provides');
 4865:         }
 4866:     } elsif ($context eq 'selfcreate') {
 4867:         if (ref($usertypes) eq 'HASH') {
 4868:             $rolename = $usertypes->{$role};
 4869:         } else {
 4870:             $rolename = $role;
 4871:         }
 4872:     } else {
 4873:         if ($role eq 'cr') {
 4874:             $rolename = &mt('Custom role');
 4875:         } else {
 4876:             $rolename = &Apache::lonnet::plaintext($role);
 4877:         }
 4878:     }
 4879:     my (@fields,%fieldtitles);
 4880:     if (ref($fieldsref) eq 'ARRAY') {
 4881:         @fields = @{$fieldsref};
 4882:     } else {
 4883:         @fields = ('lastname','firstname','middlename','generation',
 4884:                    'permanentemail','id');
 4885:     }
 4886:     if ((ref($titlesref) eq 'HASH')) {
 4887:         %fieldtitles = %{$titlesref};
 4888:     } else {
 4889:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4890:     }
 4891:     my $output;
 4892:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4893:     $output = '<tr '.$css_class.'>'.
 4894:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4895:               '<td class="LC_left_item" colspan="2"><table>';
 4896:     my $rem;
 4897:     my %checks;
 4898:     if (ref($settings) eq 'HASH') {
 4899:         if (ref($settings->{$context}) eq 'HASH') {
 4900:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4901:                 foreach my $field (@fields) {
 4902:                     if ($settings->{$context}->{$role}->{$field}) {
 4903:                         $checks{$field} = ' checked="checked" ';
 4904:                     }
 4905:                 }
 4906:             }
 4907:         }
 4908:     }
 4909:     for (my $i=0; $i<@fields; $i++) {
 4910:         my $rem = $i%($numinrow);
 4911:         if ($rem == 0) {
 4912:             if ($i > 0) {
 4913:                 $output .= '</tr>';
 4914:             }
 4915:             $output .= '<tr>';
 4916:         }
 4917:         my $check = ' ';
 4918:         if (exists($checks{$fields[$i]})) {
 4919:             $check = $checks{$fields[$i]}
 4920:         } else {
 4921:             if ($role eq 'st') {
 4922:                 if (ref($settings) ne 'HASH') {
 4923:                     $check = ' checked="checked" '; 
 4924:                 }
 4925:             }
 4926:         }
 4927:         $output .= '<td class="LC_left_item">'.
 4928:                    '<span class="LC_nobreak"><label>'.
 4929:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4930:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4931:                    '</label></span></td>';
 4932:         $rem = @fields%($numinrow);
 4933:     }
 4934:     my $colsleft = $numinrow - $rem;
 4935:     if ($colsleft > 1 ) {
 4936:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4937:                    '&nbsp;</td>';
 4938:     } elsif ($colsleft == 1) {
 4939:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4940:     }
 4941:     $output .= '</tr></table></td></tr>';
 4942:     return $output;
 4943: }
 4944: 
 4945: sub insttypes_row {
 4946:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 4947:     my %lt = &Apache::lonlocal::texthash (
 4948:                       cansearch => 'Users allowed to search',
 4949:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4950:                       lockablenames => 'User preference to lock name',
 4951:              );
 4952:     my $showdom;
 4953:     if ($context eq 'cansearch') {
 4954:         $showdom = ' ('.$dom.')';
 4955:     }
 4956:     my $class = 'LC_left_item';
 4957:     if ($context eq 'statustocreate') {
 4958:         $class = 'LC_right_item';
 4959:     }
 4960:     my $css_class = ' class="LC_odd_row"';
 4961:     if ($rownum ne '') { 
 4962:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 4963:     }
 4964:     my $output = '<tr'.$css_class.'>'.
 4965:                  '<td>'.$lt{$context}.$showdom.
 4966:                  '</td><td class="'.$class.'" colspan="2"><table>';
 4967:     my $rem;
 4968:     if (ref($types) eq 'ARRAY') {
 4969:         for (my $i=0; $i<@{$types}; $i++) {
 4970:             if (defined($usertypes->{$types->[$i]})) {
 4971:                 my $rem = $i%($numinrow);
 4972:                 if ($rem == 0) {
 4973:                     if ($i > 0) {
 4974:                         $output .= '</tr>';
 4975:                     }
 4976:                     $output .= '<tr>';
 4977:                 }
 4978:                 my $check = ' ';
 4979:                 if (ref($settings) eq 'HASH') {
 4980:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4981:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4982:                             $check = ' checked="checked" ';
 4983:                         }
 4984:                     } elsif ($context eq 'statustocreate') {
 4985:                         $check = ' checked="checked" ';
 4986:                     }
 4987:                 }
 4988:                 $output .= '<td class="LC_left_item">'.
 4989:                            '<span class="LC_nobreak"><label>'.
 4990:                            '<input type="checkbox" name="'.$context.'" '.
 4991:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4992:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4993:             }
 4994:         }
 4995:         $rem = @{$types}%($numinrow);
 4996:     }
 4997:     my $colsleft = $numinrow - $rem;
 4998:     if (($rem == 0) && (@{$types} > 0)) {
 4999:         $output .= '<tr>';
 5000:     }
 5001:     if ($colsleft > 1) {
 5002:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5003:     } else {
 5004:         $output .= '<td class="LC_left_item">';
 5005:     }
 5006:     my $defcheck = ' ';
 5007:     if (ref($settings) eq 'HASH') {  
 5008:         if (ref($settings->{$context}) eq 'ARRAY') {
 5009:             if (grep(/^default$/,@{$settings->{$context}})) {
 5010:                 $defcheck = ' checked="checked" ';
 5011:             }
 5012:         } elsif ($context eq 'statustocreate') {
 5013:             $defcheck = ' checked="checked" ';
 5014:         }
 5015:     }
 5016:     $output .= '<span class="LC_nobreak"><label>'.
 5017:                '<input type="checkbox" name="'.$context.'" '.
 5018:                'value="default"'.$defcheck.'/>'.
 5019:                $othertitle.'</label></span></td>'.
 5020:                '</tr></table></td></tr>';
 5021:     return $output;
 5022: }
 5023: 
 5024: sub sorted_searchtitles {
 5025:     my %searchtitles = &Apache::lonlocal::texthash(
 5026:                          'uname' => 'username',
 5027:                          'lastname' => 'last name',
 5028:                          'lastfirst' => 'last name, first name',
 5029:                      );
 5030:     my @titleorder = ('uname','lastname','lastfirst');
 5031:     return (\%searchtitles,\@titleorder);
 5032: }
 5033: 
 5034: sub sorted_searchtypes {
 5035:     my %srchtypes_desc = (
 5036:                            exact    => 'is exact match',
 5037:                            contains => 'contains ..',
 5038:                            begins   => 'begins with ..',
 5039:                          );
 5040:     my @srchtypeorder = ('exact','begins','contains');
 5041:     return (\%srchtypes_desc,\@srchtypeorder);
 5042: }
 5043: 
 5044: sub usertype_update_row {
 5045:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5046:     my $datatable;
 5047:     my $numinrow = 4;
 5048:     foreach my $type (@{$types}) {
 5049:         if (defined($usertypes->{$type})) {
 5050:             $$rownums ++;
 5051:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5052:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5053:                           '</td><td class="LC_left_item"><table>';
 5054:             for (my $i=0; $i<@{$fields}; $i++) {
 5055:                 my $rem = $i%($numinrow);
 5056:                 if ($rem == 0) {
 5057:                     if ($i > 0) {
 5058:                         $datatable .= '</tr>';
 5059:                     }
 5060:                     $datatable .= '<tr>';
 5061:                 }
 5062:                 my $check = ' ';
 5063:                 if (ref($settings) eq 'HASH') {
 5064:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5065:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5066:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5067:                                 $check = ' checked="checked" ';
 5068:                             }
 5069:                         }
 5070:                     }
 5071:                 }
 5072: 
 5073:                 if ($i == @{$fields}-1) {
 5074:                     my $colsleft = $numinrow - $rem;
 5075:                     if ($colsleft > 1) {
 5076:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5077:                     } else {
 5078:                         $datatable .= '<td>';
 5079:                     }
 5080:                 } else {
 5081:                     $datatable .= '<td>';
 5082:                 }
 5083:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5084:                               '<input type="checkbox" name="updateable_'.$type.
 5085:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5086:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5087:             }
 5088:             $datatable .= '</tr></table></td></tr>';
 5089:         }
 5090:     }
 5091:     return $datatable;
 5092: }
 5093: 
 5094: sub modify_login {
 5095:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5096:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5097:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5098:     %title = ( coursecatalog => 'Display course catalog',
 5099:                adminmail => 'Display administrator E-mail address',
 5100:                helpdesk  => 'Display "Contact Helpdesk" link',
 5101:                newuser => 'Link for visitors to create a user account',
 5102:                loginheader => 'Log-in box header');
 5103:     @offon = ('off','on');
 5104:     if (ref($domconfig{login}) eq 'HASH') {
 5105:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5106:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5107:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5108:             }
 5109:         }
 5110:     }
 5111:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5112:                                            \%domconfig,\%loginhash);
 5113:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5114:     foreach my $item (@toggles) {
 5115:         $loginhash{login}{$item} = $env{'form.'.$item};
 5116:     }
 5117:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5118:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5119:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5120:                                          \%loginhash);
 5121:     }
 5122: 
 5123:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5124:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5125:     if (keys(%servers) > 1) {
 5126:         foreach my $lonhost (keys(%servers)) {
 5127:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5128:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5129:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5130:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5131:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5132:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5133:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5134:                         $changes{'loginvia'}{$lonhost} = 1;
 5135:                     } else {
 5136:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5137:                         $changes{'loginvia'}{$lonhost} = 1;
 5138:                     }
 5139:                 } else {
 5140:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5141:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5142:                         $changes{'loginvia'}{$lonhost} = 1;
 5143:                     }
 5144:                 }
 5145:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5146:                     foreach my $item (@loginvia_attribs) {
 5147:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5148:                     }
 5149:                 } else {
 5150:                     foreach my $item (@loginvia_attribs) {
 5151:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5152:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5153:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5154:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5155:                                 $new = '/';
 5156:                             }
 5157:                         }
 5158:                         if (($item eq 'custompath') && 
 5159:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5160:                             $new = '';
 5161:                         }
 5162:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5163:                             $changes{'loginvia'}{$lonhost} = 1;
 5164:                         }
 5165:                         if ($item eq 'exempt') {
 5166:                             $new =~ s/^\s+//;
 5167:                             $new =~ s/\s+$//;
 5168:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5169:                             my @okips;
 5170:                             foreach my $ip (@poss_ips) {
 5171:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5172:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5173:                                         push(@okips,$ip); 
 5174:                                     }
 5175:                                 }
 5176:                             }
 5177:                             if (@okips > 0) {
 5178:                                 $new = join(',',@okips); 
 5179:                             } else {
 5180:                                 $new = ''; 
 5181:                             }
 5182:                         }
 5183:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5184:                     }
 5185:                 }
 5186:             } else {
 5187:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5188:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5189:                     $changes{'loginvia'}{$lonhost} = 1;
 5190:                     foreach my $item (@loginvia_attribs) {
 5191:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5192:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5193:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5194:                                 $new = '/';
 5195:                             }
 5196:                         }
 5197:                         if (($item eq 'custompath') && 
 5198:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5199:                             $new = '';
 5200:                         }
 5201:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5202:                     }
 5203:                 }
 5204:             }
 5205:         }
 5206:     }
 5207: 
 5208:     my $servadm = $r->dir_config('lonAdmEMail');
 5209:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5210:     if (ref($domconfig{'login'}) eq 'HASH') {
 5211:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5212:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5213:                 if ($lang eq 'nolang') {
 5214:                     push(@currlangs,$lang);
 5215:                 } elsif (defined($langchoices{$lang})) {
 5216:                     push(@currlangs,$lang);
 5217:                 } else {
 5218:                     next;
 5219:                 }
 5220:             }
 5221:         }
 5222:     }
 5223:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5224:     if (@currlangs > 0) {
 5225:         foreach my $lang (@currlangs) {
 5226:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5227:                 $changes{'helpurl'}{$lang} = 1;
 5228:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5229:                 $changes{'helpurl'}{$lang} = 1;
 5230:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5231:                 push(@newlangs,$lang);
 5232:             } else {
 5233:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5234:             }
 5235:         }
 5236:     }
 5237:     unless (grep(/^nolang$/,@currlangs)) {
 5238:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5239:             $changes{'helpurl'}{'nolang'} = 1;
 5240:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5241:             push(@newlangs,'nolang');
 5242:         }
 5243:     }
 5244:     if ($env{'form.loginhelpurl_add_lang'}) {
 5245:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5246:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5247:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5248:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5249:         }
 5250:     }
 5251:     if ((@newlangs > 0) || ($addedfile)) {
 5252:         my $error;
 5253:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5254:         if ($configuserok eq 'ok') {
 5255:             if ($switchserver) {
 5256:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5257:             } elsif ($author_ok eq 'ok') {
 5258:                 my @allnew = @newlangs;
 5259:                 if ($addedfile ne '') {
 5260:                     push(@allnew,$addedfile);
 5261:                 }
 5262:                 foreach my $lang (@allnew) {
 5263:                     my $formelem = 'loginhelpurl_'.$lang;
 5264:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5265:                         $formelem = 'loginhelpurl_add_file';
 5266:                     }
 5267:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5268:                                                                "help/$lang",'','',$newfile{$lang});
 5269:                     if ($result eq 'ok') {
 5270:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5271:                         $changes{'helpurl'}{$lang} = 1;
 5272:                     } else {
 5273:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5274:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5275:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5276:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5277: 
 5278:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5279:                         }
 5280:                     }
 5281:                 }
 5282:             } else {
 5283:                 $error = &mt("Upload of custom log-in help file(s) failed because an author role could not be assigned to a Domain Configuration user ([_1]) in domain: [_2].  Error was: [_3].",$confname,$dom,$author_ok);
 5284:             }
 5285:         } else {
 5286:             $error = &mt("Upload of custom log-in help file(s) failed because a Domain Configuration user ([_1]) could not be created in domain: [_2].  Error was: [_3].",$confname,$dom,$configuserok);
 5287:         }
 5288:         if ($error) {
 5289:             &Apache::lonnet::logthis($error);
 5290:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5291:         }
 5292:     }
 5293:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5294: 
 5295:     my $defaulthelpfile = '/adm/loginproblems.html';
 5296:     my $defaulttext = &mt('Default in use');
 5297: 
 5298:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5299:                                              $dom);
 5300:     if ($putresult eq 'ok') {
 5301:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5302:         my %defaultchecked = (
 5303:                     'coursecatalog' => 'on',
 5304:                     'helpdesk'      => 'on',
 5305:                     'adminmail'     => 'off',
 5306:                     'newuser'       => 'off',
 5307:         );
 5308:         if (ref($domconfig{'login'}) eq 'HASH') {
 5309:             foreach my $item (@toggles) {
 5310:                 if ($defaultchecked{$item} eq 'on') { 
 5311:                     if (($domconfig{'login'}{$item} eq '0') &&
 5312:                         ($env{'form.'.$item} eq '1')) {
 5313:                         $changes{$item} = 1;
 5314:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5315:                               $domconfig{'login'}{$item} eq '1') &&
 5316:                              ($env{'form.'.$item} eq '0')) {
 5317:                         $changes{$item} = 1;
 5318:                     }
 5319:                 } elsif ($defaultchecked{$item} eq 'off') {
 5320:                     if (($domconfig{'login'}{$item} eq '1') &&
 5321:                         ($env{'form.'.$item} eq '0')) {
 5322:                         $changes{$item} = 1;
 5323:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5324:                               $domconfig{'login'}{$item} eq '0') &&
 5325:                              ($env{'form.'.$item} eq '1')) {
 5326:                         $changes{$item} = 1;
 5327:                     }
 5328:                 }
 5329:             }
 5330:         }
 5331:         if (keys(%changes) > 0 || $colchgtext) {
 5332:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5333:             if (ref($lastactref) eq 'HASH') {
 5334:                 $lastactref->{'domainconfig'} = 1;
 5335:             }
 5336:             $resulttext = &mt('Changes made:').'<ul>';
 5337:             foreach my $item (sort(keys(%changes))) {
 5338:                 if ($item eq 'loginvia') {
 5339:                     if (ref($changes{$item}) eq 'HASH') {
 5340:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5341:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5342:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5343:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5344:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5345:                                     $protocol = 'http' if ($protocol ne 'https');
 5346:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5347: 
 5348:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5349:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5350:                                     } else {
 5351:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5352:                                     }
 5353:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5354:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5355:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5356:                                     }
 5357:                                     $resulttext .= '</li>';
 5358:                                 } else {
 5359:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5360:                                 }
 5361:                             } else {
 5362:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5363:                             }
 5364:                         }
 5365:                         $resulttext .= '</ul></li>';
 5366:                     }
 5367:                 } elsif ($item eq 'helpurl') {
 5368:                     if (ref($changes{$item}) eq 'HASH') {
 5369:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5370:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5371:                                 my ($chg,$link);
 5372:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5373:                                 if ($lang eq 'nolang') {
 5374:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5375:                                 } else {
 5376:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5377:                                 }
 5378:                                 $resulttext .= '<li>'.$chg.'</li>';
 5379:                             } else {
 5380:                                 my $chg;
 5381:                                 if ($lang eq 'nolang') {
 5382:                                     $chg = &mt('custom log-in help file for no preferred language');
 5383:                                 } else {
 5384:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 5385:                                 }
 5386:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 5387:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 5388:                                                       '?inhibitmenu=yes',$chg,600,500).
 5389:                                                '</li>';
 5390:                             }
 5391:                         }
 5392:                     }
 5393:                 } elsif ($item eq 'captcha') {
 5394:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5395:                         my $chgtxt;
 5396:                         if ($loginhash{'login'}{$item} eq 'notused') {
 5397:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 5398:                         } else {
 5399:                             my %captchas = &captcha_phrases();
 5400:                             if ($captchas{$loginhash{'login'}{$item}}) {
 5401:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 5402:                             } else {
 5403:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 5404:                             }
 5405:                         }
 5406:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5407:                     }
 5408:                 } elsif ($item eq 'recaptchakeys') {
 5409:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5410:                         my ($privkey,$pubkey);
 5411:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 5412:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 5413:                             $privkey = $loginhash{'login'}{$item}{'private'};
 5414:                         }
 5415:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 5416:                         if (!$pubkey) {
 5417:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 5418:                         } else {
 5419:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 5420:                         }
 5421:                         if (!$privkey) {
 5422:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 5423:                         } else {
 5424:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 5425:                         }
 5426:                         $chgtxt .= '</ul>';
 5427:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5428:                     }
 5429:                 } else {
 5430:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 5431:                 }
 5432:             }
 5433:             $resulttext .= $colchgtext.'</ul>';
 5434:         } else {
 5435:             $resulttext = &mt('No changes made to log-in page settings');
 5436:         }
 5437:     } else {
 5438:         $resulttext = '<span class="LC_error">'.
 5439: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5440:     }
 5441:     if ($errors) {
 5442:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 5443:                        $errors.'</ul>';
 5444:     }
 5445:     return $resulttext;
 5446: }
 5447: 
 5448: sub color_font_choices {
 5449:     my %choices =
 5450:         &Apache::lonlocal::texthash (
 5451:             img => "Header",
 5452:             bgs => "Background colors",
 5453:             links => "Link colors",
 5454:             images => "Images",
 5455:             font => "Font color",
 5456:             fontmenu => "Font menu",
 5457:             pgbg => "Page",
 5458:             tabbg => "Header",
 5459:             sidebg => "Border",
 5460:             link => "Link",
 5461:             alink => "Active link",
 5462:             vlink => "Visited link",
 5463:         );
 5464:     return %choices;
 5465: }
 5466: 
 5467: sub modify_rolecolors {
 5468:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 5469:     my ($resulttext,%rolehash);
 5470:     $rolehash{'rolecolors'} = {};
 5471:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5472:         if ($domconfig{'rolecolors'} eq '') {
 5473:             $domconfig{'rolecolors'} = {};
 5474:         }
 5475:     }
 5476:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5477:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5478:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5479:                                              $dom);
 5480:     if ($putresult eq 'ok') {
 5481:         if (keys(%changes) > 0) {
 5482:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5483:             if (ref($lastactref) eq 'HASH') {
 5484:                 $lastactref->{'domainconfig'} = 1;
 5485:             }
 5486:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5487:                                              $rolehash{'rolecolors'});
 5488:         } else {
 5489:             $resulttext = &mt('No changes made to default color schemes');
 5490:         }
 5491:     } else {
 5492:         $resulttext = '<span class="LC_error">'.
 5493: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5494:     }
 5495:     if ($errors) {
 5496:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5497:                        $errors.'</ul>';
 5498:     }
 5499:     return $resulttext;
 5500: }
 5501: 
 5502: sub modify_colors {
 5503:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5504:     my (%changes,%choices);
 5505:     my @bgs;
 5506:     my @links = ('link','alink','vlink');
 5507:     my @logintext;
 5508:     my @images;
 5509:     my $servadm = $r->dir_config('lonAdmEMail');
 5510:     my $errors;
 5511:     my %defaults;
 5512:     foreach my $role (@{$roles}) {
 5513:         if ($role eq 'login') {
 5514:             %choices = &login_choices();
 5515:             @logintext = ('textcol','bgcol');
 5516:         } else {
 5517:             %choices = &color_font_choices();
 5518:         }
 5519:         if ($role eq 'login') {
 5520:             @images = ('img','logo','domlogo','login');
 5521:             @bgs = ('pgbg','mainbg','sidebg');
 5522:         } else {
 5523:             @images = ('img');
 5524:             @bgs = ('pgbg','tabbg','sidebg');
 5525:         }
 5526:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 5527:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 5528:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5529:         }
 5530:         if ($role eq 'login') {
 5531:             foreach my $item (@logintext) {
 5532:                 unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'logintext'}{$item}) {
 5533:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5534:                 }
 5535:             }
 5536:         } else {
 5537:             unless($env{'form.'.$role.'_fontmenu'} eq $defaults{'fontmenu'}) {
 5538:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5539:             }
 5540:         }
 5541:         foreach my $item (@bgs) {
 5542:             unless ($env{'form.'.$role.'_'.$item} eq $defaults{'bgs'}{$item} ) {
 5543:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5544:             }
 5545:         }
 5546:         foreach my $item (@links) {
 5547:             unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'links'}{$item}) {
 5548:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5549:             }
 5550:         }
 5551:         my ($configuserok,$author_ok,$switchserver) = 
 5552:             &config_check($dom,$confname,$servadm);
 5553:         my ($width,$height) = &thumb_dimensions();
 5554:         if (ref($domconfig->{$role}) ne 'HASH') {
 5555:             $domconfig->{$role} = {};
 5556:         }
 5557:         foreach my $img (@images) {
 5558:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5559:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5560:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5561:                 } else { 
 5562:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5563:                 }
 5564:             } 
 5565: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5566: 		 && !defined($domconfig->{$role}{$img})
 5567: 		 && !$env{'form.'.$role.'_del_'.$img}
 5568: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5569: 		# import the old configured image from the .tab setting
 5570: 		# if they haven't provided a new one 
 5571: 		$domconfig->{$role}{$img} = 
 5572: 		    $env{'form.'.$role.'_import_'.$img};
 5573: 	    }
 5574:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5575:                 my $error;
 5576:                 if ($configuserok eq 'ok') {
 5577:                     if ($switchserver) {
 5578:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5579:                     } else {
 5580:                         if ($author_ok eq 'ok') {
 5581:                             my ($result,$logourl) = 
 5582:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5583:                                            $dom,$confname,$img,$width,$height);
 5584:                             if ($result eq 'ok') {
 5585:                                 $confhash->{$role}{$img} = $logourl;
 5586:                                 $changes{$role}{'images'}{$img} = 1;
 5587:                             } else {
 5588:                                 $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);
 5589:                             }
 5590:                         } else {
 5591:                             $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);
 5592:                         }
 5593:                     }
 5594:                 } else {
 5595:                     $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);
 5596:                 }
 5597:                 if ($error) {
 5598:                     &Apache::lonnet::logthis($error);
 5599:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5600:                 }
 5601:             } elsif ($domconfig->{$role}{$img} ne '') {
 5602:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5603:                     my $error;
 5604:                     if ($configuserok eq 'ok') {
 5605: # is confname an author?
 5606:                         if ($switchserver eq '') {
 5607:                             if ($author_ok eq 'ok') {
 5608:                                 my ($result,$logourl) = 
 5609:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5610:                                             $dom,$confname,$img,$width,$height);
 5611:                                 if ($result eq 'ok') {
 5612:                                     $confhash->{$role}{$img} = $logourl;
 5613: 				    $changes{$role}{'images'}{$img} = 1;
 5614:                                 }
 5615:                             }
 5616:                         }
 5617:                     }
 5618:                 }
 5619:             }
 5620:         }
 5621:         if (ref($domconfig) eq 'HASH') {
 5622:             if (ref($domconfig->{$role}) eq 'HASH') {
 5623:                 foreach my $img (@images) {
 5624:                     if ($domconfig->{$role}{$img} ne '') {
 5625:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5626:                             $confhash->{$role}{$img} = '';
 5627:                             $changes{$role}{'images'}{$img} = 1;
 5628:                         } else {
 5629:                             if ($confhash->{$role}{$img} eq '') {
 5630:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5631:                             }
 5632:                         }
 5633:                     } else {
 5634:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5635:                             $confhash->{$role}{$img} = '';
 5636:                             $changes{$role}{'images'}{$img} = 1;
 5637:                         } 
 5638:                     }
 5639:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5640:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5641:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5642:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5643:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5644:                             }
 5645:                         } else {
 5646:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5647:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5648:                             }
 5649:                         }
 5650:                     }
 5651:                 }
 5652:                 if ($domconfig->{$role}{'font'} ne '') {
 5653:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5654:                         $changes{$role}{'font'} = 1;
 5655:                     }
 5656:                 } else {
 5657:                     if ($confhash->{$role}{'font'}) {
 5658:                         $changes{$role}{'font'} = 1;
 5659:                     }
 5660:                 }
 5661:                 if ($role ne 'login') {
 5662:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5663:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5664:                             $changes{$role}{'fontmenu'} = 1;
 5665:                         }
 5666:                     } else {
 5667:                         if ($confhash->{$role}{'fontmenu'}) {
 5668:                             $changes{$role}{'fontmenu'} = 1;
 5669:                         }
 5670:                     }
 5671:                 }
 5672:                 foreach my $item (@bgs) {
 5673:                     if ($domconfig->{$role}{$item} ne '') {
 5674:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5675:                             $changes{$role}{'bgs'}{$item} = 1;
 5676:                         } 
 5677:                     } else {
 5678:                         if ($confhash->{$role}{$item}) {
 5679:                             $changes{$role}{'bgs'}{$item} = 1;
 5680:                         }
 5681:                     }
 5682:                 }
 5683:                 foreach my $item (@links) {
 5684:                     if ($domconfig->{$role}{$item} ne '') {
 5685:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5686:                             $changes{$role}{'links'}{$item} = 1;
 5687:                         }
 5688:                     } else {
 5689:                         if ($confhash->{$role}{$item}) {
 5690:                             $changes{$role}{'links'}{$item} = 1;
 5691:                         }
 5692:                     }
 5693:                 }
 5694:                 foreach my $item (@logintext) {
 5695:                     if ($domconfig->{$role}{$item} ne '') {
 5696:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5697:                             $changes{$role}{'logintext'}{$item} = 1;
 5698:                         }
 5699:                     } else {
 5700:                         if ($confhash->{$role}{$item}) {
 5701:                             $changes{$role}{'logintext'}{$item} = 1;
 5702:                         }
 5703:                     }
 5704:                 }
 5705:             } else {
 5706:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5707:                                         \@logintext,$confhash,\%changes); 
 5708:             }
 5709:         } else {
 5710:             &default_change_checker($role,\@images,\@links,\@bgs,
 5711:                                     \@logintext,$confhash,\%changes); 
 5712:         }
 5713:     }
 5714:     return ($errors,%changes);
 5715: }
 5716: 
 5717: sub config_check {
 5718:     my ($dom,$confname,$servadm) = @_;
 5719:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5720:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5721:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5722:                                                    $confname,$servadm);
 5723:     if ($configuserok eq 'ok') {
 5724:         $switchserver = &check_switchserver($dom,$confname);
 5725:         if ($switchserver eq '') {
 5726:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5727:         }
 5728:     }
 5729:     return ($configuserok,$author_ok,$switchserver);
 5730: }
 5731: 
 5732: sub default_change_checker {
 5733:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5734:     foreach my $item (@{$links}) {
 5735:         if ($confhash->{$role}{$item}) {
 5736:             $changes->{$role}{'links'}{$item} = 1;
 5737:         }
 5738:     }
 5739:     foreach my $item (@{$bgs}) {
 5740:         if ($confhash->{$role}{$item}) {
 5741:             $changes->{$role}{'bgs'}{$item} = 1;
 5742:         }
 5743:     }
 5744:     foreach my $item (@{$logintext}) {
 5745:         if ($confhash->{$role}{$item}) {
 5746:             $changes->{$role}{'logintext'}{$item} = 1;
 5747:         }
 5748:     }
 5749:     foreach my $img (@{$images}) {
 5750:         if ($env{'form.'.$role.'_del_'.$img}) {
 5751:             $confhash->{$role}{$img} = '';
 5752:             $changes->{$role}{'images'}{$img} = 1;
 5753:         }
 5754:         if ($role eq 'login') {
 5755:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5756:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5757:             }
 5758:         }
 5759:     }
 5760:     if ($confhash->{$role}{'font'}) {
 5761:         $changes->{$role}{'font'} = 1;
 5762:     }
 5763: }
 5764: 
 5765: sub display_colorchgs {
 5766:     my ($dom,$changes,$roles,$confhash) = @_;
 5767:     my (%choices,$resulttext);
 5768:     if (!grep(/^login$/,@{$roles})) {
 5769:         $resulttext = &mt('Changes made:').'<br />';
 5770:     }
 5771:     foreach my $role (@{$roles}) {
 5772:         if ($role eq 'login') {
 5773:             %choices = &login_choices();
 5774:         } else {
 5775:             %choices = &color_font_choices();
 5776:         }
 5777:         if (ref($changes->{$role}) eq 'HASH') {
 5778:             if ($role ne 'login') {
 5779:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5780:             }
 5781:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5782:                 if ($role ne 'login') {
 5783:                     $resulttext .= '<ul>';
 5784:                 }
 5785:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5786:                     if ($role ne 'login') {
 5787:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5788:                     }
 5789:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5790:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5791:                             if ($confhash->{$role}{$key}{$item}) {
 5792:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5793:                             } else {
 5794:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5795:                             }
 5796:                         } elsif ($confhash->{$role}{$item} eq '') {
 5797:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5798:                         } else {
 5799:                             my $newitem = $confhash->{$role}{$item};
 5800:                             if ($key eq 'images') {
 5801:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5802:                             }
 5803:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5804:                         }
 5805:                     }
 5806:                     if ($role ne 'login') {
 5807:                         $resulttext .= '</ul></li>';
 5808:                     }
 5809:                 } else {
 5810:                     if ($confhash->{$role}{$key} eq '') {
 5811:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5812:                     } else {
 5813:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5814:                     }
 5815:                 }
 5816:                 if ($role ne 'login') {
 5817:                     $resulttext .= '</ul>';
 5818:                 }
 5819:             }
 5820:         }
 5821:     }
 5822:     return $resulttext;
 5823: }
 5824: 
 5825: sub thumb_dimensions {
 5826:     return ('200','50');
 5827: }
 5828: 
 5829: sub check_dimensions {
 5830:     my ($inputfile) = @_;
 5831:     my ($fullwidth,$fullheight);
 5832:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5833:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5834:             my $imageinfo = <PIPE>;
 5835:             if (!close(PIPE)) {
 5836:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5837:             }
 5838:             chomp($imageinfo);
 5839:             my ($fullsize) = 
 5840:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5841:             if ($fullsize) {
 5842:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5843:             }
 5844:         }
 5845:     }
 5846:     return ($fullwidth,$fullheight);
 5847: }
 5848: 
 5849: sub check_configuser {
 5850:     my ($uhome,$dom,$confname,$servadm) = @_;
 5851:     my ($configuserok,%currroles);
 5852:     if ($uhome eq 'no_host') {
 5853:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5854:         my $configpass = &LONCAPA::Enrollment::create_password();
 5855:         $configuserok = 
 5856:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5857:                              $configpass,'','','','','',undef,$servadm);
 5858:     } else {
 5859:         $configuserok = 'ok';
 5860:         %currroles = 
 5861:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5862:     }
 5863:     return ($configuserok,%currroles);
 5864: }
 5865: 
 5866: sub check_authorstatus {
 5867:     my ($dom,$confname,%currroles) = @_;
 5868:     my $author_ok;
 5869:     if (!$currroles{':'.$dom.':au'}) {
 5870:         my $start = time;
 5871:         my $end = 0;
 5872:         $author_ok = 
 5873:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5874:                                         'au',$end,$start,'','','domconfig');
 5875:     } else {
 5876:         $author_ok = 'ok';
 5877:     }
 5878:     return $author_ok;
 5879: }
 5880: 
 5881: sub publishlogo {
 5882:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5883:     my ($output,$fname,$logourl);
 5884:     if ($action eq 'upload') {
 5885:         $fname=$env{'form.'.$formname.'.filename'};
 5886:         chop($env{'form.'.$formname});
 5887:     } else {
 5888:         ($fname) = ($formname =~ /([^\/]+)$/);
 5889:     }
 5890:     if ($savefileas ne '') {
 5891:         $fname = $savefileas;
 5892:     }
 5893:     $fname=&Apache::lonnet::clean_filename($fname);
 5894: # See if there is anything left
 5895:     unless ($fname) { return ('error: no uploaded file'); }
 5896:     $fname="$subdir/$fname";
 5897:     my $docroot=$r->dir_config('lonDocRoot');
 5898:     my $filepath="$docroot/priv";
 5899:     my $relpath = "$dom/$confname";
 5900:     my ($fnamepath,$file,$fetchthumb);
 5901:     $file=$fname;
 5902:     if ($fname=~m|/|) {
 5903:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5904:     }
 5905:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5906:     my $count;
 5907:     for ($count=5;$count<=$#parts;$count++) {
 5908:         $filepath.="/$parts[$count]";
 5909:         if ((-e $filepath)!=1) {
 5910:             mkdir($filepath,02770);
 5911:         }
 5912:     }
 5913:     # Check for bad extension and disallow upload
 5914:     if ($file=~/\.(\w+)$/ &&
 5915:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5916:         $output = 
 5917:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 5918:     } elsif ($file=~/\.(\w+)$/ &&
 5919:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5920:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5921:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5922:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5923:     } elsif (-d "$filepath/$file") {
 5924:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 5925:     } else {
 5926:         my $source = $filepath.'/'.$file;
 5927:         my $logfile;
 5928:         if (!open($logfile,">>$source".'.log')) {
 5929:             return (&mt('No write permission to Authoring Space'));
 5930:         }
 5931:         print $logfile
 5932: "\n================= Publish ".localtime()." ================\n".
 5933: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5934: # Save the file
 5935:         if (!open(FH,'>'.$source)) {
 5936:             &Apache::lonnet::logthis('Failed to create '.$source);
 5937:             return (&mt('Failed to create file'));
 5938:         }
 5939:         if ($action eq 'upload') {
 5940:             if (!print FH ($env{'form.'.$formname})) {
 5941:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5942:                 return (&mt('Failed to write file'));
 5943:             }
 5944:         } else {
 5945:             my $original = &Apache::lonnet::filelocation('',$formname);
 5946:             if(!copy($original,$source)) {
 5947:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5948:                 return (&mt('Failed to write file'));
 5949:             }
 5950:         }
 5951:         close(FH);
 5952:         chmod(0660, $source); # Permissions to rw-rw---.
 5953: 
 5954:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5955:         my $copyfile=$targetdir.'/'.$file;
 5956: 
 5957:         my @parts=split(/\//,$targetdir);
 5958:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5959:         for (my $count=5;$count<=$#parts;$count++) {
 5960:             $path.="/$parts[$count]";
 5961:             if (!-e $path) {
 5962:                 print $logfile "\nCreating directory ".$path;
 5963:                 mkdir($path,02770);
 5964:             }
 5965:         }
 5966:         my $versionresult;
 5967:         if (-e $copyfile) {
 5968:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5969:         } else {
 5970:             $versionresult = 'ok';
 5971:         }
 5972:         if ($versionresult eq 'ok') {
 5973:             if (copy($source,$copyfile)) {
 5974:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5975:                 $output = 'ok';
 5976:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5977:                 push(@{$modified_urls},[$copyfile,$source]);
 5978:                 my $metaoutput = 
 5979:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5980:                 unless ($registered_cleanup) {
 5981:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5982:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5983:                     $registered_cleanup=1;
 5984:                 }
 5985:             } else {
 5986:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5987:                 $output = &mt('Failed to copy file to RES space').", $!";
 5988:             }
 5989:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5990:                 my $inputfile = $filepath.'/'.$file;
 5991:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5992:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5993:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5994:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5995:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5996:                         system("convert -sample $thumbsize $inputfile $outfile");
 5997:                         chmod(0660, $filepath.'/tn-'.$file);
 5998:                         if (-e $outfile) {
 5999:                             my $copyfile=$targetdir.'/tn-'.$file;
 6000:                             if (copy($outfile,$copyfile)) {
 6001:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6002:                                 my $thumb_metaoutput = 
 6003:                                     &write_metadata($dom,$confname,$formname,
 6004:                                                     $targetdir,'tn-'.$file,$logfile);
 6005:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6006:                                 unless ($registered_cleanup) {
 6007:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6008:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6009:                                     $registered_cleanup=1;
 6010:                                 }
 6011:                             } else {
 6012:                                 print $logfile "\nUnable to write ".$copyfile.
 6013:                                                ':'.$!."\n";
 6014:                             }
 6015:                         }
 6016:                     }
 6017:                 }
 6018:             }
 6019:         } else {
 6020:             $output = $versionresult;
 6021:         }
 6022:     }
 6023:     return ($output,$logourl);
 6024: }
 6025: 
 6026: sub logo_versioning {
 6027:     my ($targetdir,$file,$logfile) = @_;
 6028:     my $target = $targetdir.'/'.$file;
 6029:     my ($maxversion,$fn,$extn,$output);
 6030:     $maxversion = 0;
 6031:     if ($file =~ /^(.+)\.(\w+)$/) {
 6032:         $fn=$1;
 6033:         $extn=$2;
 6034:     }
 6035:     opendir(DIR,$targetdir);
 6036:     while (my $filename=readdir(DIR)) {
 6037:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6038:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6039:         }
 6040:     }
 6041:     $maxversion++;
 6042:     print $logfile "\nCreating old version ".$maxversion."\n";
 6043:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6044:     if (copy($target,$copyfile)) {
 6045:         print $logfile "Copied old target to ".$copyfile."\n";
 6046:         $copyfile=$copyfile.'.meta';
 6047:         if (copy($target.'.meta',$copyfile)) {
 6048:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6049:             $output = 'ok';
 6050:         } else {
 6051:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6052:             $output = &mt('Failed to copy old meta').", $!, ";
 6053:         }
 6054:     } else {
 6055:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6056:         $output = &mt('Failed to copy old target').", $!, ";
 6057:     }
 6058:     return $output;
 6059: }
 6060: 
 6061: sub write_metadata {
 6062:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6063:     my (%metadatafields,%metadatakeys,$output);
 6064:     $metadatafields{'title'}=$formname;
 6065:     $metadatafields{'creationdate'}=time;
 6066:     $metadatafields{'lastrevisiondate'}=time;
 6067:     $metadatafields{'copyright'}='public';
 6068:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6069:                                          $env{'user.domain'};
 6070:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6071:     $metadatafields{'domain'}=$dom;
 6072:     {
 6073:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6074:         my $mfh;
 6075:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6076:             foreach (sort(keys(%metadatafields))) {
 6077:                 unless ($_=~/\./) {
 6078:                     my $unikey=$_;
 6079:                     $unikey=~/^([A-Za-z]+)/;
 6080:                     my $tag=$1;
 6081:                     $tag=~tr/A-Z/a-z/;
 6082:                     print $mfh "\n\<$tag";
 6083:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6084:                         my $value=$metadatafields{$unikey.'.'.$_};
 6085:                         $value=~s/\"/\'\'/g;
 6086:                         print $mfh ' '.$_.'="'.$value.'"';
 6087:                     }
 6088:                     print $mfh '>'.
 6089:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6090:                             .'</'.$tag.'>';
 6091:                 }
 6092:             }
 6093:             $output = 'ok';
 6094:             print $logfile "\nWrote metadata";
 6095:             close($mfh);
 6096:         } else {
 6097:             print $logfile "\nFailed to open metadata file";
 6098:             $output = &mt('Could not write metadata');
 6099:         }
 6100:     }
 6101:     return $output;
 6102: }
 6103: 
 6104: sub notifysubscribed {
 6105:     foreach my $targetsource (@{$modified_urls}){
 6106:         next unless (ref($targetsource) eq 'ARRAY');
 6107:         my ($target,$source)=@{$targetsource};
 6108:         if ($source ne '') {
 6109:             if (open(my $logfh,'>>'.$source.'.log')) {
 6110:                 print $logfh "\nCleanup phase: Notifications\n";
 6111:                 my @subscribed=&subscribed_hosts($target);
 6112:                 foreach my $subhost (@subscribed) {
 6113:                     print $logfh "\nNotifying host ".$subhost.':';
 6114:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6115:                     print $logfh $reply;
 6116:                 }
 6117:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6118:                 foreach my $subhost (@subscribedmeta) {
 6119:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6120:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6121:                                                         $subhost);
 6122:                     print $logfh $reply;
 6123:                 }
 6124:                 print $logfh "\n============ Done ============\n";
 6125:                 close($logfh);
 6126:             }
 6127:         }
 6128:     }
 6129:     return OK;
 6130: }
 6131: 
 6132: sub subscribed_hosts {
 6133:     my ($target) = @_;
 6134:     my @subscribed;
 6135:     if (open(my $fh,"<$target.subscription")) {
 6136:         while (my $subline=<$fh>) {
 6137:             if ($subline =~ /^($match_lonid):/) {
 6138:                 my $host = $1;
 6139:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6140:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6141:                         push(@subscribed,$host);
 6142:                     }
 6143:                 }
 6144:             }
 6145:         }
 6146:     }
 6147:     return @subscribed;
 6148: }
 6149: 
 6150: sub check_switchserver {
 6151:     my ($dom,$confname) = @_;
 6152:     my ($allowed,$switchserver);
 6153:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6154:     if ($home eq 'no_host') {
 6155:         $home = &Apache::lonnet::domain($dom,'primary');
 6156:     }
 6157:     my @ids=&Apache::lonnet::current_machine_ids();
 6158:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6159:     if (!$allowed) {
 6160: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6161:     }
 6162:     return $switchserver;
 6163: }
 6164: 
 6165: sub modify_quotas {
 6166:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6167:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6168:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6169:         $author_ok,$switchserver,$errors);
 6170:     if ($action eq 'quotas') {
 6171:         $context = 'tools'; 
 6172:     } else {
 6173:         $context = $action;
 6174:     }
 6175:     if ($context eq 'requestcourses') {
 6176:         @usertools = ('official','unofficial','community','textbook');
 6177:         @options =('norequest','approval','validate','autolimit');
 6178:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6179:         %titles = &courserequest_titles();
 6180:         $toolregexp = join('|',@usertools);
 6181:         %conditions = &courserequest_conditions();
 6182:         $confname = $dom.'-domainconfig';
 6183:         my $servadm = $r->dir_config('lonAdmEMail');
 6184:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6185:     } elsif ($context eq 'requestauthor') {
 6186:         @usertools = ('author');
 6187:         %titles = &authorrequest_titles();
 6188:     } else {
 6189:         @usertools = ('aboutme','blog','webdav','portfolio');
 6190:         %titles = &tool_titles();
 6191:     }
 6192:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6193:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6194:     foreach my $key (keys(%env)) {
 6195:         if ($context eq 'requestcourses') {
 6196:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6197:                 my $item = $1;
 6198:                 my $type = $2;
 6199:                 if ($type =~ /^limit_(.+)/) {
 6200:                     $limithash{$item}{$1} = $env{$key};
 6201:                 } else {
 6202:                     $confhash{$item}{$type} = $env{$key};
 6203:                 }
 6204:             }
 6205:         } elsif ($context eq 'requestauthor') {
 6206:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6207:                 $confhash{$1} = $env{$key};
 6208:             }
 6209:         } else {
 6210:             if ($key =~ /^form\.quota_(.+)$/) {
 6211:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6212:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6213:                 $confhash{'authorquota'}{$1} = $env{$key};
 6214:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6215:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6216:             }
 6217:         }
 6218:     }
 6219:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6220:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6221:         @approvalnotify = sort(@approvalnotify);
 6222:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6223:         my @crstypes = ('official','unofficial','community','textbook');
 6224:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6225:         foreach my $type (@hasuniquecode) {
 6226:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6227:                 $confhash{'uniquecode'}{$type} = 1;
 6228:             }
 6229:         }
 6230:         my ($newbook,@allpos);
 6231:         if ($context eq 'requestcourses') {
 6232:             if ($env{'form.addbook'}) {
 6233:                 if (($env{'form.addbook_cnum'} =~ /^$match_courseid$/) &&
 6234:                     ($env{'form.addbook_cdom'} =~ /^$match_domain$/)) {
 6235:                     if (&Apache::lonnet::homeserver($env{'form.addbook_cnum'},
 6236:                                                     $env{'form.addbook_cdom'}) eq 'no_host') {
 6237:                         $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6238:                                    '</span></li>';
 6239:                     } else {
 6240:                         $newbook = $env{'form.addbook_cdom'}.'_'.$env{'form.addbook_cnum'};
 6241:                         my $position = $env{'form.addbook_pos'};
 6242:                         $position =~ s/\D+//g;
 6243:                         if ($position ne '') {
 6244:                             $allpos[$position] = $newbook;
 6245:                         }
 6246:                     }
 6247:                 } else {
 6248:                     $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6249:                                '</span></li>';
 6250:                 }
 6251:             }
 6252:         }
 6253:         if (ref($domconfig{$action}) eq 'HASH') {
 6254:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6255:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6256:                     $changes{'notify'}{'approval'} = 1;
 6257:                 }
 6258:             } else {
 6259:                 if ($confhash{'notify'}{'approval'}) {
 6260:                     $changes{'notify'}{'approval'} = 1;
 6261:                 }
 6262:             }
 6263:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6264:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6265:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6266:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6267:                             $changes{'uniquecode'} = 1;
 6268:                         }
 6269:                     }
 6270:                     unless ($changes{'uniquecode'}) {
 6271:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6272:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6273:                                 $changes{'uniquecode'} = 1;
 6274:                             }
 6275:                         }
 6276:                     }
 6277:                } else {
 6278:                    $changes{'uniquecode'} = 1;
 6279:                }
 6280:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6281:                 $changes{'uniquecode'} = 1;
 6282:             }
 6283:             if ($context eq 'requestcourses') {
 6284:                 if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6285:                     my %deletions;
 6286:                     my @todelete = &Apache::loncommon::get_env_multiple('form.book_del');
 6287:                     if (@todelete) {
 6288:                         map { $deletions{$_} = 1; } @todelete;
 6289:                     }
 6290:                     my %imgdeletions;
 6291:                     my @todeleteimages = &Apache::loncommon::get_env_multiple('form.book_image_del');
 6292:                     if (@todeleteimages) {
 6293:                         map { $imgdeletions{$_} = 1; } @todeleteimages;
 6294:                     }
 6295:                     my $maxnum = $env{'form.book_maxnum'};
 6296:                     for (my $i=0; $i<=$maxnum; $i++) {
 6297:                         my $key = $env{'form.book_id_'.$i};
 6298:                         if (ref($domconfig{$action}{'textbooks'}{$key}) eq 'HASH') {
 6299:                             if ($deletions{$key}) {
 6300:                                 if ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6301:                                     #FIXME need to obsolete item in RES space
 6302:                                 }
 6303:                                 next;
 6304:                             } else {
 6305:                                 my $newpos = $env{'form.'.$key};
 6306:                                 $newpos =~ s/\D+//g;
 6307:                                 foreach my $item ('subject','title','author') {
 6308:                                     $confhash{'textbooks'}{$key}{$item} = $env{'form.book_'.$item.'_'.$i};
 6309:                                     if ($domconfig{$action}{'textbooks'}{$key}{$item} ne $confhash{'textbooks'}{$key}{$item}) {
 6310:                                         $changes{'textbooks'}{$key} = 1;
 6311:                                     }
 6312:                                 }
 6313:                                 $allpos[$newpos] = $key;
 6314:                             }
 6315:                             if ($imgdeletions{$key}) {
 6316:                                 $changes{'textbooks'}{$key} = 1;
 6317:                                 #FIXME need to obsolete item in RES space
 6318:                             } elsif ($env{'form.book_image_'.$i.'.filename'}) {
 6319:                                 my ($cdom,$cnum) = split(/_/,$key);
 6320:                                 my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,'book_image_'.$i,
 6321:                                                                               $cdom,$cnum,$configuserok,
 6322:                                                                               $switchserver,$author_ok);
 6323:                                 if ($imgurl) {
 6324:                                     $confhash{'textbooks'}{$key}{'image'} = $imgurl;
 6325:                                     $changes{'textbooks'}{$key} = 1; 
 6326:                                 }
 6327:                                 if ($error) {
 6328:                                     &Apache::lonnet::logthis($error);
 6329:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6330:                                 } 
 6331:                             } elsif ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6332:                                 $confhash{'textbooks'}{$key}{'image'} = 
 6333:                                     $domconfig{$action}{'textbooks'}{$key}{'image'};
 6334:                             }
 6335:                         }
 6336:                     }
 6337:                 }
 6338:             }
 6339:         } else {
 6340:             if ($confhash{'notify'}{'approval'}) {
 6341:                 $changes{'notify'}{'approval'} = 1;
 6342:             }
 6343:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 6344:                 $changes{'uniquecode'} = 1;
 6345:             }
 6346:         }
 6347:         if ($context eq 'requestcourses') {
 6348:             if ($newbook) {
 6349:                 $changes{'textbooks'}{$newbook} = 1;
 6350:                 foreach my $item ('subject','title','author') {
 6351:                     $env{'form.addbook_'.$item} =~ s/(`)/'/g;
 6352:                     if ($env{'form.addbook_'.$item}) {
 6353:                         $confhash{'textbooks'}{$newbook}{$item} = $env{'form.addbook_'.$item};
 6354:                     }
 6355:                 }
 6356:                 if ($env{'form.addbook_image.filename'} ne '') {
 6357:                     my ($cdom,$cnum) = split(/_/,$newbook);
 6358:                     my ($imageurl,$error) = 
 6359:                         &process_textbook_image($r,$dom,$confname,'addbook_image',$cdom,$cnum,$configuserok,
 6360:                                                 $switchserver,$author_ok);
 6361:                     if ($imageurl) {
 6362:                         $confhash{'textbooks'}{$newbook}{'image'} = $imageurl;
 6363:                     }
 6364:                     if ($error) {
 6365:                         &Apache::lonnet::logthis($error);
 6366:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6367:                     }
 6368:                 }
 6369:             }
 6370:             if (@allpos > 0) {
 6371:                 my $idx = 0;
 6372:                 foreach my $item (@allpos) {
 6373:                     if ($item ne '') {
 6374:                         $confhash{'textbooks'}{$item}{'order'} = $idx;
 6375:                         if (ref($domconfig{$action}) eq 'HASH') {
 6376:                             if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6377:                                 if (ref($domconfig{$action}{'textbooks'}{$item}) eq 'HASH') {
 6378:                                     if ($domconfig{$action}{'textbooks'}{$item}{'order'} ne $idx) {
 6379:                                         $changes{'textbooks'}{$item} = 1;
 6380:                                     }
 6381:                                 }
 6382:                             }
 6383:                         }
 6384:                         $idx ++;
 6385:                     }
 6386:                 }
 6387:             }
 6388:         }
 6389:     } else {
 6390:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 6391:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 6392:     }
 6393:     foreach my $item (@usertools) {
 6394:         foreach my $type (@{$types},'default','_LC_adv') {
 6395:             my $unset; 
 6396:             if ($context eq 'requestcourses') {
 6397:                 $unset = '0';
 6398:                 if ($type eq '_LC_adv') {
 6399:                     $unset = '';
 6400:                 }
 6401:                 if ($confhash{$item}{$type} eq 'autolimit') {
 6402:                     $confhash{$item}{$type} .= '=';
 6403:                     unless ($limithash{$item}{$type} =~ /\D/) {
 6404:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 6405:                     }
 6406:                 }
 6407:             } elsif ($context eq 'requestauthor') {
 6408:                 $unset = '0';
 6409:                 if ($type eq '_LC_adv') {
 6410:                     $unset = '';
 6411:                 }
 6412:             } else {
 6413:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 6414:                     $confhash{$item}{$type} = 1;
 6415:                 } else {
 6416:                     $confhash{$item}{$type} = 0;
 6417:                 }
 6418:             }
 6419:             if (ref($domconfig{$action}) eq 'HASH') {
 6420:                 if ($action eq 'requestauthor') {
 6421:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 6422:                         $changes{$type} = 1;
 6423:                     }
 6424:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 6425:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 6426:                         $changes{$item}{$type} = 1;
 6427:                     }
 6428:                 } else {
 6429:                     if ($context eq 'requestcourses') {
 6430:                         if ($confhash{$item}{$type} ne $unset) {
 6431:                             $changes{$item}{$type} = 1;
 6432:                         }
 6433:                     } else {
 6434:                         if (!$confhash{$item}{$type}) {
 6435:                             $changes{$item}{$type} = 1;
 6436:                         }
 6437:                     }
 6438:                 }
 6439:             } else {
 6440:                 if ($context eq 'requestcourses') {
 6441:                     if ($confhash{$item}{$type} ne $unset) {
 6442:                         $changes{$item}{$type} = 1;
 6443:                     }
 6444:                 } elsif ($context eq 'requestauthor') {
 6445:                     if ($confhash{$type} ne $unset) {
 6446:                         $changes{$type} = 1;
 6447:                     }
 6448:                 } else {
 6449:                     if (!$confhash{$item}{$type}) {
 6450:                         $changes{$item}{$type} = 1;
 6451:                     }
 6452:                 }
 6453:             }
 6454:         }
 6455:     }
 6456:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6457:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 6458:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6459:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 6460:                     if (exists($confhash{'defaultquota'}{$key})) {
 6461:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 6462:                             $changes{'defaultquota'}{$key} = 1;
 6463:                         }
 6464:                     } else {
 6465:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 6466:                     }
 6467:                 }
 6468:             } else {
 6469:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 6470:                     if (exists($confhash{'defaultquota'}{$key})) {
 6471:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 6472:                             $changes{'defaultquota'}{$key} = 1;
 6473:                         }
 6474:                     } else {
 6475:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 6476:                     }
 6477:                 }
 6478:             }
 6479:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6480:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 6481:                     if (exists($confhash{'authorquota'}{$key})) {
 6482:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 6483:                             $changes{'authorquota'}{$key} = 1;
 6484:                         }
 6485:                     } else {
 6486:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 6487:                     }
 6488:                 }
 6489:             }
 6490:         }
 6491:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 6492:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 6493:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6494:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6495:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 6496:                             $changes{'defaultquota'}{$key} = 1;
 6497:                         }
 6498:                     } else {
 6499:                         if (!exists($domconfig{'quotas'}{$key})) {
 6500:                             $changes{'defaultquota'}{$key} = 1;
 6501:                         }
 6502:                     }
 6503:                 } else {
 6504:                     $changes{'defaultquota'}{$key} = 1;
 6505:                 }
 6506:             }
 6507:         }
 6508:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 6509:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 6510:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6511:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6512:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 6513:                             $changes{'authorquota'}{$key} = 1;
 6514:                         }
 6515:                     } else {
 6516:                         $changes{'authorquota'}{$key} = 1;
 6517:                     }
 6518:                 } else {
 6519:                     $changes{'authorquota'}{$key} = 1;
 6520:                 }
 6521:             }
 6522:         }
 6523:     }
 6524: 
 6525:     if ($context eq 'requestauthor') {
 6526:         $domdefaults{'requestauthor'} = \%confhash;
 6527:     } else {
 6528:         foreach my $key (keys(%confhash)) {
 6529:             unless (($context eq 'requestcourses') && ($key eq 'textbooks')) {
 6530:                 $domdefaults{$key} = $confhash{$key};
 6531:             }
 6532:         }
 6533:     }
 6534: 
 6535:     my %quotahash = (
 6536:                       $action => { %confhash }
 6537:                     );
 6538:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 6539:                                              $dom);
 6540:     if ($putresult eq 'ok') {
 6541:         if (keys(%changes) > 0) {
 6542:             my $cachetime = 24*60*60;
 6543:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6544:             if (ref($lastactref) eq 'HASH') {
 6545:                 $lastactref->{'domdefaults'} = 1;
 6546:             }
 6547:             $resulttext = &mt('Changes made:').'<ul>';
 6548:             unless (($context eq 'requestcourses') ||
 6549:                     ($context eq 'requestauthor')) {
 6550:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 6551:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 6552:                     foreach my $type (@{$types},'default') {
 6553:                         if (defined($changes{'defaultquota'}{$type})) {
 6554:                             my $typetitle = $usertypes->{$type};
 6555:                             if ($type eq 'default') {
 6556:                                 $typetitle = $othertitle;
 6557:                             }
 6558:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 6559:                         }
 6560:                     }
 6561:                     $resulttext .= '</ul></li>';
 6562:                 }
 6563:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 6564:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 6565:                     foreach my $type (@{$types},'default') {
 6566:                         if (defined($changes{'authorquota'}{$type})) {
 6567:                             my $typetitle = $usertypes->{$type};
 6568:                             if ($type eq 'default') {
 6569:                                 $typetitle = $othertitle;
 6570:                             }
 6571:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 6572:                         }
 6573:                     }
 6574:                     $resulttext .= '</ul></li>';
 6575:                 }
 6576:             }
 6577:             my %newenv;
 6578:             foreach my $item (@usertools) {
 6579:                 my (%haschgs,%inconf);
 6580:                 if ($context eq 'requestauthor') {
 6581:                     %haschgs = %changes;
 6582:                     %inconf = %confhash;
 6583:                 } else {
 6584:                     if (ref($changes{$item}) eq 'HASH') {
 6585:                         %haschgs = %{$changes{$item}};
 6586:                     }
 6587:                     if (ref($confhash{$item}) eq 'HASH') {
 6588:                         %inconf = %{$confhash{$item}};
 6589:                     }
 6590:                 }
 6591:                 if (keys(%haschgs) > 0) {
 6592:                     my $newacc = 
 6593:                         &Apache::lonnet::usertools_access($env{'user.name'},
 6594:                                                           $env{'user.domain'},
 6595:                                                           $item,'reload',$context);
 6596:                     if (($context eq 'requestcourses') ||
 6597:                         ($context eq 'requestauthor')) {
 6598:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 6599:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 6600:                         }
 6601:                     } else {
 6602:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 6603:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 6604:                         }
 6605:                     }
 6606:                     unless ($context eq 'requestauthor') {
 6607:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 6608:                     }
 6609:                     foreach my $type (@{$types},'default','_LC_adv') {
 6610:                         if ($haschgs{$type}) {
 6611:                             my $typetitle = $usertypes->{$type};
 6612:                             if ($type eq 'default') {
 6613:                                 $typetitle = $othertitle;
 6614:                             } elsif ($type eq '_LC_adv') {
 6615:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 6616:                             }
 6617:                             if ($inconf{$type}) {
 6618:                                 if ($context eq 'requestcourses') {
 6619:                                     my $cond;
 6620:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 6621:                                         if ($1 eq '') {
 6622:                                             $cond = &mt('(Automatic processing of any request).');
 6623:                                         } else {
 6624:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 6625:                                         }
 6626:                                     } else { 
 6627:                                         $cond = $conditions{$inconf{$type}};
 6628:                                     }
 6629:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 6630:                                 } elsif ($context eq 'requestauthor') {
 6631:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 6632:                                                              $titles{$inconf{$type}},$typetitle);
 6633: 
 6634:                                 } else {
 6635:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 6636:                                 }
 6637:                             } else {
 6638:                                 if ($type eq '_LC_adv') {
 6639:                                     if ($inconf{$type} eq '0') {
 6640:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6641:                                     } else { 
 6642:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 6643:                                     }
 6644:                                 } else {
 6645:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6646:                                 }
 6647:                             }
 6648:                         }
 6649:                     }
 6650:                     unless ($context eq 'requestauthor') {
 6651:                         $resulttext .= '</ul></li>';
 6652:                     }
 6653:                 }
 6654:             }
 6655:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 6656:                 if (ref($changes{'notify'}) eq 'HASH') {
 6657:                     if ($changes{'notify'}{'approval'}) {
 6658:                         if (ref($confhash{'notify'}) eq 'HASH') {
 6659:                             if ($confhash{'notify'}{'approval'}) {
 6660:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 6661:                             } else {
 6662:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 6663:                             }
 6664:                         }
 6665:                     }
 6666:                 }
 6667:             }
 6668:             if ($action eq 'requestcourses') {
 6669:                 my @offon = ('off','on');
 6670:                 if ($changes{'uniquecode'}) {
 6671:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6672:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 6673:                         $resulttext .= '<li>'.
 6674:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 6675:                                        '</li>';
 6676:                     } else {
 6677:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 6678:                                        '</li>';
 6679:                     }
 6680:                 }
 6681:                 if (ref($changes{'textbooks'}) eq 'HASH') {
 6682:                     $resulttext .= '<li>'.&mt('Available textbooks updated').'<ul>';
 6683:                     foreach my $key (sort(keys(%{$changes{'textbooks'}}))) {
 6684:                         my %coursehash = &Apache::lonnet::coursedescription($key);
 6685:                         my $coursetitle = $coursehash{'description'};
 6686:                         my $position = $confhash{'textbooks'}{$key}{'order'} + 1;
 6687:                         $resulttext .= '<li>';
 6688:                         foreach my $item ('subject','title','author') {
 6689:                             my $name = $item.':';
 6690:                             $name =~ s/^(\w)/\U$1/;
 6691:                             $resulttext .= &mt($name).' '.$confhash{'textbooks'}{$key}{$item}.'<br />';
 6692:                         }
 6693:                         $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 6694:                         if ($confhash{'textbooks'}{$key}{'image'}) {
 6695:                             $resulttext .= ' '.&mt('Image: [_1]',
 6696:                                                '<img src="'.$confhash{'textbooks'}{$key}{'image'}.'"'.
 6697:                                                ' alt="Textbook cover" />').'<br />';
 6698:                         }
 6699:                         $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 6700:                     }
 6701:                     $resulttext .= '</ul></li>';
 6702:                 }
 6703:             }
 6704:             $resulttext .= '</ul>';
 6705:             if (keys(%newenv)) {
 6706:                 &Apache::lonnet::appenv(\%newenv);
 6707:             }
 6708:         } else {
 6709:             if ($context eq 'requestcourses') {
 6710:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 6711:             } elsif ($context eq 'requestauthor') {
 6712:                 $resulttext = &mt('No changes made to rights to request author space.');
 6713:             } else {
 6714:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 6715:             }
 6716:         }
 6717:     } else {
 6718:         $resulttext = '<span class="LC_error">'.
 6719: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6720:     }
 6721:     if ($errors) {
 6722:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 6723:                        '<ul>'.$errors.'</ul></p>';
 6724:     }
 6725:     return $resulttext;
 6726: }
 6727: 
 6728: sub process_textbook_image {
 6729:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$configuserok,$switchserver,$author_ok) = @_;
 6730:     my $filename = $env{'form.'.$caller.'.filename'};
 6731:     my ($error,$url);
 6732:     my ($width,$height) = (50,50);
 6733:     if ($configuserok eq 'ok') {
 6734:         if ($switchserver) {
 6735:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 6736:                          $switchserver);
 6737:         } elsif ($author_ok eq 'ok') {
 6738:             my ($result,$imageurl) =
 6739:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 6740:                              "textbooks/$dom/$cnum/cover",$width,$height);
 6741:             if ($result eq 'ok') {
 6742:                 $url = $imageurl;
 6743:             } else {
 6744:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 6745:             }
 6746:         } else {
 6747:             $error = &mt("Upload of [_1] failed because an author role could not be assigned to a Domain Configuration user ([_2]) in domain: [_3].  Error was: [_4].",$filename,$confname,$dom,$author_ok);
 6748:         }
 6749:     } else {
 6750:         $error = &mt("Upload of [_1] failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$filename,$confname,$dom,$configuserok);
 6751:     }
 6752:     return ($url,$error);
 6753: }
 6754: 
 6755: sub modify_autoenroll {
 6756:     my ($dom,$lastactref,%domconfig) = @_;
 6757:     my ($resulttext,%changes);
 6758:     my %currautoenroll;
 6759:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 6760:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 6761:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 6762:         }
 6763:     }
 6764:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6765:     my %title = ( run => 'Auto-enrollment active',
 6766:                   sender => 'Sender for notification messages',
 6767:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6768:     my @offon = ('off','on');
 6769:     my $sender_uname = $env{'form.sender_uname'};
 6770:     my $sender_domain = $env{'form.sender_domain'};
 6771:     if ($sender_domain eq '') {
 6772:         $sender_uname = '';
 6773:     } elsif ($sender_uname eq '') {
 6774:         $sender_domain = '';
 6775:     }
 6776:     my $coowners = $env{'form.autoassign_coowners'};
 6777:     my %autoenrollhash =  (
 6778:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6779:                                        'sender_uname' => $sender_uname,
 6780:                                        'sender_domain' => $sender_domain,
 6781:                                        'co-owners' => $coowners,
 6782:                                 }
 6783:                      );
 6784:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6785:                                              $dom);
 6786:     if ($putresult eq 'ok') {
 6787:         if (exists($currautoenroll{'run'})) {
 6788:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6789:                  $changes{'run'} = 1;
 6790:              }
 6791:         } elsif ($autorun) {
 6792:             if ($env{'form.autoenroll_run'} ne '1') {
 6793:                  $changes{'run'} = 1;
 6794:             }
 6795:         }
 6796:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6797:             $changes{'sender'} = 1;
 6798:         }
 6799:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6800:             $changes{'sender'} = 1;
 6801:         }
 6802:         if ($currautoenroll{'co-owners'} ne '') {
 6803:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6804:                 $changes{'coowners'} = 1;
 6805:             }
 6806:         } elsif ($coowners) {
 6807:             $changes{'coowners'} = 1;
 6808:         }      
 6809:         if (keys(%changes) > 0) {
 6810:             $resulttext = &mt('Changes made:').'<ul>';
 6811:             if ($changes{'run'}) {
 6812:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6813:             }
 6814:             if ($changes{'sender'}) {
 6815:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6816:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6817:                 } else {
 6818:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6819:                 }
 6820:             }
 6821:             if ($changes{'coowners'}) {
 6822:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6823:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6824:                 if (ref($lastactref) eq 'HASH') {
 6825:                     $lastactref->{'domainconfig'} = 1;
 6826:                 }
 6827:             }
 6828:             $resulttext .= '</ul>';
 6829:         } else {
 6830:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6831:         }
 6832:     } else {
 6833:         $resulttext = '<span class="LC_error">'.
 6834: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6835:     }
 6836:     return $resulttext;
 6837: }
 6838: 
 6839: sub modify_autoupdate {
 6840:     my ($dom,%domconfig) = @_;
 6841:     my ($resulttext,%currautoupdate,%fields,%changes);
 6842:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6843:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6844:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6845:         }
 6846:     }
 6847:     my @offon = ('off','on');
 6848:     my %title = &Apache::lonlocal::texthash (
 6849:                    run => 'Auto-update:',
 6850:                    classlists => 'Updates to user information in classlists?'
 6851:                 );
 6852:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6853:     my %fieldtitles = &Apache::lonlocal::texthash (
 6854:                         id => 'Student/Employee ID',
 6855:                         permanentemail => 'E-mail address',
 6856:                         lastname => 'Last Name',
 6857:                         firstname => 'First Name',
 6858:                         middlename => 'Middle Name',
 6859:                         generation => 'Generation',
 6860:                       );
 6861:     $othertitle = &mt('All users');
 6862:     if (keys(%{$usertypes}) >  0) {
 6863:         $othertitle = &mt('Other users');
 6864:     }
 6865:     foreach my $key (keys(%env)) {
 6866:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6867:             my ($usertype,$item) = ($1,$2);
 6868:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6869:                 if ($usertype eq 'default') {   
 6870:                     push(@{$fields{$1}},$2);
 6871:                 } elsif (ref($types) eq 'ARRAY') {
 6872:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6873:                         push(@{$fields{$1}},$2);
 6874:                     }
 6875:                 }
 6876:             }
 6877:         }
 6878:     }
 6879:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6880:     @lockablenames = sort(@lockablenames);
 6881:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6882:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6883:         if (@changed) {
 6884:             $changes{'lockablenames'} = 1;
 6885:         }
 6886:     } else {
 6887:         if (@lockablenames) {
 6888:             $changes{'lockablenames'} = 1;
 6889:         }
 6890:     }
 6891:     my %updatehash = (
 6892:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6893:                                       classlists => $env{'form.classlists'},
 6894:                                       fields => {%fields},
 6895:                                       lockablenames => \@lockablenames,
 6896:                                     }
 6897:                      );
 6898:     foreach my $key (keys(%currautoupdate)) {
 6899:         if (($key eq 'run') || ($key eq 'classlists')) {
 6900:             if (exists($updatehash{autoupdate}{$key})) {
 6901:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6902:                     $changes{$key} = 1;
 6903:                 }
 6904:             }
 6905:         } elsif ($key eq 'fields') {
 6906:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6907:                 foreach my $item (@{$types},'default') {
 6908:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6909:                         my $change = 0;
 6910:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6911:                             if (!exists($fields{$item})) {
 6912:                                 $change = 1;
 6913:                                 last;
 6914:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6915:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6916:                                     $change = 1;
 6917:                                     last;
 6918:                                 }
 6919:                             }
 6920:                         }
 6921:                         if ($change) {
 6922:                             push(@{$changes{$key}},$item);
 6923:                         }
 6924:                     } 
 6925:                 }
 6926:             }
 6927:         } elsif ($key eq 'lockablenames') {
 6928:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6929:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6930:                 if (@changed) {
 6931:                     $changes{'lockablenames'} = 1;
 6932:                 }
 6933:             } else {
 6934:                 if (@lockablenames) {
 6935:                     $changes{'lockablenames'} = 1;
 6936:                 }
 6937:             }
 6938:         }
 6939:     }
 6940:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6941:         if (@lockablenames) {
 6942:             $changes{'lockablenames'} = 1;
 6943:         }
 6944:     }
 6945:     foreach my $item (@{$types},'default') {
 6946:         if (defined($fields{$item})) {
 6947:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6948:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6949:                     my $change = 0;
 6950:                     if (ref($fields{$item}) eq 'ARRAY') {
 6951:                         foreach my $type (@{$fields{$item}}) {
 6952:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6953:                                 $change = 1;
 6954:                                 last;
 6955:                             }
 6956:                         }
 6957:                     }
 6958:                     if ($change) {
 6959:                         push(@{$changes{'fields'}},$item);
 6960:                     }
 6961:                 } else {
 6962:                     push(@{$changes{'fields'}},$item);
 6963:                 }
 6964:             } else {
 6965:                 push(@{$changes{'fields'}},$item);
 6966:             }
 6967:         }
 6968:     }
 6969:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6970:                                              $dom);
 6971:     if ($putresult eq 'ok') {
 6972:         if (keys(%changes) > 0) {
 6973:             $resulttext = &mt('Changes made:').'<ul>';
 6974:             foreach my $key (sort(keys(%changes))) {
 6975:                 if ($key eq 'lockablenames') {
 6976:                     $resulttext .= '<li>';
 6977:                     if (@lockablenames) {
 6978:                         $usertypes->{'default'} = $othertitle;
 6979:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6980:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6981:                     } else {
 6982:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6983:                     }
 6984:                     $resulttext .= '</li>';
 6985:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6986:                     foreach my $item (@{$changes{$key}}) {
 6987:                         my @newvalues;
 6988:                         foreach my $type (@{$fields{$item}}) {
 6989:                             push(@newvalues,$fieldtitles{$type});
 6990:                         }
 6991:                         my $newvaluestr;
 6992:                         if (@newvalues > 0) {
 6993:                             $newvaluestr = join(', ',@newvalues);
 6994:                         } else {
 6995:                             $newvaluestr = &mt('none');
 6996:                         }
 6997:                         if ($item eq 'default') {
 6998:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6999:                         } else {
 7000:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7001:                         }
 7002:                     }
 7003:                 } else {
 7004:                     my $newvalue;
 7005:                     if ($key eq 'run') {
 7006:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7007:                     } else {
 7008:                         $newvalue = $offon[$env{'form.'.$key}];
 7009:                     }
 7010:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7011:                 }
 7012:             }
 7013:             $resulttext .= '</ul>';
 7014:         } else {
 7015:             $resulttext = &mt('No changes made to autoupdates');
 7016:         }
 7017:     } else {
 7018:         $resulttext = '<span class="LC_error">'.
 7019: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7020:     }
 7021:     return $resulttext;
 7022: }
 7023: 
 7024: sub modify_autocreate {
 7025:     my ($dom,%domconfig) = @_;
 7026:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7027:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7028:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7029:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7030:         }
 7031:     }
 7032:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7033:                  req => 'Auto-creation of validated requests for official courses',
 7034:                  xmldc => 'Identity of course creator of courses from XML files',
 7035:                );
 7036:     my @types = ('xml','req');
 7037:     foreach my $item (@types) {
 7038:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7039:         $newvals{$item} =~ s/\D//g;
 7040:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7041:     }
 7042:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7043:     my %domcoords = &get_active_dcs($dom);
 7044:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7045:         $newvals{'xmldc'} = '';
 7046:     } 
 7047:     %autocreatehash =  (
 7048:                         autocreate => { xml => $newvals{'xml'},
 7049:                                         req => $newvals{'req'},
 7050:                                       }
 7051:                        );
 7052:     if ($newvals{'xmldc'} ne '') {
 7053:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7054:     }
 7055:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7056:                                              $dom);
 7057:     if ($putresult eq 'ok') {
 7058:         my @items = @types;
 7059:         if ($newvals{'xml'}) {
 7060:             push(@items,'xmldc');
 7061:         }
 7062:         foreach my $item (@items) {
 7063:             if (exists($currautocreate{$item})) {
 7064:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7065:                     $changes{$item} = 1;
 7066:                 }
 7067:             } elsif ($newvals{$item}) {
 7068:                 $changes{$item} = 1;
 7069:             }
 7070:         }
 7071:         if (keys(%changes) > 0) {
 7072:             my @offon = ('off','on'); 
 7073:             $resulttext = &mt('Changes made:').'<ul>';
 7074:             foreach my $item (@types) {
 7075:                 if ($changes{$item}) {
 7076:                     my $newtxt = $offon[$newvals{$item}];
 7077:                     $resulttext .= '<li>'.
 7078:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7079:                                        '<b>','</b>').
 7080:                                    '</li>';
 7081:                 }
 7082:             }
 7083:             if ($changes{'xmldc'}) {
 7084:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7085:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7086:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7087:             }
 7088:             $resulttext .= '</ul>';
 7089:         } else {
 7090:             $resulttext = &mt('No changes made to auto-creation settings');
 7091:         }
 7092:     } else {
 7093:         $resulttext = '<span class="LC_error">'.
 7094:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7095:     }
 7096:     return $resulttext;
 7097: }
 7098: 
 7099: sub modify_directorysrch {
 7100:     my ($dom,%domconfig) = @_;
 7101:     my ($resulttext,%changes);
 7102:     my %currdirsrch;
 7103:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7104:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7105:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7106:         }
 7107:     }
 7108:     my %title = ( available => 'Directory search available',
 7109:                   localonly => 'Other domains can search',
 7110:                   searchby => 'Search types',
 7111:                   searchtypes => 'Search latitude');
 7112:     my @offon = ('off','on');
 7113:     my @otherdoms = ('Yes','No');
 7114: 
 7115:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7116:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7117:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7118: 
 7119:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7120:     if (keys(%{$usertypes}) == 0) {
 7121:         @cansearch = ('default');
 7122:     } else {
 7123:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7124:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7125:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7126:                     push(@{$changes{'cansearch'}},$type);
 7127:                 }
 7128:             }
 7129:             foreach my $type (@cansearch) {
 7130:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7131:                     push(@{$changes{'cansearch'}},$type);
 7132:                 }
 7133:             }
 7134:         } else {
 7135:             push(@{$changes{'cansearch'}},@cansearch);
 7136:         }
 7137:     }
 7138: 
 7139:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7140:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7141:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7142:                 push(@{$changes{'searchby'}},$by);
 7143:             }
 7144:         }
 7145:         foreach my $by (@searchby) {
 7146:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7147:                 push(@{$changes{'searchby'}},$by);
 7148:             }
 7149:         }
 7150:     } else {
 7151:         push(@{$changes{'searchby'}},@searchby);
 7152:     }
 7153: 
 7154:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7155:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7156:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7157:                 push(@{$changes{'searchtypes'}},$type);
 7158:             }
 7159:         }
 7160:         foreach my $type (@searchtypes) {
 7161:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7162:                 push(@{$changes{'searchtypes'}},$type);
 7163:             }
 7164:         }
 7165:     } else {
 7166:         if (exists($currdirsrch{'searchtypes'})) {
 7167:             foreach my $type (@searchtypes) {  
 7168:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7169:                     push(@{$changes{'searchtypes'}},$type);
 7170:                 }
 7171:             }
 7172:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7173:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7174:             }   
 7175:         } else {
 7176:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7177:         }
 7178:     }
 7179: 
 7180:     my %dirsrch_hash =  (
 7181:             directorysrch => { available => $env{'form.dirsrch_available'},
 7182:                                cansearch => \@cansearch,
 7183:                                localonly => $env{'form.dirsrch_localonly'},
 7184:                                searchby => \@searchby,
 7185:                                searchtypes => \@searchtypes,
 7186:                              }
 7187:             );
 7188:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7189:                                              $dom);
 7190:     if ($putresult eq 'ok') {
 7191:         if (exists($currdirsrch{'available'})) {
 7192:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7193:                  $changes{'available'} = 1;
 7194:              }
 7195:         } else {
 7196:             if ($env{'form.dirsrch_available'} eq '1') {
 7197:                 $changes{'available'} = 1;
 7198:             }
 7199:         }
 7200:         if (exists($currdirsrch{'localonly'})) {
 7201:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7202:                  $changes{'localonly'} = 1;
 7203:              }
 7204:         } else {
 7205:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7206:                 $changes{'localonly'} = 1;
 7207:             }
 7208:         }
 7209:         if (keys(%changes) > 0) {
 7210:             $resulttext = &mt('Changes made:').'<ul>';
 7211:             if ($changes{'available'}) {
 7212:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7213:             }
 7214:             if ($changes{'localonly'}) {
 7215:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7216:             }
 7217: 
 7218:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7219:                 my $chgtext;
 7220:                 if (ref($usertypes) eq 'HASH') {
 7221:                     if (keys(%{$usertypes}) > 0) {
 7222:                         foreach my $type (@{$types}) {
 7223:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7224:                                 $chgtext .= $usertypes->{$type}.'; ';
 7225:                             }
 7226:                         }
 7227:                         if (grep(/^default$/,@cansearch)) {
 7228:                             $chgtext .= $othertitle;
 7229:                         } else {
 7230:                             $chgtext =~ s/\; $//;
 7231:                         }
 7232:                         $resulttext .=
 7233:                             '<li>'.
 7234:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 7235:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 7236:                             '</li>';
 7237:                     }
 7238:                 }
 7239:             }
 7240:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 7241:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 7242:                 my $chgtext;
 7243:                 foreach my $type (@{$titleorder}) {
 7244:                     if (grep(/^\Q$type\E$/,@searchby)) {
 7245:                         if (defined($searchtitles->{$type})) {
 7246:                             $chgtext .= $searchtitles->{$type}.'; ';
 7247:                         }
 7248:                     }
 7249:                 }
 7250:                 $chgtext =~ s/\; $//;
 7251:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 7252:             }
 7253:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 7254:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 7255:                 my $chgtext;
 7256:                 foreach my $type (@{$srchtypeorder}) {
 7257:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 7258:                         if (defined($srchtypes_desc->{$type})) {
 7259:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 7260:                         }
 7261:                     }
 7262:                 }
 7263:                 $chgtext =~ s/\; $//;
 7264:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 7265:             }
 7266:             $resulttext .= '</ul>';
 7267:         } else {
 7268:             $resulttext = &mt('No changes made to institution directory search settings');
 7269:         }
 7270:     } else {
 7271:         $resulttext = '<span class="LC_error">'.
 7272:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 7273:     }
 7274:     return $resulttext;
 7275: }
 7276: 
 7277: sub modify_contacts {
 7278:     my ($dom,$lastactref,%domconfig) = @_;
 7279:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 7280:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 7281:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 7282:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 7283:         }
 7284:     }
 7285:     my (%others,%to,%bcc);
 7286:     my @contacts = ('supportemail','adminemail');
 7287:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 7288:                     'requestsmail','updatesmail','idconflictsmail');
 7289:     my @toggles = ('reporterrors','reportupdates');
 7290:     foreach my $type (@mailings) {
 7291:         @{$newsetting{$type}} = 
 7292:             &Apache::loncommon::get_env_multiple('form.'.$type);
 7293:         foreach my $item (@contacts) {
 7294:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 7295:                 $contacts_hash{contacts}{$type}{$item} = 1;
 7296:             } else {
 7297:                 $contacts_hash{contacts}{$type}{$item} = 0;
 7298:             }
 7299:         }  
 7300:         $others{$type} = $env{'form.'.$type.'_others'};
 7301:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 7302:         if ($type eq 'helpdeskmail') {
 7303:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 7304:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 7305:         }
 7306:     }
 7307:     foreach my $item (@contacts) {
 7308:         $to{$item} = $env{'form.'.$item};
 7309:         $contacts_hash{'contacts'}{$item} = $to{$item};
 7310:     }
 7311:     foreach my $item (@toggles) {
 7312:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 7313:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 7314:         }
 7315:     }
 7316:     if (keys(%currsetting) > 0) {
 7317:         foreach my $item (@contacts) {
 7318:             if ($to{$item} ne $currsetting{$item}) {
 7319:                 $changes{$item} = 1;
 7320:             }
 7321:         }
 7322:         foreach my $type (@mailings) {
 7323:             foreach my $item (@contacts) {
 7324:                 if (ref($currsetting{$type}) eq 'HASH') {
 7325:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 7326:                         push(@{$changes{$type}},$item);
 7327:                     }
 7328:                 } else {
 7329:                     push(@{$changes{$type}},@{$newsetting{$type}});
 7330:                 }
 7331:             }
 7332:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 7333:                 push(@{$changes{$type}},'others');
 7334:             }
 7335:             if ($type eq 'helpdeskmail') {   
 7336:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 7337:                     push(@{$changes{$type}},'bcc'); 
 7338:                 }
 7339:             }
 7340:         }
 7341:     } else {
 7342:         my %default;
 7343:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 7344:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 7345:         $default{'errormail'} = 'adminemail';
 7346:         $default{'packagesmail'} = 'adminemail';
 7347:         $default{'helpdeskmail'} = 'supportemail';
 7348:         $default{'lonstatusmail'} = 'adminemail';
 7349:         $default{'requestsmail'} = 'adminemail';
 7350:         $default{'updatesmail'} = 'adminemail';
 7351:         foreach my $item (@contacts) {
 7352:            if ($to{$item} ne $default{$item}) {
 7353:               $changes{$item} = 1;
 7354:            }
 7355:         }
 7356:         foreach my $type (@mailings) {
 7357:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 7358:                
 7359:                 push(@{$changes{$type}},@{$newsetting{$type}});
 7360:             }
 7361:             if ($others{$type} ne '') {
 7362:                 push(@{$changes{$type}},'others');
 7363:             }
 7364:             if ($type eq 'helpdeskmail') {
 7365:                 if ($bcc{$type} ne '') {
 7366:                     push(@{$changes{$type}},'bcc');
 7367:                 }
 7368:             }
 7369:         }
 7370:     }
 7371:     foreach my $item (@toggles) {
 7372:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 7373:             $changes{$item} = 1;
 7374:         } elsif ((!$env{'form.'.$item}) &&
 7375:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 7376:             $changes{$item} = 1;
 7377:         }
 7378:     }
 7379:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 7380:                                              $dom);
 7381:     if ($putresult eq 'ok') {
 7382:         if (keys(%changes) > 0) {
 7383:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7384:             if (ref($lastactref) eq 'HASH') {
 7385:                 $lastactref->{'domainconfig'} = 1;
 7386:             }
 7387:             my ($titles,$short_titles)  = &contact_titles();
 7388:             $resulttext = &mt('Changes made:').'<ul>';
 7389:             foreach my $item (@contacts) {
 7390:                 if ($changes{$item}) {
 7391:                     $resulttext .= '<li>'.$titles->{$item}.
 7392:                                     &mt(' set to: ').
 7393:                                     '<span class="LC_cusr_emph">'.
 7394:                                     $to{$item}.'</span></li>';
 7395:                 }
 7396:             }
 7397:             foreach my $type (@mailings) {
 7398:                 if (ref($changes{$type}) eq 'ARRAY') {
 7399:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 7400:                     my @text;
 7401:                     foreach my $item (@{$newsetting{$type}}) {
 7402:                         push(@text,$short_titles->{$item});
 7403:                     }
 7404:                     if ($others{$type} ne '') {
 7405:                         push(@text,$others{$type});
 7406:                     }
 7407:                     $resulttext .= '<span class="LC_cusr_emph">'.
 7408:                                    join(', ',@text).'</span>';
 7409:                     if ($type eq 'helpdeskmail') {
 7410:                         if ($bcc{$type} ne '') {
 7411:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 7412:                         }
 7413:                     }
 7414:                     $resulttext .= '</li>';
 7415:                 }
 7416:             }
 7417:             my @offon = ('off','on');
 7418:             if ($changes{'reporterrors'}) {
 7419:                 $resulttext .= '<li>'.
 7420:                                &mt('E-mail error reports to [_1] set to "'.
 7421:                                    $offon[$env{'form.reporterrors'}].'".',
 7422:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7423:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 7424:                                '</li>';
 7425:             }
 7426:             if ($changes{'reportupdates'}) {
 7427:                 $resulttext .= '<li>'.
 7428:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 7429:                                     $offon[$env{'form.reportupdates'}].'".',
 7430:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7431:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 7432:                                 '</li>';
 7433:             }
 7434:             $resulttext .= '</ul>';
 7435:         } else {
 7436:             $resulttext = &mt('No changes made to contact information');
 7437:         }
 7438:     } else {
 7439:         $resulttext = '<span class="LC_error">'.
 7440:             &mt('An error occurred: [_1].',$putresult).'</span>';
 7441:     }
 7442:     return $resulttext;
 7443: }
 7444: 
 7445: sub modify_usercreation {
 7446:     my ($dom,%domconfig) = @_;
 7447:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 7448:     my $warningmsg;
 7449:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 7450:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 7451:             if ($key eq 'cancreate') {
 7452:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 7453:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 7454:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 7455:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 7456:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7457:                         } else {
 7458:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7459:                         }
 7460:                     }
 7461:                 }
 7462:             } elsif ($key eq 'email_rule') {
 7463:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 7464:             } else {
 7465:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 7466:             }
 7467:         }
 7468:     }
 7469:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 7470:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 7471:     my @contexts = ('author','course','requestcrs');
 7472:     foreach my $item(@contexts) {
 7473:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 7474:     }
 7475:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 7476:         foreach my $item (@contexts) {
 7477:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 7478:                 push(@{$changes{'cancreate'}},$item);
 7479:             }
 7480:         }
 7481:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 7482:         foreach my $item (@contexts) {
 7483:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 7484:                 if ($cancreate{$item} ne 'any') {
 7485:                     push(@{$changes{'cancreate'}},$item);
 7486:                 }
 7487:             } else {
 7488:                 if ($cancreate{$item} ne 'none') {
 7489:                     push(@{$changes{'cancreate'}},$item);
 7490:                 }
 7491:             }
 7492:         }
 7493:     } else {
 7494:         foreach my $item (@contexts)  {
 7495:             push(@{$changes{'cancreate'}},$item);
 7496:         }
 7497:     }
 7498: 
 7499:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 7500:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 7501:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 7502:                 push(@{$changes{'username_rule'}},$type);
 7503:             }
 7504:         }
 7505:         foreach my $type (@username_rule) {
 7506:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 7507:                 push(@{$changes{'username_rule'}},$type);
 7508:             }
 7509:         }
 7510:     } else {
 7511:         push(@{$changes{'username_rule'}},@username_rule);
 7512:     }
 7513: 
 7514:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 7515:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 7516:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 7517:                 push(@{$changes{'id_rule'}},$type);
 7518:             }
 7519:         }
 7520:         foreach my $type (@id_rule) {
 7521:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 7522:                 push(@{$changes{'id_rule'}},$type);
 7523:             }
 7524:         }
 7525:     } else {
 7526:         push(@{$changes{'id_rule'}},@id_rule);
 7527:     }
 7528: 
 7529:     my @authen_contexts = ('author','course','domain');
 7530:     my @authtypes = ('int','krb4','krb5','loc');
 7531:     my %authhash;
 7532:     foreach my $item (@authen_contexts) {
 7533:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 7534:         foreach my $auth (@authtypes) {
 7535:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 7536:                 $authhash{$item}{$auth} = 1;
 7537:             } else {
 7538:                 $authhash{$item}{$auth} = 0;
 7539:             }
 7540:         }
 7541:     }
 7542:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 7543:         foreach my $item (@authen_contexts) {
 7544:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 7545:                 foreach my $auth (@authtypes) {
 7546:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 7547:                         push(@{$changes{'authtypes'}},$item);
 7548:                         last;
 7549:                     }
 7550:                 }
 7551:             }
 7552:         }
 7553:     } else {
 7554:         foreach my $item (@authen_contexts) {
 7555:             push(@{$changes{'authtypes'}},$item);
 7556:         }
 7557:     }
 7558: 
 7559:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 7560:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 7561:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 7562:     $save_usercreate{'id_rule'} = \@id_rule;
 7563:     $save_usercreate{'username_rule'} = \@username_rule,
 7564:     $save_usercreate{'authtypes'} = \%authhash;
 7565: 
 7566:     my %usercreation_hash =  (
 7567:         usercreation     => \%save_usercreate,
 7568:     );
 7569: 
 7570:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 7571:                                              $dom);
 7572: 
 7573:     if ($putresult eq 'ok') {
 7574:         if (keys(%changes) > 0) {
 7575:             $resulttext = &mt('Changes made:').'<ul>';
 7576:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 7577:                 my %lt = &usercreation_types();
 7578:                 foreach my $type (@{$changes{'cancreate'}}) {
 7579:                     my $chgtext = $lt{$type}.', ';
 7580:                     if ($cancreate{$type} eq 'none') {
 7581:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 7582:                     } elsif ($cancreate{$type} eq 'any') {
 7583:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 7584:                     } elsif ($cancreate{$type} eq 'official') {
 7585:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 7586:                     } elsif ($cancreate{$type} eq 'unofficial') {
 7587:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 7588:                     }
 7589:                     $resulttext .= '<li>'.$chgtext.'</li>';
 7590:                 }
 7591:             }
 7592:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 7593:                 my ($rules,$ruleorder) = 
 7594:                     &Apache::lonnet::inst_userrules($dom,'username');
 7595:                 my $chgtext = '<ul>';
 7596:                 foreach my $type (@username_rule) {
 7597:                     if (ref($rules->{$type}) eq 'HASH') {
 7598:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 7599:                     }
 7600:                 }
 7601:                 $chgtext .= '</ul>';
 7602:                 if (@username_rule > 0) {
 7603:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 7604:                 } else {
 7605:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 7606:                 }
 7607:             }
 7608:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 7609:                 my ($idrules,$idruleorder) = 
 7610:                     &Apache::lonnet::inst_userrules($dom,'id');
 7611:                 my $chgtext = '<ul>';
 7612:                 foreach my $type (@id_rule) {
 7613:                     if (ref($idrules->{$type}) eq 'HASH') {
 7614:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 7615:                     }
 7616:                 }
 7617:                 $chgtext .= '</ul>';
 7618:                 if (@id_rule > 0) {
 7619:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 7620:                 } else {
 7621:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 7622:                 }
 7623:             }
 7624:             my %authname = &authtype_names();
 7625:             my %context_title = &context_names();
 7626:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7627:                 my $chgtext = '<ul>';
 7628:                 foreach my $type (@{$changes{'authtypes'}}) {
 7629:                     my @allowed;
 7630:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7631:                     foreach my $auth (@authtypes) {
 7632:                         if ($authhash{$type}{$auth}) {
 7633:                             push(@allowed,$authname{$auth});
 7634:                         }
 7635:                     }
 7636:                     if (@allowed > 0) {
 7637:                         $chgtext .= join(', ',@allowed).'</li>';
 7638:                     } else {
 7639:                         $chgtext .= &mt('none').'</li>';
 7640:                     }
 7641:                 }
 7642:                 $chgtext .= '</ul>';
 7643:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7644:                 $resulttext .= '</li>';
 7645:             }
 7646:             $resulttext .= '</ul>';
 7647:         } else {
 7648:             $resulttext = &mt('No changes made to user creation settings');
 7649:         }
 7650:     } else {
 7651:         $resulttext = '<span class="LC_error">'.
 7652:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7653:     }
 7654:     if ($warningmsg ne '') {
 7655:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7656:     }
 7657:     return $resulttext;
 7658: }
 7659: 
 7660: sub modify_selfcreation {
 7661:     my ($dom,%domconfig) = @_;
 7662:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 7663:     my (%save_usercreate,%save_usermodify);
 7664: #
 7665: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 7666: #
 7667:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 7668:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 7669:             if ($key eq 'cancreate') {
 7670:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 7671:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 7672:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 7673:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 7674:                             ($item eq 'emailusername') || ($item eq 'notify')) {
 7675:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7676:                         } else {
 7677:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7678:                         }
 7679:                     }
 7680:                 }
 7681:             } elsif ($key eq 'email_rule') {
 7682:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 7683:             } else {
 7684:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 7685:             }
 7686:         }
 7687:     }
 7688: #
 7689: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 7690: #
 7691:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7692:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7693:             if ($key eq 'selfcreate') {
 7694:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 7695:             } else {
 7696:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 7697:             }
 7698:         }
 7699:     }
 7700: 
 7701:     my @contexts = ('selfcreate');
 7702:     @{$cancreate{'selfcreate'}} = ();
 7703:     %{$cancreate{'emailusername'}} = ();
 7704:     @{$cancreate{'statustocreate'}} = ();
 7705:     my %selfcreatetypes = (
 7706:                              sso   => 'users authenticated by institutional single sign on',
 7707:                              login => 'users authenticated by institutional log-in',
 7708:                              email => 'users who provide a valid e-mail address for use as username (automatic creation)',
 7709:                              emailapproval => 'users who provide a valid e-mail address for use as username (queued for Domain Coordinator review)',
 7710:                           );
 7711: #
 7712: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 7713: # is permitted.
 7714: #
 7715:     foreach my $item ('login','sso','email') { 
 7716:         if ($item eq 'email') {
 7717:             if ($env{'form.cancreate_email'} eq 'email') {
 7718:                 push(@{$cancreate{'selfcreate'}},'email');
 7719:             } elsif ($env{'form.cancreate_email'} eq 'emailapproval') {
 7720:                 push(@{$cancreate{'selfcreate'}},'emailapproval');
 7721:             }
 7722:         } else {
 7723:             if ($env{'form.cancreate_'.$item}) {
 7724:                 push(@{$cancreate{'selfcreate'}},$item);
 7725:             }
 7726:         }
 7727:     }
 7728:     my (@email_rule,%userinfo,%savecaptcha);
 7729:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 7730: #
 7731: # Populate $cancreate{'emailusername'} hash ref with information types (if new user will provide data
 7732: # value set to one), if self-creation with e-mail address permitted.
 7733: #
 7734:     if (($env{'form.cancreate_email'} eq 'email') || ($env{'form.cancreate_email'} eq 'emailapproval')) {
 7735:         push(@contexts,'emailusername'); 
 7736:         map { $userinfo{$_} = 1; } &Apache::loncommon::get_env_multiple('form.canmodify_emailusername');
 7737:         if (ref($infofields) eq 'ARRAY') {
 7738:             foreach my $field (@{$infofields}) {
 7739:                 if ($userinfo{$field}) {
 7740:                     $cancreate{'emailusername'}{$field} = 1;
 7741:                 }
 7742:             }
 7743:         }
 7744: 
 7745: #
 7746: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 7747: # queued requests for self-creation of account using e-mail address as username
 7748: #
 7749: 
 7750:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 7751:         @approvalnotify = sort(@approvalnotify);
 7752:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 7753:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 7754:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 7755:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 7756:                     push(@{$changes{'cancreate'}},'notify');
 7757:                 }
 7758:             } else {
 7759:                 if ($cancreate{'notify'}{'approval'}) {
 7760:                     push(@{$changes{'cancreate'}},'notify');
 7761:                 }
 7762:             }
 7763:         } elsif ($cancreate{'notify'}{'approval'}) {
 7764:             push(@{$changes{'cancreate'}},'notify');
 7765:         }
 7766: 
 7767: #
 7768: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 7769: #
 7770:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 7771:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 7772:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 7773:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 7774:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 7775:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 7776:                         push(@{$changes{'email_rule'}},$type);
 7777:                     }
 7778:                 }
 7779:             }
 7780:             if (@email_rule > 0) {
 7781:                 foreach my $type (@email_rule) {
 7782:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 7783:                         push(@{$changes{'email_rule'}},$type);
 7784:                     }
 7785:                 }
 7786:             }
 7787:         } elsif (@email_rule > 0) {
 7788:             push(@{$changes{'email_rule'}},@email_rule);
 7789:         }
 7790:     }
 7791: #  
 7792: # Check if domain default is set appropriately, if selef-creation of accounts is to be available for
 7793: # institutional log-in.
 7794: #
 7795:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 7796:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7797:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 7798:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 7799:             $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.').' '.
 7800:                           &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.');
 7801:         }
 7802:     }
 7803:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7804:     my @fields = ('lastname','firstname','middlename','generation',
 7805:                   'permanentemail','id');
 7806:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7807: #
 7808: # Where usernames may created for institutional log-in and/or institutional single sign on:
 7809: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 7810: # may self-create accounts 
 7811: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 7812: # which the user may supply, if institutional data is unavailable.
 7813: #
 7814:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 7815:         if (ref($types) eq 'ARRAY') {
 7816:             if (@{$types} > 0) {
 7817:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 7818:                 push(@contexts,'statustocreate');
 7819:             } else {
 7820:                 undef($cancreate{'statustocreate'});
 7821:             } 
 7822:             push(@{$types},'default');
 7823:             $usertypes->{'default'} = $othertitle;
 7824:             foreach my $type (@{$types}) {
 7825:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 7826:                 foreach my $field (@fields) {
 7827:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 7828:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 7829:                     } else {
 7830:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 7831:                     }
 7832:                 }
 7833:             }
 7834:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 7835:                 foreach my $type (@{$types}) {
 7836:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 7837:                         foreach my $field (@fields) {
 7838:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 7839:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 7840:                                 push(@{$changes{'selfcreate'}},$type);
 7841:                                 last;
 7842:                             }
 7843:                         }
 7844:                     }
 7845:                 }
 7846:             } else {
 7847:                 foreach my $type (@{$types}) {
 7848:                     push(@{$changes{'selfcreate'}},$type);
 7849:                 }
 7850:             }
 7851:         }
 7852:     }
 7853:     foreach my $item (@contexts) {
 7854:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 7855:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 7856:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 7857:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 7858:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7859:                             push(@{$changes{'cancreate'}},$item);
 7860:                         }
 7861:                     }
 7862:                 }
 7863:             }
 7864:             if (ref($cancreate{$item}) eq 'ARRAY') {
 7865:                 foreach my $type (@{$cancreate{$item}}) {
 7866:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 7867:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7868:                             push(@{$changes{'cancreate'}},$item);
 7869:                         }
 7870:                     }
 7871:                 }
 7872:             }
 7873:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 7874:             if (ref($cancreate{$item}) eq 'HASH') {
 7875:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 7876:                     if (!$cancreate{$item}{$curr}) {
 7877:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7878:                             push(@{$changes{'cancreate'}},$item);
 7879:                         }
 7880:                     }
 7881:                 }
 7882:                 foreach my $field (keys(%{$cancreate{$item}})) {
 7883:                     if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 7884:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7885:                             push(@{$changes{'cancreate'}},$item);
 7886:                         }
 7887:                     }
 7888:                 }
 7889:             }
 7890:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 7891:             if (ref($cancreate{$item}) eq 'ARRAY') {
 7892:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 7893:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7894:                         push(@{$changes{'cancreate'}},$item);
 7895:                     }
 7896:                 }
 7897:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 7898:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 7899:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7900:                         push(@{$changes{'cancreate'}},$item);
 7901:                     }
 7902:                 }
 7903:             }
 7904:         } elsif ($item eq 'emailusername') {
 7905:             if (keys(%userinfo) > 0) {
 7906:                 push(@{$changes{'cancreate'}},$item);
 7907:             }
 7908:         }
 7909:     }
 7910: #
 7911: # Populate %save_usercreate hash with updates to self-creation configuration.
 7912: #
 7913:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 7914:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 7915:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 7916:     if (ref($cancreate{'notify'}) eq 'HASH') {
 7917:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 7918:     }
 7919:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 7920:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 7921:     }
 7922:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 7923:     $save_usercreate{'emailrule'} = \@email_rule;
 7924: 
 7925:     my %userconfig_hash = (
 7926:             usercreation     => \%save_usercreate,
 7927:             usermodification => \%save_usermodify,
 7928:     );
 7929:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 7930:                                              $dom);
 7931: #
 7932: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 7933: #
 7934:     if ($putresult eq 'ok') {
 7935:         if (keys(%changes) > 0) {
 7936:             $resulttext = &mt('Changes made:').'<ul>';
 7937:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 7938:                 my %lt = &selfcreation_types();
 7939:                 foreach my $type (@{$changes{'cancreate'}}) {
 7940:                     my $chgtext;
 7941:                     if ($type eq 'selfcreate') {
 7942:                         if (@{$cancreate{$type}} == 0) {
 7943:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 7944:                         } else {
 7945:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 7946:                                         '<ul>';
 7947:                             foreach my $case (@{$cancreate{$type}}) {
 7948:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 7949:                             }
 7950:                             $chgtext .= '</ul>';
 7951:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 7952:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 7953:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 7954:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 7955:                                             $chgtext .= '<br />'.
 7956:                                                         '<span class="LC_warning">'.
 7957:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 7958:                                                         '</span>';
 7959:                                         }
 7960:                                     }
 7961:                                 }
 7962:                             }
 7963:                         }
 7964:                     } elsif ($type eq 'statustocreate') {
 7965:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 7966:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 7967:                             if (@{$cancreate{'selfcreate'}} > 0) {
 7968:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 7969:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 7970:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 7971:                                         $chgtext .= '<br />'.
 7972:                                                     '<span class="LC_warning">'.
 7973:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 7974:                                                     '</span>';
 7975:                                     }
 7976:                                 } elsif (ref($usertypes) eq 'HASH') {
 7977:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 7978:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 7979:                                     } else {
 7980:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 7981:                                     }
 7982:                                     $chgtext .= '<ul>';
 7983:                                     foreach my $case (@{$cancreate{$type}}) {
 7984:                                         if ($case eq 'default') {
 7985:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 7986:                                         } else {
 7987:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 7988:                                         }
 7989:                                     }
 7990:                                     $chgtext .= '</ul>';
 7991:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 7992:                                         $chgtext .= '<br /><span class="LC_warning">'.
 7993:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 7994:                                                     '</span>';
 7995:                                     }
 7996:                                 }
 7997:                             } else {
 7998:                                 if (@{$cancreate{$type}} == 0) {
 7999:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8000:                                 } else {
 8001:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8002:                                 }
 8003:                             }
 8004:                         }
 8005:                     } elsif ($type eq 'captcha') {
 8006:                         if ($savecaptcha{$type} eq 'notused') {
 8007:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8008:                         } else {
 8009:                             my %captchas = &captcha_phrases();
 8010:                             if ($captchas{$savecaptcha{$type}}) {
 8011:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8012:                             } else {
 8013:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8014:                             }
 8015:                         }
 8016:                     } elsif ($type eq 'recaptchakeys') {
 8017:                         my ($privkey,$pubkey);
 8018:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8019:                             $pubkey = $savecaptcha{$type}{'public'};
 8020:                             $privkey = $savecaptcha{$type}{'private'};
 8021:                         }
 8022:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8023:                         if (!$pubkey) {
 8024:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8025:                         } else {
 8026:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8027:                         }
 8028:                         if (!$privkey) {
 8029:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8030:                         } else {
 8031:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8032:                         }
 8033:                         $chgtext .= '</ul>';
 8034:                     } elsif ($type eq 'emailusername') {
 8035:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8036:                             if (keys(%{$cancreate{'emailusername'}}) > 0) {
 8037:                                 $chgtext .= &mt('When self-creating account with e-mail as username, user will provide the following information:').
 8038:                                             '<ul>';
 8039:                                 foreach my $field (@{$infofields}) {
 8040:                                     if ($cancreate{'emailusername'}{$field}) {
 8041:                                         $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8042:                                     }
 8043:                                 }
 8044:                                 $chgtext .= '</ul>';
 8045:                             } else {
 8046:                                 $chgtext .= &mt('When self creating account with e-mail as username, user will not provide any information besides e-mail address');
 8047:                             }
 8048:                         }
 8049:                     } elsif ($type eq 'notify') {
 8050:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8051:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8052:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8053:                                 if ($cancreate{'notify'}{'approval'}) {
 8054:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8055:                                 }
 8056:                             }
 8057:                         }
 8058:                     }
 8059:                     if ($chgtext) {
 8060:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8061:                     }
 8062:                 }
 8063:             }
 8064:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8065:                 my ($emailrules,$emailruleorder) =
 8066:                     &Apache::lonnet::inst_userrules($dom,'email');
 8067:                 my $chgtext = '<ul>';
 8068:                 foreach my $type (@email_rule) {
 8069:                     if (ref($emailrules->{$type}) eq 'HASH') {
 8070:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 8071:                     }
 8072:                 }
 8073:                 $chgtext .= '</ul>';
 8074:                 if (@email_rule > 0) {
 8075:                     $resulttext .= '<li>'.
 8076:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 8077:                                        $chgtext.
 8078:                                    '</li>';
 8079:                 } else {
 8080:                     $resulttext .= '<li>'.
 8081:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 8082:                                    '</li>';
 8083:                 }
 8084:             }
 8085:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 8086:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 8087:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8088:                 foreach my $type (@{$changes{'selfcreate'}}) {
 8089:                     my $typename = $type;
 8090:                     if (ref($usertypes) eq 'HASH') {
 8091:                         if ($usertypes->{$type} ne '') {
 8092:                             $typename = $usertypes->{$type};
 8093:                         }
 8094:                     }
 8095:                     my @modifiable;
 8096:                     $resulttext .= '<li>'.
 8097:                                     &mt('Self-creation of account by users with status: [_1]',
 8098:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 8099:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 8100:                     foreach my $field (@fields) {
 8101:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 8102:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 8103:                         }
 8104:                     }
 8105:                     if (@modifiable > 0) {
 8106:                         $resulttext .= join(', ',@modifiable);
 8107:                     } else {
 8108:                         $resulttext .= &mt('none');
 8109:                     }
 8110:                     $resulttext .= '</li>';
 8111:                 }
 8112:                 $resulttext .= '</ul></li>';
 8113:             }
 8114:             $resulttext .= '</ul>';
 8115:         } else {
 8116:             $resulttext = &mt('No changes made to self-creation settings');
 8117:         }
 8118:     } else {
 8119:         $resulttext = '<span class="LC_error">'.
 8120:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8121:     }
 8122:     if ($warningmsg ne '') {
 8123:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8124:     }
 8125:     return $resulttext;
 8126: }
 8127: 
 8128: sub process_captcha {
 8129:     my ($container,$changes,$newsettings,$current) = @_;
 8130:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 8131:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 8132:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 8133:         $newsettings->{'captcha'} = 'original';
 8134:     }
 8135:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 8136:         if ($container eq 'cancreate') {
 8137:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8138:                 push(@{$changes->{'cancreate'}},'captcha');
 8139:             } elsif (!defined($changes->{'cancreate'})) {
 8140:                 $changes->{'cancreate'} = ['captcha'];
 8141:             }
 8142:         } else {
 8143:             $changes->{'captcha'} = 1;
 8144:         }
 8145:     }
 8146:     my ($newpub,$newpriv,$currpub,$currpriv);
 8147:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 8148:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 8149:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 8150:         $newpub =~ s/\W//g;
 8151:         $newpriv =~ s/\W//g;
 8152:         $newsettings->{'recaptchakeys'} = {
 8153:                                              public  => $newpub,
 8154:                                              private => $newpriv,
 8155:                                           };
 8156:     }
 8157:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 8158:         $currpub = $current->{'recaptchakeys'}{'public'};
 8159:         $currpriv = $current->{'recaptchakeys'}{'private'};
 8160:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 8161:             $newsettings->{'recaptchakeys'} = {
 8162:                                                  public  => '',
 8163:                                                  private => '',
 8164:                                               }
 8165:         }
 8166:     }
 8167:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 8168:         if ($container eq 'cancreate') {
 8169:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8170:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 8171:             } elsif (!defined($changes->{'cancreate'})) {
 8172:                 $changes->{'cancreate'} = ['recaptchakeys'];
 8173:             }
 8174:         } else {
 8175:             $changes->{'recaptchakeys'} = 1;
 8176:         }
 8177:     }
 8178:     return;
 8179: }
 8180: 
 8181: sub modify_usermodification {
 8182:     my ($dom,%domconfig) = @_;
 8183:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 8184:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8185:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8186:             if ($key eq 'selfcreate') {
 8187:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 8188:             } else {  
 8189:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 8190:             }
 8191:         }
 8192:     }
 8193:     my @contexts = ('author','course');
 8194:     my %context_title = (
 8195:                            author => 'In author context',
 8196:                            course => 'In course context',
 8197:                         );
 8198:     my @fields = ('lastname','firstname','middlename','generation',
 8199:                   'permanentemail','id');
 8200:     my %roles = (
 8201:                   author => ['ca','aa'],
 8202:                   course => ['st','ep','ta','in','cr'],
 8203:                 );
 8204:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8205:     foreach my $context (@contexts) {
 8206:         foreach my $role (@{$roles{$context}}) {
 8207:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 8208:             foreach my $item (@fields) {
 8209:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 8210:                     $modifyhash{$context}{$role}{$item} = 1;
 8211:                 } else {
 8212:                     $modifyhash{$context}{$role}{$item} = 0;
 8213:                 }
 8214:             }
 8215:         }
 8216:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 8217:             foreach my $role (@{$roles{$context}}) {
 8218:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 8219:                     foreach my $field (@fields) {
 8220:                         if ($modifyhash{$context}{$role}{$field} ne 
 8221:                                 $curr_usermodification{$context}{$role}{$field}) {
 8222:                             push(@{$changes{$context}},$role);
 8223:                             last;
 8224:                         }
 8225:                     }
 8226:                 }
 8227:             }
 8228:         } else {
 8229:             foreach my $context (@contexts) {
 8230:                 foreach my $role (@{$roles{$context}}) {
 8231:                     push(@{$changes{$context}},$role);
 8232:                 }
 8233:             }
 8234:         }
 8235:     }
 8236:     my %usermodification_hash =  (
 8237:                                    usermodification => \%modifyhash,
 8238:                                  );
 8239:     my $putresult = &Apache::lonnet::put_dom('configuration',
 8240:                                              \%usermodification_hash,$dom);
 8241:     if ($putresult eq 'ok') {
 8242:         if (keys(%changes) > 0) {
 8243:             $resulttext = &mt('Changes made: ').'<ul>';
 8244:             foreach my $context (@contexts) {
 8245:                 if (ref($changes{$context}) eq 'ARRAY') {
 8246:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 8247:                     if (ref($changes{$context}) eq 'ARRAY') {
 8248:                         foreach my $role (@{$changes{$context}}) {
 8249:                             my $rolename;
 8250:                             if ($role eq 'cr') {
 8251:                                 $rolename = &mt('Custom');
 8252:                             } else {
 8253:                                 $rolename = &Apache::lonnet::plaintext($role);
 8254:                             }
 8255:                             my @modifiable;
 8256:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 8257:                             foreach my $field (@fields) {
 8258:                                 if ($modifyhash{$context}{$role}{$field}) {
 8259:                                     push(@modifiable,$fieldtitles{$field});
 8260:                                 }
 8261:                             }
 8262:                             if (@modifiable > 0) {
 8263:                                 $resulttext .= join(', ',@modifiable);
 8264:                             } else {
 8265:                                 $resulttext .= &mt('none'); 
 8266:                             }
 8267:                             $resulttext .= '</li>';
 8268:                         }
 8269:                         $resulttext .= '</ul></li>';
 8270:                     }
 8271:                 }
 8272:             }
 8273:             $resulttext .= '</ul>';
 8274:         } else {
 8275:             $resulttext = &mt('No changes made to user modification settings');
 8276:         }
 8277:     } else {
 8278:         $resulttext = '<span class="LC_error">'.
 8279:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8280:     }
 8281:     return $resulttext;
 8282: }
 8283: 
 8284: sub modify_defaults {
 8285:     my ($dom,$lastactref,%domconfig) = @_;
 8286:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 8287:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8288:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 8289:     my @authtypes = ('internal','krb4','krb5','localauth');
 8290:     foreach my $item (@items) {
 8291:         $newvalues{$item} = $env{'form.'.$item};
 8292:         if ($item eq 'auth_def') {
 8293:             if ($newvalues{$item} ne '') {
 8294:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 8295:                     push(@errors,$item);
 8296:                 }
 8297:             }
 8298:         } elsif ($item eq 'lang_def') {
 8299:             if ($newvalues{$item} ne '') {
 8300:                 if ($newvalues{$item} =~ /^(\w+)/) {
 8301:                     my $langcode = $1;
 8302:                     if ($langcode ne 'x_chef') {
 8303:                         if (code2language($langcode) eq '') {
 8304:                             push(@errors,$item);
 8305:                         }
 8306:                     }
 8307:                 } else {
 8308:                     push(@errors,$item);
 8309:                 }
 8310:             }
 8311:         } elsif ($item eq 'timezone_def') {
 8312:             if ($newvalues{$item} ne '') {
 8313:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 8314:                     push(@errors,$item);   
 8315:                 }
 8316:             }
 8317:         } elsif ($item eq 'datelocale_def') {
 8318:             if ($newvalues{$item} ne '') {
 8319:                 my @datelocale_ids = DateTime::Locale->ids();
 8320:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 8321:                     push(@errors,$item);
 8322:                 }
 8323:             }
 8324:         } elsif ($item eq 'portal_def') {
 8325:             if ($newvalues{$item} ne '') {
 8326:                 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])\/?$/) {
 8327:                     push(@errors,$item);
 8328:                 }
 8329:             }
 8330:         }
 8331:         if (grep(/^\Q$item\E$/,@errors)) {
 8332:             $newvalues{$item} = $domdefaults{$item};
 8333:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 8334:             $changes{$item} = 1;
 8335:         }
 8336:         $domdefaults{$item} = $newvalues{$item};
 8337:     }
 8338:     my %defaults_hash = (
 8339:                          defaults => \%newvalues,
 8340:                         );
 8341:     my $title = &defaults_titles();
 8342:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 8343:                                              $dom);
 8344:     if ($putresult eq 'ok') {
 8345:         if (keys(%changes) > 0) {
 8346:             $resulttext = &mt('Changes made:').'<ul>';
 8347:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 8348:             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";
 8349:             foreach my $item (sort(keys(%changes))) {
 8350:                 my $value = $env{'form.'.$item};
 8351:                 if ($value eq '') {
 8352:                     $value = &mt('none');
 8353:                 } elsif ($item eq 'auth_def') {
 8354:                     my %authnames = &authtype_names();
 8355:                     my %shortauth = (
 8356:                              internal => 'int',
 8357:                              krb4 => 'krb4',
 8358:                              krb5 => 'krb5',
 8359:                              localauth  => 'loc',
 8360:                     );
 8361:                     $value = $authnames{$shortauth{$value}};
 8362:                 }
 8363:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 8364:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 8365:             }
 8366:             $resulttext .= '</ul>';
 8367:             $mailmsgtext .= "\n";
 8368:             my $cachetime = 24*60*60;
 8369:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8370:             if (ref($lastactref) eq 'HASH') {
 8371:                 $lastactref->{'domdefaults'} = 1;
 8372:             }
 8373:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 8374:                 my $notify = 1;
 8375:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 8376:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 8377:                         $notify = 0;
 8378:                     }
 8379:                 }
 8380:                 if ($notify) {
 8381:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 8382:                                                "LON-CAPA Domain Settings Change - $dom",
 8383:                                                $mailmsgtext);
 8384:                 }
 8385:             }
 8386:         } else {
 8387:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 8388:         }
 8389:     } else {
 8390:         $resulttext = '<span class="LC_error">'.
 8391:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8392:     }
 8393:     if (@errors > 0) {
 8394:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 8395:         foreach my $item (@errors) {
 8396:             $resulttext .= ' "'.$title->{$item}.'",';
 8397:         }
 8398:         $resulttext =~ s/,$//;
 8399:     }
 8400:     return $resulttext;
 8401: }
 8402: 
 8403: sub modify_scantron {
 8404:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 8405:     my ($resulttext,%confhash,%changes,$errors);
 8406:     my $custom = 'custom.tab';
 8407:     my $default = 'default.tab';
 8408:     my $servadm = $r->dir_config('lonAdmEMail');
 8409:     my ($configuserok,$author_ok,$switchserver) = 
 8410:         &config_check($dom,$confname,$servadm);
 8411:     if ($env{'form.scantronformat.filename'} ne '') {
 8412:         my $error;
 8413:         if ($configuserok eq 'ok') {
 8414:             if ($switchserver) {
 8415:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 8416:             } else {
 8417:                 if ($author_ok eq 'ok') {
 8418:                     my ($result,$scantronurl) =
 8419:                         &publishlogo($r,'upload','scantronformat',$dom,
 8420:                                      $confname,'scantron','','',$custom);
 8421:                     if ($result eq 'ok') {
 8422:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 8423:                         $changes{'scantronformat'} = 1;
 8424:                     } else {
 8425:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 8426:                     }
 8427:                 } else {
 8428:                     $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);
 8429:                 }
 8430:             }
 8431:         } else {
 8432:             $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);
 8433:         }
 8434:         if ($error) {
 8435:             &Apache::lonnet::logthis($error);
 8436:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8437:         }
 8438:     }
 8439:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 8440:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 8441:             if ($env{'form.scantronformat_del'}) {
 8442:                 $confhash{'scantron'}{'scantronformat'} = '';
 8443:                 $changes{'scantronformat'} = 1;
 8444:             }
 8445:         }
 8446:     }
 8447:     if (keys(%confhash) > 0) {
 8448:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 8449:                                                  $dom);
 8450:         if ($putresult eq 'ok') {
 8451:             if (keys(%changes) > 0) {
 8452:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 8453:                     $resulttext = &mt('Changes made:').'<ul>';
 8454:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 8455:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 8456:                     } else {
 8457:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 8458:                     }
 8459:                     $resulttext .= '</ul>';
 8460:                 } else {
 8461:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 8462:                 }
 8463:                 $resulttext .= '</ul>';
 8464:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 8465:                 if (ref($lastactref) eq 'HASH') {
 8466:                     $lastactref->{'domainconfig'} = 1;
 8467:                 }
 8468:             } else {
 8469:                 $resulttext = &mt('No changes made to bubblesheet format file');
 8470:             }
 8471:         } else {
 8472:             $resulttext = '<span class="LC_error">'.
 8473:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 8474:         }
 8475:     } else {
 8476:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 8477:     }
 8478:     if ($errors) {
 8479:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8480:                        $errors.'</ul>';
 8481:     }
 8482:     return $resulttext;
 8483: }
 8484: 
 8485: sub modify_coursecategories {
 8486:     my ($dom,%domconfig) = @_;
 8487:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 8488:         $cathash);
 8489:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 8490:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8491:         $cathash = $domconfig{'coursecategories'}{'cats'};
 8492:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 8493:             $changes{'togglecats'} = 1;
 8494:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 8495:         }
 8496:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 8497:             $changes{'categorize'} = 1;
 8498:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 8499:         }
 8500:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 8501:             $changes{'togglecatscomm'} = 1;
 8502:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 8503:         }
 8504:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 8505:             $changes{'categorizecomm'} = 1;
 8506:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 8507:         }
 8508:     } else {
 8509:         $changes{'togglecats'} = 1;
 8510:         $changes{'categorize'} = 1;
 8511:         $changes{'togglecatscomm'} = 1;
 8512:         $changes{'categorizecomm'} = 1;
 8513:         $domconfig{'coursecategories'} = {
 8514:                                              togglecats => $env{'form.togglecats'},
 8515:                                              categorize => $env{'form.categorize'},
 8516:                                              togglecatscomm => $env{'form.togglecatscomm'},
 8517:                                              categorizecomm => $env{'form.categorizecomm'},
 8518:                                          };
 8519:     }
 8520:     if (ref($cathash) eq 'HASH') {
 8521:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 8522:             push (@deletecategory,'instcode::0');
 8523:         }
 8524:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 8525:             push(@deletecategory,'communities::0');
 8526:         }
 8527:     }
 8528:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 8529:     if (ref($cathash) eq 'HASH') {
 8530:         if (@deletecategory > 0) {
 8531:             #FIXME Need to remove category from all courses using a deleted category 
 8532:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 8533:             foreach my $item (@deletecategory) {
 8534:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 8535:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 8536:                     $deletions{$item} = 1;
 8537:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 8538:                 }
 8539:             }
 8540:         }
 8541:         foreach my $item (keys(%{$cathash})) {
 8542:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8543:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 8544:                 $reorderings{$item} = 1;
 8545:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 8546:             }
 8547:             if ($env{'form.addcategory_name_'.$item} ne '') {
 8548:                 my $newcat = $env{'form.addcategory_name_'.$item};
 8549:                 my $newdepth = $depth+1;
 8550:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8551:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 8552:                 $adds{$newitem} = 1; 
 8553:             }
 8554:             if ($env{'form.subcat_'.$item} ne '') {
 8555:                 my $newcat = $env{'form.subcat_'.$item};
 8556:                 my $newdepth = $depth+1;
 8557:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8558:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 8559:                 $adds{$newitem} = 1;
 8560:             }
 8561:         }
 8562:     }
 8563:     if ($env{'form.instcode'} eq '1') {
 8564:         if (ref($cathash) eq 'HASH') {
 8565:             my $newitem = 'instcode::0';
 8566:             if ($cathash->{$newitem} eq '') {  
 8567:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 8568:                 $adds{$newitem} = 1;
 8569:             }
 8570:         } else {
 8571:             my $newitem = 'instcode::0';
 8572:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 8573:             $adds{$newitem} = 1;
 8574:         }
 8575:     }
 8576:     if ($env{'form.communities'} eq '1') {
 8577:         if (ref($cathash) eq 'HASH') {
 8578:             my $newitem = 'communities::0';
 8579:             if ($cathash->{$newitem} eq '') {
 8580:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 8581:                 $adds{$newitem} = 1;
 8582:             }
 8583:         } else {
 8584:             my $newitem = 'communities::0';
 8585:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 8586:             $adds{$newitem} = 1;
 8587:         }
 8588:     }
 8589:     if ($env{'form.addcategory_name'} ne '') {
 8590:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 8591:             ($env{'form.addcategory_name'} ne 'communities')) {
 8592:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 8593:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 8594:             $adds{$newitem} = 1;
 8595:         }
 8596:     }
 8597:     my $putresult;
 8598:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8599:         if (keys(%deletions) > 0) {
 8600:             foreach my $key (keys(%deletions)) {
 8601:                 if ($predelallitems{$key} ne '') {
 8602:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 8603:                 }
 8604:             }
 8605:         }
 8606:         my (@chkcats,@chktrails,%chkallitems);
 8607:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 8608:         if (ref($chkcats[0]) eq 'ARRAY') {
 8609:             my $depth = 0;
 8610:             my $chg = 0;
 8611:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 8612:                 my $name = $chkcats[0][$i];
 8613:                 my $item;
 8614:                 if ($name eq '') {
 8615:                     $chg ++;
 8616:                 } else {
 8617:                     $item = &escape($name).'::0';
 8618:                     if ($chg) {
 8619:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 8620:                     }
 8621:                     $depth ++; 
 8622:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 8623:                     $depth --;
 8624:                 }
 8625:             }
 8626:         }
 8627:     }
 8628:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8629:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 8630:         if ($putresult eq 'ok') {
 8631:             my %title = (
 8632:                          togglecats     => 'Show/Hide a course in catalog',
 8633:                          categorize     => 'Assign a category to a course',
 8634:                          togglecatscomm => 'Show/Hide a community in catalog',
 8635:                          categorizecomm => 'Assign a category to a community',
 8636:                         );
 8637:             my %level = (
 8638:                          dom  => 'set in Domain ("Modify Course/Community")',
 8639:                          crs  => 'set in Course ("Course Configuration")',
 8640:                          comm => 'set in Community ("Community Configuration")',
 8641:                         );
 8642:             $resulttext = &mt('Changes made:').'<ul>';
 8643:             if ($changes{'togglecats'}) {
 8644:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 8645:             }
 8646:             if ($changes{'categorize'}) {
 8647:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 8648:             }
 8649:             if ($changes{'togglecatscomm'}) {
 8650:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 8651:             }
 8652:             if ($changes{'categorizecomm'}) {
 8653:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 8654:             }
 8655:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8656:                 my $cathash;
 8657:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8658:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 8659:                 } else {
 8660:                     $cathash = {};
 8661:                 } 
 8662:                 my (@cats,@trails,%allitems);
 8663:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 8664:                 if (keys(%deletions) > 0) {
 8665:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 8666:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 8667:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 8668:                     }
 8669:                     $resulttext .= '</ul></li>';
 8670:                 }
 8671:                 if (keys(%reorderings) > 0) {
 8672:                     my %sort_by_trail;
 8673:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 8674:                     foreach my $key (keys(%reorderings)) {
 8675:                         if ($allitems{$key} ne '') {
 8676:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 8677:                         }
 8678:                     }
 8679:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 8680:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 8681:                     }
 8682:                     $resulttext .= '</ul></li>';
 8683:                 }
 8684:                 if (keys(%adds) > 0) {
 8685:                     my %sort_by_trail;
 8686:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 8687:                     foreach my $key (keys(%adds)) {
 8688:                         if ($allitems{$key} ne '') {
 8689:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 8690:                         }
 8691:                     }
 8692:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 8693:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 8694:                     }
 8695:                     $resulttext .= '</ul></li>';
 8696:                 }
 8697:             }
 8698:             $resulttext .= '</ul>';
 8699:         } else {
 8700:             $resulttext = '<span class="LC_error">'.
 8701:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8702:         }
 8703:     } else {
 8704:         $resulttext = &mt('No changes made to course and community categories');
 8705:     }
 8706:     return $resulttext;
 8707: }
 8708: 
 8709: sub modify_serverstatuses {
 8710:     my ($dom,%domconfig) = @_;
 8711:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 8712:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 8713:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 8714:     }
 8715:     my @pages = &serverstatus_pages();
 8716:     foreach my $type (@pages) {
 8717:         $newserverstatus{$type}{'namedusers'} = '';
 8718:         $newserverstatus{$type}{'machines'} = '';
 8719:         if (defined($env{'form.'.$type.'_namedusers'})) {
 8720:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 8721:             my @okusers;
 8722:             foreach my $user (@users) {
 8723:                 my ($uname,$udom) = split(/:/,$user);
 8724:                 if (($udom =~ /^$match_domain$/) &&   
 8725:                     (&Apache::lonnet::domain($udom)) &&
 8726:                     ($uname =~ /^$match_username$/)) {
 8727:                     if (!grep(/^\Q$user\E/,@okusers)) {
 8728:                         push(@okusers,$user);
 8729:                     }
 8730:                 }
 8731:             }
 8732:             if (@okusers > 0) {
 8733:                  @okusers = sort(@okusers);
 8734:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 8735:             }
 8736:         }
 8737:         if (defined($env{'form.'.$type.'_machines'})) {
 8738:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 8739:             my @okmachines;
 8740:             foreach my $ip (@machines) {
 8741:                 my @parts = split(/\./,$ip);
 8742:                 next if (@parts < 4);
 8743:                 my $badip = 0;
 8744:                 for (my $i=0; $i<4; $i++) {
 8745:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 8746:                         $badip = 1;
 8747:                         last;
 8748:                     }
 8749:                 }
 8750:                 if (!$badip) {
 8751:                     push(@okmachines,$ip);     
 8752:                 }
 8753:             }
 8754:             @okmachines = sort(@okmachines);
 8755:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 8756:         }
 8757:     }
 8758:     my %serverstatushash =  (
 8759:                                 serverstatuses => \%newserverstatus,
 8760:                             );
 8761:     foreach my $type (@pages) {
 8762:         foreach my $setting ('namedusers','machines') {
 8763:             my (@current,@new);
 8764:             if (ref($currserverstatus{$type}) eq 'HASH') {
 8765:                 if ($currserverstatus{$type}{$setting} ne '') { 
 8766:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 8767:                 }
 8768:             }
 8769:             if ($newserverstatus{$type}{$setting} ne '') {
 8770:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 8771:             }
 8772:             if (@current > 0) {
 8773:                 if (@new > 0) {
 8774:                     foreach my $item (@current) {
 8775:                         if (!grep(/^\Q$item\E$/,@new)) {
 8776:                             $changes{$type}{$setting} = 1;
 8777:                             last;
 8778:                         }
 8779:                     }
 8780:                     foreach my $item (@new) {
 8781:                         if (!grep(/^\Q$item\E$/,@current)) {
 8782:                             $changes{$type}{$setting} = 1;
 8783:                             last;
 8784:                         }
 8785:                     }
 8786:                 } else {
 8787:                     $changes{$type}{$setting} = 1;
 8788:                 }
 8789:             } elsif (@new > 0) {
 8790:                 $changes{$type}{$setting} = 1;
 8791:             }
 8792:         }
 8793:     }
 8794:     if (keys(%changes) > 0) {
 8795:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 8796:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8797:                                                  \%serverstatushash,$dom);
 8798:         if ($putresult eq 'ok') {
 8799:             $resulttext .= &mt('Changes made:').'<ul>';
 8800:             foreach my $type (@pages) {
 8801:                 if (ref($changes{$type}) eq 'HASH') {
 8802:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 8803:                     if ($changes{$type}{'namedusers'}) {
 8804:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 8805:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 8806:                         } else {
 8807:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 8808:                         }
 8809:                     }
 8810:                     if ($changes{$type}{'machines'}) {
 8811:                         if ($newserverstatus{$type}{'machines'} eq '') {
 8812:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 8813:                         } else {
 8814:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 8815:                         }
 8816: 
 8817:                     }
 8818:                     $resulttext .= '</ul></li>';
 8819:                 }
 8820:             }
 8821:             $resulttext .= '</ul>';
 8822:         } else {
 8823:             $resulttext = '<span class="LC_error">'.
 8824:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 8825: 
 8826:         }
 8827:     } else {
 8828:         $resulttext = &mt('No changes made to access to server status pages');
 8829:     }
 8830:     return $resulttext;
 8831: }
 8832: 
 8833: sub modify_helpsettings {
 8834:     my ($r,$dom,$confname,%domconfig) = @_;
 8835:     my ($resulttext,$errors,%changes,%helphash);
 8836:     my %defaultchecked = ('submitbugs' => 'on');
 8837:     my @offon = ('off','on');
 8838:     my @toggles = ('submitbugs');
 8839:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 8840:         foreach my $item (@toggles) {
 8841:             if ($defaultchecked{$item} eq 'on') { 
 8842:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 8843:                     if ($env{'form.'.$item} eq '0') {
 8844:                         $changes{$item} = 1;
 8845:                     }
 8846:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8847:                     $changes{$item} = 1;
 8848:                 }
 8849:             } elsif ($defaultchecked{$item} eq 'off') {
 8850:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 8851:                     if ($env{'form.'.$item} eq '1') {
 8852:                         $changes{$item} = 1;
 8853:                     }
 8854:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8855:                     $changes{$item} = 1;
 8856:                 }
 8857:             }
 8858:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 8859:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 8860:             }
 8861:         }
 8862:     }
 8863:     my $putresult;
 8864:     if (keys(%changes) > 0) {
 8865:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 8866:         if ($putresult eq 'ok') {
 8867:             $resulttext = &mt('Changes made:').'<ul>';
 8868:             foreach my $item (sort(keys(%changes))) {
 8869:                 if ($item eq 'submitbugs') {
 8870:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 8871:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 8872:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 8873:                 }
 8874:             }
 8875:             $resulttext .= '</ul>';
 8876:         } else {
 8877:             $resulttext = &mt('No changes made to help settings');
 8878:             $errors .= '<li><span class="LC_error">'.
 8879:                        &mt('An error occurred storing the settings: [_1]',
 8880:                            $putresult).'</span></li>';
 8881:         }
 8882:     }
 8883:     if ($errors) {
 8884:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8885:                        $errors.'</ul>';
 8886:     }
 8887:     return $resulttext;
 8888: }
 8889: 
 8890: sub modify_coursedefaults {
 8891:     my ($dom,$lastactref,%domconfig) = @_;
 8892:     my ($resulttext,$errors,%changes,%defaultshash);
 8893:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 8894:     my @toggles = ('canuse_pdfforms');
 8895:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 8896:                    'uploadquota_community','uploadquota_textbook');
 8897:     my @types = ('official','unofficial','community','textbook');
 8898:     my %staticdefaults = (
 8899:                            anonsurvey_threshold => 10,
 8900:                            uploadquota          => 500,
 8901:                          );
 8902: 
 8903:     $defaultshash{'coursedefaults'} = {};
 8904: 
 8905:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 8906:         if ($domconfig{'coursedefaults'} eq '') {
 8907:             $domconfig{'coursedefaults'} = {};
 8908:         }
 8909:     }
 8910: 
 8911:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 8912:         foreach my $item (@toggles) {
 8913:             if ($defaultchecked{$item} eq 'on') {
 8914:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 8915:                     ($env{'form.'.$item} eq '0')) {
 8916:                     $changes{$item} = 1;
 8917:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 8918:                     $changes{$item} = 1;
 8919:                 }
 8920:             } elsif ($defaultchecked{$item} eq 'off') {
 8921:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 8922:                     ($env{'form.'.$item} eq '1')) {
 8923:                     $changes{$item} = 1;
 8924:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 8925:                     $changes{$item} = 1;
 8926:                 }
 8927:             }
 8928:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 8929:         }
 8930:         foreach my $item (@numbers) {
 8931:             my ($currdef,$newdef);
 8932:             $newdef = $env{'form.'.$item};
 8933:             if ($item eq 'anonsurvey_threshold') {
 8934:                 $currdef = $domconfig{'coursedefaults'}{$item};
 8935:                 $newdef =~ s/\D//g;
 8936:                 if ($newdef eq '' || $newdef < 1) {
 8937:                     $newdef = 1;
 8938:                 }
 8939:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 8940:             } else {
 8941:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 8942:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8943:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 8944:                 }
 8945:                 $newdef =~ s/[^\w.\-]//g;
 8946:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 8947:             }
 8948:             if ($currdef ne $newdef) {
 8949:                 my $staticdef;
 8950:                 if ($item eq 'anonsurvey_threshold') {
 8951:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
 8952:                         $changes{$item} = 1;
 8953:                     }
 8954:                 } else {
 8955:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
 8956:                         $changes{'uploadquota'} = 1;
 8957:                     }
 8958:                 }
 8959:             }
 8960:         }
 8961:         my $officialcreds = $env{'form.official_credits'};
 8962:         $officialcreds =~ s/[^\d.]+//g;
 8963:         my $unofficialcreds = $env{'form.unofficial_credits'};
 8964:         $unofficialcreds =~ s/[^\d.]+//g;
 8965:         my $textbookcreds = $env{'form.textbook_credits'};
 8966:         $textbookcreds =~ s/[^\d.]+//g;
 8967:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 8968:                 ($env{'form.coursecredits'} eq '1')) {
 8969:                 $changes{'coursecredits'} = 1;
 8970:         } else {
 8971:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 8972:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
 8973:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
 8974:                 $changes{'coursecredits'} = 1;
 8975:             }
 8976:         }
 8977:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 8978:             official   => $officialcreds,
 8979:             unofficial => $unofficialcreds,
 8980:             textbook   => $textbookcreds,
 8981:         }
 8982:     }
 8983:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8984:                                              $dom);
 8985:     if ($putresult eq 'ok') {
 8986:         if (keys(%changes) > 0) {
 8987:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8988:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || ($changes{'uploadquota'})) {
 8989:                 if ($changes{'canuse_pdfforms'}) {
 8990:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 8991:                 }
 8992:                 if ($changes{'coursecredits'}) {
 8993:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 8994:                         $domdefaults{'officialcredits'} =
 8995:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 8996:                         $domdefaults{'unofficialcredits'} =
 8997:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 8998:                         $domdefaults{'textbookcredits'} =
 8999:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
 9000:                     }
 9001:                 }
 9002:                 if ($changes{'uploadquota'}) {
 9003:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9004:                         foreach my $type (@types) {
 9005:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
 9006:                         }
 9007:                     }
 9008:                 }
 9009:                 my $cachetime = 24*60*60;
 9010:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9011:                 if (ref($lastactref) eq 'HASH') {
 9012:                     $lastactref->{'domdefaults'} = 1;
 9013:                 }
 9014:             }
 9015:             $resulttext = &mt('Changes made:').'<ul>';
 9016:             foreach my $item (sort(keys(%changes))) {
 9017:                 if ($item eq 'canuse_pdfforms') {
 9018:                     if ($env{'form.'.$item} eq '1') {
 9019:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 9020:                     } else {
 9021:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 9022:                     }
 9023:                 } elsif ($item eq 'anonsurvey_threshold') {
 9024:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 9025:                 } elsif ($item eq 'uploadquota') {
 9026:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9027:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
 9028:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
 9029:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
 9030:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
 9031: 
 9032:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
 9033:                                        '</ul>'.
 9034:                                        '</li>';
 9035:                     } else {
 9036:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
 9037:                     }
 9038:                 } elsif ($item eq 'coursecredits') {
 9039:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 9040:                         if (($domdefaults{'officialcredits'} eq '') &&
 9041:                             ($domdefaults{'unofficialcredits'} eq '') &&
 9042:                             ($domdefaults{'textbookcredits'} eq '')) {
 9043:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 9044:                         } else {
 9045:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
 9046:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
 9047:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
 9048:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
 9049:                                            '</ul>'.
 9050:                                            '</li>';
 9051:                         }
 9052:                     } else {
 9053:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 9054:                     }
 9055:                 }
 9056:             }
 9057:             $resulttext .= '</ul>';
 9058:         } else {
 9059:             $resulttext = &mt('No changes made to course defaults');
 9060:         }
 9061:     } else {
 9062:         $resulttext = '<span class="LC_error">'.
 9063:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9064:     }
 9065:     return $resulttext;
 9066: }
 9067: 
 9068: sub modify_usersessions {
 9069:     my ($dom,$lastactref,%domconfig) = @_;
 9070:     my @hostingtypes = ('version','excludedomain','includedomain');
 9071:     my @offloadtypes = ('primary','default');
 9072:     my %types = (
 9073:                   remote => \@hostingtypes,
 9074:                   hosted => \@hostingtypes,
 9075:                   spares => \@offloadtypes,
 9076:                 );
 9077:     my @prefixes = ('remote','hosted','spares');
 9078:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 9079:     my (%by_ip,%by_location,@intdoms);
 9080:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 9081:     my @locations = sort(keys(%by_location));
 9082:     my (%defaultshash,%changes);
 9083:     foreach my $prefix (@prefixes) {
 9084:         $defaultshash{'usersessions'}{$prefix} = {};
 9085:     }
 9086:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9087:     my $resulttext;
 9088:     my %iphost = &Apache::lonnet::get_iphost();
 9089:     foreach my $prefix (@prefixes) {
 9090:         next if ($prefix eq 'spares');
 9091:         foreach my $type (@{$types{$prefix}}) {
 9092:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 9093:             if ($type eq 'version') {
 9094:                 my $value = $env{'form.'.$prefix.'_'.$type};
 9095:                 my $okvalue;
 9096:                 if ($value ne '') {
 9097:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 9098:                         $okvalue = $value;
 9099:                     }
 9100:                 }
 9101:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9102:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 9103:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 9104:                             if ($inuse == 0) {
 9105:                                 $changes{$prefix}{$type} = 1;
 9106:                             } else {
 9107:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 9108:                                     $changes{$prefix}{$type} = 1;
 9109:                                 }
 9110:                                 if ($okvalue ne '') {
 9111:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9112:                                 } 
 9113:                             }
 9114:                         } else {
 9115:                             if (($inuse == 1) && ($okvalue ne '')) {
 9116:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9117:                                 $changes{$prefix}{$type} = 1;
 9118:                             }
 9119:                         }
 9120:                     } else {
 9121:                         if (($inuse == 1) && ($okvalue ne '')) {
 9122:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9123:                             $changes{$prefix}{$type} = 1;
 9124:                         }
 9125:                     }
 9126:                 } else {
 9127:                     if (($inuse == 1) && ($okvalue ne '')) {
 9128:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9129:                         $changes{$prefix}{$type} = 1;
 9130:                     }
 9131:                 }
 9132:             } else {
 9133:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 9134:                 my @okvals;
 9135:                 foreach my $val (@vals) {
 9136:                     if ($val =~ /:/) {
 9137:                         my @items = split(/:/,$val);
 9138:                         foreach my $item (@items) {
 9139:                             if (ref($by_location{$item}) eq 'ARRAY') {
 9140:                                 push(@okvals,$item);
 9141:                             }
 9142:                         }
 9143:                     } else {
 9144:                         if (ref($by_location{$val}) eq 'ARRAY') {
 9145:                             push(@okvals,$val);
 9146:                         }
 9147:                     }
 9148:                 }
 9149:                 @okvals = sort(@okvals);
 9150:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9151:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 9152:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 9153:                             if ($inuse == 0) {
 9154:                                 $changes{$prefix}{$type} = 1; 
 9155:                             } else {
 9156:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9157:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 9158:                                 if (@changed > 0) {
 9159:                                     $changes{$prefix}{$type} = 1;
 9160:                                 }
 9161:                             }
 9162:                         } else {
 9163:                             if ($inuse == 1) {
 9164:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9165:                                 $changes{$prefix}{$type} = 1;
 9166:                             }
 9167:                         } 
 9168:                     } else {
 9169:                         if ($inuse == 1) {
 9170:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9171:                             $changes{$prefix}{$type} = 1;
 9172:                         }
 9173:                     }
 9174:                 } else {
 9175:                     if ($inuse == 1) {
 9176:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9177:                         $changes{$prefix}{$type} = 1;
 9178:                     }
 9179:                 }
 9180:             }
 9181:         }
 9182:     }
 9183: 
 9184:     my @alldoms = &Apache::lonnet::all_domains();
 9185:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9186:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 9187:     my $savespares;
 9188: 
 9189:     foreach my $lonhost (sort(keys(%servers))) {
 9190:         my $serverhomeID =
 9191:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 9192:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 9193:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 9194:         my %spareschg;
 9195:         foreach my $type (@{$types{'spares'}}) {
 9196:             my @okspares;
 9197:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 9198:             foreach my $server (@checked) {
 9199:                 if (&Apache::lonnet::hostname($server) ne '') {
 9200:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 9201:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 9202:                             push(@okspares,$server);
 9203:                         }
 9204:                     }
 9205:                 }
 9206:             }
 9207:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 9208:             my $newspare;
 9209:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 9210:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 9211:                     $newspare = $new;
 9212:                 }
 9213:             }
 9214:             my @spares;
 9215:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 9216:                 @spares = sort(@okspares,$newspare);
 9217:             } else {
 9218:                 @spares = sort(@okspares);
 9219:             }
 9220:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 9221:             if (ref($spareid{$lonhost}) eq 'HASH') {
 9222:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 9223:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 9224:                     if (@diffs > 0) {
 9225:                         $spareschg{$type} = 1;
 9226:                     }
 9227:                 }
 9228:             }
 9229:         }
 9230:         if (keys(%spareschg) > 0) {
 9231:             $changes{'spares'}{$lonhost} = \%spareschg;
 9232:         }
 9233:     }
 9234: 
 9235:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9236:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 9237:             if (ref($changes{'spares'}) eq 'HASH') {
 9238:                 if (keys(%{$changes{'spares'}}) > 0) {
 9239:                     $savespares = 1;
 9240:                 }
 9241:             }
 9242:         } else {
 9243:             $savespares = 1;
 9244:         }
 9245:     }
 9246: 
 9247:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 9248:     if ((keys(%changes) > 0) || ($savespares)) {
 9249:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9250:                                                  $dom);
 9251:         if ($putresult eq 'ok') {
 9252:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 9253:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 9254:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 9255:                 }
 9256:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 9257:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 9258:                 }
 9259:             }
 9260:             my $cachetime = 24*60*60;
 9261:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9262:             if (ref($lastactref) eq 'HASH') {
 9263:                 $lastactref->{'domdefaults'} = 1;
 9264:             }
 9265:             if (keys(%changes) > 0) {
 9266:                 my %lt = &usersession_titles();
 9267:                 $resulttext = &mt('Changes made:').'<ul>';
 9268:                 foreach my $prefix (@prefixes) {
 9269:                     if (ref($changes{$prefix}) eq 'HASH') {
 9270:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 9271:                         if ($prefix eq 'spares') {
 9272:                             if (ref($changes{$prefix}) eq 'HASH') {
 9273:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 9274:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 9275:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 9276:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
 9277:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
 9278:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 9279:                                         foreach my $type (@{$types{$prefix}}) {
 9280:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 9281:                                                 my $offloadto = &mt('None');
 9282:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 9283:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 9284:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 9285:                                                     }
 9286:                                                 }
 9287:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 9288:                                             }
 9289:                                         }
 9290:                                     }
 9291:                                     $resulttext .= '</li>';
 9292:                                 }
 9293:                             }
 9294:                         } else {
 9295:                             foreach my $type (@{$types{$prefix}}) {
 9296:                                 if (defined($changes{$prefix}{$type})) {
 9297:                                     my $newvalue;
 9298:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 9299:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 9300:                                             if ($type eq 'version') {
 9301:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 9302:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 9303:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 9304:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 9305:                                                 }
 9306:                                             }
 9307:                                         }
 9308:                                     }
 9309:                                     if ($newvalue eq '') {
 9310:                                         if ($type eq 'version') {
 9311:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 9312:                                         } else {
 9313:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 9314:                                         }
 9315:                                     } else {
 9316:                                         if ($type eq 'version') {
 9317:                                             $newvalue .= ' '.&mt('(or later)'); 
 9318:                                         }
 9319:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 9320:                                     }
 9321:                                 }
 9322:                             }
 9323:                         }
 9324:                         $resulttext .= '</ul>';
 9325:                     }
 9326:                 }
 9327:                 $resulttext .= '</ul>';
 9328:             } else {
 9329:                 $resulttext = $nochgmsg;
 9330:             }
 9331:         } else {
 9332:             $resulttext = '<span class="LC_error">'.
 9333:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9334:         }
 9335:     } else {
 9336:         $resulttext = $nochgmsg;
 9337:     }
 9338:     return $resulttext;
 9339: }
 9340: 
 9341: sub modify_loadbalancing {
 9342:     my ($dom,%domconfig) = @_;
 9343:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9344:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9345:     my ($othertitle,$usertypes,$types) =
 9346:         &Apache::loncommon::sorted_inst_types($dom);
 9347:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9348:     my @sparestypes = ('primary','default');
 9349:     my %typetitles = &sparestype_titles();
 9350:     my $resulttext;
 9351:     my (%currbalancer,%currtargets,%currrules,%existing);
 9352:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 9353:         %existing = %{$domconfig{'loadbalancing'}};
 9354:     }
 9355:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 9356:                               \%currtargets,\%currrules);
 9357:     my ($saveloadbalancing,%defaultshash,%changes);
 9358:     my ($alltypes,$othertypes,$titles) =
 9359:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 9360:     my %ruletitles = &offloadtype_text();
 9361:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 9362:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 9363:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 9364:         if ($balancer eq '') {
 9365:             next;
 9366:         }
 9367:         if (!exists($servers{$balancer})) {
 9368:             if (exists($currbalancer{$balancer})) {
 9369:                 push(@{$changes{'delete'}},$balancer);
 9370:             }
 9371:             next;
 9372:         }
 9373:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 9374:             push(@{$changes{'delete'}},$balancer);
 9375:             next;
 9376:         }
 9377:         if (!exists($currbalancer{$balancer})) {
 9378:             push(@{$changes{'add'}},$balancer);
 9379:         }
 9380:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 9381:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 9382:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 9383:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 9384:             $saveloadbalancing = 1;
 9385:         }
 9386:         foreach my $sparetype (@sparestypes) {
 9387:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 9388:             my @offloadto;
 9389:             foreach my $target (@targets) {
 9390:                 if (($servers{$target}) && ($target ne $balancer)) {
 9391:                     if ($sparetype eq 'default') {
 9392:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 9393:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 9394:                         }
 9395:                     }
 9396:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 9397:                         push(@offloadto,$target);
 9398:                     }
 9399:                 }
 9400:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 9401:             }
 9402:         }
 9403:         if (ref($currtargets{$balancer}) eq 'HASH') {
 9404:             foreach my $sparetype (@sparestypes) {
 9405:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 9406:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 9407:                     if (@targetdiffs > 0) {
 9408:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 9409:                     }
 9410:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9411:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9412:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 9413:                     }
 9414:                 }
 9415:             }
 9416:         } else {
 9417:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 9418:                 foreach my $sparetype (@sparestypes) {
 9419:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9420:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9421:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 9422:                         }
 9423:                     }
 9424:                 }
 9425:             }
 9426:         }
 9427:         my $ishomedom;
 9428:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 9429:             $ishomedom = 1;
 9430:         }
 9431:         if (ref($alltypes) eq 'ARRAY') {
 9432:             foreach my $type (@{$alltypes}) {
 9433:                 my $rule;
 9434:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
 9435:                          (!$ishomedom)) {
 9436:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 9437:                 }
 9438:                 if ($rule eq 'specific') {
 9439:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 9440:                 }
 9441:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 9442:                 if (ref($currrules{$balancer}) eq 'HASH') {
 9443:                     if ($rule ne $currrules{$balancer}{$type}) {
 9444:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 9445:                     }
 9446:                 } elsif ($rule ne '') {
 9447:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 9448:                 }
 9449:             }
 9450:         }
 9451:     }
 9452:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 9453:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 9454:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 9455:             $defaultshash{'loadbalancing'} = {};
 9456:         }
 9457:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9458:                                                  \%defaultshash,$dom);
 9459:         if ($putresult eq 'ok') {
 9460:             if (keys(%changes) > 0) {
 9461:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 9462:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 9463:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 9464:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
 9465:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
 9466:                     }
 9467:                 }
 9468:                 if (ref($changes{'add'}) eq 'ARRAY') {
 9469:                     foreach my $balancer (sort(@{$changes{'add'}})) {
 9470:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 9471:                     }
 9472:                 }
 9473:                 if (ref($changes{'curr'}) eq 'HASH') {
 9474:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 9475:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 9476:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 9477:                                 my %offloadstr;
 9478:                                 foreach my $sparetype (@sparestypes) {
 9479:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9480:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9481:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 9482:                                         }
 9483:                                     }
 9484:                                 }
 9485:                                 if (keys(%offloadstr) == 0) {
 9486:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 9487:                                 } else {
 9488:                                     my $showoffload;
 9489:                                     foreach my $sparetype (@sparestypes) {
 9490:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 9491:                                         if (defined($offloadstr{$sparetype})) {
 9492:                                             $showoffload .= $offloadstr{$sparetype};
 9493:                                         } else {
 9494:                                             $showoffload .= &mt('None');
 9495:                                         }
 9496:                                         $showoffload .= ('&nbsp;'x3);
 9497:                                     }
 9498:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 9499:                                 }
 9500:                             }
 9501:                         }
 9502:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 9503:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 9504:                                 foreach my $type (@{$alltypes}) {
 9505:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 9506:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 9507:                                         my $balancetext;
 9508:                                         if ($rule eq '') {
 9509:                                             $balancetext =  $ruletitles{'default'};
 9510:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
 9511:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
 9512:                                             $balancetext =  $ruletitles{$rule};
 9513:                                         } else {
 9514:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 9515:                                         }
 9516:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
 9517:                                     }
 9518:                                 }
 9519:                             }
 9520:                         }
 9521:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
 9522:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
 9523:                     }
 9524:                 }
 9525:                 if ($resulttext ne '') {
 9526:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 9527:                 } else {
 9528:                     $resulttext = $nochgmsg;
 9529:                 }
 9530:             } else {
 9531:                 $resulttext = $nochgmsg;
 9532:             }
 9533:         } else {
 9534:             $resulttext = '<span class="LC_error">'.
 9535:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9536:         }
 9537:     } else {
 9538:         $resulttext = $nochgmsg;
 9539:     }
 9540:     return $resulttext;
 9541: }
 9542: 
 9543: sub recurse_check {
 9544:     my ($chkcats,$categories,$depth,$name) = @_;
 9545:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 9546:         my $chg = 0;
 9547:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 9548:             my $category = $chkcats->[$depth]{$name}[$j];
 9549:             my $item;
 9550:             if ($category eq '') {
 9551:                 $chg ++;
 9552:             } else {
 9553:                 my $deeper = $depth + 1;
 9554:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 9555:                 if ($chg) {
 9556:                     $categories->{$item} -= $chg;
 9557:                 }
 9558:                 &recurse_check($chkcats,$categories,$deeper,$category);
 9559:                 $deeper --;
 9560:             }
 9561:         }
 9562:     }
 9563:     return;
 9564: }
 9565: 
 9566: sub recurse_cat_deletes {
 9567:     my ($item,$coursecategories,$deletions) = @_;
 9568:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9569:     my $subdepth = $depth + 1;
 9570:     if (ref($coursecategories) eq 'HASH') {
 9571:         foreach my $subitem (keys(%{$coursecategories})) {
 9572:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 9573:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 9574:                 delete($coursecategories->{$subitem});
 9575:                 $deletions->{$subitem} = 1;
 9576:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 9577:             }
 9578:         }
 9579:     }
 9580:     return;
 9581: }
 9582: 
 9583: sub get_active_dcs {
 9584:     my ($dom) = @_;
 9585:     my $now = time;
 9586:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 9587:     my %domcoords;
 9588:     my $numdcs = 0;
 9589:     foreach my $server (keys(%dompersonnel)) {
 9590:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 9591:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 9592:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 9593:         }
 9594:     }
 9595:     return %domcoords;
 9596: }
 9597: 
 9598: sub active_dc_picker {
 9599:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
 9600:     my %domcoords = &get_active_dcs($dom); 
 9601:     my @domcoord = keys(%domcoords);
 9602:     if (keys(%currhash)) {
 9603:         foreach my $dc (keys(%currhash)) {
 9604:             unless (exists($domcoords{$dc})) {
 9605:                 push(@domcoord,$dc);
 9606:             }
 9607:         }
 9608:     }
 9609:     @domcoord = sort(@domcoord);
 9610:     my $numdcs = scalar(@domcoord);
 9611:     my $rows = 0;
 9612:     my $table;
 9613:     if ($numdcs > 1) {
 9614:         $table = '<table>';
 9615:         for (my $i=0; $i<@domcoord; $i++) {
 9616:             my $rem = $i%($numinrow);
 9617:             if ($rem == 0) {
 9618:                 if ($i > 0) {
 9619:                     $table .= '</tr>';
 9620:                 }
 9621:                 $table .= '<tr>';
 9622:                 $rows ++;
 9623:             }
 9624:             my $check = '';
 9625:             if ($inputtype eq 'radio') {
 9626:                 if (keys(%currhash) == 0) {
 9627:                     if (!$i) {
 9628:                         $check = ' checked="checked"';
 9629:                     }
 9630:                 } elsif (exists($currhash{$domcoord[$i]})) {
 9631:                     $check = ' checked="checked"';
 9632:                 }
 9633:             } else {
 9634:                 if (exists($currhash{$domcoord[$i]})) {
 9635:                     $check = ' checked="checked"';
 9636:                 }
 9637:             }
 9638:             if ($i == @domcoord - 1) {
 9639:                 my $colsleft = $numinrow - $rem;
 9640:                 if ($colsleft > 1) {
 9641:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
 9642:                 } else {
 9643:                     $table .= '<td class="LC_left_item">';
 9644:                 }
 9645:             } else {
 9646:                 $table .= '<td class="LC_left_item">';
 9647:             }
 9648:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
 9649:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
 9650:             $table .= '<span class="LC_nobreak"><label>'.
 9651:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
 9652:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
 9653:             if ($user ne $dcname.':'.$dcdom) {
 9654:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
 9655:             }
 9656:             $table .= '</label></span></td>';
 9657:         }
 9658:         $table .= '</tr></table>';
 9659:     } elsif ($numdcs == 1) {
 9660:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
 9661:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
 9662:         if ($inputtype eq 'radio') {
 9663:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
 9664:             if ($user ne $dcname.':'.$dcdom) {
 9665:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
 9666:             }
 9667:         } else {
 9668:             my $check;
 9669:             if (exists($currhash{$domcoord[0]})) {
 9670:                 $check = ' checked="checked"';
 9671:             }
 9672:             $table .= '<span class="LC_nobreak"><label>'.
 9673:                       '<input type="checkbox" name="'.$name.'" '.
 9674:                       'value="'.$domcoord[0].'"'.$check.' />'.$user;
 9675:             if ($user ne $dcname.':'.$dcdom) {
 9676:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
 9677:             }
 9678:             $table .= '</label></span>';
 9679:             $rows ++;
 9680:         }
 9681:     }
 9682:     return ($numdcs,$table,$rows);
 9683: }
 9684: 
 9685: sub usersession_titles {
 9686:     return &Apache::lonlocal::texthash(
 9687:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 9688:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 9689:                spares => 'Servers offloaded to, when busy',
 9690:                version => 'LON-CAPA version requirement',
 9691:                excludedomain => 'Allow all, but exclude specific domains',
 9692:                includedomain => 'Deny all, but include specific domains',
 9693:                primary => 'Primary (checked first)',
 9694:                default => 'Default',
 9695:            );
 9696: }
 9697: 
 9698: sub id_for_thisdom {
 9699:     my (%servers) = @_;
 9700:     my %altids;
 9701:     foreach my $server (keys(%servers)) {
 9702:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 9703:         if ($serverhome ne $server) {
 9704:             $altids{$serverhome} = $server;
 9705:         }
 9706:     }
 9707:     return %altids;
 9708: }
 9709: 
 9710: sub count_servers {
 9711:     my ($currbalancer,%servers) = @_;
 9712:     my (@spares,$numspares);
 9713:     foreach my $lonhost (sort(keys(%servers))) {
 9714:         next if ($currbalancer eq $lonhost);
 9715:         push(@spares,$lonhost);
 9716:     }
 9717:     if ($currbalancer) {
 9718:         $numspares = scalar(@spares);
 9719:     } else {
 9720:         $numspares = scalar(@spares) - 1;
 9721:     }
 9722:     return ($numspares,@spares);
 9723: }
 9724: 
 9725: sub lonbalance_targets_js {
 9726:     my ($dom,$types,$servers,$settings) = @_;
 9727:     my $select = &mt('Select');
 9728:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 9729:     if (ref($servers) eq 'HASH') {
 9730:         $alltargets = join("','",sort(keys(%{$servers})));
 9731:         my @homedoms;
 9732:         foreach my $server (sort(keys(%{$servers}))) {
 9733:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 9734:                 push(@homedoms,'1');
 9735:             } else {
 9736:                 push(@homedoms,'0');
 9737:             }
 9738:         }
 9739:         $allishome = join("','",@homedoms);
 9740:     }
 9741:     if (ref($types) eq 'ARRAY') {
 9742:         if (@{$types} > 0) {
 9743:             @alltypes = @{$types};
 9744:         }
 9745:     }
 9746:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 9747:     $allinsttypes = join("','",@alltypes);
 9748:     my (%currbalancer,%currtargets,%currrules,%existing);
 9749:     if (ref($settings) eq 'HASH') {
 9750:         %existing = %{$settings};
 9751:     }
 9752:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 9753:                               \%currtargets,\%currrules);
 9754:     my $balancers = join("','",sort(keys(%currbalancer)));
 9755:     return <<"END";
 9756: 
 9757: <script type="text/javascript">
 9758: // <![CDATA[
 9759: 
 9760: currBalancers = new Array('$balancers');
 9761: 
 9762: function toggleTargets(balnum) {
 9763:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 9764:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 9765:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 9766:     var prevbalancer = prevhostitem.value;
 9767:     var baltotal = document.getElementById('loadbalancing_total').value;
 9768:     prevhostitem.value = balancer;
 9769:     if (prevbalancer != '') {
 9770:         var prevIdx = currBalancers.indexOf(prevbalancer);
 9771:         if (prevIdx != -1) {
 9772:             currBalancers.splice(prevIdx,1);
 9773:         }
 9774:     }
 9775:     if (balancer == '') {
 9776:         hideSpares(balnum);
 9777:     } else {
 9778:         var currIdx = currBalancers.indexOf(balancer);
 9779:         if (currIdx == -1) {
 9780:             currBalancers.push(balancer);
 9781:         }
 9782:         var homedoms = new Array('$allishome');
 9783:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 9784:         showSpares(balancer,ishomedom,balnum);
 9785:     }
 9786:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 9787:     return;
 9788: }
 9789: 
 9790: function showSpares(balancer,ishomedom,balnum) {
 9791:     var alltargets = new Array('$alltargets');
 9792:     var insttypes = new Array('$allinsttypes');
 9793:     var offloadtypes = new Array('primary','default');
 9794: 
 9795:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 9796:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 9797:  
 9798:     for (var i=0; i<offloadtypes.length; i++) {
 9799:         var count = 0;
 9800:         for (var j=0; j<alltargets.length; j++) {
 9801:             if (alltargets[j] != balancer) {
 9802:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 9803:                 item.value = alltargets[j];
 9804:                 item.style.textAlign='left';
 9805:                 item.style.textFace='normal';
 9806:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 9807:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 9808:                     item.disabled = '';
 9809:                 } else {
 9810:                     item.disabled = 'disabled';
 9811:                     item.checked = false;
 9812:                 }
 9813:                 count ++;
 9814:             }
 9815:         }
 9816:     }
 9817:     for (var k=0; k<insttypes.length; k++) {
 9818:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 9819:             if (ishomedom == 1) {
 9820:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 9821:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 9822:             } else {
 9823:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 9824:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 9825:             }
 9826:         } else {
 9827:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 9828:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 9829:         }
 9830:         if ((insttypes[k] != '_LC_external') && 
 9831:             ((insttypes[k] != '_LC_internetdom') ||
 9832:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 9833:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 9834:             item.options.length = 0;
 9835:             item.options[0] = new Option("","",true,true);
 9836:             var idx = 0;
 9837:             for (var m=0; m<alltargets.length; m++) {
 9838:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 9839:                     idx ++;
 9840:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 9841:                 }
 9842:             }
 9843:         }
 9844:     }
 9845:     return;
 9846: }
 9847: 
 9848: function hideSpares(balnum) {
 9849:     var alltargets = new Array('$alltargets');
 9850:     var insttypes = new Array('$allinsttypes');
 9851:     var offloadtypes = new Array('primary','default');
 9852: 
 9853:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 9854:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 9855: 
 9856:     var total = alltargets.length - 1;
 9857:     for (var i=0; i<offloadtypes; i++) {
 9858:         for (var j=0; j<total; j++) {
 9859:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 9860:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 9861:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 9862:         }
 9863:     }
 9864:     for (var k=0; k<insttypes.length; k++) {
 9865:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 9866:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 9867:         if (insttypes[k] != '_LC_external') {
 9868:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 9869:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 9870:         }
 9871:     }
 9872:     return;
 9873: }
 9874: 
 9875: function checkOffloads(item,balnum,type) {
 9876:     var alltargets = new Array('$alltargets');
 9877:     var offloadtypes = new Array('primary','default');
 9878:     if (item.checked) {
 9879:         var total = alltargets.length - 1;
 9880:         var other;
 9881:         if (type == offloadtypes[0]) {
 9882:             other = offloadtypes[1];
 9883:         } else {
 9884:             other = offloadtypes[0];
 9885:         }
 9886:         for (var i=0; i<total; i++) {
 9887:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 9888:             if (server == item.value) {
 9889:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 9890:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 9891:                 }
 9892:             }
 9893:         }
 9894:     }
 9895:     return;
 9896: }
 9897: 
 9898: function singleServerToggle(balnum,type) {
 9899:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 9900:     if (offloadtoSelIdx == 0) {
 9901:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 9902:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9903: 
 9904:     } else {
 9905:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 9906:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 9907:     }
 9908:     return;
 9909: }
 9910: 
 9911: function balanceruleChange(formname,balnum,type) {
 9912:     if (type == '_LC_external') {
 9913:         return;
 9914:     }
 9915:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 9916:     for (var i=0; i<typesRules.length; i++) {
 9917:         if (formname.elements[typesRules[i]].checked) {
 9918:             if (formname.elements[typesRules[i]].value != 'specific') {
 9919:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 9920:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9921:             } else {
 9922:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 9923:             }
 9924:         }
 9925:     }
 9926:     return;
 9927: }
 9928: 
 9929: function balancerDeleteChange(balnum) {
 9930:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 9931:     var baltotal = document.getElementById('loadbalancing_total').value;
 9932:     var addtarget;
 9933:     var removetarget;
 9934:     var action = 'delete';
 9935:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 9936:         var lonhost = hostitem.value;
 9937:         var currIdx = currBalancers.indexOf(lonhost);
 9938:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 9939:             if (currIdx != -1) {
 9940:                 currBalancers.splice(currIdx,1);
 9941:             }
 9942:             addtarget = lonhost;
 9943:         } else {
 9944:             if (currIdx == -1) {
 9945:                 currBalancers.push(lonhost);
 9946:             }
 9947:             removetarget = lonhost;
 9948:             action = 'undelete';
 9949:         }
 9950:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 9951:     }
 9952:     return;
 9953: }
 9954: 
 9955: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 9956:     if (baltotal > 1) {
 9957:         var offloadtypes = new Array('primary','default');
 9958:         var alltargets = new Array('$alltargets');
 9959:         var insttypes = new Array('$allinsttypes');
 9960:         for (var i=0; i<baltotal; i++) {
 9961:             if (i != balnum) {
 9962:                 for (var j=0; j<offloadtypes.length; j++) {
 9963:                     var total = alltargets.length - 1;
 9964:                     for (var k=0; k<total; k++) {
 9965:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 9966:                         var server = serveritem.value;
 9967:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 9968:                             if (server == addtarget) {
 9969:                                 serveritem.disabled = '';
 9970:                             }
 9971:                         }
 9972:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 9973:                             if (server == removetarget) {
 9974:                                 serveritem.disabled = 'disabled';
 9975:                                 serveritem.checked = false;
 9976:                             }
 9977:                         }
 9978:                     }
 9979:                 }
 9980:                 for (var j=0; j<insttypes.length; j++) {
 9981:                     if (insttypes[j] != '_LC_external') {
 9982:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 9983:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 9984:                             var currSel = singleserver.selectedIndex;
 9985:                             var currVal = singleserver.options[currSel].value;
 9986:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 9987:                                 var numoptions = singleserver.options.length;
 9988:                                 var needsnew = 1;
 9989:                                 for (var k=0; k<numoptions; k++) {
 9990:                                     if (singleserver.options[k] == addtarget) {
 9991:                                         needsnew = 0;
 9992:                                         break;
 9993:                                     }
 9994:                                 }
 9995:                                 if (needsnew == 1) {
 9996:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 9997:                                 }
 9998:                             }
 9999:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
10000:                                 singleserver.options.length = 0;
10001:                                 if ((currVal) && (currVal != removetarget)) {
10002:                                     singleserver.options[0] = new Option("","",false,false);
10003:                                 } else {
10004:                                     singleserver.options[0] = new Option("","",true,true);
10005:                                 }
10006:                                 var idx = 0;
10007:                                 for (var m=0; m<alltargets.length; m++) {
10008:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
10009:                                         idx ++;
10010:                                         if (currVal == alltargets[m]) {
10011:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
10012:                                         } else {
10013:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
10014:                                         }
10015:                                     }
10016:                                 }
10017:                             }
10018:                         }
10019:                     }
10020:                 }
10021:             }
10022:         }
10023:     }
10024:     return;
10025: }
10026: 
10027: // ]]>
10028: </script>
10029: 
10030: END
10031: }
10032: 
10033: sub new_spares_js {
10034:     my @sparestypes = ('primary','default');
10035:     my $types = join("','",@sparestypes);
10036:     my $select = &mt('Select');
10037:     return <<"END";
10038: 
10039: <script type="text/javascript">
10040: // <![CDATA[
10041: 
10042: function updateNewSpares(formname,lonhost) {
10043:     var types = new Array('$types');
10044:     var include = new Array();
10045:     var exclude = new Array();
10046:     for (var i=0; i<types.length; i++) {
10047:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
10048:         for (var j=0; j<spareboxes.length; j++) {
10049:             if (formname.elements[spareboxes[j]].checked) {
10050:                 exclude.push(formname.elements[spareboxes[j]].value);
10051:             } else {
10052:                 include.push(formname.elements[spareboxes[j]].value);
10053:             }
10054:         }
10055:     }
10056:     for (var i=0; i<types.length; i++) {
10057:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
10058:         var selIdx = newSpare.selectedIndex;
10059:         var currnew = newSpare.options[selIdx].value;
10060:         var okSpares = new Array();
10061:         for (var j=0; j<newSpare.options.length; j++) {
10062:             var possible = newSpare.options[j].value;
10063:             if (possible != '') {
10064:                 if (exclude.indexOf(possible) == -1) {
10065:                     okSpares.push(possible);
10066:                 } else {
10067:                     if (currnew == possible) {
10068:                         selIdx = 0;
10069:                     }
10070:                 }
10071:             }
10072:         }
10073:         for (var k=0; k<include.length; k++) {
10074:             if (okSpares.indexOf(include[k]) == -1) {
10075:                 okSpares.push(include[k]);
10076:             }
10077:         }
10078:         okSpares.sort();
10079:         newSpare.options.length = 0;
10080:         if (selIdx == 0) {
10081:             newSpare.options[0] = new Option("$select","",true,true);
10082:         } else {
10083:             newSpare.options[0] = new Option("$select","",false,false);
10084:         }
10085:         for (var m=0; m<okSpares.length; m++) {
10086:             var idx = m+1;
10087:             var selThis = 0;
10088:             if (selIdx != 0) {
10089:                 if (okSpares[m] == currnew) {
10090:                     selThis = 1;
10091:                 }
10092:             }
10093:             if (selThis == 1) {
10094:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
10095:             } else {
10096:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
10097:             }
10098:         }
10099:     }
10100:     return;
10101: }
10102: 
10103: function checkNewSpares(lonhost,type) {
10104:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
10105:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
10106:     if (chosen != '') { 
10107:         var othertype;
10108:         var othernewSpare;
10109:         if (type == 'primary') {
10110:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
10111:         }
10112:         if (type == 'default') {
10113:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
10114:         }
10115:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
10116:             othernewSpare.selectedIndex = 0;
10117:         }
10118:     }
10119:     return;
10120: }
10121: 
10122: // ]]>
10123: </script>
10124: 
10125: END
10126: 
10127: }
10128: 
10129: sub common_domprefs_js {
10130:     return <<"END";
10131: 
10132: <script type="text/javascript">
10133: // <![CDATA[
10134: 
10135: function getIndicesByName(formname,item) {
10136:     var group = new Array();
10137:     for (var i=0;i<formname.elements.length;i++) {
10138:         if (formname.elements[i].name == item) {
10139:             group.push(formname.elements[i].id);
10140:         }
10141:     }
10142:     return group;
10143: }
10144: 
10145: // ]]>
10146: </script>
10147: 
10148: END
10149: 
10150: }
10151: 
10152: sub recaptcha_js {
10153:     my %lt = &captcha_phrases();
10154:     return <<"END";
10155: 
10156: <script type="text/javascript">
10157: // <![CDATA[
10158: 
10159: function updateCaptcha(caller,context) {
10160:     var privitem;
10161:     var pubitem;
10162:     var privtext;
10163:     var pubtext;
10164:     if (document.getElementById(context+'_recaptchapub')) {
10165:         pubitem = document.getElementById(context+'_recaptchapub');
10166:     } else {
10167:         return;
10168:     }
10169:     if (document.getElementById(context+'_recaptchapriv')) {
10170:         privitem = document.getElementById(context+'_recaptchapriv');
10171:     } else {
10172:         return;
10173:     }
10174:     if (document.getElementById(context+'_recaptchapubtxt')) {
10175:         pubtext = document.getElementById(context+'_recaptchapubtxt');
10176:     } else {
10177:         return;
10178:     }
10179:     if (document.getElementById(context+'_recaptchaprivtxt')) {
10180:         privtext = document.getElementById(context+'_recaptchaprivtxt');
10181:     } else {
10182:         return;
10183:     }
10184:     if (caller.checked) {
10185:         if (caller.value == 'recaptcha') {
10186:             pubitem.type = 'text';
10187:             privitem.type = 'text';
10188:             pubitem.size = '40';
10189:             privitem.size = '40';
10190:             pubtext.innerHTML = "$lt{'pub'}";
10191:             privtext.innerHTML = "$lt{'priv'}";
10192:         } else {
10193:             pubitem.type = 'hidden';
10194:             privitem.type = 'hidden';
10195:             pubtext.innerHTML = '';
10196:             privtext.innerHTML = '';
10197:         }
10198:     }
10199:     return;
10200: }
10201: 
10202: // ]]>
10203: </script>
10204: 
10205: END
10206: 
10207: }
10208: 
10209: sub credits_js {
10210:     return <<"END";
10211: 
10212: <script type="text/javascript">
10213: // <![CDATA[
10214: 
10215: function toggleCredits(domForm) {
10216:     if (document.getElementById('credits')) {
10217:         creditsitem = document.getElementById('credits');
10218:         var creditsLength = domForm.coursecredits.length;
10219:         if (creditsLength) {
10220:             var currval;
10221:             for (var i=0; i<creditsLength; i++) {
10222:                 if (domForm.coursecredits[i].checked) {
10223:                    currval = domForm.coursecredits[i].value;
10224:                 }
10225:             }
10226:             if (currval == 1) {
10227:                 creditsitem.style.display = 'block';
10228:             } else {
10229:                 creditsitem.style.display = 'none';
10230:             }
10231:         }
10232:     }
10233:     return;
10234: }
10235: 
10236: // ]]>
10237: </script>
10238: 
10239: END
10240: 
10241: }
10242: 
10243: sub captcha_phrases {
10244:     return &Apache::lonlocal::texthash (
10245:                  priv => 'Private key',
10246:                  pub  => 'Public key',
10247:                  original  => 'original (CAPTCHA)',
10248:                  recaptcha => 'successor (ReCAPTCHA)',
10249:                  notused   => 'unused',
10250:     );
10251: }
10252: 
10253: sub devalidate_remote_domconfs {
10254:     my ($dom,$cachekeys) = @_;
10255:     return unless (ref($cachekeys) eq 'HASH');
10256:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10257:     my %thismachine;
10258:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
10259:     my @posscached = ('domainconfig','domdefaults');
10260:     if (keys(%servers) > 1) {
10261:         foreach my $server (keys(%servers)) {
10262:             next if ($thismachine{$server});
10263:             my @cached;
10264:             foreach my $name (@posscached) {
10265:                 if ($cachekeys->{$name}) {
10266:                     push(@cached,&escape($name).':'.&escape($dom));
10267:                 }
10268:             }
10269:             if (@cached) {
10270:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
10271:             }
10272:         }
10273:     }
10274:     return;
10275: }
10276: 
10277: 1;

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