File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.52: download - view: text, annotated - select for diffs
Sun Jul 20 00:11:43 2014 UTC (9 years, 10 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  - Backport 1.250

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.52 2014/07/20 00:11:43 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 Apache::lonuserutils();
  168: use Apache::loncoursequeueadmin();
  169: use LONCAPA qw(:DEFAULT :match);
  170: use LONCAPA::Enrollment;
  171: use LONCAPA::lonauthcgi();
  172: use File::Copy;
  173: use Locale::Language;
  174: use DateTime::TimeZone;
  175: use DateTime::Locale;
  176: 
  177: my $registered_cleanup;
  178: my $modified_urls;
  179: 
  180: sub handler {
  181:     my $r=shift;
  182:     if ($r->header_only) {
  183:         &Apache::loncommon::content_type($r,'text/html');
  184:         $r->send_http_header;
  185:         return OK;
  186:     }
  187: 
  188:     my $context = 'domain';
  189:     my $dom = $env{'request.role.domain'};
  190:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  191:     if (&Apache::lonnet::allowed('mau',$dom)) {
  192:         &Apache::loncommon::content_type($r,'text/html');
  193:         $r->send_http_header;
  194:     } else {
  195:         $env{'user.error.msg'}=
  196:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  197:         return HTTP_NOT_ACCEPTABLE;
  198:     }
  199: 
  200:     $registered_cleanup=0;
  201:     @{$modified_urls}=();
  202: 
  203:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  204:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  205:                                             ['phase','actions']);
  206:     my $phase = 'pickactions';
  207:     if ( exists($env{'form.phase'}) ) {
  208:         $phase = $env{'form.phase'};
  209:     }
  210:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  211:     my %domconfig =
  212:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  213:                 'quotas','autoenroll','autoupdate','autocreate',
  214:                 'directorysrch','usercreation','usermodification',
  215:                 'contacts','defaults','scantron','coursecategories',
  216:                 'serverstatuses','requestcourses','coursedefaults',
  217:                 'usersessions','loadbalancing','requestauthor',
  218:                 'selfenrollment','inststatus'],$dom);
  219:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  220:                        'autoupdate','autocreate','directorysrch','contacts',
  221:                        'usercreation','selfcreation','usermodification','scantron',
  222:                        'requestcourses','requestauthor','coursecategories',
  223:                        'serverstatuses','coursedefaults','selfenrollment',
  224:                        'usersessions');
  225:     my %existing;
  226:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  227:         %existing = %{$domconfig{'loadbalancing'}};
  228:     }
  229:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  230:         push(@prefs_order,'loadbalancing');
  231:     }
  232:     my %prefs = (
  233:         'rolecolors' =>
  234:                    { text => 'Default color schemes',
  235:                      help => 'Domain_Configuration_Color_Schemes',
  236:                      header => [{col1 => 'Student Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Coordinator Settings',
  239:                                  col2 => '',},
  240:                                 {col1 => 'Author Settings',
  241:                                  col2 => '',},
  242:                                 {col1 => 'Administrator Settings',
  243:                                  col2 => '',}],
  244:                       print => \&print_rolecolors,
  245:                       modify => \&modify_rolecolors,
  246:                     },
  247:         'login' =>
  248:                     { text => 'Log-in page options',
  249:                       help => 'Domain_Configuration_Login_Page',
  250:                       header => [{col1 => 'Log-in Page Items',
  251:                                   col2 => '',},
  252:                                  {col1 => 'Log-in Help',
  253:                                   col2 => 'Value'}],
  254:                       print => \&print_login,
  255:                       modify => \&modify_login,
  256:                     },
  257:         'defaults' => 
  258:                     { text => 'Default authentication/language/timezone/portal/types',
  259:                       help => 'Domain_Configuration_LangTZAuth',
  260:                       header => [{col1 => 'Setting',
  261:                                   col2 => 'Value'},
  262:                                  {col1 => 'Institutional user types',
  263:                                   col2 => 'Assignable to e-mail usernames'}],
  264:                       print => \&print_defaults,
  265:                       modify => \&modify_defaults,
  266:                     },
  267:         'quotas' => 
  268:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  269:                       help => 'Domain_Configuration_Quotas',
  270:                       header => [{col1 => 'User affiliation',
  271:                                   col2 => 'Available tools',
  272:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  273:                       print => \&print_quotas,
  274:                       modify => \&modify_quotas,
  275:                     },
  276:         'autoenroll' =>
  277:                    { text => 'Auto-enrollment settings',
  278:                      help => 'Domain_Configuration_Auto_Enrollment',
  279:                      header => [{col1 => 'Configuration setting',
  280:                                  col2 => 'Value(s)'}],
  281:                      print => \&print_autoenroll,
  282:                      modify => \&modify_autoenroll,
  283:                    },
  284:         'autoupdate' => 
  285:                    { text => 'Auto-update settings',
  286:                      help => 'Domain_Configuration_Auto_Updates',
  287:                      header => [{col1 => 'Setting',
  288:                                  col2 => 'Value',},
  289:                                 {col1 => 'Setting',
  290:                                  col2 => 'Affiliation'},
  291:                                 {col1 => 'User population',
  292:                                  col2 => 'Updatable user data'}],
  293:                      print => \&print_autoupdate,
  294:                      modify => \&modify_autoupdate,
  295:                   },
  296:         'autocreate' => 
  297:                   { text => 'Auto-course creation settings',
  298:                      help => 'Domain_Configuration_Auto_Creation',
  299:                      header => [{col1 => 'Configuration Setting',
  300:                                  col2 => 'Value',}],
  301:                      print => \&print_autocreate,
  302:                      modify => \&modify_autocreate,
  303:                   },
  304:         'directorysrch' => 
  305:                   { text => 'Institutional directory searches',
  306:                     help => 'Domain_Configuration_InstDirectory_Search',
  307:                     header => [{col1 => 'Setting',
  308:                                 col2 => 'Value',}],
  309:                     print => \&print_directorysrch,
  310:                     modify => \&modify_directorysrch,
  311:                   },
  312:         'contacts' =>
  313:                   { text => 'Contact Information',
  314:                     help => 'Domain_Configuration_Contact_Info',
  315:                     header => [{col1 => 'Setting',
  316:                                 col2 => 'Value',}],
  317:                     print => \&print_contacts,
  318:                     modify => \&modify_contacts,
  319:                   },
  320:         'usercreation' => 
  321:                   { text => 'User creation',
  322:                     help => 'Domain_Configuration_User_Creation',
  323:                     header => [{col1 => 'Format rule type',
  324:                                 col2 => 'Format rules in force'},
  325:                                {col1 => 'User account creation',
  326:                                 col2 => 'Usernames which may be created',},
  327:                                {col1 => 'Context',
  328:                                 col2 => 'Assignable authentication types'}],
  329:                     print => \&print_usercreation,
  330:                     modify => \&modify_usercreation,
  331:                   },
  332:         'selfcreation' => 
  333:                   { text => 'Users self-creating accounts',
  334:                     help => 'Domain_Configuration_Self_Creation', 
  335:                     header => [{col1 => 'Self-creation with institutional username',
  336:                                 col2 => 'Enabled?'},
  337:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  338:                                 col2 => 'Information user can enter'},
  339:                                {col1 => 'Self-creation with e-mail as username',
  340:                                 col2 => 'Settings'}],
  341:                     print => \&print_selfcreation,
  342:                     modify => \&modify_selfcreation,
  343:                   },
  344:         'usermodification' =>
  345:                   { text => 'User modification',
  346:                     help => 'Domain_Configuration_User_Modification',
  347:                     header => [{col1 => 'Target user has role',
  348:                                 col2 => 'User information updatable in author context'},
  349:                                {col1 => 'Target user has role',
  350:                                 col2 => 'User information updatable in course context'}],
  351:                     print => \&print_usermodification,
  352:                     modify => \&modify_usermodification,
  353:                   },
  354:         'scantron' =>
  355:                   { text => 'Bubblesheet format file',
  356:                     help => 'Domain_Configuration_Scantron_Format',
  357:                     header => [ {col1 => 'Item',
  358:                                  col2 => '',
  359:                               }],
  360:                     print => \&print_scantron,
  361:                     modify => \&modify_scantron,
  362:                   },
  363:         'requestcourses' => 
  364:                  {text => 'Request creation of courses',
  365:                   help => 'Domain_Configuration_Request_Courses',
  366:                   header => [{col1 => 'User affiliation',
  367:                               col2 => 'Availability/Processing of requests',},
  368:                              {col1 => 'Setting',
  369:                               col2 => 'Value'},
  370:                              {col1 => 'Available textbooks',
  371:                               col2 => ''},
  372:                              {col1 => 'Available templates',
  373:                               col2 => ''},
  374:                              {col1 => 'Validation (not official courses)',
  375:                               col2 => 'Value'},],
  376:                   print => \&print_quotas,
  377:                   modify => \&modify_quotas,
  378:                  },
  379:         'requestauthor' =>
  380:                  {text => 'Request Authoring Space',
  381:                   help => 'Domain_Configuration_Request_Author',
  382:                   header => [{col1 => 'User affiliation',
  383:                               col2 => 'Availability/Processing of requests',},
  384:                              {col1 => 'Setting',
  385:                               col2 => 'Value'}],
  386:                   print => \&print_quotas,
  387:                   modify => \&modify_quotas,
  388:                  },
  389:         'coursecategories' =>
  390:                   { text => 'Cataloging of courses/communities',
  391:                     help => 'Domain_Configuration_Cataloging_Courses',
  392:                     header => [{col1 => 'Catalog type/availability',
  393:                                 col2 => '',},
  394:                                {col1 => 'Category settings for standard catalog',
  395:                                 col2 => '',},
  396:                                {col1 => 'Categories',
  397:                                 col2 => '',
  398:                                }],
  399:                     print => \&print_coursecategories,
  400:                     modify => \&modify_coursecategories,
  401:                   },
  402:         'serverstatuses' =>
  403:                  {text   => 'Access to server status pages',
  404:                   help   => 'Domain_Configuration_Server_Status',
  405:                   header => [{col1 => 'Status Page',
  406:                               col2 => 'Other named users',
  407:                               col3 => 'Specific IPs',
  408:                             }],
  409:                   print => \&print_serverstatuses,
  410:                   modify => \&modify_serverstatuses,
  411:                  },
  412:         'coursedefaults' => 
  413:                  {text => 'Course/Community defaults',
  414:                   help => 'Domain_Configuration_Course_Defaults',
  415:                   header => [{col1 => 'Defaults which can be overridden for each course by a DC',
  416:                               col2 => 'Value',},],
  417:                   print => \&print_coursedefaults,
  418:                   modify => \&modify_coursedefaults,
  419:                  },
  420:         'selfenrollment' => 
  421:                  {text   => 'Self-enrollment in Course/Community',
  422:                   help   => 'Domain_Configuration_Selfenrollment',
  423:                   header => [{col1 => 'Configuration Rights',
  424:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  425:                              {col1 => 'Defaults',
  426:                               col2 => 'Value'},
  427:                              {col1 => 'Self-enrollment validation (optional)',
  428:                               col2 => 'Value'},],
  429:                   print => \&print_selfenrollment,
  430:                   modify => \&modify_selfenrollment,
  431:                  },
  432:         'usersessions' =>
  433:                  {text  => 'User session hosting/offloading',
  434:                   help  => 'Domain_Configuration_User_Sessions',
  435:                   header => [{col1 => 'Domain server',
  436:                               col2 => 'Servers to offload sessions to when busy'},
  437:                              {col1 => 'Hosting of users from other domains',
  438:                               col2 => 'Rules'},
  439:                              {col1 => "Hosting domain's own users elsewhere",
  440:                               col2 => 'Rules'}],
  441:                   print => \&print_usersessions,
  442:                   modify => \&modify_usersessions,
  443:                  },
  444:          'loadbalancing' =>
  445:                  {text  => 'Dedicated Load Balancer(s)',
  446:                   help  => 'Domain_Configuration_Load_Balancing',
  447:                   header => [{col1 => 'Balancers',
  448:                               col2 => 'Default destinations',
  449:                               col3 => 'User affiliation',
  450:                               col4 => 'Overrides'},
  451:                             ],
  452:                   print => \&print_loadbalancing,
  453:                   modify => \&modify_loadbalancing,
  454:                  },
  455:     );
  456:     if (keys(%servers) > 1) {
  457:         $prefs{'login'}  = { text   => 'Log-in page options',
  458:                              help   => 'Domain_Configuration_Login_Page',
  459:                             header => [{col1 => 'Log-in Service',
  460:                                         col2 => 'Server Setting',},
  461:                                        {col1 => 'Log-in Page Items',
  462:                                         col2 => ''},
  463:                                        {col1 => 'Log-in Help',
  464:                                         col2 => 'Value'}],
  465:                             print => \&print_login,
  466:                             modify => \&modify_login,
  467:                            };
  468:     }
  469: 
  470:     my @roles = ('student','coordinator','author','admin');
  471:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  472:     &Apache::lonhtmlcommon::add_breadcrumb
  473:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  474:       text=>"Settings to display/modify"});
  475:     my $confname = $dom.'-domainconfig';
  476: 
  477:     if ($phase eq 'process') {
  478:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  479:                                                               \%prefs,\%domconfig,$confname,\@roles);
  480:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  481:             $r->rflush();
  482:             &devalidate_remote_domconfs($dom,$result);
  483:         }
  484:     } elsif ($phase eq 'display') {
  485:         my $js = &recaptcha_js().
  486:                  &toggle_display_js();
  487:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  488:             my ($othertitle,$usertypes,$types) =
  489:                 &Apache::loncommon::sorted_inst_types($dom);
  490:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  491:                                           $domconfig{'loadbalancing'}).
  492:                    &new_spares_js().
  493:                    &common_domprefs_js().
  494:                    &Apache::loncommon::javascript_array_indexof();
  495:         }
  496:         if (grep(/^requestcourses$/,@actions)) {
  497:             my $javascript_validations;
  498:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  499:             $js .= <<END;
  500: <script type="text/javascript">
  501: $javascript_validations
  502: </script>
  503: $coursebrowserjs
  504: END
  505:         }
  506:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  507:     } else {
  508: # check if domconfig user exists for the domain.
  509:         my $servadm = $r->dir_config('lonAdmEMail');
  510:         my ($configuserok,$author_ok,$switchserver) =
  511:             &config_check($dom,$confname,$servadm);
  512:         unless ($configuserok eq 'ok') {
  513:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  514:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  515:                           $confname).
  516:                       '<br />'
  517:             );
  518:             if ($switchserver) {
  519:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  520:                           '<br />'.
  521:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  522:                           '<br />'.
  523:                           &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).
  524:                           '<br />'.
  525:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  526:                 );
  527:             } else {
  528:                 $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.').
  529:                           '<br />'.
  530:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  531:                 );
  532:             }
  533:             $r->print(&Apache::loncommon::end_page());
  534:             return OK;
  535:         }
  536:         if (keys(%domconfig) == 0) {
  537:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  538:             my @ids=&Apache::lonnet::current_machine_ids();
  539:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  540:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  541:                 my @loginimages = ('img','logo','domlogo','login');
  542:                 my $custom_img_count = 0;
  543:                 foreach my $img (@loginimages) {
  544:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  545:                         $custom_img_count ++;
  546:                     }
  547:                 }
  548:                 foreach my $role (@roles) {
  549:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  550:                         $custom_img_count ++;
  551:                     }
  552:                 }
  553:                 if ($custom_img_count > 0) {
  554:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  555:                     my $switch_server = &check_switchserver($dom,$confname);
  556:                     $r->print(
  557:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  558:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  559:     &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 />'.
  560:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  561:                     if ($switch_server) {
  562:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  563:                     }
  564:                     $r->print(&Apache::loncommon::end_page());
  565:                     return OK;
  566:                 }
  567:             }
  568:         }
  569:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  570:     }
  571:     return OK;
  572: }
  573: 
  574: sub process_changes {
  575:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  576:     my %domconfig;
  577:     if (ref($values) eq 'HASH') {
  578:         %domconfig = %{$values};
  579:     }
  580:     my $output;
  581:     if ($action eq 'login') {
  582:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  583:     } elsif ($action eq 'rolecolors') {
  584:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  585:                                      $lastactref,%domconfig);
  586:     } elsif ($action eq 'quotas') {
  587:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  588:     } elsif ($action eq 'autoenroll') {
  589:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  590:     } elsif ($action eq 'autoupdate') {
  591:         $output = &modify_autoupdate($dom,%domconfig);
  592:     } elsif ($action eq 'autocreate') {
  593:         $output = &modify_autocreate($dom,%domconfig);
  594:     } elsif ($action eq 'directorysrch') {
  595:         $output = &modify_directorysrch($dom,%domconfig);
  596:     } elsif ($action eq 'usercreation') {
  597:         $output = &modify_usercreation($dom,%domconfig);
  598:     } elsif ($action eq 'selfcreation') {
  599:         $output = &modify_selfcreation($dom,%domconfig);
  600:     } elsif ($action eq 'usermodification') {
  601:         $output = &modify_usermodification($dom,%domconfig);
  602:     } elsif ($action eq 'contacts') {
  603:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  604:     } elsif ($action eq 'defaults') {
  605:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  606:     } elsif ($action eq 'scantron') {
  607:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  608:     } elsif ($action eq 'coursecategories') {
  609:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  610:     } elsif ($action eq 'serverstatuses') {
  611:         $output = &modify_serverstatuses($dom,%domconfig);
  612:     } elsif ($action eq 'requestcourses') {
  613:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  614:     } elsif ($action eq 'requestauthor') {
  615:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  616:     } elsif ($action eq 'coursedefaults') {
  617:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  618:     } elsif ($action eq 'selfenrollment') {
  619:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  620:     } elsif ($action eq 'usersessions') {
  621:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  622:     } elsif ($action eq 'loadbalancing') {
  623:         $output = &modify_loadbalancing($dom,%domconfig);
  624:     }
  625:     return $output;
  626: }
  627: 
  628: sub print_config_box {
  629:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  630:     my $rowtotal = 0;
  631:     my $output;
  632:     if ($action eq 'coursecategories') {
  633:         $output = &coursecategories_javascript($settings);
  634:     } elsif ($action eq 'defaults') {
  635:         $output = &defaults_javascript($settings); 
  636:     }
  637:     $output .=
  638:          '<table class="LC_nested_outer">
  639:           <tr>
  640:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  641:            &mt($item->{text}).'&nbsp;'.
  642:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  643:           '</tr>';
  644:     $rowtotal ++;
  645:     my $numheaders = 1;
  646:     if (ref($item->{'header'}) eq 'ARRAY') {
  647:         $numheaders = scalar(@{$item->{'header'}});
  648:     }
  649:     if ($numheaders > 1) {
  650:         my $colspan = '';
  651:         my $rightcolspan = '';
  652:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  653:             (($action eq 'login') && ($numheaders < 3))) {
  654:             $colspan = ' colspan="2"';
  655:         }
  656:         if ($action eq 'usersessions') {
  657:             $rightcolspan = ' colspan="3"'; 
  658:         }
  659:         $output .= '
  660:           <tr>
  661:            <td>
  662:             <table class="LC_nested">
  663:              <tr class="LC_info_row">
  664:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  665:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  666:              </tr>';
  667:         $rowtotal ++;
  668:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  669:             ($action eq 'usermodification') || ($action eq 'defaults') ||
  670:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
  671:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  672:         } elsif ($action eq 'coursecategories') {
  673:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  674:         } elsif ($action eq 'login') {
  675:             if ($numheaders == 3) {
  676:                 $colspan = ' colspan="2"';
  677:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  678:             } else {
  679:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  680:             }
  681:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  682:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  683:         } elsif ($action eq 'rolecolors') {
  684:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  685:         }
  686:         $output .= '
  687:            </table>
  688:           </td>
  689:          </tr>
  690:          <tr>
  691:            <td>
  692:             <table class="LC_nested">
  693:              <tr class="LC_info_row">
  694:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  695:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  696:              </tr>';
  697:             $rowtotal ++;
  698:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  699:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  700:             ($action eq 'usersessions') || ($action eq 'coursecategories')) {
  701:             if ($action eq 'coursecategories') {
  702:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  703:                 $colspan = ' colspan="2"';
  704:             } else {
  705:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  706:             }
  707:             $output .= '
  708:            </table>
  709:           </td>
  710:          </tr>
  711:          <tr>
  712:            <td>
  713:             <table class="LC_nested">
  714:              <tr class="LC_info_row">
  715:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  716:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  717:              </tr>'."\n";
  718:             if ($action eq 'coursecategories') {
  719:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  720:             } else {
  721:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  722:             }
  723:             $rowtotal ++;
  724:         } elsif (($action eq 'usermodification') || ($action eq 'defaults')) {
  725:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  726:         } elsif ($action eq 'login') {
  727:             if ($numheaders == 3) {
  728:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  729:            </table>
  730:           </td>
  731:          </tr>
  732:          <tr>
  733:            <td>
  734:             <table class="LC_nested">
  735:              <tr class="LC_info_row">
  736:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  737:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  738:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  739:                 $rowtotal ++;
  740:             } else {
  741:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  742:             }
  743:         } elsif ($action eq 'requestcourses') {
  744:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  745:             $rowtotal ++;
  746:             $output .= &print_studentcode($settings,\$rowtotal).'
  747:            </table>
  748:           </td>
  749:          </tr>
  750:          <tr>
  751:            <td>
  752:             <table class="LC_nested">
  753:              <tr class="LC_info_row">
  754:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  755:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  756:                        &textbookcourses_javascript($settings).
  757:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  758:             </table>
  759:            </td>
  760:           </tr>
  761:          <tr>
  762:            <td>
  763:             <table class="LC_nested">
  764:              <tr class="LC_info_row">
  765:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  766:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  767:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  768:             </table>
  769:            </td>
  770:           </tr>
  771:           <tr>
  772:            <td>
  773:             <table class="LC_nested">
  774:              <tr class="LC_info_row">
  775:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  776:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  777:              </tr>'.
  778:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  779:         } elsif ($action eq 'requestauthor') {
  780:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  781:             $rowtotal ++;
  782:         } elsif ($action eq 'rolecolors') {
  783:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  784:            </table>
  785:           </td>
  786:          </tr>
  787:          <tr>
  788:            <td>
  789:             <table class="LC_nested">
  790:              <tr class="LC_info_row">
  791:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  792:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  793:               <td class="LC_right_item" valign="top">'.
  794:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  795:              </tr>'.
  796:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  797:            </table>
  798:           </td>
  799:          </tr>
  800:          <tr>
  801:            <td>
  802:             <table class="LC_nested">
  803:              <tr class="LC_info_row">
  804:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  805:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  806:              </tr>'.
  807:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  808:             $rowtotal += 2;
  809:         }
  810:     } else {
  811:         $output .= '
  812:           <tr>
  813:            <td>
  814:             <table class="LC_nested">
  815:              <tr class="LC_info_row">';
  816:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  817:             $output .= '  
  818:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  819:         } elsif ($action eq 'serverstatuses') {
  820:             $output .= '
  821:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  822:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  823: 
  824:         } else {
  825:             $output .= '
  826:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  827:         }
  828:         if (defined($item->{'header'}->[0]->{'col3'})) {
  829:             $output .= '<td class="LC_left_item" valign="top">'.
  830:                        &mt($item->{'header'}->[0]->{'col2'});
  831:             if ($action eq 'serverstatuses') {
  832:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  833:             } 
  834:         } else {
  835:             $output .= '<td class="LC_right_item" valign="top">'.
  836:                        &mt($item->{'header'}->[0]->{'col2'});
  837:         }
  838:         $output .= '</td>';
  839:         if ($item->{'header'}->[0]->{'col3'}) {
  840:             if (defined($item->{'header'}->[0]->{'col4'})) {
  841:                 $output .= '<td class="LC_left_item" valign="top">'.
  842:                             &mt($item->{'header'}->[0]->{'col3'});
  843:             } else {
  844:                 $output .= '<td class="LC_right_item" valign="top">'.
  845:                            &mt($item->{'header'}->[0]->{'col3'});
  846:             }
  847:             if ($action eq 'serverstatuses') {
  848:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  849:             }
  850:             $output .= '</td>';
  851:         }
  852:         if ($item->{'header'}->[0]->{'col4'}) {
  853:             $output .= '<td class="LC_right_item" valign="top">'.
  854:                        &mt($item->{'header'}->[0]->{'col4'});
  855:         }
  856:         $output .= '</tr>';
  857:         $rowtotal ++;
  858:         if ($action eq 'quotas') {
  859:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  860:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  861:                  ($action eq 'contacts') || ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  862:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  863:         } elsif ($action eq 'scantron') {
  864:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  865:         } elsif ($action eq 'helpsettings') {
  866:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  867:         } elsif ($action eq 'coursedefaults') {
  868:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  869:         }
  870:     }
  871:     $output .= '
  872:    </table>
  873:   </td>
  874:  </tr>
  875: </table><br />';
  876:     return ($output,$rowtotal);
  877: }
  878: 
  879: sub print_login {
  880:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  881:     my ($css_class,$datatable);
  882:     my %choices = &login_choices();
  883: 
  884:     if ($caller eq 'service') {
  885:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  886:         my $choice = $choices{'disallowlogin'};
  887:         $css_class = ' class="LC_odd_row"';
  888:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  889:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  890:                       '<th>'.$choices{'server'}.'</th>'.
  891:                       '<th>'.$choices{'serverpath'}.'</th>'.
  892:                       '<th>'.$choices{'custompath'}.'</th>'.
  893:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  894:         my %disallowed;
  895:         if (ref($settings) eq 'HASH') {
  896:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  897:                %disallowed = %{$settings->{'loginvia'}};
  898:             }
  899:         }
  900:         foreach my $lonhost (sort(keys(%servers))) {
  901:             my $direct = 'selected="selected"';
  902:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  903:                 if ($disallowed{$lonhost}{'server'} ne '') {
  904:                     $direct = '';
  905:                 }
  906:             }
  907:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  908:                           '<td><select name="'.$lonhost.'_server">'.
  909:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  910:                           '</option>';
  911:             foreach my $hostid (sort(keys(%servers))) {
  912:                 next if ($servers{$hostid} eq $servers{$lonhost});
  913:                 my $selected = '';
  914:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  915:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  916:                         $selected = 'selected="selected"';
  917:                     }
  918:                 }
  919:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  920:                               $servers{$hostid}.'</option>';
  921:             }
  922:             $datatable .= '</select></td>'.
  923:                           '<td><select name="'.$lonhost.'_serverpath">';
  924:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  925:                 my $pathname = $path;
  926:                 if ($path eq 'custom') {
  927:                     $pathname = &mt('Custom Path').' ->';
  928:                 }
  929:                 my $selected = '';
  930:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  931:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  932:                         $selected = 'selected="selected"';
  933:                     }
  934:                 } elsif ($path eq '') {
  935:                     $selected = 'selected="selected"';
  936:                 }
  937:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  938:             }
  939:             $datatable .= '</select></td>';
  940:             my ($custom,$exempt);
  941:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  942:                 $custom = $disallowed{$lonhost}{'custompath'};
  943:                 $exempt = $disallowed{$lonhost}{'exempt'};
  944:             }
  945:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  946:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  947:                           '</tr>';
  948:         }
  949:         $datatable .= '</table></td></tr>';
  950:         return $datatable;
  951:     } elsif ($caller eq 'page') {
  952:         my %defaultchecked = ( 
  953:                                'coursecatalog' => 'on',
  954:                                'helpdesk'      => 'on',
  955:                                'adminmail'     => 'off',
  956:                                'newuser'       => 'off',
  957:                              );
  958:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  959:         my (%checkedon,%checkedoff);
  960:         foreach my $item (@toggles) {
  961:             if ($defaultchecked{$item} eq 'on') { 
  962:                 $checkedon{$item} = ' checked="checked" ';
  963:                 $checkedoff{$item} = ' ';
  964:             } elsif ($defaultchecked{$item} eq 'off') {
  965:                 $checkedoff{$item} = ' checked="checked" ';
  966:                 $checkedon{$item} = ' ';
  967:             }
  968:         }
  969:         my @images = ('img','logo','domlogo','login');
  970:         my @logintext = ('textcol','bgcol');
  971:         my @bgs = ('pgbg','mainbg','sidebg');
  972:         my @links = ('link','alink','vlink');
  973:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  974:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  975:         my (%is_custom,%designs);
  976:         my %defaults = (
  977:                        font => $defaultdesign{'login.font'},
  978:                        );
  979:         foreach my $item (@images) {
  980:             $defaults{$item} = $defaultdesign{'login.'.$item};
  981:             $defaults{'showlogo'}{$item} = 1;
  982:         }
  983:         foreach my $item (@bgs) {
  984:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  985:         }
  986:         foreach my $item (@logintext) {
  987:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  988:         }
  989:         foreach my $item (@links) {
  990:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  991:         }
  992:         if (ref($settings) eq 'HASH') {
  993:             foreach my $item (@toggles) {
  994:                 if ($settings->{$item} eq '1') {
  995:                     $checkedon{$item} =  ' checked="checked" ';
  996:                     $checkedoff{$item} = ' ';
  997:                 } elsif ($settings->{$item} eq '0') {
  998:                     $checkedoff{$item} =  ' checked="checked" ';
  999:                     $checkedon{$item} = ' ';
 1000:                 }
 1001:             }
 1002:             foreach my $item (@images) {
 1003:                 if (defined($settings->{$item})) {
 1004:                     $designs{$item} = $settings->{$item};
 1005:                     $is_custom{$item} = 1;
 1006:                 }
 1007:                 if (defined($settings->{'showlogo'}{$item})) {
 1008:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1009:                 }
 1010:             }
 1011:             foreach my $item (@logintext) {
 1012:                 if ($settings->{$item} ne '') {
 1013:                     $designs{'logintext'}{$item} = $settings->{$item};
 1014:                     $is_custom{$item} = 1;
 1015:                 }
 1016:             }
 1017:             if ($settings->{'font'} ne '') {
 1018:                 $designs{'font'} = $settings->{'font'};
 1019:                 $is_custom{'font'} = 1;
 1020:             }
 1021:             foreach my $item (@bgs) {
 1022:                 if ($settings->{$item} ne '') {
 1023:                     $designs{'bgs'}{$item} = $settings->{$item};
 1024:                     $is_custom{$item} = 1;
 1025:                 }
 1026:             }
 1027:             foreach my $item (@links) {
 1028:                 if ($settings->{$item} ne '') {
 1029:                     $designs{'links'}{$item} = $settings->{$item};
 1030:                     $is_custom{$item} = 1;
 1031:                 }
 1032:             }
 1033:         } else {
 1034:             if ($designhash{$dom.'.login.font'} ne '') {
 1035:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1036:                 $is_custom{'font'} = 1;
 1037:             }
 1038:             foreach my $item (@images) {
 1039:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1040:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1041:                     $is_custom{$item} = 1;
 1042:                 }
 1043:             }
 1044:             foreach my $item (@bgs) {
 1045:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1046:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1047:                     $is_custom{$item} = 1;
 1048:                 }
 1049:             }
 1050:             foreach my $item (@links) {
 1051:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1052:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1053:                     $is_custom{$item} = 1;
 1054:                 }
 1055:             }
 1056:         }
 1057:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1058:                                                       logo => 'Institution Logo',
 1059:                                                       domlogo => 'Domain Logo',
 1060:                                                       login => 'Login box');
 1061:         my $itemcount = 1;
 1062:         foreach my $item (@toggles) {
 1063:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1064:             $datatable .=  
 1065:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1066:                 '</td><td>'.
 1067:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1068:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1069:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1070:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1071:                 '</tr>';
 1072:             $itemcount ++;
 1073:         }
 1074:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1075:         $datatable .= '</tr></table></td></tr>';
 1076:     } elsif ($caller eq 'help') {
 1077:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1078:         my $switchserver = &check_switchserver($dom,$confname);
 1079:         my $itemcount = 1;
 1080:         $defaulturl = '/adm/loginproblems.html';
 1081:         $defaulttype = 'default';
 1082:         %lt = &Apache::lonlocal::texthash (
 1083:                      del     => 'Delete?',
 1084:                      rep     => 'Replace:',
 1085:                      upl     => 'Upload:',
 1086:                      default => 'Default',
 1087:                      custom  => 'Custom',
 1088:                                              );
 1089:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1090:         my @currlangs;
 1091:         if (ref($settings) eq 'HASH') {
 1092:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1093:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1094:                     next if ($settings->{'helpurl'}{$key} eq '');
 1095:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1096:                     $type{$key} = 'custom';
 1097:                     unless ($key eq 'nolang') {
 1098:                         push(@currlangs,$key);
 1099:                     }
 1100:                 }
 1101:             } elsif ($settings->{'helpurl'} ne '') {
 1102:                 $type{'nolang'} = 'custom';
 1103:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1104:             }
 1105:         }
 1106:         foreach my $lang ('nolang',sort(@currlangs)) {
 1107:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1108:             $datatable .= '<tr'.$css_class.'>';
 1109:             if ($url{$lang} eq '') {
 1110:                 $url{$lang} = $defaulturl;
 1111:             }
 1112:             if ($type{$lang} eq '') {
 1113:                 $type{$lang} = $defaulttype;
 1114:             }
 1115:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1116:             if ($lang eq 'nolang') {
 1117:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1118:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1119:             } else {
 1120:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1121:                                   $langchoices{$lang},
 1122:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1123:             }
 1124:             $datatable .= '</span></td>'."\n".
 1125:                           '<td class="LC_left_item">';
 1126:             if ($type{$lang} eq 'custom') {
 1127:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1128:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1129:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1130:             } else {
 1131:                 $datatable .= $lt{'upl'};
 1132:             }
 1133:             $datatable .='<br />';
 1134:             if ($switchserver) {
 1135:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1136:             } else {
 1137:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1138:             }
 1139:             $datatable .= '</td></tr>';
 1140:             $itemcount ++;
 1141:         }
 1142:         my @addlangs;
 1143:         foreach my $lang (sort(keys(%langchoices))) {
 1144:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1145:             push(@addlangs,$lang);
 1146:         }
 1147:         if (@addlangs > 0) {
 1148:             my %toadd;
 1149:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1150:             $toadd{''} = &mt('Select');
 1151:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1152:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1153:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1154:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1155:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1156:             if ($switchserver) {
 1157:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1158:             } else {
 1159:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1160:             }
 1161:             $datatable .= '</td></tr>';
 1162:             $itemcount ++;
 1163:         }
 1164:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1165:     }
 1166:     return $datatable;
 1167: }
 1168: 
 1169: sub login_choices {
 1170:     my %choices =
 1171:         &Apache::lonlocal::texthash (
 1172:             coursecatalog => 'Display Course/Community Catalog link?',
 1173:             adminmail     => "Display Administrator's E-mail Address?",
 1174:             helpdesk      => 'Display "Contact Helpdesk" link',
 1175:             disallowlogin => "Login page requests redirected",
 1176:             hostid        => "Server",
 1177:             server        => "Redirect to:",
 1178:             serverpath    => "Path",
 1179:             custompath    => "Custom", 
 1180:             exempt        => "Exempt IP(s)",
 1181:             directlogin   => "No redirect",
 1182:             newuser       => "Link to create a user account",
 1183:             img           => "Header",
 1184:             logo          => "Main Logo",
 1185:             domlogo       => "Domain Logo",
 1186:             login         => "Log-in Header", 
 1187:             textcol       => "Text color",
 1188:             bgcol         => "Box color",
 1189:             bgs           => "Background colors",
 1190:             links         => "Link colors",
 1191:             font          => "Font color",
 1192:             pgbg          => "Header",
 1193:             mainbg        => "Page",
 1194:             sidebg        => "Login box",
 1195:             link          => "Link",
 1196:             alink         => "Active link",
 1197:             vlink         => "Visited link",
 1198:         );
 1199:     return %choices;
 1200: }
 1201: 
 1202: sub print_rolecolors {
 1203:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1204:     my %choices = &color_font_choices();
 1205:     my @bgs = ('pgbg','tabbg','sidebg');
 1206:     my @links = ('link','alink','vlink');
 1207:     my @images = ('img');
 1208:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1209:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1210:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1211:     my (%is_custom,%designs);
 1212:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1213:     if (ref($settings) eq 'HASH') {
 1214:         if (ref($settings->{$role}) eq 'HASH') {
 1215:             if ($settings->{$role}->{'img'} ne '') {
 1216:                 $designs{'img'} = $settings->{$role}->{'img'};
 1217:                 $is_custom{'img'} = 1;
 1218:             }
 1219:             if ($settings->{$role}->{'font'} ne '') {
 1220:                 $designs{'font'} = $settings->{$role}->{'font'};
 1221:                 $is_custom{'font'} = 1;
 1222:             }
 1223:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1224:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1225:                 $is_custom{'fontmenu'} = 1;
 1226:             }
 1227:             foreach my $item (@bgs) {
 1228:                 if ($settings->{$role}->{$item} ne '') {
 1229:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1230:                     $is_custom{$item} = 1;
 1231:                 }
 1232:             }
 1233:             foreach my $item (@links) {
 1234:                 if ($settings->{$role}->{$item} ne '') {
 1235:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1236:                     $is_custom{$item} = 1;
 1237:                 }
 1238:             }
 1239:         }
 1240:     } else {
 1241:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1242:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1243:             $is_custom{'img'} = 1;
 1244:         }
 1245:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1246:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1247:             $is_custom{'fontmenu'} = 1; 
 1248:         }
 1249:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1250:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1251:             $is_custom{'font'} = 1;
 1252:         }
 1253:         foreach my $item (@bgs) {
 1254:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1255:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1256:                 $is_custom{$item} = 1;
 1257:             
 1258:             }
 1259:         }
 1260:         foreach my $item (@links) {
 1261:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1262:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1263:                 $is_custom{$item} = 1;
 1264:             }
 1265:         }
 1266:     }
 1267:     my $itemcount = 1;
 1268:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1269:     $datatable .= '</tr></table></td></tr>';
 1270:     return $datatable;
 1271: }
 1272: 
 1273: sub role_defaults {
 1274:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1275:     my %defaults;
 1276:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1277:         return %defaults;
 1278:     }
 1279:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1280:     if ($role eq 'login') {
 1281:         %defaults = (
 1282:                        font => $defaultdesign{$role.'.font'},
 1283:                     );
 1284:         if (ref($logintext) eq 'ARRAY') {
 1285:             foreach my $item (@{$logintext}) {
 1286:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1287:             }
 1288:         }
 1289:         foreach my $item (@{$images}) {
 1290:             $defaults{'showlogo'}{$item} = 1;
 1291:         }
 1292:     } else {
 1293:         %defaults = (
 1294:                        img => $defaultdesign{$role.'.img'},
 1295:                        font => $defaultdesign{$role.'.font'},
 1296:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1297:                     );
 1298:     }
 1299:     foreach my $item (@{$bgs}) {
 1300:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1301:     }
 1302:     foreach my $item (@{$links}) {
 1303:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1304:     }
 1305:     foreach my $item (@{$images}) {
 1306:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1307:     }
 1308:     return %defaults;
 1309: }
 1310: 
 1311: sub display_color_options {
 1312:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1313:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1314:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1315:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1316:     my $datatable = '<tr'.$css_class.'>'.
 1317:         '<td>'.$choices->{'font'}.'</td>';
 1318:     if (!$is_custom->{'font'}) {
 1319:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1320:     } else {
 1321:         $datatable .= '<td>&nbsp;</td>';
 1322:     }
 1323:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1324: 
 1325:     $datatable .= '<td><span class="LC_nobreak">'.
 1326:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1327:                   ' value="'.$current_color.'" />&nbsp;'.
 1328:                   '&nbsp;</td></tr>';
 1329:     unless ($role eq 'login') { 
 1330:         $datatable .= '<tr'.$css_class.'>'.
 1331:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1332:         if (!$is_custom->{'fontmenu'}) {
 1333:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1334:         } else {
 1335:             $datatable .= '<td>&nbsp;</td>';
 1336:         }
 1337: 	$current_color = $designs->{'fontmenu'} ?
 1338: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1339:         $datatable .= '<td><span class="LC_nobreak">'.
 1340:                       '<input class="colorchooser" type="text" size="10" name="'
 1341: 		      .$role.'_fontmenu"'.
 1342:                       ' value="'.$current_color.'" />&nbsp;'.
 1343:                       '&nbsp;</td></tr>';
 1344:     }
 1345:     my $switchserver = &check_switchserver($dom,$confname);
 1346:     foreach my $img (@{$images}) {
 1347: 	$itemcount ++;
 1348:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1349:         $datatable .= '<tr'.$css_class.'>'.
 1350:                       '<td>'.$choices->{$img};
 1351:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1352:         if ($role eq 'login') {
 1353:             if ($img eq 'login') {
 1354:                 $login_hdr_pick =
 1355:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1356:                 $logincolors =
 1357:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1358:                                        $designs,$defaults);
 1359:             } elsif ($img ne 'domlogo') {
 1360:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1361:             }
 1362:         }
 1363:         $datatable .= '</td>';
 1364:         if ($designs->{$img} ne '') {
 1365:             $imgfile = $designs->{$img};
 1366: 	    $img_import = ($imgfile =~ m{^/adm/});
 1367:         } else {
 1368:             $imgfile = $defaults->{$img};
 1369:         }
 1370:         if ($imgfile) {
 1371:             my ($showfile,$fullsize);
 1372:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1373:                 my $urldir = $1;
 1374:                 my $filename = $2;
 1375:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1376:                 if (@info) {
 1377:                     my $thumbfile = 'tn-'.$filename;
 1378:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1379:                     if (@thumb) {
 1380:                         $showfile = $urldir.'/'.$thumbfile;
 1381:                     } else {
 1382:                         $showfile = $imgfile;
 1383:                     }
 1384:                 } else {
 1385:                     $showfile = '';
 1386:                 }
 1387:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1388:                 $showfile = $imgfile;
 1389:                 my $imgdir = $1;
 1390:                 my $filename = $2;
 1391:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1392:                     $showfile = "/$imgdir/tn-".$filename;
 1393:                 } else {
 1394:                     my $input = $londocroot.$imgfile;
 1395:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1396:                     if (!-e $output) {
 1397:                         my ($width,$height) = &thumb_dimensions();
 1398:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1399:                         if ($fullwidth ne '' && $fullheight ne '') {
 1400:                             if ($fullwidth > $width && $fullheight > $height) { 
 1401:                                 my $size = $width.'x'.$height;
 1402:                                 system("convert -sample $size $input $output");
 1403:                                 $showfile = "/$imgdir/tn-".$filename;
 1404:                             }
 1405:                         }
 1406:                     }
 1407:                 }
 1408:             }
 1409:             if ($showfile) {
 1410:                 if ($showfile =~ m{^/(adm|res)/}) {
 1411:                     if ($showfile =~ m{^/res/}) {
 1412:                         my $local_showfile =
 1413:                             &Apache::lonnet::filelocation('',$showfile);
 1414:                         &Apache::lonnet::repcopy($local_showfile);
 1415:                     }
 1416:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1417:                 }
 1418:                 if ($imgfile) {
 1419:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1420:                         if ($imgfile =~ m{^/res/}) {
 1421:                             my $local_imgfile =
 1422:                                 &Apache::lonnet::filelocation('',$imgfile);
 1423:                             &Apache::lonnet::repcopy($local_imgfile);
 1424:                         }
 1425:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1426:                     } else {
 1427:                         $fullsize = $imgfile;
 1428:                     }
 1429:                 }
 1430:                 $datatable .= '<td>';
 1431:                 if ($img eq 'login') {
 1432:                     $datatable .= $login_hdr_pick;
 1433:                 } 
 1434:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1435:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1436:             } else {
 1437:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1438:                               &mt('Upload:').'<br />';
 1439:             }
 1440:         } else {
 1441:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1442:                           &mt('Upload:').'<br />';
 1443:         }
 1444:         if ($switchserver) {
 1445:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1446:         } else {
 1447:             if ($img ne 'login') { # suppress file selection for Log-in header
 1448:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1449:             }
 1450:         }
 1451:         $datatable .= '</td></tr>';
 1452:     }
 1453:     $itemcount ++;
 1454:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1455:     $datatable .= '<tr'.$css_class.'>'.
 1456:                   '<td>'.$choices->{'bgs'}.'</td>';
 1457:     my $bgs_def;
 1458:     foreach my $item (@{$bgs}) {
 1459:         if (!$is_custom->{$item}) {
 1460:             $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>';
 1461:         }
 1462:     }
 1463:     if ($bgs_def) {
 1464:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1465:     } else {
 1466:         $datatable .= '<td>&nbsp;</td>';
 1467:     }
 1468:     $datatable .= '<td class="LC_right_item">'.
 1469:                   '<table border="0"><tr>';
 1470: 
 1471:     foreach my $item (@{$bgs}) {
 1472:         $datatable .= '<td align="center">'.$choices->{$item};
 1473: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1474:         if ($designs->{'bgs'}{$item}) {
 1475:             $datatable .= '&nbsp;';
 1476:         }
 1477:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1478:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1479:     }
 1480:     $datatable .= '</tr></table></td></tr>';
 1481:     $itemcount ++;
 1482:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1483:     $datatable .= '<tr'.$css_class.'>'.
 1484:                   '<td>'.$choices->{'links'}.'</td>';
 1485:     my $links_def;
 1486:     foreach my $item (@{$links}) {
 1487:         if (!$is_custom->{$item}) {
 1488:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1489:         }
 1490:     }
 1491:     if ($links_def) {
 1492:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1493:     } else {
 1494:         $datatable .= '<td>&nbsp;</td>';
 1495:     }
 1496:     $datatable .= '<td class="LC_right_item">'.
 1497:                   '<table border="0"><tr>';
 1498:     foreach my $item (@{$links}) {
 1499: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1500:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1501:         if ($designs->{'links'}{$item}) {
 1502:             $datatable.='&nbsp;';
 1503:         }
 1504:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1505:                       '" /></td>';
 1506:     }
 1507:     $$rowtotal += $itemcount;
 1508:     return $datatable;
 1509: }
 1510: 
 1511: sub logo_display_options {
 1512:     my ($img,$defaults,$designs) = @_;
 1513:     my $checkedon;
 1514:     if (ref($defaults) eq 'HASH') {
 1515:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1516:             if ($defaults->{'showlogo'}{$img}) {
 1517:                 $checkedon = 'checked="checked" ';     
 1518:             }
 1519:         } 
 1520:     }
 1521:     if (ref($designs) eq 'HASH') {
 1522:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1523:             if (defined($designs->{'showlogo'}{$img})) {
 1524:                 if ($designs->{'showlogo'}{$img} == 0) {
 1525:                     $checkedon = '';
 1526:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1527:                     $checkedon = 'checked="checked" ';
 1528:                 }
 1529:             }
 1530:         }
 1531:     }
 1532:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1533:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1534:            &mt('show').'</label>'."\n";
 1535: }
 1536: 
 1537: sub login_header_options  {
 1538:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1539:     my $output = '';
 1540:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1541:         $output .= &mt('Text default(s):').'<br />';
 1542:         if (!$is_custom->{'textcol'}) {
 1543:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1544:                        '&nbsp;&nbsp;&nbsp;';
 1545:         }
 1546:         if (!$is_custom->{'bgcol'}) {
 1547:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1548:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1549:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1550:         }
 1551:         $output .= '<br />';
 1552:     }
 1553:     $output .='<br />';
 1554:     return $output;
 1555: }
 1556: 
 1557: sub login_text_colors {
 1558:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1559:     my $color_menu = '<table border="0"><tr>';
 1560:     foreach my $item (@{$logintext}) {
 1561:         $color_menu .= '<td align="center">'.$choices->{$item};
 1562:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1563:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1564:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1565:     }
 1566:     $color_menu .= '</tr></table><br />';
 1567:     return $color_menu;
 1568: }
 1569: 
 1570: sub image_changes {
 1571:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1572:     my $output;
 1573:     if ($img eq 'login') {
 1574:             # suppress image for Log-in header
 1575:     } elsif (!$is_custom) {
 1576:         if ($img ne 'domlogo') {
 1577:             $output .= &mt('Default image:').'<br />';
 1578:         } else {
 1579:             $output .= &mt('Default in use:').'<br />';
 1580:         }
 1581:     }
 1582:     if ($img eq 'login') { # suppress image for Log-in header
 1583:         $output .= '<td>'.$logincolors;
 1584:     } else {
 1585:         if ($img_import) {
 1586:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1587:         }
 1588:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1589:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1590:         if ($is_custom) {
 1591:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1592:                        '<input type="checkbox" name="'.
 1593:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1594:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1595:         } else {
 1596:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1597:         }
 1598:     }
 1599:     return $output;
 1600: }
 1601: 
 1602: sub print_quotas {
 1603:     my ($dom,$settings,$rowtotal,$action) = @_;
 1604:     my $context;
 1605:     if ($action eq 'quotas') {
 1606:         $context = 'tools';
 1607:     } else {
 1608:         $context = $action;
 1609:     }
 1610:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1611:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1612:     my $typecount = 0;
 1613:     my ($css_class,%titles);
 1614:     if ($context eq 'requestcourses') {
 1615:         @usertools = ('official','unofficial','community','textbook');
 1616:         @options =('norequest','approval','validate','autolimit');
 1617:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1618:         %titles = &courserequest_titles();
 1619:     } elsif ($context eq 'requestauthor') {
 1620:         @usertools = ('author');
 1621:         @options = ('norequest','approval','automatic');
 1622:         %titles = &authorrequest_titles();
 1623:     } else {
 1624:         @usertools = ('aboutme','blog','webdav','portfolio');
 1625:         %titles = &tool_titles();
 1626:     }
 1627:     if (ref($types) eq 'ARRAY') {
 1628:         foreach my $type (@{$types}) {
 1629:             my ($currdefquota,$currauthorquota);
 1630:             unless (($context eq 'requestcourses') ||
 1631:                     ($context eq 'requestauthor')) {
 1632:                 if (ref($settings) eq 'HASH') {
 1633:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1634:                         $currdefquota = $settings->{defaultquota}->{$type};
 1635:                     } else {
 1636:                         $currdefquota = $settings->{$type};
 1637:                     }
 1638:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1639:                         $currauthorquota = $settings->{authorquota}->{$type};
 1640:                     }
 1641:                 }
 1642:             }
 1643:             if (defined($usertypes->{$type})) {
 1644:                 $typecount ++;
 1645:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1646:                 $datatable .= '<tr'.$css_class.'>'.
 1647:                               '<td>'.$usertypes->{$type}.'</td>'.
 1648:                               '<td class="LC_left_item">';
 1649:                 if ($context eq 'requestcourses') {
 1650:                     $datatable .= '<table><tr>';
 1651:                 }
 1652:                 my %cell;  
 1653:                 foreach my $item (@usertools) {
 1654:                     if ($context eq 'requestcourses') {
 1655:                         my ($curroption,$currlimit);
 1656:                         if (ref($settings) eq 'HASH') {
 1657:                             if (ref($settings->{$item}) eq 'HASH') {
 1658:                                 $curroption = $settings->{$item}->{$type};
 1659:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1660:                                     $currlimit = $1; 
 1661:                                 }
 1662:                             }
 1663:                         }
 1664:                         if (!$curroption) {
 1665:                             $curroption = 'norequest';
 1666:                         }
 1667:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1668:                         foreach my $option (@options) {
 1669:                             my $val = $option;
 1670:                             if ($option eq 'norequest') {
 1671:                                 $val = 0;  
 1672:                             }
 1673:                             if ($option eq 'validate') {
 1674:                                 my $canvalidate = 0;
 1675:                                 if (ref($validations{$item}) eq 'HASH') { 
 1676:                                     if ($validations{$item}{$type}) {
 1677:                                         $canvalidate = 1;
 1678:                                     }
 1679:                                 }
 1680:                                 next if (!$canvalidate);
 1681:                             }
 1682:                             my $checked = '';
 1683:                             if ($option eq $curroption) {
 1684:                                 $checked = ' checked="checked"';
 1685:                             } elsif ($option eq 'autolimit') {
 1686:                                 if ($curroption =~ /^autolimit/) {
 1687:                                     $checked = ' checked="checked"';
 1688:                                 }                       
 1689:                             } 
 1690:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1691:                                   '<input type="radio" name="crsreq_'.$item.
 1692:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1693:                                   $titles{$option}.'</label>';
 1694:                             if ($option eq 'autolimit') {
 1695:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1696:                                                 $item.'_limit_'.$type.'" size="1" '.
 1697:                                                 'value="'.$currlimit.'" />';
 1698:                             }
 1699:                             $cell{$item} .= '</span> ';
 1700:                             if ($option eq 'autolimit') {
 1701:                                 $cell{$item} .= $titles{'unlimited'};
 1702:                             }
 1703:                         }
 1704:                     } elsif ($context eq 'requestauthor') {
 1705:                         my $curroption;
 1706:                         if (ref($settings) eq 'HASH') {
 1707:                             $curroption = $settings->{$type};
 1708:                         }
 1709:                         if (!$curroption) {
 1710:                             $curroption = 'norequest';
 1711:                         }
 1712:                         foreach my $option (@options) {
 1713:                             my $val = $option;
 1714:                             if ($option eq 'norequest') {
 1715:                                 $val = 0;
 1716:                             }
 1717:                             my $checked = '';
 1718:                             if ($option eq $curroption) {
 1719:                                 $checked = ' checked="checked"';
 1720:                             }
 1721:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1722:                                   '<input type="radio" name="authorreq_'.$type.
 1723:                                   '" value="'.$val.'"'.$checked.' />'.
 1724:                                   $titles{$option}.'</label></span>&nbsp; ';
 1725:                         }
 1726:                     } else {
 1727:                         my $checked = 'checked="checked" ';
 1728:                         if (ref($settings) eq 'HASH') {
 1729:                             if (ref($settings->{$item}) eq 'HASH') {
 1730:                                 if ($settings->{$item}->{$type} == 0) {
 1731:                                     $checked = '';
 1732:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1733:                                     $checked =  'checked="checked" ';
 1734:                                 }
 1735:                             }
 1736:                         }
 1737:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1738:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1739:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1740:                                       '</label></span>&nbsp; ';
 1741:                     }
 1742:                 }
 1743:                 if ($context eq 'requestcourses') {
 1744:                     $datatable .= '</tr><tr>';
 1745:                     foreach my $item (@usertools) {
 1746:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1747:                     }
 1748:                     $datatable .= '</tr></table>';
 1749:                 }
 1750:                 $datatable .= '</td>';
 1751:                 unless (($context eq 'requestcourses') ||
 1752:                         ($context eq 'requestauthor')) {
 1753:                     $datatable .= 
 1754:                               '<td class="LC_right_item">'.
 1755:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1756:                               '<input type="text" name="quota_'.$type.
 1757:                               '" value="'.$currdefquota.
 1758:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1759:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1760:                               '<input type="text" name="authorquota_'.$type.
 1761:                               '" value="'.$currauthorquota.
 1762:                               '" size="5" /></span></td>';
 1763:                 }
 1764:                 $datatable .= '</tr>';
 1765:             }
 1766:         }
 1767:     }
 1768:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1769:         $defaultquota = '20';
 1770:         $authorquota = '500';
 1771:         if (ref($settings) eq 'HASH') {
 1772:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1773:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1774:             } elsif (defined($settings->{'default'})) {
 1775:                 $defaultquota = $settings->{'default'};
 1776:             }
 1777:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1778:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1779:             }
 1780:         }
 1781:     }
 1782:     $typecount ++;
 1783:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1784:     $datatable .= '<tr'.$css_class.'>'.
 1785:                   '<td>'.$othertitle.'</td>'.
 1786:                   '<td class="LC_left_item">';
 1787:     if ($context eq 'requestcourses') {
 1788:         $datatable .= '<table><tr>';
 1789:     }
 1790:     my %defcell;
 1791:     foreach my $item (@usertools) {
 1792:         if ($context eq 'requestcourses') {
 1793:             my ($curroption,$currlimit);
 1794:             if (ref($settings) eq 'HASH') {
 1795:                 if (ref($settings->{$item}) eq 'HASH') {
 1796:                     $curroption = $settings->{$item}->{'default'};
 1797:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1798:                         $currlimit = $1;
 1799:                     }
 1800:                 }
 1801:             }
 1802:             if (!$curroption) {
 1803:                 $curroption = 'norequest';
 1804:             }
 1805:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1806:             foreach my $option (@options) {
 1807:                 my $val = $option;
 1808:                 if ($option eq 'norequest') {
 1809:                     $val = 0;
 1810:                 }
 1811:                 if ($option eq 'validate') {
 1812:                     my $canvalidate = 0;
 1813:                     if (ref($validations{$item}) eq 'HASH') {
 1814:                         if ($validations{$item}{'default'}) {
 1815:                             $canvalidate = 1;
 1816:                         }
 1817:                     }
 1818:                     next if (!$canvalidate);
 1819:                 }
 1820:                 my $checked = '';
 1821:                 if ($option eq $curroption) {
 1822:                     $checked = ' checked="checked"';
 1823:                 } elsif ($option eq 'autolimit') {
 1824:                     if ($curroption =~ /^autolimit/) {
 1825:                         $checked = ' checked="checked"';
 1826:                     }
 1827:                 }
 1828:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1829:                                   '<input type="radio" name="crsreq_'.$item.
 1830:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1831:                                   $titles{$option}.'</label>';
 1832:                 if ($option eq 'autolimit') {
 1833:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1834:                                        $item.'_limit_default" size="1" '.
 1835:                                        'value="'.$currlimit.'" />';
 1836:                 }
 1837:                 $defcell{$item} .= '</span> ';
 1838:                 if ($option eq 'autolimit') {
 1839:                     $defcell{$item} .= $titles{'unlimited'};
 1840:                 }
 1841:             }
 1842:         } elsif ($context eq 'requestauthor') {
 1843:             my $curroption;
 1844:             if (ref($settings) eq 'HASH') {
 1845:                 $curroption = $settings->{'default'};
 1846:             }
 1847:             if (!$curroption) {
 1848:                 $curroption = 'norequest';
 1849:             }
 1850:             foreach my $option (@options) {
 1851:                 my $val = $option;
 1852:                 if ($option eq 'norequest') {
 1853:                     $val = 0;
 1854:                 }
 1855:                 my $checked = '';
 1856:                 if ($option eq $curroption) {
 1857:                     $checked = ' checked="checked"';
 1858:                 }
 1859:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1860:                               '<input type="radio" name="authorreq_default"'.
 1861:                               ' value="'.$val.'"'.$checked.' />'.
 1862:                               $titles{$option}.'</label></span>&nbsp; ';
 1863:             }
 1864:         } else {
 1865:             my $checked = 'checked="checked" ';
 1866:             if (ref($settings) eq 'HASH') {
 1867:                 if (ref($settings->{$item}) eq 'HASH') {
 1868:                     if ($settings->{$item}->{'default'} == 0) {
 1869:                         $checked = '';
 1870:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1871:                         $checked = 'checked="checked" ';
 1872:                     }
 1873:                 }
 1874:             }
 1875:             $datatable .= '<span class="LC_nobreak"><label>'.
 1876:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1877:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1878:                           '</label></span>&nbsp; ';
 1879:         }
 1880:     }
 1881:     if ($context eq 'requestcourses') {
 1882:         $datatable .= '</tr><tr>';
 1883:         foreach my $item (@usertools) {
 1884:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1885:         }
 1886:         $datatable .= '</tr></table>';
 1887:     }
 1888:     $datatable .= '</td>';
 1889:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1890:         $datatable .= '<td class="LC_right_item">'.
 1891:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1892:                       '<input type="text" name="defaultquota" value="'.
 1893:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1894:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1895:                       '<input type="text" name="authorquota" value="'.
 1896:                       $authorquota.'" size="5" /></span></td>';
 1897:     }
 1898:     $datatable .= '</tr>';
 1899:     $typecount ++;
 1900:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1901:     $datatable .= '<tr'.$css_class.'>'.
 1902:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1903:     if ($context eq 'requestcourses') {
 1904:         $datatable .= &mt('(overrides affiliation, if set)').
 1905:                       '</td>'.
 1906:                       '<td class="LC_left_item">'.
 1907:                       '<table><tr>';
 1908:     } else {
 1909:         $datatable .= &mt('(overrides affiliation, if checked)').
 1910:                       '</td>'.
 1911:                       '<td class="LC_left_item" colspan="2">'.
 1912:                       '<br />';
 1913:     }
 1914:     my %advcell;
 1915:     foreach my $item (@usertools) {
 1916:         if ($context eq 'requestcourses') {
 1917:             my ($curroption,$currlimit);
 1918:             if (ref($settings) eq 'HASH') {
 1919:                 if (ref($settings->{$item}) eq 'HASH') {
 1920:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1921:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1922:                         $currlimit = $1;
 1923:                     }
 1924:                 }
 1925:             }
 1926:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1927:             my $checked = '';
 1928:             if ($curroption eq '') {
 1929:                 $checked = ' checked="checked"';
 1930:             }
 1931:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1932:                                '<input type="radio" name="crsreq_'.$item.
 1933:                                '__LC_adv" value=""'.$checked.' />'.
 1934:                                &mt('No override set').'</label></span>&nbsp; ';
 1935:             foreach my $option (@options) {
 1936:                 my $val = $option;
 1937:                 if ($option eq 'norequest') {
 1938:                     $val = 0;
 1939:                 }
 1940:                 if ($option eq 'validate') {
 1941:                     my $canvalidate = 0;
 1942:                     if (ref($validations{$item}) eq 'HASH') {
 1943:                         if ($validations{$item}{'_LC_adv'}) {
 1944:                             $canvalidate = 1;
 1945:                         }
 1946:                     }
 1947:                     next if (!$canvalidate);
 1948:                 }
 1949:                 my $checked = '';
 1950:                 if ($val eq $curroption) {
 1951:                     $checked = ' checked="checked"';
 1952:                 } elsif ($option eq 'autolimit') {
 1953:                     if ($curroption =~ /^autolimit/) {
 1954:                         $checked = ' checked="checked"';
 1955:                     }
 1956:                 }
 1957:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1958:                                   '<input type="radio" name="crsreq_'.$item.
 1959:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1960:                                   $titles{$option}.'</label>';
 1961:                 if ($option eq 'autolimit') {
 1962:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1963:                                        $item.'_limit__LC_adv" size="1" '.
 1964:                                        'value="'.$currlimit.'" />';
 1965:                 }
 1966:                 $advcell{$item} .= '</span> ';
 1967:                 if ($option eq 'autolimit') {
 1968:                     $advcell{$item} .= $titles{'unlimited'};
 1969:                 }
 1970:             }
 1971:         } elsif ($context eq 'requestauthor') {
 1972:             my $curroption;
 1973:             if (ref($settings) eq 'HASH') {
 1974:                 $curroption = $settings->{'_LC_adv'};
 1975:             }
 1976:             my $checked = '';
 1977:             if ($curroption eq '') {
 1978:                 $checked = ' checked="checked"';
 1979:             }
 1980:             $datatable .= '<span class="LC_nobreak"><label>'.
 1981:                           '<input type="radio" name="authorreq__LC_adv"'.
 1982:                           ' value=""'.$checked.' />'.
 1983:                           &mt('No override set').'</label></span>&nbsp; ';
 1984:             foreach my $option (@options) {
 1985:                 my $val = $option;
 1986:                 if ($option eq 'norequest') {
 1987:                     $val = 0;
 1988:                 }
 1989:                 my $checked = '';
 1990:                 if ($val eq $curroption) {
 1991:                     $checked = ' checked="checked"';
 1992:                 }
 1993:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1994:                               '<input type="radio" name="authorreq__LC_adv"'.
 1995:                               ' value="'.$val.'"'.$checked.' />'.
 1996:                               $titles{$option}.'</label></span>&nbsp; ';
 1997:             }
 1998:         } else {
 1999:             my $checked = 'checked="checked" ';
 2000:             if (ref($settings) eq 'HASH') {
 2001:                 if (ref($settings->{$item}) eq 'HASH') {
 2002:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2003:                         $checked = '';
 2004:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2005:                         $checked = 'checked="checked" ';
 2006:                     }
 2007:                 }
 2008:             }
 2009:             $datatable .= '<span class="LC_nobreak"><label>'.
 2010:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2011:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2012:                           '</label></span>&nbsp; ';
 2013:         }
 2014:     }
 2015:     if ($context eq 'requestcourses') {
 2016:         $datatable .= '</tr><tr>';
 2017:         foreach my $item (@usertools) {
 2018:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2019:         }
 2020:         $datatable .= '</tr></table>';
 2021:     }
 2022:     $datatable .= '</td></tr>';
 2023:     $$rowtotal += $typecount;
 2024:     return $datatable;
 2025: }
 2026: 
 2027: sub print_requestmail {
 2028:     my ($dom,$action,$settings,$rowtotal) = @_;
 2029:     my ($now,$datatable,%currapp);
 2030:     $now = time;
 2031:     if (ref($settings) eq 'HASH') {
 2032:         if (ref($settings->{'notify'}) eq 'HASH') {
 2033:             if ($settings->{'notify'}{'approval'} ne '') {
 2034:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2035:             }
 2036:         }
 2037:     }
 2038:     my $numinrow = 2;
 2039:     my $css_class;
 2040:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2041:     my $text;
 2042:     if ($action eq 'requestcourses') {
 2043:         $text = &mt('Receive notification of course requests requiring approval');
 2044:     } elsif ($action eq 'requestauthor') {
 2045:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2046:     } else {
 2047:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2048:     }
 2049:     $datatable = '<tr'.$css_class.'>'.
 2050:                  ' <td>'.$text.'</td>'.
 2051:                  ' <td class="LC_left_item">';
 2052:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2053:                                                  $action.'notifyapproval',%currapp);
 2054:     if ($numdc > 0) {
 2055:         $datatable .= $table;
 2056:     } else {
 2057:         $datatable .= &mt('There are no active Domain Coordinators');
 2058:     }
 2059:     $datatable .='</td></tr>';
 2060:     return $datatable;
 2061: }
 2062: 
 2063: sub print_studentcode {
 2064:     my ($settings,$rowtotal) = @_;
 2065:     my $rownum = 0; 
 2066:     my ($output,%current);
 2067:     my @crstypes = ('official','unofficial','community','textbook');
 2068:     if (ref($settings) eq 'HASH') {
 2069:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2070:             foreach my $type (@crstypes) {
 2071:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2072:             }
 2073:         }
 2074:     }
 2075:     $output .= '<tr>'.
 2076:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2077:                '<td class="LC_left_item">';
 2078:     foreach my $type (@crstypes) {
 2079:         my $check = ' ';
 2080:         if ($current{$type}) {
 2081:             $check = ' checked="checked" ';
 2082:         }
 2083:         $output .= '<span class="LC_nobreak"><label>'.
 2084:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2085:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2086:     }
 2087:     $output .= '</td></tr>';
 2088:     $$rowtotal ++;
 2089:     return $output;
 2090: }
 2091: 
 2092: sub print_textbookcourses {
 2093:     my ($dom,$type,$settings,$rowtotal) = @_;
 2094:     my $rownum = 0;
 2095:     my $css_class;
 2096:     my $itemcount = 1;
 2097:     my $maxnum = 0;
 2098:     my $bookshash;
 2099:     if (ref($settings) eq 'HASH') {
 2100:         $bookshash = $settings->{$type};
 2101:     }
 2102:     my %ordered;
 2103:     if (ref($bookshash) eq 'HASH') {
 2104:         foreach my $item (keys(%{$bookshash})) {
 2105:             if (ref($bookshash->{$item}) eq 'HASH') {
 2106:                 my $num = $bookshash->{$item}{'order'};
 2107:                 $ordered{$num} = $item;
 2108:             }
 2109:         }
 2110:     }
 2111:     my $confname = $dom.'-domainconfig';
 2112:     my $switchserver = &check_switchserver($dom,$confname);
 2113:     my $maxnum = scalar(keys(%ordered));
 2114:     my $datatable;
 2115:     if (keys(%ordered)) {
 2116:         my @items = sort { $a <=> $b } keys(%ordered);
 2117:         for (my $i=0; $i<@items; $i++) {
 2118:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2119:             my $key = $ordered{$items[$i]};
 2120:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2121:             my $coursetitle = $coursehash{'description'};
 2122:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2123:             if (ref($bookshash->{$key}) eq 'HASH') {
 2124:                 $subject = $bookshash->{$key}->{'subject'};
 2125:                 $title = $bookshash->{$key}->{'title'};
 2126:                 if ($type eq 'textbooks') {
 2127:                     $publisher = $bookshash->{$key}->{'publisher'};
 2128:                     $author = $bookshash->{$key}->{'author'};
 2129:                     $image = $bookshash->{$key}->{'image'};
 2130:                     if ($image ne '') {
 2131:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2132:                         my $imagethumb = "$path/tn-".$imagefile;
 2133:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2134:                     }
 2135:                 }
 2136:             }
 2137:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2138:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2139:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2140:             for (my $k=0; $k<=$maxnum; $k++) {
 2141:                 my $vpos = $k+1;
 2142:                 my $selstr;
 2143:                 if ($k == $i) {
 2144:                     $selstr = ' selected="selected" ';
 2145:                 }
 2146:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2147:             }
 2148:             $datatable .= '</select>'.('&nbsp;'x2).
 2149:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2150:                 &mt('Delete?').'</label></span></td>'.
 2151:                 '<td colspan="2">'.
 2152:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2153:                 ('&nbsp;'x2).
 2154:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2155:             if ($type eq 'textbooks') {
 2156:                 $datatable .= ('&nbsp;'x2).
 2157:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2158:                               ('&nbsp;'x2).
 2159:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2160:                               ('&nbsp;'x2).
 2161:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2162:                 if ($image) {
 2163:                     $datatable .= '<span class="LC_nobreak">'.
 2164:                                   $imgsrc.
 2165:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2166:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2167:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2168:                 }
 2169:                 if ($switchserver) {
 2170:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2171:                 } else {
 2172:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2173:                 }
 2174:             }
 2175:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2176:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2177:                           $coursetitle.'</span></td></tr>'."\n";
 2178:             $itemcount ++;
 2179:         }
 2180:     }
 2181:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2182:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2183:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2184:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2185:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2186:     for (my $k=0; $k<$maxnum+1; $k++) {
 2187:         my $vpos = $k+1;
 2188:         my $selstr;
 2189:         if ($k == $maxnum) {
 2190:             $selstr = ' selected="selected" ';
 2191:         }
 2192:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2193:     }
 2194:     $datatable .= '</select>&nbsp;'."\n".
 2195:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2196:                   '<td colspan="2">'.
 2197:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2198:                   ('&nbsp;'x2).
 2199:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2200:                   ('&nbsp;'x2);
 2201:     if ($type eq 'textbooks') {
 2202:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2203:                       ('&nbsp;'x2).
 2204:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2205:                       ('&nbsp;'x2).
 2206:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2207:         if ($switchserver) {
 2208:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2209:         } else {
 2210:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2211:         }
 2212:     }
 2213:     $datatable .= '</span>'."\n".
 2214:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2215:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2216:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2217:                   &Apache::loncommon::selectcourse_link
 2218:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2219:                   '</span></td>'."\n".
 2220:                   '</tr>'."\n";
 2221:     $itemcount ++;
 2222:     return $datatable;
 2223: }
 2224: 
 2225: sub textbookcourses_javascript {
 2226:     my ($settings) = @_;
 2227:     return unless(ref($settings) eq 'HASH');
 2228:     my (%ordered,%total,%jstext);
 2229:     foreach my $type ('textbooks','templates') {
 2230:         $total{$type} = 0;
 2231:         if (ref($settings->{$type}) eq 'HASH') {
 2232:             foreach my $item (keys(%{$settings->{$type}})) {
 2233:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2234:                     my $num = $settings->{$type}->{$item}{'order'};
 2235:                     $ordered{$type}{$num} = $item;
 2236:                 }
 2237:             }
 2238:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2239:         }
 2240:         my @jsarray = ();
 2241:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2242:             push(@jsarray,$ordered{$type}{$item});
 2243:         }
 2244:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2245:     }
 2246:     return <<"ENDSCRIPT";
 2247: <script type="text/javascript">
 2248: // <![CDATA[
 2249: function reorderBooks(form,item,caller) {
 2250:     var changedVal;
 2251: $jstext{'textbooks'};
 2252: $jstext{'templates'};
 2253:     var newpos;
 2254:     var maxh;
 2255:     if (caller == 'textbooks') {  
 2256:         newpos = 'textbooks_addbook_pos';
 2257:         maxh = 1 + $total{'textbooks'};
 2258:     } else {
 2259:         newpos = 'templates_addbook_pos';
 2260:         maxh = 1 + $total{'templates'};
 2261:     }
 2262:     var current = new Array;
 2263:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2264:     if (item == newpos) {
 2265:         changedVal = newitemVal;
 2266:     } else {
 2267:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2268:         current[newitemVal] = newpos;
 2269:     }
 2270:     if (caller == 'textbooks') {
 2271:         for (var i=0; i<textbooks.length; i++) {
 2272:             var elementName = 'textbooks_'+textbooks[i];
 2273:             if (elementName != item) {
 2274:                 if (form.elements[elementName]) {
 2275:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2276:                     current[currVal] = elementName;
 2277:                 }
 2278:             }
 2279:         }
 2280:     }
 2281:     if (caller == 'templates') {
 2282:         for (var i=0; i<templates.length; i++) {
 2283:             var elementName = 'templates_'+templates[i];
 2284:             if (elementName != item) {
 2285:                 if (form.elements[elementName]) {
 2286:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2287:                     current[currVal] = elementName;
 2288:                 }
 2289:             }
 2290:         }
 2291:     }
 2292:     var oldVal;
 2293:     for (var j=0; j<maxh; j++) {
 2294:         if (current[j] == undefined) {
 2295:             oldVal = j;
 2296:         }
 2297:     }
 2298:     if (oldVal < changedVal) {
 2299:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2300:            var elementName = current[k];
 2301:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2302:         }
 2303:     } else {
 2304:         for (var k=changedVal; k<oldVal; k++) {
 2305:             var elementName = current[k];
 2306:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2307:         }
 2308:     }
 2309:     return;
 2310: }
 2311: 
 2312: // ]]>
 2313: </script>
 2314: 
 2315: ENDSCRIPT
 2316: }
 2317: 
 2318: sub print_autoenroll {
 2319:     my ($dom,$settings,$rowtotal) = @_;
 2320:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2321:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2322:     if (ref($settings) eq 'HASH') {
 2323:         if (exists($settings->{'run'})) {
 2324:             if ($settings->{'run'} eq '0') {
 2325:                 $runoff = ' checked="checked" ';
 2326:                 $runon = ' ';
 2327:             } else {
 2328:                 $runon = ' checked="checked" ';
 2329:                 $runoff = ' ';
 2330:             }
 2331:         } else {
 2332:             if ($autorun) {
 2333:                 $runon = ' checked="checked" ';
 2334:                 $runoff = ' ';
 2335:             } else {
 2336:                 $runoff = ' checked="checked" ';
 2337:                 $runon = ' ';
 2338:             }
 2339:         }
 2340:         if (exists($settings->{'co-owners'})) {
 2341:             if ($settings->{'co-owners'} eq '0') {
 2342:                 $coownersoff = ' checked="checked" ';
 2343:                 $coownerson = ' ';
 2344:             } else {
 2345:                 $coownerson = ' checked="checked" ';
 2346:                 $coownersoff = ' ';
 2347:             }
 2348:         } else {
 2349:             $coownersoff = ' checked="checked" ';
 2350:             $coownerson = ' ';
 2351:         }
 2352:         if (exists($settings->{'sender_domain'})) {
 2353:             $defdom = $settings->{'sender_domain'};
 2354:         }
 2355:     } else {
 2356:         if ($autorun) {
 2357:             $runon = ' checked="checked" ';
 2358:             $runoff = ' ';
 2359:         } else {
 2360:             $runoff = ' checked="checked" ';
 2361:             $runon = ' ';
 2362:         }
 2363:     }
 2364:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2365:     my $notif_sender;
 2366:     if (ref($settings) eq 'HASH') {
 2367:         $notif_sender = $settings->{'sender_uname'};
 2368:     }
 2369:     my $datatable='<tr class="LC_odd_row">'.
 2370:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2371:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2372:                   '<input type="radio" name="autoenroll_run"'.
 2373:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2374:                   '<label><input type="radio" name="autoenroll_run"'.
 2375:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2376:                   '</tr><tr>'.
 2377:                   '<td>'.&mt('Notification messages - sender').
 2378:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2379:                   &mt('username').':&nbsp;'.
 2380:                   '<input type="text" name="sender_uname" value="'.
 2381:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2382:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2383:                   '<tr class="LC_odd_row">'.
 2384:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2385:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2386:                   '<input type="radio" name="autoassign_coowners"'.
 2387:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2388:                   '<label><input type="radio" name="autoassign_coowners"'.
 2389:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2390:                   '</tr>';
 2391:     $$rowtotal += 3;
 2392:     return $datatable;
 2393: }
 2394: 
 2395: sub print_autoupdate {
 2396:     my ($position,$dom,$settings,$rowtotal) = @_;
 2397:     my $datatable;
 2398:     if ($position eq 'top') {
 2399:         my $updateon = ' ';
 2400:         my $updateoff = ' checked="checked" ';
 2401:         my $classlistson = ' ';
 2402:         my $classlistsoff = ' checked="checked" ';
 2403:         if (ref($settings) eq 'HASH') {
 2404:             if ($settings->{'run'} eq '1') {
 2405:                 $updateon = $updateoff;
 2406:                 $updateoff = ' ';
 2407:             }
 2408:             if ($settings->{'classlists'} eq '1') {
 2409:                 $classlistson = $classlistsoff;
 2410:                 $classlistsoff = ' ';
 2411:             }
 2412:         }
 2413:         my %title = (
 2414:                    run => 'Auto-update active?',
 2415:                    classlists => 'Update information in classlists?',
 2416:                     );
 2417:         $datatable = '<tr class="LC_odd_row">'. 
 2418:                   '<td>'.&mt($title{'run'}).'</td>'.
 2419:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2420:                   '<input type="radio" name="autoupdate_run"'.
 2421:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2422:                   '<label><input type="radio" name="autoupdate_run"'.
 2423:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2424:                   '</tr><tr>'.
 2425:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2426:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2427:                   '<label><input type="radio" name="classlists"'.
 2428:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2429:                   '<label><input type="radio" name="classlists"'.
 2430:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2431:                   '</tr>';
 2432:         $$rowtotal += 2;
 2433:     } elsif ($position eq 'middle') {
 2434:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2435:         my $numinrow = 3;
 2436:         my $locknamesettings;
 2437:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2438:                                      $dom,$numinrow,$othertitle,
 2439:                                     'lockablenames');
 2440:         $$rowtotal ++;
 2441:     } else {
 2442:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2443:         my @fields = ('lastname','firstname','middlename','generation',
 2444:                       'permanentemail','id');
 2445:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2446:         my $numrows = 0;
 2447:         if (ref($types) eq 'ARRAY') {
 2448:             if (@{$types} > 0) {
 2449:                 $datatable = 
 2450:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2451:                                          \@fields,$types,\$numrows);
 2452:                     $$rowtotal += @{$types}; 
 2453:             }
 2454:         }
 2455:         $datatable .= 
 2456:             &usertype_update_row($settings,{'default' => $othertitle},
 2457:                                  \%fieldtitles,\@fields,['default'],
 2458:                                  \$numrows);
 2459:         $$rowtotal ++;     
 2460:     }
 2461:     return $datatable;
 2462: }
 2463: 
 2464: sub print_autocreate {
 2465:     my ($dom,$settings,$rowtotal) = @_;
 2466:     my (%createon,%createoff,%currhash);
 2467:     my @types = ('xml','req');
 2468:     if (ref($settings) eq 'HASH') {
 2469:         foreach my $item (@types) {
 2470:             $createoff{$item} = ' checked="checked" ';
 2471:             $createon{$item} = ' ';
 2472:             if (exists($settings->{$item})) {
 2473:                 if ($settings->{$item}) {
 2474:                     $createon{$item} = ' checked="checked" ';
 2475:                     $createoff{$item} = ' ';
 2476:                 }
 2477:             }
 2478:         }
 2479:         if ($settings->{'xmldc'} ne '') {
 2480:             $currhash{$settings->{'xmldc'}} = 1;
 2481:         }
 2482:     } else {
 2483:         foreach my $item (@types) {
 2484:             $createoff{$item} = ' checked="checked" ';
 2485:             $createon{$item} = ' ';
 2486:         }
 2487:     }
 2488:     $$rowtotal += 2;
 2489:     my $numinrow = 2;
 2490:     my $datatable='<tr class="LC_odd_row">'.
 2491:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2492:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2493:                   '<input type="radio" name="autocreate_xml"'.
 2494:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2495:                   '<label><input type="radio" name="autocreate_xml"'.
 2496:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2497:                   '</td></tr><tr>'.
 2498:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2499:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2500:                   '<input type="radio" name="autocreate_req"'.
 2501:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2502:                   '<label><input type="radio" name="autocreate_req"'.
 2503:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2504:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2505:                                                    'autocreate_xmldc',%currhash);
 2506:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2507:     if ($numdc > 1) {
 2508:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2509:                       '</td><td class="LC_left_item">';
 2510:     } else {
 2511:         $datatable .= &mt('Course creation processed as:').
 2512:                       '</td><td class="LC_right_item">';
 2513:     }
 2514:     $datatable .= $dctable.'</td></tr>';
 2515:     $$rowtotal += $rows;
 2516:     return $datatable;
 2517: }
 2518: 
 2519: sub print_directorysrch {
 2520:     my ($dom,$settings,$rowtotal) = @_;
 2521:     my $srchon = ' ';
 2522:     my $srchoff = ' checked="checked" ';
 2523:     my ($exacton,$containson,$beginson);
 2524:     my $localon = ' ';
 2525:     my $localoff = ' checked="checked" ';
 2526:     if (ref($settings) eq 'HASH') {
 2527:         if ($settings->{'available'} eq '1') {
 2528:             $srchon = $srchoff;
 2529:             $srchoff = ' ';
 2530:         }
 2531:         if ($settings->{'localonly'} eq '1') {
 2532:             $localon = $localoff;
 2533:             $localoff = ' ';
 2534:         }
 2535:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2536:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2537:                 if ($type eq 'exact') {
 2538:                     $exacton = ' checked="checked" ';
 2539:                 } elsif ($type eq 'contains') {
 2540:                     $containson = ' checked="checked" ';
 2541:                 } elsif ($type eq 'begins') {
 2542:                     $beginson = ' checked="checked" ';
 2543:                 }
 2544:             }
 2545:         } else {
 2546:             if ($settings->{'searchtypes'} eq 'exact') {
 2547:                 $exacton = ' checked="checked" ';
 2548:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2549:                 $containson = ' checked="checked" ';
 2550:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2551:                 $exacton = ' checked="checked" ';
 2552:                 $containson = ' checked="checked" ';
 2553:             }
 2554:         }
 2555:     }
 2556:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2557:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2558: 
 2559:     my $numinrow = 4;
 2560:     my $cansrchrow = 0;
 2561:     my $datatable='<tr class="LC_odd_row">'.
 2562:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2563:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2564:                   '<input type="radio" name="dirsrch_available"'.
 2565:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2566:                   '<label><input type="radio" name="dirsrch_available"'.
 2567:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2568:                   '</tr><tr>'.
 2569:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2570:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2571:                   '<input type="radio" name="dirsrch_localonly"'.
 2572:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2573:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2574:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2575:                   '</tr>';
 2576:     $$rowtotal += 2;
 2577:     if (ref($usertypes) eq 'HASH') {
 2578:         if (keys(%{$usertypes}) > 0) {
 2579:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2580:                                          $numinrow,$othertitle,'cansearch');
 2581:             $cansrchrow = 1;
 2582:         }
 2583:     }
 2584:     if ($cansrchrow) {
 2585:         $$rowtotal ++;
 2586:         $datatable .= '<tr>';
 2587:     } else {
 2588:         $datatable .= '<tr class="LC_odd_row">';
 2589:     }
 2590:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2591:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2592:     foreach my $title (@{$titleorder}) {
 2593:         if (defined($searchtitles->{$title})) {
 2594:             my $check = ' ';
 2595:             if (ref($settings) eq 'HASH') {
 2596:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2597:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2598:                         $check = ' checked="checked" ';
 2599:                     }
 2600:                 }
 2601:             }
 2602:             $datatable .= '<td class="LC_left_item">'.
 2603:                           '<span class="LC_nobreak"><label>'.
 2604:                           '<input type="checkbox" name="searchby" '.
 2605:                           'value="'.$title.'"'.$check.'/>'.
 2606:                           $searchtitles->{$title}.'</label></span></td>';
 2607:         }
 2608:     }
 2609:     $datatable .= '</tr></table></td></tr>';
 2610:     $$rowtotal ++;
 2611:     if ($cansrchrow) {
 2612:         $datatable .= '<tr class="LC_odd_row">';
 2613:     } else {
 2614:         $datatable .= '<tr>';
 2615:     }
 2616:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2617:                   '<td class="LC_left_item" colspan="2">'.
 2618:                   '<span class="LC_nobreak"><label>'.
 2619:                   '<input type="checkbox" name="searchtypes" '.
 2620:                   $exacton.' value="exact" />'.&mt('Exact match').
 2621:                   '</label>&nbsp;'.
 2622:                   '<label><input type="checkbox" name="searchtypes" '.
 2623:                   $beginson.' value="begins" />'.&mt('Begins with').
 2624:                   '</label>&nbsp;'.
 2625:                   '<label><input type="checkbox" name="searchtypes" '.
 2626:                   $containson.' value="contains" />'.&mt('Contains').
 2627:                   '</label></span></td></tr>';
 2628:     $$rowtotal ++;
 2629:     return $datatable;
 2630: }
 2631: 
 2632: sub print_contacts {
 2633:     my ($dom,$settings,$rowtotal) = @_;
 2634:     my $datatable;
 2635:     my @contacts = ('adminemail','supportemail');
 2636:     my (%checked,%to,%otheremails,%bccemails);
 2637:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2638:                     'requestsmail','updatesmail','idconflictsmail');
 2639:     foreach my $type (@mailings) {
 2640:         $otheremails{$type} = '';
 2641:     }
 2642:     $bccemails{'helpdeskmail'} = '';
 2643:     if (ref($settings) eq 'HASH') {
 2644:         foreach my $item (@contacts) {
 2645:             if (exists($settings->{$item})) {
 2646:                 $to{$item} = $settings->{$item};
 2647:             }
 2648:         }
 2649:         foreach my $type (@mailings) {
 2650:             if (exists($settings->{$type})) {
 2651:                 if (ref($settings->{$type}) eq 'HASH') {
 2652:                     foreach my $item (@contacts) {
 2653:                         if ($settings->{$type}{$item}) {
 2654:                             $checked{$type}{$item} = ' checked="checked" ';
 2655:                         }
 2656:                     }
 2657:                     $otheremails{$type} = $settings->{$type}{'others'};
 2658:                     if ($type eq 'helpdeskmail') {
 2659:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2660:                     }
 2661:                 }
 2662:             } elsif ($type eq 'lonstatusmail') {
 2663:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2664:             }
 2665:         }
 2666:     } else {
 2667:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2668:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2669:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2670:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2671:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2672:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2673:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2674:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2675:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2676:     }
 2677:     my ($titles,$short_titles) = &contact_titles();
 2678:     my $rownum = 0;
 2679:     my $css_class;
 2680:     foreach my $item (@contacts) {
 2681:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2682:         $datatable .= '<tr'.$css_class.'>'. 
 2683:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2684:                   '</span></td><td class="LC_right_item">'.
 2685:                   '<input type="text" name="'.$item.'" value="'.
 2686:                   $to{$item}.'" /></td></tr>';
 2687:         $rownum ++;
 2688:     }
 2689:     foreach my $type (@mailings) {
 2690:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2691:         $datatable .= '<tr'.$css_class.'>'.
 2692:                       '<td><span class="LC_nobreak">'.
 2693:                       $titles->{$type}.': </span></td>'.
 2694:                       '<td class="LC_left_item">'.
 2695:                       '<span class="LC_nobreak">';
 2696:         foreach my $item (@contacts) {
 2697:             $datatable .= '<label>'.
 2698:                           '<input type="checkbox" name="'.$type.'"'.
 2699:                           $checked{$type}{$item}.
 2700:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2701:                           '</label>&nbsp;';
 2702:         }
 2703:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2704:                       '<input type="text" name="'.$type.'_others" '.
 2705:                       'value="'.$otheremails{$type}.'"  />';
 2706:         if ($type eq 'helpdeskmail') {
 2707:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2708:                           '<input type="text" name="'.$type.'_bcc" '.
 2709:                           'value="'.$bccemails{$type}.'"  />';
 2710:         }
 2711:         $datatable .= '</td></tr>'."\n";
 2712:         $rownum ++;
 2713:     }
 2714:     my %choices;
 2715:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2716:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2717:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2718:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2719:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2720:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2721:     my @toggles = ('reporterrors','reportupdates');
 2722:     my %defaultchecked = ('reporterrors'  => 'on',
 2723:                           'reportupdates' => 'on');
 2724:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2725:                                                \%choices,$rownum);
 2726:     $datatable .= $reports;
 2727:     $$rowtotal += $rownum;
 2728:     return $datatable;
 2729: }
 2730: 
 2731: sub print_helpsettings {
 2732:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2733:     my ($datatable,$itemcount);
 2734:     $itemcount = 1;
 2735:     my (%choices,%defaultchecked,@toggles);
 2736:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2737:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2738:                                  &mt('LON-CAPA bug tracker'),600,500));
 2739:     %defaultchecked = ('submitbugs' => 'on');
 2740:     @toggles = ('submitbugs',);
 2741: 
 2742:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2743:                                                  \%choices,$itemcount);
 2744:     return $datatable;
 2745: }
 2746: 
 2747: sub radiobutton_prefs {
 2748:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2749:         $additional) = @_;
 2750:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2751:                    (ref($choices) eq 'HASH'));
 2752: 
 2753:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2754: 
 2755:     foreach my $item (@{$toggles}) {
 2756:         if ($defaultchecked->{$item} eq 'on') {
 2757:             $checkedon{$item} = ' checked="checked" ';
 2758:             $checkedoff{$item} = ' ';
 2759:         } elsif ($defaultchecked->{$item} eq 'off') {
 2760:             $checkedoff{$item} = ' checked="checked" ';
 2761:             $checkedon{$item} = ' ';
 2762:         }
 2763:     }
 2764:     if (ref($settings) eq 'HASH') {
 2765:         foreach my $item (@{$toggles}) {
 2766:             if ($settings->{$item} eq '1') {
 2767:                 $checkedon{$item} =  ' checked="checked" ';
 2768:                 $checkedoff{$item} = ' ';
 2769:             } elsif ($settings->{$item} eq '0') {
 2770:                 $checkedoff{$item} =  ' checked="checked" ';
 2771:                 $checkedon{$item} = ' ';
 2772:             }
 2773:         }
 2774:     }
 2775:     if ($onclick) {
 2776:         $onclick = ' onclick="'.$onclick.'"';
 2777:     }
 2778:     foreach my $item (@{$toggles}) {
 2779:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2780:         $datatable .=
 2781:             '<tr'.$css_class.'><td valign="top">'.
 2782:             '<span class="LC_nobreak">'.$choices->{$item}.
 2783:             '</span></td>'.
 2784:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2785:             '<label><input type="radio" name="'.
 2786:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2787:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2788:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2789:             '</span>'.$additional.
 2790:             '</td>'.
 2791:             '</tr>';
 2792:         $itemcount ++;
 2793:     }
 2794:     return ($datatable,$itemcount);
 2795: }
 2796: 
 2797: sub print_coursedefaults {
 2798:     my ($position,$dom,$settings,$rowtotal) = @_;
 2799:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2800:     my $itemcount = 1;
 2801:     my %choices =  &Apache::lonlocal::texthash (
 2802:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2803:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2804:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2805:         coursecredits        => 'Credits can be specified for courses',
 2806:     );
 2807:     my %staticdefaults = (
 2808:                            anonsurvey_threshold => 10,
 2809:                            uploadquota          => 500,
 2810:                          );
 2811:     if ($position eq 'top') {
 2812:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2813:         @toggles = ('canuse_pdfforms');
 2814:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2815:                                                  \%choices,$itemcount);
 2816:     } else {
 2817:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2818:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2819:             %curruploadquota);
 2820:         my $currusecredits = 0;
 2821:         my @types = ('official','unofficial','community','textbook');
 2822:         if (ref($settings) eq 'HASH') {
 2823:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2824:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2825:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2826:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2827:                 }
 2828:             }
 2829:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2830:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2831:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2832:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2833:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2834:                     ($def_textbook_credits ne '')) {
 2835:                     $currusecredits = 1;
 2836:                 }
 2837:             }
 2838:         }
 2839:         if (!$currdefresponder) {
 2840:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2841:         } elsif ($currdefresponder < 1) {
 2842:             $currdefresponder = 1;
 2843:         }
 2844:         foreach my $type (@types) {
 2845:             if ($curruploadquota{$type} eq '') {
 2846:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2847:             }
 2848:         }
 2849:         $datatable .=
 2850:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2851:                 $choices{'anonsurvey_threshold'}.
 2852:                 '</span></td>'.
 2853:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2854:                 '<input type="text" name="anonsurvey_threshold"'.
 2855:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2856:                 '</td></tr>'."\n";
 2857:         $itemcount ++;
 2858:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2859:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2860:                       $choices{'uploadquota'}.
 2861:                       '</span></td>'.
 2862:                       '<td align="right" class="LC_right_item">'.
 2863:                       '<table><tr>';
 2864:         foreach my $type (@types) {
 2865:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2866:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2867:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2868:         }
 2869:         $datatable .= '</tr></table></td></tr>'."\n";
 2870:         $itemcount ++;
 2871:         my $onclick = "toggleDisplay(this.form,'credits');";
 2872:         my $display = 'none';
 2873:         if ($currusecredits) {
 2874:             $display = 'block';
 2875:         }
 2876:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2877:                          '<span class="LC_nobreak">'.
 2878:                          &mt('Default credits for official courses [_1]',
 2879:                          '<input type="text" name="official_credits" value="'.
 2880:                          $def_official_credits.'" size="3" />').
 2881:                          '</span><br />'.
 2882:                          '<span class="LC_nobreak">'.
 2883:                          &mt('Default credits for unofficial courses [_1]',
 2884:                          '<input type="text" name="unofficial_credits" value="'.
 2885:                          $def_unofficial_credits.'" size="3" />').
 2886:                          '</span><br />'.
 2887:                          '<span class="LC_nobreak">'.
 2888:                          &mt('Default credits for textbook courses [_1]',
 2889:                          '<input type="text" name="textbook_credits" value="'.
 2890:                          $def_textbook_credits.'" size="3" />').
 2891:                          '</span></div>'."\n";
 2892:         %defaultchecked = ('coursecredits' => 'off');
 2893:         @toggles = ('coursecredits');
 2894:         my $current = {
 2895:                         'coursecredits' => $currusecredits,
 2896:                       };
 2897:         (my $table,$itemcount) =
 2898:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2899:                                \%choices,$itemcount,$onclick,$additional);
 2900:         $datatable .= $table;
 2901:         $itemcount ++;
 2902:     }
 2903:     $$rowtotal += $itemcount;
 2904:     return $datatable;
 2905: }
 2906: 
 2907: sub print_selfenrollment {
 2908:     my ($position,$dom,$settings,$rowtotal) = @_;
 2909:     my ($css_class,$datatable);
 2910:     my $itemcount = 1;
 2911:     my @types = ('official','unofficial','community','textbook');
 2912:     if (($position eq 'top') || ($position eq 'middle')) {
 2913:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 2914:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 2915:         my @rows;
 2916:         my $key;
 2917:         if ($position eq 'top') {
 2918:             $key = 'admin'; 
 2919:             if (ref($rowsref) eq 'ARRAY') {
 2920:                 @rows = @{$rowsref};
 2921:             }
 2922:         } elsif ($position eq 'middle') {
 2923:             $key = 'default';
 2924:             @rows = ('types','registered','approval','limit');
 2925:         }
 2926:         foreach my $row (@rows) {
 2927:             if (defined($titlesref->{$row})) {
 2928:                 $itemcount ++;
 2929:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2930:                 $datatable .= '<tr'.$css_class.'>'.
 2931:                               '<td>'.$titlesref->{$row}.'</td>'.
 2932:                               '<td class="LC_left_item">'.
 2933:                               '<table><tr>';
 2934:                 my (%current,%currentcap);
 2935:                 if (ref($settings) eq 'HASH') {
 2936:                     if (ref($settings->{$key}) eq 'HASH') {
 2937:                         foreach my $type (@types) {
 2938:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2939:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 2940:                             }
 2941:                             if (($row eq 'limit') && ($key eq 'default')) {
 2942:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2943:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 2944:                                 }
 2945:                             }
 2946:                         }
 2947:                     }
 2948:                 }
 2949:                 my %roles = (
 2950:                              '0' => &Apache::lonnet::plaintext('dc'),
 2951:                             ); 
 2952:             
 2953:                 foreach my $type (@types) {
 2954:                     unless (($row eq 'registered') && ($key eq 'default')) {
 2955:                         $datatable .= '<th>'.&mt($type).'</th>';
 2956:                     }
 2957:                 }
 2958:                 unless (($row eq 'registered') && ($key eq 'default')) {
 2959:                     $datatable .= '</tr><tr>';
 2960:                 }
 2961:                 foreach my $type (@types) {
 2962:                     if ($type eq 'community') {
 2963:                         $roles{'1'} = &mt('Community personnel');
 2964:                     } else {
 2965:                         $roles{'1'} = &mt('Course personnel');
 2966:                     }
 2967:                     $datatable .= '<td style="vertical-align: top">';
 2968:                     if ($position eq 'top') {
 2969:                         my %checked;
 2970:                         if ($current{$type} eq '0') {
 2971:                             $checked{'0'} = ' checked="checked"';
 2972:                         } else {
 2973:                             $checked{'1'} = ' checked="checked"';
 2974:                         }
 2975:                         foreach my $role ('1','0') {
 2976:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2977:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 2978:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 2979:                                           $roles{$role}.'</label></span> ';
 2980:                         }
 2981:                     } else {
 2982:                         if ($row eq 'types') {
 2983:                             my %checked;
 2984:                             if ($current{$type} =~ /^(all|dom)$/) {
 2985:                                 $checked{$1} = ' checked="checked"';
 2986:                             } else {
 2987:                                 $checked{''} = ' checked="checked"';
 2988:                             }
 2989:                             foreach my $val ('','dom','all') {
 2990:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2991:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2992:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2993:                             }
 2994:                         } elsif ($row eq 'registered') {
 2995:                             my %checked;
 2996:                             if ($current{$type} eq '1') {
 2997:                                 $checked{'1'} = ' checked="checked"';
 2998:                             } else {
 2999:                                 $checked{'0'} = ' checked="checked"';
 3000:                             }
 3001:                             foreach my $val ('0','1') {
 3002:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3003:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3004:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3005:                             }
 3006:                         } elsif ($row eq 'approval') {
 3007:                             my %checked;
 3008:                             if ($current{$type} =~ /^([12])$/) {
 3009:                                 $checked{$1} = ' checked="checked"';
 3010:                             } else {
 3011:                                 $checked{'0'} = ' checked="checked"';
 3012:                             }
 3013:                             for my $val (0..2) {
 3014:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3015:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3016:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3017:                             }
 3018:                         } elsif ($row eq 'limit') {
 3019:                             my %checked;
 3020:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3021:                                 $checked{$1} = ' checked="checked"';
 3022:                             } else {
 3023:                                 $checked{'none'} = ' checked="checked"';
 3024:                             }
 3025:                             my $cap;
 3026:                             if ($currentcap{$type} =~ /^\d+$/) {
 3027:                                 $cap = $currentcap{$type};
 3028:                             }
 3029:                             foreach my $val ('none','allstudents','selfenrolled') {
 3030:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3031:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3032:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3033:                             }
 3034:                             $datatable .= '<br />'.
 3035:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3036:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3037:                                           '</span>'; 
 3038:                         }
 3039:                     }
 3040:                     $datatable .= '</td>';
 3041:                 }
 3042:                 $datatable .= '</tr>';
 3043:             }
 3044:             $datatable .= '</table></td></tr>';
 3045:         }
 3046:     } elsif ($position eq 'bottom') {
 3047:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3048:     }
 3049:     $$rowtotal += $itemcount;
 3050:     return $datatable;
 3051: }
 3052: 
 3053: sub print_validation_rows {
 3054:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3055:     my ($itemsref,$namesref,$fieldsref);
 3056:     if ($caller eq 'selfenroll') { 
 3057:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3058:     } elsif ($caller eq 'requestcourses') {
 3059:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3060:     }
 3061:     my %currvalidation;
 3062:     if (ref($settings) eq 'HASH') {
 3063:         if (ref($settings->{'validation'}) eq 'HASH') {
 3064:             %currvalidation = %{$settings->{'validation'}};
 3065:         }
 3066:     }
 3067:     my $datatable;
 3068:     my $itemcount = 0;
 3069:     foreach my $item (@{$itemsref}) {
 3070:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3071:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3072:                       $namesref->{$item}.
 3073:                       '</span></td>'.
 3074:                       '<td class="LC_left_item">';
 3075:         if (($item eq 'url') || ($item eq 'button')) {
 3076:             $datatable .= '<span class="LC_nobreak">'.
 3077:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3078:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3079:         } elsif ($item eq 'fields') {
 3080:             my @currfields;
 3081:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3082:                 @currfields = @{$currvalidation{$item}};
 3083:             }
 3084:             foreach my $field (@{$fieldsref}) {
 3085:                 my $check = '';
 3086:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3087:                     $check = ' checked="checked"';
 3088:                 }
 3089:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3090:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3091:                               ' value="'.$field.'"'.$check.' />'.$field.
 3092:                               '</label></span> ';
 3093:             }
 3094:         } elsif ($item eq 'markup') {
 3095:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3096:                            $currvalidation{$item}.
 3097:                               '</textarea>';
 3098:         }
 3099:         $datatable .= '</td></tr>'."\n";
 3100:         if (ref($rowtotal)) {
 3101:             $itemcount ++;
 3102:         }
 3103:     }
 3104:     if ($caller eq 'requestcourses') {
 3105:         my %currhash;
 3106:         if (ref($settings) eq 'HASH') {
 3107:             if (ref($settings->{'validation'}) eq 'HASH') {
 3108:                 if ($settings->{'validation'}{'dc'} ne '') {
 3109:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 3110:                 }
 3111:             }
 3112:         }
 3113:         my $numinrow = 2;
 3114:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3115:                                                        'validationdc',%currhash);
 3116:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3117:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 3118:         if ($numdc > 1) {
 3119:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 3120:         } else {
 3121:             $datatable .=  &mt('Course creation processed as: ');
 3122:         }
 3123:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3124:         $itemcount ++;
 3125:     }
 3126:     if (ref($rowtotal)) {
 3127:         $$rowtotal += $itemcount;
 3128:     }
 3129:     return $datatable;
 3130: }
 3131: 
 3132: sub print_usersessions {
 3133:     my ($position,$dom,$settings,$rowtotal) = @_;
 3134:     my ($css_class,$datatable,%checked,%choices);
 3135:     my (%by_ip,%by_location,@intdoms);
 3136:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3137: 
 3138:     my @alldoms = &Apache::lonnet::all_domains();
 3139:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3140:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3141:     my %altids = &id_for_thisdom(%servers);
 3142:     my $itemcount = 1;
 3143:     if ($position eq 'top') {
 3144:         if (keys(%serverhomes) > 1) {
 3145:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3146:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 3147:         } else {
 3148:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3149:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3150:         }
 3151:     } else {
 3152:         if (keys(%by_location) == 0) {
 3153:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3154:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3155:         } else {
 3156:             my %lt = &usersession_titles();
 3157:             my $numinrow = 5;
 3158:             my $prefix;
 3159:             my @types;
 3160:             if ($position eq 'bottom') {
 3161:                 $prefix = 'remote';
 3162:                 @types = ('version','excludedomain','includedomain');
 3163:             } else {
 3164:                 $prefix = 'hosted';
 3165:                 @types = ('excludedomain','includedomain');
 3166:             }
 3167:             my (%current,%checkedon,%checkedoff);
 3168:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3169:             my @locations = sort(keys(%by_location));
 3170:             foreach my $type (@types) {
 3171:                 $checkedon{$type} = '';
 3172:                 $checkedoff{$type} = ' checked="checked"';
 3173:             }
 3174:             if (ref($settings) eq 'HASH') {
 3175:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3176:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3177:                         $current{$key} = $settings->{$prefix}{$key};
 3178:                         if ($key eq 'version') {
 3179:                             if ($current{$key} ne '') {
 3180:                                 $checkedon{$key} = ' checked="checked"';
 3181:                                 $checkedoff{$key} = '';
 3182:                             }
 3183:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3184:                             $checkedon{$key} = ' checked="checked"';
 3185:                             $checkedoff{$key} = '';
 3186:                         }
 3187:                     }
 3188:                 }
 3189:             }
 3190:             foreach my $type (@types) {
 3191:                 next if ($type ne 'version' && !@locations);
 3192:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3193:                 $datatable .= '<tr'.$css_class.'>
 3194:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3195:                                <span class="LC_nobreak">&nbsp;
 3196:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3197:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3198:                 if ($type eq 'version') {
 3199:                     my $selector = '<select name="'.$prefix.'_version">';
 3200:                     foreach my $version (@lcversions) {
 3201:                         my $selected = '';
 3202:                         if ($current{'version'} eq $version) {
 3203:                             $selected = ' selected="selected"';
 3204:                         }
 3205:                         $selector .= ' <option value="'.$version.'"'.
 3206:                                      $selected.'>'.$version.'</option>';
 3207:                     }
 3208:                     $selector .= '</select> ';
 3209:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3210:                 } else {
 3211:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3212:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3213:                                  ' />'.('&nbsp;'x2).
 3214:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3215:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3216:                                  "\n".
 3217:                                  '</div><div><table>';
 3218:                     my $rem;
 3219:                     for (my $i=0; $i<@locations; $i++) {
 3220:                         my ($showloc,$value,$checkedtype);
 3221:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3222:                             my $ip = $by_location{$locations[$i]}->[0];
 3223:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3224:                                  $value = join(':',@{$by_ip{$ip}});
 3225:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3226:                                 if (ref($current{$type}) eq 'ARRAY') {
 3227:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3228:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3229:                                             $checkedtype = ' checked="checked"';
 3230:                                             last;
 3231:                                         }
 3232:                                     }
 3233:                                 }
 3234:                             }
 3235:                         }
 3236:                         $rem = $i%($numinrow);
 3237:                         if ($rem == 0) {
 3238:                             if ($i > 0) {
 3239:                                 $datatable .= '</tr>';
 3240:                             }
 3241:                             $datatable .= '<tr>';
 3242:                         }
 3243:                         $datatable .= '<td class="LC_left_item">'.
 3244:                                       '<span class="LC_nobreak"><label>'.
 3245:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3246:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3247:                                       '</label></span></td>';
 3248:                     }
 3249:                     $rem = @locations%($numinrow);
 3250:                     my $colsleft = $numinrow - $rem;
 3251:                     if ($colsleft > 1 ) {
 3252:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3253:                                       '&nbsp;</td>';
 3254:                     } elsif ($colsleft == 1) {
 3255:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3256:                     }
 3257:                     $datatable .= '</tr></table>';
 3258:                 }
 3259:                 $datatable .= '</td></tr>';
 3260:                 $itemcount ++;
 3261:             }
 3262:         }
 3263:     }
 3264:     $$rowtotal += $itemcount;
 3265:     return $datatable;
 3266: }
 3267: 
 3268: sub build_location_hashes {
 3269:     my ($intdoms,$by_ip,$by_location) = @_;
 3270:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3271:                   (ref($by_location) eq 'HASH')); 
 3272:     my %iphost = &Apache::lonnet::get_iphost();
 3273:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3274:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3275:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3276:         foreach my $id (@{$iphost{$primary_ip}}) {
 3277:             my $intdom = &Apache::lonnet::internet_dom($id);
 3278:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3279:                 push(@{$intdoms},$intdom);
 3280:             }
 3281:         }
 3282:     }
 3283:     foreach my $ip (keys(%iphost)) {
 3284:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3285:             foreach my $id (@{$iphost{$ip}}) {
 3286:                 my $location = &Apache::lonnet::internet_dom($id);
 3287:                 if ($location) {
 3288:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3289:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3290:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3291:                             push(@{$by_ip->{$ip}},$location);
 3292:                         }
 3293:                     } else {
 3294:                         $by_ip->{$ip} = [$location];
 3295:                     }
 3296:                 }
 3297:             }
 3298:         }
 3299:     }
 3300:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3301:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3302:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3303:             my $first = $by_ip->{$ip}->[0];
 3304:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3305:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3306:                     push(@{$by_location->{$first}},$ip);
 3307:                 }
 3308:             } else {
 3309:                 $by_location->{$first} = [$ip];
 3310:             }
 3311:         }
 3312:     }
 3313:     return;
 3314: }
 3315: 
 3316: sub current_offloads_to {
 3317:     my ($dom,$settings,$servers) = @_;
 3318:     my (%spareid,%otherdomconfigs);
 3319:     if (ref($servers) eq 'HASH') {
 3320:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3321:             my $gotspares;
 3322:             if (ref($settings) eq 'HASH') {
 3323:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3324:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3325:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3326:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3327:                         $gotspares = 1;
 3328:                     }
 3329:                 }
 3330:             }
 3331:             unless ($gotspares) {
 3332:                 my $gotspares;
 3333:                 my $serverhomeID =
 3334:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3335:                 my $serverhomedom =
 3336:                     &Apache::lonnet::host_domain($serverhomeID);
 3337:                 if ($serverhomedom ne $dom) {
 3338:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3339:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3340:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3341:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3342:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3343:                                 $gotspares = 1;
 3344:                             }
 3345:                         }
 3346:                     } else {
 3347:                         $otherdomconfigs{$serverhomedom} =
 3348:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3349:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3350:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3351:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3352:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3353:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3354:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3355:                                         $gotspares = 1;
 3356:                                     }
 3357:                                 }
 3358:                             }
 3359:                         }
 3360:                     }
 3361:                 }
 3362:             }
 3363:             unless ($gotspares) {
 3364:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3365:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3366:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3367:                } else {
 3368:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3369:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3370:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3371:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3372:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3373:                     } else {
 3374:                         my %what = (
 3375:                              spareid => 1,
 3376:                         );
 3377:                         my ($result,$returnhash) = 
 3378:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3379:                         if ($result eq 'ok') { 
 3380:                             if (ref($returnhash) eq 'HASH') {
 3381:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3382:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3383:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3384:                                 }
 3385:                             }
 3386:                         }
 3387:                     }
 3388:                 }
 3389:             }
 3390:         }
 3391:     }
 3392:     return %spareid;
 3393: }
 3394: 
 3395: sub spares_row {
 3396:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3397:     my $css_class;
 3398:     my $numinrow = 4;
 3399:     my $itemcount = 1;
 3400:     my $datatable;
 3401:     my %typetitles = &sparestype_titles();
 3402:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3403:         foreach my $server (sort(keys(%{$servers}))) {
 3404:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3405:             my ($othercontrol,$serverdom);
 3406:             if ($serverhome ne $server) {
 3407:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3408:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3409:             } else {
 3410:                 $serverdom = &Apache::lonnet::host_domain($server);
 3411:                 if ($serverdom ne $dom) {
 3412:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3413:                 }
 3414:             }
 3415:             next unless (ref($spareid->{$server}) eq 'HASH');
 3416:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3417:             $datatable .= '<tr'.$css_class.'>
 3418:                            <td rowspan="2">
 3419:                             <span class="LC_nobreak">'.
 3420:                           &mt('[_1] when busy, offloads to:'
 3421:                               ,'<b>'.$server.'</b>').
 3422:                           "\n";
 3423:             my (%current,%canselect);
 3424:             my @choices = 
 3425:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3426:             foreach my $type ('primary','default') {
 3427:                 if (ref($spareid->{$server}) eq 'HASH') {
 3428:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3429:                         my @spares = @{$spareid->{$server}{$type}};
 3430:                         if (@spares > 0) {
 3431:                             if ($othercontrol) {
 3432:                                 $current{$type} = join(', ',@spares);
 3433:                             } else {
 3434:                                 $current{$type} .= '<table>';
 3435:                                 my $numspares = scalar(@spares);
 3436:                                 for (my $i=0;  $i<@spares; $i++) {
 3437:                                     my $rem = $i%($numinrow);
 3438:                                     if ($rem == 0) {
 3439:                                         if ($i > 0) {
 3440:                                             $current{$type} .= '</tr>';
 3441:                                         }
 3442:                                         $current{$type} .= '<tr>';
 3443:                                     }
 3444:                                     $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;'.
 3445:                                                        $spareid->{$server}{$type}[$i].
 3446:                                                        '</label></td>'."\n";
 3447:                                 }
 3448:                                 my $rem = @spares%($numinrow);
 3449:                                 my $colsleft = $numinrow - $rem;
 3450:                                 if ($colsleft > 1 ) {
 3451:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3452:                                                        '" class="LC_left_item">'.
 3453:                                                        '&nbsp;</td>';
 3454:                                 } elsif ($colsleft == 1) {
 3455:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3456:                                 }
 3457:                                 $current{$type} .= '</tr></table>';
 3458:                             }
 3459:                         }
 3460:                     }
 3461:                     if ($current{$type} eq '') {
 3462:                         $current{$type} = &mt('None specified');
 3463:                     }
 3464:                     if ($othercontrol) {
 3465:                         if ($type eq 'primary') {
 3466:                             $canselect{$type} = $othercontrol;
 3467:                         }
 3468:                     } else {
 3469:                         $canselect{$type} = 
 3470:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3471:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3472:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3473:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3474:                         if (@choices > 0) {
 3475:                             foreach my $lonhost (@choices) {
 3476:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3477:                             }
 3478:                         }
 3479:                         $canselect{$type} .= '</select>'."\n";
 3480:                     }
 3481:                 } else {
 3482:                     $current{$type} = &mt('Could not be determined');
 3483:                     if ($type eq 'primary') {
 3484:                         $canselect{$type} =  $othercontrol;
 3485:                     }
 3486:                 }
 3487:                 if ($type eq 'default') {
 3488:                     $datatable .= '<tr'.$css_class.'>';
 3489:                 }
 3490:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3491:                               '<td>'.$current{$type}.'</td>'."\n".
 3492:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3493:             }
 3494:             $itemcount ++;
 3495:         }
 3496:     }
 3497:     $$rowtotal += $itemcount;
 3498:     return $datatable;
 3499: }
 3500: 
 3501: sub possible_newspares {
 3502:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3503:     my $serverhostname = &Apache::lonnet::hostname($server);
 3504:     my %excluded;
 3505:     if ($serverhostname ne '') {
 3506:         %excluded = (
 3507:                        $serverhostname => 1,
 3508:                     );
 3509:     }
 3510:     if (ref($currspares) eq 'HASH') {
 3511:         foreach my $type (keys(%{$currspares})) {
 3512:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3513:                 if (@{$currspares->{$type}} > 0) {
 3514:                     foreach my $curr (@{$currspares->{$type}}) {
 3515:                         my $hostname = &Apache::lonnet::hostname($curr);
 3516:                         $excluded{$hostname} = 1;
 3517:                     }
 3518:                 }
 3519:             }
 3520:         }
 3521:     }
 3522:     my @choices;
 3523:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3524:         if (keys(%{$serverhomes}) > 1) {
 3525:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3526:                 unless ($excluded{$name}) {
 3527:                     if (exists($altids->{$serverhomes->{$name}})) {
 3528:                         push(@choices,$altids->{$serverhomes->{$name}});
 3529:                     } else {
 3530:                         push(@choices,$serverhomes->{$name});
 3531:                     }
 3532:                 }
 3533:             }
 3534:         }
 3535:     }
 3536:     return sort(@choices);
 3537: }
 3538: 
 3539: sub print_loadbalancing {
 3540:     my ($dom,$settings,$rowtotal) = @_;
 3541:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3542:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3543:     my $numinrow = 1;
 3544:     my $datatable;
 3545:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3546:     my (%currbalancer,%currtargets,%currrules,%existing);
 3547:     if (ref($settings) eq 'HASH') {
 3548:         %existing = %{$settings};
 3549:     }
 3550:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3551:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3552:                                   \%currtargets,\%currrules);
 3553:     } else {
 3554:         return;
 3555:     }
 3556:     my ($othertitle,$usertypes,$types) =
 3557:         &Apache::loncommon::sorted_inst_types($dom);
 3558:     my $rownum = 8;
 3559:     if (ref($types) eq 'ARRAY') {
 3560:         $rownum += scalar(@{$types});
 3561:     }
 3562:     my @css_class = ('LC_odd_row','LC_even_row');
 3563:     my $balnum = 0;
 3564:     my $islast;
 3565:     my (@toshow,$disabledtext);
 3566:     if (keys(%currbalancer) > 0) {
 3567:         @toshow = sort(keys(%currbalancer));
 3568:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3569:             push(@toshow,'');
 3570:         }
 3571:     } else {
 3572:         @toshow = ('');
 3573:         $disabledtext = &mt('No existing load balancer');
 3574:     }
 3575:     foreach my $lonhost (@toshow) {
 3576:         if ($balnum == scalar(@toshow)-1) {
 3577:             $islast = 1;
 3578:         } else {
 3579:             $islast = 0;
 3580:         }
 3581:         my $cssidx = $balnum%2;
 3582:         my $targets_div_style = 'display: none';
 3583:         my $disabled_div_style = 'display: block';
 3584:         my $homedom_div_style = 'display: none';
 3585:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3586:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3587:                       '<p>';
 3588:         if ($lonhost eq '') {
 3589:             $datatable .= '<span class="LC_nobreak">';
 3590:             if (keys(%currbalancer) > 0) {
 3591:                 $datatable .= &mt('Add balancer:');
 3592:             } else {
 3593:                 $datatable .= &mt('Enable balancer:');
 3594:             }
 3595:             $datatable .= '&nbsp;'.
 3596:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3597:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3598:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3599:                           '<option value="" selected="selected">'.&mt('None').
 3600:                           '</option>'."\n";
 3601:             foreach my $server (sort(keys(%servers))) {
 3602:                 next if ($currbalancer{$server});
 3603:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3604:             }
 3605:             $datatable .=
 3606:                 '</select>'."\n".
 3607:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3608:         } else {
 3609:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3610:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3611:                            &mt('Stop balancing').'</label>'.
 3612:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3613:             $targets_div_style = 'display: block';
 3614:             $disabled_div_style = 'display: none';
 3615:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3616:                 $homedom_div_style = 'display: block';
 3617:             }
 3618:         }
 3619:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3620:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3621:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3622:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3623:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3624:         my @sparestypes = ('primary','default');
 3625:         my %typetitles = &sparestype_titles();
 3626:         foreach my $sparetype (@sparestypes) {
 3627:             my $targettable;
 3628:             for (my $i=0; $i<$numspares; $i++) {
 3629:                 my $checked;
 3630:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3631:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3632:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3633:                             $checked = ' checked="checked"';
 3634:                         }
 3635:                     }
 3636:                 }
 3637:                 my ($chkboxval,$disabled);
 3638:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3639:                     $chkboxval = $spares[$i];
 3640:                 }
 3641:                 if (exists($currbalancer{$spares[$i]})) {
 3642:                     $disabled = ' disabled="disabled"';
 3643:                 }
 3644:                 $targettable .=
 3645:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3646:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3647:                     '</span></label></td>';
 3648:                 my $rem = $i%($numinrow);
 3649:                 if ($rem == 0) {
 3650:                     if (($i > 0) && ($i < $numspares-1)) {
 3651:                         $targettable .= '</tr>';
 3652:                     }
 3653:                     if ($i < $numspares-1) {
 3654:                         $targettable .= '<tr>';
 3655:                     }
 3656:                 }
 3657:             }
 3658:             if ($targettable ne '') {
 3659:                 my $rem = $numspares%($numinrow);
 3660:                 my $colsleft = $numinrow - $rem;
 3661:                 if ($colsleft > 1 ) {
 3662:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3663:                                     '&nbsp;</td>';
 3664:                 } elsif ($colsleft == 1) {
 3665:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3666:                 }
 3667:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3668:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3669:             }
 3670:         }
 3671:         $datatable .= '</div></td></tr>'.
 3672:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3673:                                            $othertitle,$usertypes,$types,\%servers,
 3674:                                            \%currbalancer,$lonhost,
 3675:                                            $targets_div_style,$homedom_div_style,
 3676:                                            $css_class[$cssidx],$balnum,$islast);
 3677:         $$rowtotal += $rownum;
 3678:         $balnum ++;
 3679:     }
 3680:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3681:     return $datatable;
 3682: }
 3683: 
 3684: sub get_loadbalancers_config {
 3685:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3686:     return unless ((ref($servers) eq 'HASH') &&
 3687:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3688:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3689:     if (keys(%{$existing}) > 0) {
 3690:         my $oldlonhost;
 3691:         foreach my $key (sort(keys(%{$existing}))) {
 3692:             if ($key eq 'lonhost') {
 3693:                 $oldlonhost = $existing->{'lonhost'};
 3694:                 $currbalancer->{$oldlonhost} = 1;
 3695:             } elsif ($key eq 'targets') {
 3696:                 if ($oldlonhost) {
 3697:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3698:                 }
 3699:             } elsif ($key eq 'rules') {
 3700:                 if ($oldlonhost) {
 3701:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3702:                 }
 3703:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3704:                 $currbalancer->{$key} = 1;
 3705:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3706:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3707:             }
 3708:         }
 3709:     } else {
 3710:         my ($balancerref,$targetsref) =
 3711:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3712:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3713:             foreach my $server (sort(keys(%{$balancerref}))) {
 3714:                 $currbalancer->{$server} = 1;
 3715:                 $currtargets->{$server} = $targetsref->{$server};
 3716:             }
 3717:         }
 3718:     }
 3719:     return;
 3720: }
 3721: 
 3722: sub loadbalancing_rules {
 3723:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3724:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3725:         $css_class,$balnum,$islast) = @_;
 3726:     my $output;
 3727:     my $num = 0;
 3728:     my ($alltypes,$othertypes,$titles) =
 3729:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3730:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3731:         foreach my $type (@{$alltypes}) {
 3732:             $num ++;
 3733:             my $current;
 3734:             if (ref($currrules) eq 'HASH') {
 3735:                 $current = $currrules->{$type};
 3736:             }
 3737:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3738:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3739:                     $current = '';
 3740:                 }
 3741:             }
 3742:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3743:                                              $servers,$currbalancer,$lonhost,$dom,
 3744:                                              $targets_div_style,$homedom_div_style,
 3745:                                              $css_class,$balnum,$num,$islast);
 3746:         }
 3747:     }
 3748:     return $output;
 3749: }
 3750: 
 3751: sub loadbalancing_titles {
 3752:     my ($dom,$intdom,$usertypes,$types) = @_;
 3753:     my %othertypes = (
 3754:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3755:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3756:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3757:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3758:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3759:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3760:                      );
 3761:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3762:     if (ref($types) eq 'ARRAY') {
 3763:         unshift(@alltypes,@{$types},'default');
 3764:     }
 3765:     my %titles;
 3766:     foreach my $type (@alltypes) {
 3767:         if ($type =~ /^_LC_/) {
 3768:             $titles{$type} = $othertypes{$type};
 3769:         } elsif ($type eq 'default') {
 3770:             $titles{$type} = &mt('All users from [_1]',$dom);
 3771:             if (ref($types) eq 'ARRAY') {
 3772:                 if (@{$types} > 0) {
 3773:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3774:                 }
 3775:             }
 3776:         } elsif (ref($usertypes) eq 'HASH') {
 3777:             $titles{$type} = $usertypes->{$type};
 3778:         }
 3779:     }
 3780:     return (\@alltypes,\%othertypes,\%titles);
 3781: }
 3782: 
 3783: sub loadbalance_rule_row {
 3784:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3785:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3786:     my @rulenames;
 3787:     my %ruletitles = &offloadtype_text();
 3788:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3789:         @rulenames = ('balancer','offloadedto');
 3790:     } else {
 3791:         @rulenames = ('default','homeserver');
 3792:         if ($type eq '_LC_external') {
 3793:             push(@rulenames,'externalbalancer');
 3794:         } else {
 3795:             push(@rulenames,'specific');
 3796:         }
 3797:         push(@rulenames,'none');
 3798:     }
 3799:     my $style = $targets_div_style;
 3800:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3801:         $style = $homedom_div_style;
 3802:     }
 3803:     my $space;
 3804:     if ($islast && $num == 1) {
 3805:         $space = '<div display="inline-block">&nbsp;</div>';
 3806:     }
 3807:     my $output =
 3808:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3809:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3810:         '<td valaign="top">'.$space.
 3811:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3812:     for (my $i=0; $i<@rulenames; $i++) {
 3813:         my $rule = $rulenames[$i];
 3814:         my ($checked,$extra);
 3815:         if ($rulenames[$i] eq 'default') {
 3816:             $rule = '';
 3817:         }
 3818:         if ($rulenames[$i] eq 'specific') {
 3819:             if (ref($servers) eq 'HASH') {
 3820:                 my $default;
 3821:                 if (($current ne '') && (exists($servers->{$current}))) {
 3822:                     $checked = ' checked="checked"';
 3823:                 }
 3824:                 unless ($checked) {
 3825:                     $default = ' selected="selected"';
 3826:                 }
 3827:                 $extra =
 3828:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3829:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3830:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3831:                     '<option value=""'.$default.'></option>'."\n";
 3832:                 foreach my $server (sort(keys(%{$servers}))) {
 3833:                     if (ref($currbalancer) eq 'HASH') {
 3834:                         next if (exists($currbalancer->{$server}));
 3835:                     }
 3836:                     my $selected;
 3837:                     if ($server eq $current) {
 3838:                         $selected = ' selected="selected"';
 3839:                     }
 3840:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3841:                 }
 3842:                 $extra .= '</select>';
 3843:             }
 3844:         } elsif ($rule eq $current) {
 3845:             $checked = ' checked="checked"';
 3846:         }
 3847:         $output .= '<span class="LC_nobreak"><label>'.
 3848:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3849:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3850:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3851:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3852:                    '</label>'.$extra.'</span><br />'."\n";
 3853:     }
 3854:     $output .= '</div></td></tr>'."\n";
 3855:     return $output;
 3856: }
 3857: 
 3858: sub offloadtype_text {
 3859:     my %ruletitles = &Apache::lonlocal::texthash (
 3860:            'default'          => 'Offloads to default destinations',
 3861:            'homeserver'       => "Offloads to user's home server",
 3862:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3863:            'specific'         => 'Offloads to specific server',
 3864:            'none'             => 'No offload',
 3865:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3866:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3867:     );
 3868:     return %ruletitles;
 3869: }
 3870: 
 3871: sub sparestype_titles {
 3872:     my %typestitles = &Apache::lonlocal::texthash (
 3873:                           'primary' => 'primary',
 3874:                           'default' => 'default',
 3875:                       );
 3876:     return %typestitles;
 3877: }
 3878: 
 3879: sub contact_titles {
 3880:     my %titles = &Apache::lonlocal::texthash (
 3881:                    'supportemail' => 'Support E-mail address',
 3882:                    'adminemail'   => 'Default Server Admin E-mail address',
 3883:                    'errormail'    => 'Error reports to be e-mailed to',
 3884:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3885:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3886:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3887:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3888:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3889:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3890:                  );
 3891:     my %short_titles = &Apache::lonlocal::texthash (
 3892:                            adminemail   => 'Admin E-mail address',
 3893:                            supportemail => 'Support E-mail',
 3894:                        );   
 3895:     return (\%titles,\%short_titles);
 3896: }
 3897: 
 3898: sub tool_titles {
 3899:     my %titles = &Apache::lonlocal::texthash (
 3900:                      aboutme    => 'Personal web page',
 3901:                      blog       => 'Blog',
 3902:                      webdav     => 'WebDAV',
 3903:                      portfolio  => 'Portfolio',
 3904:                      official   => 'Official courses (with institutional codes)',
 3905:                      unofficial => 'Unofficial courses',
 3906:                      community  => 'Communities',
 3907:                      textbook   => 'Textbook courses',
 3908:                  );
 3909:     return %titles;
 3910: }
 3911: 
 3912: sub courserequest_titles {
 3913:     my %titles = &Apache::lonlocal::texthash (
 3914:                                    official   => 'Official',
 3915:                                    unofficial => 'Unofficial',
 3916:                                    community  => 'Communities',
 3917:                                    textbook   => 'Textbook',
 3918:                                    norequest  => 'Not allowed',
 3919:                                    approval   => 'Approval by Dom. Coord.',
 3920:                                    validate   => 'With validation',
 3921:                                    autolimit  => 'Numerical limit',
 3922:                                    unlimited  => '(blank for unlimited)',
 3923:                  );
 3924:     return %titles;
 3925: }
 3926: 
 3927: sub authorrequest_titles {
 3928:     my %titles = &Apache::lonlocal::texthash (
 3929:                                    norequest  => 'Not allowed',
 3930:                                    approval   => 'Approval by Dom. Coord.',
 3931:                                    automatic  => 'Automatic approval',
 3932:                  );
 3933:     return %titles;
 3934: }
 3935: 
 3936: sub courserequest_conditions {
 3937:     my %conditions = &Apache::lonlocal::texthash (
 3938:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3939:        validate   => '(Processing of request subject to institutional validation).',
 3940:                  );
 3941:     return %conditions;
 3942: }
 3943: 
 3944: 
 3945: sub print_usercreation {
 3946:     my ($position,$dom,$settings,$rowtotal) = @_;
 3947:     my $numinrow = 4;
 3948:     my $datatable;
 3949:     if ($position eq 'top') {
 3950:         $$rowtotal ++;
 3951:         my $rowcount = 0;
 3952:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3953:         if (ref($rules) eq 'HASH') {
 3954:             if (keys(%{$rules}) > 0) {
 3955:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3956:                                                 $ruleorder,$numinrow,$rowcount);
 3957:                 $$rowtotal ++;
 3958:                 $rowcount ++;
 3959:             }
 3960:         }
 3961:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3962:         if (ref($idrules) eq 'HASH') {
 3963:             if (keys(%{$idrules}) > 0) {
 3964:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3965:                                                 $idruleorder,$numinrow,$rowcount);
 3966:                 $$rowtotal ++;
 3967:                 $rowcount ++;
 3968:             }
 3969:         }
 3970:         if ($rowcount == 0) {
 3971:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3972:             $$rowtotal ++;
 3973:             $rowcount ++;
 3974:         }
 3975:     } elsif ($position eq 'middle') {
 3976:         my @creators = ('author','course','requestcrs');
 3977:         my ($rules,$ruleorder) =
 3978:             &Apache::lonnet::inst_userrules($dom,'username');
 3979:         my %lt = &usercreation_types();
 3980:         my %checked;
 3981:         if (ref($settings) eq 'HASH') {
 3982:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3983:                 foreach my $item (@creators) {
 3984:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3985:                 }
 3986:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3987:                 foreach my $item (@creators) {
 3988:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3989:                         $checked{$item} = 'none';
 3990:                     }
 3991:                 }
 3992:             }
 3993:         }
 3994:         my $rownum = 0;
 3995:         foreach my $item (@creators) {
 3996:             $rownum ++;
 3997:             if ($checked{$item} eq '') {
 3998:                 $checked{$item} = 'any';
 3999:             }
 4000:             my $css_class;
 4001:             if ($rownum%2) {
 4002:                 $css_class = '';
 4003:             } else {
 4004:                 $css_class = ' class="LC_odd_row" ';
 4005:             }
 4006:             $datatable .= '<tr'.$css_class.'>'.
 4007:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 4008:                          '</span></td><td align="right">';
 4009:             my @options = ('any');
 4010:             if (ref($rules) eq 'HASH') {
 4011:                 if (keys(%{$rules}) > 0) {
 4012:                     push(@options,('official','unofficial'));
 4013:                 }
 4014:             }
 4015:             push(@options,'none');
 4016:             foreach my $option (@options) {
 4017:                 my $type = 'radio';
 4018:                 my $check = ' ';
 4019:                 if ($checked{$item} eq $option) {
 4020:                     $check = ' checked="checked" ';
 4021:                 } 
 4022:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4023:                               '<input type="'.$type.'" name="can_createuser_'.
 4024:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 4025:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 4026:             }
 4027:             $datatable .= '</td></tr>';
 4028:         }
 4029:     } else {
 4030:         my @contexts = ('author','course','domain');
 4031:         my @authtypes = ('int','krb4','krb5','loc');
 4032:         my %checked;
 4033:         if (ref($settings) eq 'HASH') {
 4034:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 4035:                 foreach my $item (@contexts) {
 4036:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 4037:                         foreach my $auth (@authtypes) {
 4038:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 4039:                                 $checked{$item}{$auth} = ' checked="checked" ';
 4040:                             }
 4041:                         }
 4042:                     }
 4043:                 }
 4044:             }
 4045:         } else {
 4046:             foreach my $item (@contexts) {
 4047:                 foreach my $auth (@authtypes) {
 4048:                     $checked{$item}{$auth} = ' checked="checked" ';
 4049:                 }
 4050:             }
 4051:         }
 4052:         my %title = &context_names();
 4053:         my %authname = &authtype_names();
 4054:         my $rownum = 0;
 4055:         my $css_class; 
 4056:         foreach my $item (@contexts) {
 4057:             if ($rownum%2) {
 4058:                 $css_class = '';
 4059:             } else {
 4060:                 $css_class = ' class="LC_odd_row" ';
 4061:             }
 4062:             $datatable .=   '<tr'.$css_class.'>'.
 4063:                             '<td>'.$title{$item}.
 4064:                             '</td><td class="LC_left_item">'.
 4065:                             '<span class="LC_nobreak">';
 4066:             foreach my $auth (@authtypes) {
 4067:                 $datatable .= '<label>'. 
 4068:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4069:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4070:                               $authname{$auth}.'</label>&nbsp;';
 4071:             }
 4072:             $datatable .= '</span></td></tr>';
 4073:             $rownum ++;
 4074:         }
 4075:         $$rowtotal += $rownum;
 4076:     }
 4077:     return $datatable;
 4078: }
 4079: 
 4080: sub print_selfcreation {
 4081:     my ($position,$dom,$settings,$rowtotal) = @_;
 4082:     my (@selfcreate,$createsettings,$processing,$datatable);
 4083:     if (ref($settings) eq 'HASH') {
 4084:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4085:             $createsettings = $settings->{'cancreate'};
 4086:             if (ref($createsettings) eq 'HASH') {
 4087:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4088:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4089:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4090:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4091:                         @selfcreate = ('email','login','sso');
 4092:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4093:                         @selfcreate = ($createsettings->{'selfcreate'});
 4094:                     }
 4095:                 }
 4096:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4097:                     $processing = $createsettings->{'selfcreateprocessing'};
 4098:                 }
 4099:             }
 4100:         }
 4101:     }
 4102:     my %radiohash;
 4103:     my $numinrow = 4;
 4104:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4105:     if ($position eq 'top') {
 4106:         my %choices = &Apache::lonlocal::texthash (
 4107:                                                       cancreate_login      => 'Institutional Login',
 4108:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4109:                                                   );
 4110:         my @toggles = sort(keys(%choices));
 4111:         my %defaultchecked = (
 4112:                                'cancreate_login' => 'off',
 4113:                                'cancreate_sso'   => 'off',
 4114:                              );
 4115:         my ($onclick,$itemcount);
 4116:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4117:                                                      \%choices,$itemcount,$onclick);
 4118:         $$rowtotal += $itemcount;
 4119:         
 4120:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4121: 
 4122:         if (ref($usertypes) eq 'HASH') {
 4123:             if (keys(%{$usertypes}) > 0) {
 4124:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4125:                                              $dom,$numinrow,$othertitle,
 4126:                                              'statustocreate',$$rowtotal);
 4127:                 $$rowtotal ++;
 4128:             }
 4129:         }
 4130:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 4131:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4132:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 4133:         my $rem;
 4134:         my $numperrow = 2;
 4135:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4136:         $datatable .= '<tr'.$css_class.'>'.
 4137:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 4138:                      '<td class="LC_left_item">'."\n".
 4139:                      '<table><tr><td>'."\n";
 4140:         for (my $i=0; $i<@fields; $i++) {
 4141:             $rem = $i%($numperrow);
 4142:             if ($rem == 0) {
 4143:                 if ($i > 0) {
 4144:                     $datatable .= '</tr>';
 4145:                 }
 4146:                 $datatable .= '<tr>';
 4147:             }
 4148:             my $currval;
 4149:             if (ref($createsettings) eq 'HASH') {
 4150:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 4151:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 4152:                 }
 4153:             }
 4154:             $datatable .= '<td class="LC_left_item">'.
 4155:                           '<span class="LC_nobreak">'.
 4156:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 4157:                           'value="'.$currval.'" size="10" />&nbsp;'.
 4158:                           $fieldtitles{$fields[$i]}.'</span></td>';
 4159:         }
 4160:         my $colsleft = $numperrow - $rem;
 4161:         if ($colsleft > 1 ) {
 4162:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4163:                          '&nbsp;</td>';
 4164:         } elsif ($colsleft == 1) {
 4165:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4166:         }
 4167:         $datatable .= '</tr></table></td></tr>';
 4168:         $$rowtotal ++;
 4169:     } elsif ($position eq 'middle') {
 4170:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4171:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4172:         $usertypes->{'default'} = $othertitle;
 4173:         if (ref($types) eq 'ARRAY') {
 4174:             push(@{$types},'default');
 4175:             $usertypes->{'default'} = $othertitle;
 4176:             foreach my $status (@{$types}) {
 4177:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4178:                                                        $numinrow,$$rowtotal,$usertypes);
 4179:                 $$rowtotal ++;
 4180:             }
 4181:         }
 4182:     } else {
 4183:         my %choices = &Apache::lonlocal::texthash (
 4184:                                                       cancreate_email => 'E-mail address as username',
 4185:                                                   );
 4186:         my @toggles = sort(keys(%choices));
 4187:         my %defaultchecked = (
 4188:                                'cancreate_email' => 'off',
 4189:                              );
 4190:         my $itemcount = 0;
 4191:         my $display = 'none';
 4192:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 4193:             $display = 'block';
 4194:         }
 4195:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 4196:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 4197:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4198:         my $usertypes = {};
 4199:         my $order = [];
 4200:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 4201:             $usertypes = $domdefaults{'inststatustypes'};
 4202:             $order = $domdefaults{'inststatusguest'};
 4203:         }
 4204:         if (ref($order) eq 'ARRAY') {
 4205:             push(@{$order},'default');
 4206:             if (@{$order} > 1) {
 4207:                 $usertypes->{'default'} = &mt('Other users');
 4208:                 $additional .= '<table><tr>';
 4209:                 foreach my $status (@{$order}) {
 4210:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 4211:                 }
 4212:                 $additional .= '</tr><tr>';
 4213:                 foreach my $status (@{$order}) {
 4214:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 4215:                 }
 4216:                 $additional .= '</tr></table>';
 4217:             } else {
 4218:                 $usertypes->{'default'} = &mt('All users');
 4219:                 $additional .= &email_as_username($rowtotal,$processing);
 4220:             }
 4221:         }
 4222:         $additional .= '</div>'."\n";
 4223: 
 4224:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4225:                                                      \%choices,$$rowtotal,$onclick,$additional);
 4226:         $$rowtotal ++;
 4227:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4228:         $$rowtotal ++;
 4229:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4230:         $numinrow = 1;
 4231:         if (ref($order) eq 'ARRAY') {
 4232:             foreach my $status (@{$order}) {
 4233:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4234:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4235:                 $$rowtotal ++;
 4236:             }
 4237:         }
 4238:         my ($emailrules,$emailruleorder) =
 4239:             &Apache::lonnet::inst_userrules($dom,'email');
 4240:         if (ref($emailrules) eq 'HASH') {
 4241:             if (keys(%{$emailrules}) > 0) {
 4242:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4243:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4244:                 $$rowtotal ++;
 4245:             }
 4246:         }
 4247:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4248:     }
 4249:     return $datatable;
 4250: }
 4251: 
 4252: sub email_as_username {
 4253:     my ($rowtotal,$processing,$type) = @_;
 4254:     my %choices =
 4255:         &Apache::lonlocal::texthash (
 4256:                                       automatic => 'Automatic approval',
 4257:                                       approval  => 'Queued for approval',
 4258:                                     );
 4259:     my $output;
 4260:     foreach my $option ('automatic','approval') {
 4261:         my $checked;
 4262:         if (ref($processing) eq 'HASH') {
 4263:             if ($type eq '') {   
 4264:                 if (!exists($processing->{'default'})) {
 4265:                     if ($option eq 'automatic') {
 4266:                         $checked = ' checked="checked"';
 4267:                     }
 4268:                 } else {
 4269:                     if ($processing->{'default'} eq $option) {
 4270:                         $checked = ' checked="checked"';
 4271:                     }
 4272:                 }
 4273:             } else {
 4274:                 if (!exists($processing->{$type})) {
 4275:                     if ($option eq 'automatic') {
 4276:                         $checked = ' checked="checked"';
 4277:                     }
 4278:                 } else {
 4279:                     if ($processing->{$type} eq $option) {
 4280:                         $checked = ' checked="checked"';
 4281:                     }
 4282:                 }
 4283:             }
 4284:         } elsif ($option eq 'automatic') {
 4285:             $checked = ' checked="checked"'; 
 4286:         }
 4287:         my $name = 'cancreate_emailprocess';
 4288:         if (($type ne '') && ($type ne 'default')) {
 4289:             $name .= '_'.$type;
 4290:         }
 4291:         $output .= '<span class="LC_nobreak"><label>'.
 4292:                    '<input type="radio" name="'.$name.'"'.
 4293:                    $checked.' value="'.$option.'" />'.
 4294:                    $choices{$option}.'</label></span>';
 4295:         if ($type eq '') {
 4296:             $output .= '&nbsp;';
 4297:         } else {
 4298:             $output .= '<br />';
 4299:         }
 4300:     }
 4301:     $$rowtotal ++;
 4302:     return $output;
 4303: }
 4304: 
 4305: sub captcha_choice {
 4306:     my ($context,$settings,$itemcount) = @_;
 4307:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4308:     my %lt = &captcha_phrases();
 4309:     $keyentry = 'hidden';
 4310:     if ($context eq 'cancreate') {
 4311:         $rowname = &mt('CAPTCHA validation');
 4312:     } elsif ($context eq 'login') {
 4313:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4314:     }
 4315:     if (ref($settings) eq 'HASH') {
 4316:         if ($settings->{'captcha'}) {
 4317:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4318:         } else {
 4319:             $checked{'original'} = ' checked="checked"';
 4320:         }
 4321:         if ($settings->{'captcha'} eq 'recaptcha') {
 4322:             $pubtext = $lt{'pub'};
 4323:             $privtext = $lt{'priv'};
 4324:             $keyentry = 'text';
 4325:         }
 4326:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4327:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4328:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4329:         }
 4330:     } else {
 4331:         $checked{'original'} = ' checked="checked"';
 4332:     }
 4333:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4334:     my $output = '<tr'.$css_class.'>'.
 4335:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4336:                  '<table><tr><td>'."\n";
 4337:     foreach my $option ('original','recaptcha','notused') {
 4338:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4339:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4340:                    $lt{$option}.'</label></span>';
 4341:         unless ($option eq 'notused') {
 4342:             $output .= ('&nbsp;'x2)."\n";
 4343:         }
 4344:     }
 4345: #
 4346: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4347: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4348: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4349: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4350: #
 4351:     $output .= '</td></tr>'."\n".
 4352:                '<tr><td>'."\n".
 4353:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4354:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4355:                $currpub.'" size="40" /></span><br />'."\n".
 4356:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4357:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4358:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4359:                '</td></tr>';
 4360:     return $output;
 4361: }
 4362: 
 4363: sub user_formats_row {
 4364:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4365:     my $output;
 4366:     my %text = (
 4367:                    'username' => 'new usernames',
 4368:                    'id'       => 'IDs',
 4369:                    'email'    => 'self-created accounts (e-mail)',
 4370:                );
 4371:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4372:     $output = '<tr '.$css_class.'>'.
 4373:               '<td><span class="LC_nobreak">';
 4374:     if ($type eq 'email') {
 4375:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4376:     } else {
 4377:         $output .= &mt("Format rules to check for $text{$type}: ");
 4378:     }
 4379:     $output .= '</span></td>'.
 4380:                '<td class="LC_left_item" colspan="2"><table>';
 4381:     my $rem;
 4382:     if (ref($ruleorder) eq 'ARRAY') {
 4383:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4384:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4385:                 my $rem = $i%($numinrow);
 4386:                 if ($rem == 0) {
 4387:                     if ($i > 0) {
 4388:                         $output .= '</tr>';
 4389:                     }
 4390:                     $output .= '<tr>';
 4391:                 }
 4392:                 my $check = ' ';
 4393:                 if (ref($settings) eq 'HASH') {
 4394:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4395:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4396:                             $check = ' checked="checked" ';
 4397:                         }
 4398:                     }
 4399:                 }
 4400:                 $output .= '<td class="LC_left_item">'.
 4401:                            '<span class="LC_nobreak"><label>'.
 4402:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4403:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4404:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4405:             }
 4406:         }
 4407:         $rem = @{$ruleorder}%($numinrow);
 4408:     }
 4409:     my $colsleft = $numinrow - $rem;
 4410:     if ($colsleft > 1 ) {
 4411:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4412:                    '&nbsp;</td>';
 4413:     } elsif ($colsleft == 1) {
 4414:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4415:     }
 4416:     $output .= '</tr></table></td></tr>';
 4417:     return $output;
 4418: }
 4419: 
 4420: sub usercreation_types {
 4421:     my %lt = &Apache::lonlocal::texthash (
 4422:                     author     => 'When adding a co-author',
 4423:                     course     => 'When adding a user to a course',
 4424:                     requestcrs => 'When requesting a course',
 4425:                     any        => 'Any',
 4426:                     official   => 'Institutional only ',
 4427:                     unofficial => 'Non-institutional only',
 4428:                     none       => 'None',
 4429:     );
 4430:     return %lt;
 4431: }
 4432: 
 4433: sub selfcreation_types {
 4434:     my %lt = &Apache::lonlocal::texthash (
 4435:                     selfcreate => 'User creates own account',
 4436:                     any        => 'Any',
 4437:                     official   => 'Institutional only ',
 4438:                     unofficial => 'Non-institutional only',
 4439:                     email      => 'E-mail address',
 4440:                     login      => 'Institutional Login',
 4441:                     sso        => 'SSO',
 4442:              );
 4443: }
 4444: 
 4445: sub authtype_names {
 4446:     my %lt = &Apache::lonlocal::texthash(
 4447:                       int    => 'Internal',
 4448:                       krb4   => 'Kerberos 4',
 4449:                       krb5   => 'Kerberos 5',
 4450:                       loc    => 'Local',
 4451:                   );
 4452:     return %lt;
 4453: }
 4454: 
 4455: sub context_names {
 4456:     my %context_title = &Apache::lonlocal::texthash(
 4457:        author => 'Creating users when an Author',
 4458:        course => 'Creating users when in a course',
 4459:        domain => 'Creating users when a Domain Coordinator',
 4460:     );
 4461:     return %context_title;
 4462: }
 4463: 
 4464: sub print_usermodification {
 4465:     my ($position,$dom,$settings,$rowtotal) = @_;
 4466:     my $numinrow = 4;
 4467:     my ($context,$datatable,$rowcount);
 4468:     if ($position eq 'top') {
 4469:         $rowcount = 0;
 4470:         $context = 'author'; 
 4471:         foreach my $role ('ca','aa') {
 4472:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4473:                                                    $numinrow,$rowcount);
 4474:             $$rowtotal ++;
 4475:             $rowcount ++;
 4476:         }
 4477:     } elsif ($position eq 'bottom') {
 4478:         $context = 'course';
 4479:         $rowcount = 0;
 4480:         foreach my $role ('st','ep','ta','in','cr') {
 4481:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4482:                                                    $numinrow,$rowcount);
 4483:             $$rowtotal ++;
 4484:             $rowcount ++;
 4485:         }
 4486:     }
 4487:     return $datatable;
 4488: }
 4489: 
 4490: sub print_defaults {
 4491:     my ($position,$dom,$settings,$rowtotal) = @_;
 4492:     my $rownum = 0;
 4493:     my ($datatable,$css_class);
 4494:     if ($position eq 'top') {
 4495:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4496:                      'datelocale_def','portal_def');
 4497:         my %defaults;
 4498:         if (ref($settings) eq 'HASH') {
 4499:             %defaults = %{$settings};
 4500:         } else {
 4501:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4502:             foreach my $item (@items) {
 4503:                 $defaults{$item} = $domdefaults{$item};
 4504:             }
 4505:         }
 4506:         my $titles = &defaults_titles($dom);
 4507:         foreach my $item (@items) {
 4508:             if ($rownum%2) {
 4509:                 $css_class = '';
 4510:             } else {
 4511:                 $css_class = ' class="LC_odd_row" ';
 4512:             }
 4513:             $datatable .= '<tr'.$css_class.'>'.
 4514:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4515:                           '</span></td><td class="LC_right_item" colspan="3">';
 4516:             if ($item eq 'auth_def') {
 4517:                 my @authtypes = ('internal','krb4','krb5','localauth');
 4518:                 my %shortauth = (
 4519:                                  internal => 'int',
 4520:                                  krb4 => 'krb4',
 4521:                                  krb5 => 'krb5',
 4522:                                  localauth  => 'loc'
 4523:                                 );
 4524:                 my %authnames = &authtype_names();
 4525:                 foreach my $auth (@authtypes) {
 4526:                     my $checked = ' ';
 4527:                     if ($defaults{$item} eq $auth) {
 4528:                         $checked = ' checked="checked" ';
 4529:                     }
 4530:                     $datatable .= '<label><input type="radio" name="'.$item.
 4531:                                   '" value="'.$auth.'"'.$checked.'/>'.
 4532:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4533:                 }
 4534:             } elsif ($item eq 'timezone_def') {
 4535:                 my $includeempty = 1;
 4536:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4537:             } elsif ($item eq 'datelocale_def') {
 4538:                 my $includeempty = 1;
 4539:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4540:             } elsif ($item eq 'lang_def') {
 4541:                 my %langchoices = &get_languages_hash();
 4542:                 $langchoices{''} = 'No language preference';
 4543:                 %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4544:                 $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4545:                                                               \%langchoices);
 4546:             } else {
 4547:                 my $size;
 4548:                 if ($item eq 'portal_def') {
 4549:                     $size = ' size="25"';
 4550:                 }
 4551:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4552:                               $defaults{$item}.'"'.$size.' />';
 4553:             }
 4554:             $datatable .= '</td></tr>';
 4555:             $rownum ++;
 4556:         }
 4557:     } else {
 4558:         my (%defaults);
 4559:         if (ref($settings) eq 'HASH') {
 4560:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 4561:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 4562:                 my $maxnum = @{$settings->{'inststatusorder'}};
 4563:                 for (my $i=0; $i<$maxnum; $i++) {
 4564:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 4565:                     my $item = $settings->{'inststatusorder'}->[$i];
 4566:                     my $title = $settings->{'inststatustypes'}->{$item};
 4567:                     my $guestok;
 4568:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 4569:                         $guestok = 1;
 4570:                     }
 4571:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 4572:                     $datatable .= '<tr'.$css_class.'>'.
 4573:                                   '<td><span class="LC_nobreak">'.
 4574:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 4575:                     for (my $k=0; $k<=$maxnum; $k++) {
 4576:                         my $vpos = $k+1;
 4577:                         my $selstr;
 4578:                         if ($k == $i) {
 4579:                             $selstr = ' selected="selected" ';
 4580:                         }
 4581:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4582:                     }
 4583:                     my ($checkedon,$checkedoff);
 4584:                     $checkedoff = ' checked="checked"';
 4585:                     if ($guestok) {
 4586:                         $checkedon = $checkedoff;
 4587:                         $checkedoff = ''; 
 4588:                     }
 4589:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 4590:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 4591:                                   &mt('delete').'</span></td>'.
 4592:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 4593:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 4594:                                   '</span></td>'.
 4595:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4596:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 4597:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 4598:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 4599:                                   &mt('No').'</label></span></td></tr>';
 4600:                 }
 4601:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 4602:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 4603:                 $datatable .= '<tr '.$css_class.'>'.
 4604:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 4605:                 for (my $k=0; $k<=$maxnum; $k++) {
 4606:                     my $vpos = $k+1;
 4607:                     my $selstr;
 4608:                     if ($k == $maxnum) {
 4609:                         $selstr = ' selected="selected" ';
 4610:                     }
 4611:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4612:                 }
 4613:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 4614:                               '<input type="text" size="10" name="addinststatus" value="" /></span>'.
 4615:                               '&nbsp;'.&mt('(new)').
 4616:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 4617:                               &mt('Name displayed:').
 4618:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 4619:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 4620:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 4621:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 4622:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 4623:                               &mt('No').'</label></span></td></tr>';
 4624:                               '</tr>'."\n";
 4625:                 $rownum ++;
 4626:             }
 4627:         }
 4628:     }
 4629:     $$rowtotal += $rownum;
 4630:     return $datatable;
 4631: }
 4632: 
 4633: sub get_languages_hash {
 4634:     my %langchoices;
 4635:     foreach my $id (&Apache::loncommon::languageids()) {
 4636:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4637:         if ($code ne '') {
 4638:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4639:         }
 4640:     }
 4641:     return %langchoices;
 4642: }
 4643: 
 4644: sub defaults_titles {
 4645:     my ($dom) = @_;
 4646:     my %titles = &Apache::lonlocal::texthash (
 4647:                    'auth_def'      => 'Default authentication type',
 4648:                    'auth_arg_def'  => 'Default authentication argument',
 4649:                    'lang_def'      => 'Default language',
 4650:                    'timezone_def'  => 'Default timezone',
 4651:                    'datelocale_def' => 'Default locale for dates',
 4652:                    'portal_def'     => 'Portal/Default URL',
 4653:                  );
 4654:     if ($dom) {
 4655:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4656:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4657:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4658:         $protocol = 'http' if ($protocol ne 'https');
 4659:         if ($uint_dom) {
 4660:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4661:                                          $uint_dom);
 4662:         }
 4663:     }
 4664:     return (\%titles);
 4665: }
 4666: 
 4667: sub print_scantronformat {
 4668:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4669:     my $itemcount = 1;
 4670:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4671:         %confhash);
 4672:     my $switchserver = &check_switchserver($dom,$confname);
 4673:     my %lt = &Apache::lonlocal::texthash (
 4674:                 default => 'Default bubblesheet format file error',
 4675:                 custom  => 'Custom bubblesheet format file error',
 4676:              );
 4677:     my %scantronfiles = (
 4678:         default => 'default.tab',
 4679:         custom => 'custom.tab',
 4680:     );
 4681:     foreach my $key (keys(%scantronfiles)) {
 4682:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4683:                               .$scantronfiles{$key};
 4684:     }
 4685:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4686:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4687:         if (!$switchserver) {
 4688:             my $servadm = $r->dir_config('lonAdmEMail');
 4689:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4690:             if ($configuserok eq 'ok') {
 4691:                 if ($author_ok eq 'ok') {
 4692:                     my %legacyfile = (
 4693:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4694:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4695:                     );
 4696:                     my %md5chk;
 4697:                     foreach my $type (keys(%legacyfile)) {
 4698:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4699:                         chomp($md5chk{$type});
 4700:                     }
 4701:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4702:                         foreach my $type (keys(%legacyfile)) {
 4703:                             ($scantronurls{$type},my $error) = 
 4704:                                 &legacy_scantronformat($r,$dom,$confname,
 4705:                                                  $type,$legacyfile{$type},
 4706:                                                  $scantronurls{$type},
 4707:                                                  $scantronfiles{$type});
 4708:                             if ($error ne '') {
 4709:                                 $error{$type} = $error;
 4710:                             }
 4711:                         }
 4712:                         if (keys(%error) == 0) {
 4713:                             $is_custom = 1;
 4714:                             $confhash{'scantron'}{'scantronformat'} = 
 4715:                                 $scantronurls{'custom'};
 4716:                             my $putresult = 
 4717:                                 &Apache::lonnet::put_dom('configuration',
 4718:                                                          \%confhash,$dom);
 4719:                             if ($putresult ne 'ok') {
 4720:                                 $error{'custom'} = 
 4721:                                     '<span class="LC_error">'.
 4722:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4723:                             }
 4724:                         }
 4725:                     } else {
 4726:                         ($scantronurls{'default'},my $error) =
 4727:                             &legacy_scantronformat($r,$dom,$confname,
 4728:                                           'default',$legacyfile{'default'},
 4729:                                           $scantronurls{'default'},
 4730:                                           $scantronfiles{'default'});
 4731:                         if ($error eq '') {
 4732:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4733:                             my $putresult =
 4734:                                 &Apache::lonnet::put_dom('configuration',
 4735:                                                          \%confhash,$dom);
 4736:                             if ($putresult ne 'ok') {
 4737:                                 $error{'default'} =
 4738:                                     '<span class="LC_error">'.
 4739:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4740:                             }
 4741:                         } else {
 4742:                             $error{'default'} = $error;
 4743:                         }
 4744:                     }
 4745:                 }
 4746:             }
 4747:         } else {
 4748:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4749:         }
 4750:     }
 4751:     if (ref($settings) eq 'HASH') {
 4752:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4753:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4754:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4755:                 $scantronurl = '';
 4756:             } else {
 4757:                 $scantronurl = $settings->{'scantronformat'};
 4758:             }
 4759:             $is_custom = 1;
 4760:         } else {
 4761:             $scantronurl = $scantronurls{'default'};
 4762:         }
 4763:     } else {
 4764:         if ($is_custom) {
 4765:             $scantronurl = $scantronurls{'custom'};
 4766:         } else {
 4767:             $scantronurl = $scantronurls{'default'};
 4768:         }
 4769:     }
 4770:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4771:     $datatable .= '<tr'.$css_class.'>';
 4772:     if (!$is_custom) {
 4773:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4774:                       '<span class="LC_nobreak">';
 4775:         if ($scantronurl) {
 4776:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4777:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4778:         } else {
 4779:             $datatable = &mt('File unavailable for display');
 4780:         }
 4781:         $datatable .= '</span></td>';
 4782:         if (keys(%error) == 0) { 
 4783:             $datatable .= '<td valign="bottom">';
 4784:             if (!$switchserver) {
 4785:                 $datatable .= &mt('Upload:').'<br />';
 4786:             }
 4787:         } else {
 4788:             my $errorstr;
 4789:             foreach my $key (sort(keys(%error))) {
 4790:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4791:             }
 4792:             $datatable .= '<td>'.$errorstr;
 4793:         }
 4794:     } else {
 4795:         if (keys(%error) > 0) {
 4796:             my $errorstr;
 4797:             foreach my $key (sort(keys(%error))) {
 4798:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4799:             } 
 4800:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4801:         } elsif ($scantronurl) {
 4802:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4803:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4804:             $datatable .= '<td><span class="LC_nobreak">'.
 4805:                           $link.
 4806:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4807:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4808:                           '<td><span class="LC_nobreak">&nbsp;'.
 4809:                           &mt('Replace:').'</span><br />';
 4810:         }
 4811:     }
 4812:     if (keys(%error) == 0) {
 4813:         if ($switchserver) {
 4814:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4815:         } else {
 4816:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4817:                          '<input type="file" name="scantronformat" /></span>';
 4818:         }
 4819:     }
 4820:     $datatable .= '</td></tr>';
 4821:     $$rowtotal ++;
 4822:     return $datatable;
 4823: }
 4824: 
 4825: sub legacy_scantronformat {
 4826:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4827:     my ($url,$error);
 4828:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4829:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4830:         (my $result,$url) =
 4831:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4832:                          '','',$newfile);
 4833:         if ($result ne 'ok') {
 4834:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4835:         }
 4836:     }
 4837:     return ($url,$error);
 4838: }
 4839: 
 4840: sub print_coursecategories {
 4841:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4842:     my $datatable;
 4843:     if ($position eq 'top') {
 4844:         my (%checked);
 4845:         my @catitems = ('unauth','auth');
 4846:         my @cattypes = ('std','domonly','codesrch','none');
 4847:         $checked{'unauth'} = 'std';
 4848:         $checked{'auth'} = 'std';
 4849:         if (ref($settings) eq 'HASH') {
 4850:             foreach my $type (@cattypes) {
 4851:                 if ($type eq $settings->{'unauth'}) {
 4852:                     $checked{'unauth'} = $type;
 4853:                 }
 4854:                 if ($type eq $settings->{'auth'}) {
 4855:                     $checked{'auth'} = $type;
 4856:                 }
 4857:             }
 4858:         }
 4859:         my %lt = &Apache::lonlocal::texthash (
 4860:                                                unauth   => 'Catalog type for unauthenticated users',
 4861:                                                auth     => 'Catalog type for authenticated users',
 4862:                                                none     => 'No catalog',
 4863:                                                std      => 'Standard catalog',
 4864:                                                domonly  => 'Domain-only catalog',
 4865:                                                codesrch => "Code search form",
 4866:                                              );
 4867:        my $itemcount = 0;
 4868:        foreach my $item (@catitems) {
 4869:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 4870:            $datatable .= '<tr '.$css_class.'>'.
 4871:                          '<td>'.$lt{$item}.'</td>'.
 4872:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 4873:            foreach my $type (@cattypes) {
 4874:                my $ischecked;
 4875:                if ($checked{$item} eq $type) {
 4876:                    $ischecked=' checked="checked"';
 4877:                }
 4878:                $datatable .= '<label>'.
 4879:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 4880:                              ' />'.$lt{$type}.'</label>&nbsp;';
 4881:            }
 4882:            $datatable .= '</td></tr>';
 4883:            $itemcount ++;
 4884:         }
 4885:         $$rowtotal += $itemcount;
 4886:     } elsif ($position eq 'middle') {
 4887:         my $toggle_cats_crs = ' ';
 4888:         my $toggle_cats_dom = ' checked="checked" ';
 4889:         my $can_cat_crs = ' ';
 4890:         my $can_cat_dom = ' checked="checked" ';
 4891:         my $toggle_catscomm_comm = ' ';
 4892:         my $toggle_catscomm_dom = ' checked="checked" ';
 4893:         my $can_catcomm_comm = ' ';
 4894:         my $can_catcomm_dom = ' checked="checked" ';
 4895: 
 4896:         if (ref($settings) eq 'HASH') {
 4897:             if ($settings->{'togglecats'} eq 'crs') {
 4898:                 $toggle_cats_crs = $toggle_cats_dom;
 4899:                 $toggle_cats_dom = ' ';
 4900:             }
 4901:             if ($settings->{'categorize'} eq 'crs') {
 4902:                 $can_cat_crs = $can_cat_dom;
 4903:                 $can_cat_dom = ' ';
 4904:             }
 4905:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4906:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4907:                 $toggle_catscomm_dom = ' ';
 4908:             }
 4909:             if ($settings->{'categorizecomm'} eq 'comm') {
 4910:                 $can_catcomm_comm = $can_catcomm_dom;
 4911:                 $can_catcomm_dom = ' ';
 4912:             }
 4913:         }
 4914:         my %title = &Apache::lonlocal::texthash (
 4915:                      togglecats     => 'Show/Hide a course in catalog',
 4916:                      togglecatscomm => 'Show/Hide a community in catalog',
 4917:                      categorize     => 'Assign a category to a course',
 4918:                      categorizecomm => 'Assign a category to a community',
 4919:                     );
 4920:         my %level = &Apache::lonlocal::texthash (
 4921:                      dom  => 'Set in Domain',
 4922:                      crs  => 'Set in Course',
 4923:                      comm => 'Set in Community',
 4924:                     );
 4925:         $datatable = '<tr class="LC_odd_row">'.
 4926:                   '<td>'.$title{'togglecats'}.'</td>'.
 4927:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4928:                   '<input type="radio" name="togglecats"'.
 4929:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4930:                   '<label><input type="radio" name="togglecats"'.
 4931:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4932:                   '</tr><tr>'.
 4933:                   '<td>'.$title{'categorize'}.'</td>'.
 4934:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4935:                   '<label><input type="radio" name="categorize"'.
 4936:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4937:                   '<label><input type="radio" name="categorize"'.
 4938:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4939:                   '</tr><tr class="LC_odd_row">'.
 4940:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4941:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4942:                   '<input type="radio" name="togglecatscomm"'.
 4943:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4944:                   '<label><input type="radio" name="togglecatscomm"'.
 4945:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4946:                   '</tr><tr>'.
 4947:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4948:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4949:                   '<label><input type="radio" name="categorizecomm"'.
 4950:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4951:                   '<label><input type="radio" name="categorizecomm"'.
 4952:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4953:                   '</tr>';
 4954:         $$rowtotal += 4;
 4955:     } else {
 4956:         my $css_class;
 4957:         my $itemcount = 1;
 4958:         my $cathash; 
 4959:         if (ref($settings) eq 'HASH') {
 4960:             $cathash = $settings->{'cats'};
 4961:         }
 4962:         if (ref($cathash) eq 'HASH') {
 4963:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4964:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4965:                                                    \%allitems,\%idx,\@jsarray);
 4966:             my $maxdepth = scalar(@cats);
 4967:             my $colattrib = '';
 4968:             if ($maxdepth > 2) {
 4969:                 $colattrib = ' colspan="2" ';
 4970:             }
 4971:             my @path;
 4972:             if (@cats > 0) {
 4973:                 if (ref($cats[0]) eq 'ARRAY') {
 4974:                     my $numtop = @{$cats[0]};
 4975:                     my $maxnum = $numtop;
 4976:                     my %default_names = (
 4977:                           instcode    => &mt('Official courses'),
 4978:                           communities => &mt('Communities'),
 4979:                     );
 4980: 
 4981:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4982:                         ($cathash->{'instcode::0'} eq '') ||
 4983:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4984:                         ($cathash->{'communities::0'} eq '')) {
 4985:                         $maxnum ++;
 4986:                     }
 4987:                     my $lastidx;
 4988:                     for (my $i=0; $i<$numtop; $i++) {
 4989:                         my $parent = $cats[0][$i];
 4990:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4991:                         my $item = &escape($parent).'::0';
 4992:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4993:                         $lastidx = $idx{$item};
 4994:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4995:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4996:                         for (my $k=0; $k<=$maxnum; $k++) {
 4997:                             my $vpos = $k+1;
 4998:                             my $selstr;
 4999:                             if ($k == $i) {
 5000:                                 $selstr = ' selected="selected" ';
 5001:                             }
 5002:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5003:                         }
 5004:                         $datatable .= '</select></span></td><td>';
 5005:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 5006:                             $datatable .=  '<span class="LC_nobreak">'
 5007:                                            .$default_names{$parent}.'</span>';
 5008:                             if ($parent eq 'instcode') {
 5009:                                 $datatable .= '<br /><span class="LC_nobreak">('
 5010:                                               .&mt('with institutional codes')
 5011:                                               .')</span></td><td'.$colattrib.'>';
 5012:                             } else {
 5013:                                 $datatable .= '<table><tr><td>';
 5014:                             }
 5015:                             $datatable .= '<span class="LC_nobreak">'
 5016:                                           .'<label><input type="radio" name="'
 5017:                                           .$parent.'" value="1" checked="checked" />'
 5018:                                           .&mt('Display').'</label>';
 5019:                             if ($parent eq 'instcode') {
 5020:                                 $datatable .= '&nbsp;';
 5021:                             } else {
 5022:                                 $datatable .= '</span></td></tr><tr><td>'
 5023:                                               .'<span class="LC_nobreak">';
 5024:                             }
 5025:                             $datatable .= '<label><input type="radio" name="'
 5026:                                           .$parent.'" value="0" />'
 5027:                                           .&mt('Do not display').'</label></span>';
 5028:                             if ($parent eq 'communities') {
 5029:                                 $datatable .= '</td></tr></table>';
 5030:                             }
 5031:                             $datatable .= '</td>';
 5032:                         } else {
 5033:                             $datatable .= $parent
 5034:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 5035:                                           .'<input type="checkbox" name="deletecategory" '
 5036:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 5037:                         }
 5038:                         my $depth = 1;
 5039:                         push(@path,$parent);
 5040:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 5041:                         pop(@path);
 5042:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 5043:                         $itemcount ++;
 5044:                     }
 5045:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5046:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 5047:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 5048:                     for (my $k=0; $k<=$maxnum; $k++) {
 5049:                         my $vpos = $k+1;
 5050:                         my $selstr;
 5051:                         if ($k == $numtop) {
 5052:                             $selstr = ' selected="selected" ';
 5053:                         }
 5054:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5055:                     }
 5056:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 5057:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 5058:                                   .'</tr>'."\n";
 5059:                     $itemcount ++;
 5060:                     foreach my $default ('instcode','communities') {
 5061:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 5062:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5063:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 5064:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 5065:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5066:                             for (my $k=0; $k<=$maxnum; $k++) {
 5067:                                 my $vpos = $k+1;
 5068:                                 my $selstr;
 5069:                                 if ($k == $maxnum) {
 5070:                                     $selstr = ' selected="selected" ';
 5071:                                 }
 5072:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5073:                             }
 5074:                             $datatable .= '</select></span></td>'.
 5075:                                           '<td><span class="LC_nobreak">'.
 5076:                                           $default_names{$default}.'</span>';
 5077:                             if ($default eq 'instcode') {
 5078:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5079:                                               .&mt('with institutional codes').')</span>';
 5080:                             }
 5081:                             $datatable .= '</td>'
 5082:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5083:                                           .&mt('Display').'</label>&nbsp;'
 5084:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5085:                                           .&mt('Do not display').'</label></span></td></tr>';
 5086:                         }
 5087:                     }
 5088:                 }
 5089:             } else {
 5090:                 $datatable .= &initialize_categories($itemcount);
 5091:             }
 5092:         } else {
 5093:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 5094:                           .&initialize_categories($itemcount);
 5095:         }
 5096:         $$rowtotal += $itemcount;
 5097:     }
 5098:     return $datatable;
 5099: }
 5100: 
 5101: sub print_serverstatuses {
 5102:     my ($dom,$settings,$rowtotal) = @_;
 5103:     my $datatable;
 5104:     my @pages = &serverstatus_pages();
 5105:     my (%namedaccess,%machineaccess);
 5106:     foreach my $type (@pages) {
 5107:         $namedaccess{$type} = '';
 5108:         $machineaccess{$type}= '';
 5109:     }
 5110:     if (ref($settings) eq 'HASH') {
 5111:         foreach my $type (@pages) {
 5112:             if (exists($settings->{$type})) {
 5113:                 if (ref($settings->{$type}) eq 'HASH') {
 5114:                     foreach my $key (keys(%{$settings->{$type}})) {
 5115:                         if ($key eq 'namedusers') {
 5116:                             $namedaccess{$type} = $settings->{$type}->{$key};
 5117:                         } elsif ($key eq 'machines') {
 5118:                             $machineaccess{$type} = $settings->{$type}->{$key};
 5119:                         }
 5120:                     }
 5121:                 }
 5122:             }
 5123:         }
 5124:     }
 5125:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 5126:     my $rownum = 0;
 5127:     my $css_class;
 5128:     foreach my $type (@pages) {
 5129:         $rownum ++;
 5130:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 5131:         $datatable .= '<tr'.$css_class.'>'.
 5132:                       '<td><span class="LC_nobreak">'.
 5133:                       $titles->{$type}.'</span></td>'.
 5134:                       '<td class="LC_left_item">'.
 5135:                       '<input type="text" name="'.$type.'_namedusers" '.
 5136:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 5137:                       '<td class="LC_right_item">'.
 5138:                       '<span class="LC_nobreak">'.
 5139:                       '<input type="text" name="'.$type.'_machines" '.
 5140:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 5141:                       '</td></tr>'."\n";
 5142:     }
 5143:     $$rowtotal += $rownum;
 5144:     return $datatable;
 5145: }
 5146: 
 5147: sub serverstatus_pages {
 5148:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 5149:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 5150:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 5151:             'uniquecodes','diskusage');
 5152: }
 5153: 
 5154: sub defaults_javascript {
 5155:     my ($settings) = @_;
 5156:     return unless (ref($settings) eq 'HASH');
 5157:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5158:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 5159:         if ($maxnum eq '') {
 5160:             $maxnum = 0;
 5161:         }
 5162:         $maxnum ++;
 5163:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 5164:         return <<"ENDSCRIPT";
 5165: <script type="text/javascript">
 5166: // <![CDATA[
 5167: function reorderTypes(form,caller) {
 5168:     var changedVal;
 5169: $jstext 
 5170:     var newpos = 'addinststatus_pos';
 5171:     var current = new Array;
 5172:     var maxh = $maxnum;
 5173:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5174:     var oldVal;
 5175:     if (caller == newpos) {
 5176:         changedVal = newitemVal;
 5177:     } else {
 5178:         var curritem = 'inststatus_pos_'+caller;
 5179:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 5180:         current[newitemVal] = newpos;
 5181:     }
 5182:     for (var i=0; i<inststatuses.length; i++) {
 5183:         if (inststatuses[i] != caller) {
 5184:             var elementName = 'inststatus_pos_'+inststatuses[i];
 5185:             if (form.elements[elementName]) {
 5186:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5187:                 current[currVal] = elementName;
 5188:             }
 5189:         }
 5190:     }
 5191:     for (var j=0; j<maxh; j++) {
 5192:         if (current[j] == undefined) {
 5193:             oldVal = j;
 5194:         }
 5195:     }
 5196:     if (oldVal < changedVal) {
 5197:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5198:            var elementName = current[k];
 5199:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5200:         }
 5201:     } else {
 5202:         for (var k=changedVal; k<oldVal; k++) {
 5203:             var elementName = current[k];
 5204:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5205:         }
 5206:     }
 5207:     return;
 5208: }
 5209: 
 5210: // ]]>
 5211: </script>
 5212: 
 5213: ENDSCRIPT
 5214:     }
 5215: }
 5216: 
 5217: sub coursecategories_javascript {
 5218:     my ($settings) = @_;
 5219:     my ($output,$jstext,$cathash);
 5220:     if (ref($settings) eq 'HASH') {
 5221:         $cathash = $settings->{'cats'};
 5222:     }
 5223:     if (ref($cathash) eq 'HASH') {
 5224:         my (@cats,@jsarray,%idx);
 5225:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 5226:         if (@jsarray > 0) {
 5227:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 5228:             for (my $i=0; $i<@jsarray; $i++) {
 5229:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 5230:                     my $catstr = join('","',@{$jsarray[$i]});
 5231:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 5232:                 }
 5233:             }
 5234:         }
 5235:     } else {
 5236:         $jstext  = '    var categories = Array(1);'."\n".
 5237:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 5238:     }
 5239:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 5240:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 5241:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category.'); 
 5242:     $output = <<"ENDSCRIPT";
 5243: <script type="text/javascript">
 5244: // <![CDATA[
 5245: function reorderCats(form,parent,item,idx) {
 5246:     var changedVal;
 5247: $jstext
 5248:     var newpos = 'addcategory_pos';
 5249:     if (parent == '') {
 5250:         var has_instcode = 0;
 5251:         var maxtop = categories[idx].length;
 5252:         for (var j=0; j<maxtop; j++) {
 5253:             if (categories[idx][j] == 'instcode::0') {
 5254:                 has_instcode == 1;
 5255:             }
 5256:         }
 5257:         if (has_instcode == 0) {
 5258:             categories[idx][maxtop] = 'instcode_pos';
 5259:         }
 5260:     } else {
 5261:         newpos += '_'+parent;
 5262:     }
 5263:     var maxh = 1 + categories[idx].length;
 5264:     var current = new Array;
 5265:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5266:     if (item == newpos) {
 5267:         changedVal = newitemVal;
 5268:     } else {
 5269:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5270:         current[newitemVal] = newpos;
 5271:     }
 5272:     for (var i=0; i<categories[idx].length; i++) {
 5273:         var elementName = categories[idx][i];
 5274:         if (elementName != item) {
 5275:             if (form.elements[elementName]) {
 5276:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5277:                 current[currVal] = elementName;
 5278:             }
 5279:         }
 5280:     }
 5281:     var oldVal;
 5282:     for (var j=0; j<maxh; j++) {
 5283:         if (current[j] == undefined) {
 5284:             oldVal = j;
 5285:         }
 5286:     }
 5287:     if (oldVal < changedVal) {
 5288:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5289:            var elementName = current[k];
 5290:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5291:         }
 5292:     } else {
 5293:         for (var k=changedVal; k<oldVal; k++) {
 5294:             var elementName = current[k];
 5295:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5296:         }
 5297:     }
 5298:     return;
 5299: }
 5300: 
 5301: function categoryCheck(form) {
 5302:     if (form.elements['addcategory_name'].value == 'instcode') {
 5303:         alert('$instcode_reserved\\n$choose_again');
 5304:         return false;
 5305:     }
 5306:     if (form.elements['addcategory_name'].value == 'communities') {
 5307:         alert('$communities_reserved\\n$choose_again');
 5308:         return false;
 5309:     }
 5310:     return true;
 5311: }
 5312: 
 5313: // ]]>
 5314: </script>
 5315: 
 5316: ENDSCRIPT
 5317:     return $output;
 5318: }
 5319: 
 5320: sub initialize_categories {
 5321:     my ($itemcount) = @_;
 5322:     my ($datatable,$css_class,$chgstr);
 5323:     my %default_names = (
 5324:                       instcode    => 'Official courses (with institutional codes)',
 5325:                       communities => 'Communities',
 5326:                         );
 5327:     my $select0 = ' selected="selected"';
 5328:     my $select1 = '';
 5329:     foreach my $default ('instcode','communities') {
 5330:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5331:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 5332:         if ($default eq 'communities') {
 5333:             $select1 = $select0;
 5334:             $select0 = '';
 5335:         }
 5336:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5337:                      .'<select name="'.$default.'_pos">'
 5338:                      .'<option value="0"'.$select0.'>1</option>'
 5339:                      .'<option value="1"'.$select1.'>2</option>'
 5340:                      .'<option value="2">3</option></select>&nbsp;'
 5341:                      .$default_names{$default}
 5342:                      .'</span></td><td><span class="LC_nobreak">'
 5343:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5344:                      .&mt('Display').'</label>&nbsp;<label>'
 5345:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5346:                  .'</label></span></td></tr>';
 5347:         $itemcount ++;
 5348:     }
 5349:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5350:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5351:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5352:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5353:                   .'<option value="0">1</option>'
 5354:                   .'<option value="1">2</option>'
 5355:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5356:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5357:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5358:     return $datatable;
 5359: }
 5360: 
 5361: sub build_category_rows {
 5362:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5363:     my ($text,$name,$item,$chgstr);
 5364:     if (ref($cats) eq 'ARRAY') {
 5365:         my $maxdepth = scalar(@{$cats});
 5366:         if (ref($cats->[$depth]) eq 'HASH') {
 5367:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5368:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5369:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5370:                 $text .= '<td><table class="LC_data_table">';
 5371:                 my ($idxnum,$parent_name,$parent_item);
 5372:                 my $higher = $depth - 1;
 5373:                 if ($higher == 0) {
 5374:                     $parent_name = &escape($parent).'::'.$higher;
 5375:                 } else {
 5376:                     if (ref($path) eq 'ARRAY') {
 5377:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5378:                     }
 5379:                 }
 5380:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5381:                 for (my $j=0; $j<=$numchildren; $j++) {
 5382:                     if ($j < $numchildren) {
 5383:                         $name = $cats->[$depth]{$parent}[$j];
 5384:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5385:                         $idxnum = $idx->{$item};
 5386:                     } else {
 5387:                         $name = $parent_name;
 5388:                         $item = $parent_item;
 5389:                     }
 5390:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5391:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5392:                     for (my $i=0; $i<=$numchildren; $i++) {
 5393:                         my $vpos = $i+1;
 5394:                         my $selstr;
 5395:                         if ($j == $i) {
 5396:                             $selstr = ' selected="selected" ';
 5397:                         }
 5398:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5399:                     }
 5400:                     $text .= '</select>&nbsp;';
 5401:                     if ($j < $numchildren) {
 5402:                         my $deeper = $depth+1;
 5403:                         $text .= $name.'&nbsp;'
 5404:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5405:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5406:                         if(ref($path) eq 'ARRAY') {
 5407:                             push(@{$path},$name);
 5408:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5409:                             pop(@{$path});
 5410:                         }
 5411:                     } else {
 5412:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5413:                         if ($j == $numchildren) {
 5414:                             $text .= $name;
 5415:                         } else {
 5416:                             $text .= $item;
 5417:                         }
 5418:                         $text .= '" value="" />';
 5419:                     }
 5420:                     $text .= '</td></tr>';
 5421:                 }
 5422:                 $text .= '</table></td>';
 5423:             } else {
 5424:                 my $higher = $depth-1;
 5425:                 if ($higher == 0) {
 5426:                     $name = &escape($parent).'::'.$higher;
 5427:                 } else {
 5428:                     if (ref($path) eq 'ARRAY') {
 5429:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5430:                     }
 5431:                 }
 5432:                 my $colspan;
 5433:                 if ($parent ne 'instcode') {
 5434:                     $colspan = $maxdepth - $depth - 1;
 5435:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5436:                 }
 5437:             }
 5438:         }
 5439:     }
 5440:     return $text;
 5441: }
 5442: 
 5443: sub modifiable_userdata_row {
 5444:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5445:     my ($role,$rolename,$statustype);
 5446:     $role = $item;
 5447:     if ($context eq 'cancreate') {
 5448:         if ($item =~ /^emailusername_(.+)$/) {
 5449:             $statustype = $1;
 5450:             $role = 'emailusername';
 5451:             if (ref($usertypes) eq 'HASH') {
 5452:                 if ($usertypes->{$statustype}) {
 5453:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5454:                 } else {
 5455:                     $rolename = &mt('Data provided by user');
 5456:                 }
 5457:             }
 5458:         }
 5459:     } elsif ($context eq 'selfcreate') {
 5460:         if (ref($usertypes) eq 'HASH') {
 5461:             $rolename = $usertypes->{$role};
 5462:         } else {
 5463:             $rolename = $role;
 5464:         }
 5465:     } else {
 5466:         if ($role eq 'cr') {
 5467:             $rolename = &mt('Custom role');
 5468:         } else {
 5469:             $rolename = &Apache::lonnet::plaintext($role);
 5470:         }
 5471:     }
 5472:     my (@fields,%fieldtitles);
 5473:     if (ref($fieldsref) eq 'ARRAY') {
 5474:         @fields = @{$fieldsref};
 5475:     } else {
 5476:         @fields = ('lastname','firstname','middlename','generation',
 5477:                    'permanentemail','id');
 5478:     }
 5479:     if ((ref($titlesref) eq 'HASH')) {
 5480:         %fieldtitles = %{$titlesref};
 5481:     } else {
 5482:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5483:     }
 5484:     my $output;
 5485:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5486:     $output = '<tr '.$css_class.'>'.
 5487:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5488:               '<td class="LC_left_item" colspan="2"><table>';
 5489:     my $rem;
 5490:     my %checks;
 5491:     if (ref($settings) eq 'HASH') {
 5492:         if (ref($settings->{$context}) eq 'HASH') {
 5493:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5494:                 my $hashref = $settings->{$context}->{$role};
 5495:                 if ($role eq 'emailusername') {
 5496:                     if ($statustype) {
 5497:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5498:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5499:                             if (ref($hashref) eq 'HASH') { 
 5500:                                 foreach my $field (@fields) {
 5501:                                     if ($hashref->{$field}) {
 5502:                                         $checks{$field} = $hashref->{$field};
 5503:                                     }
 5504:                                 }
 5505:                             }
 5506:                         }
 5507:                     }
 5508:                 } else {
 5509:                     if (ref($hashref) eq 'HASH') {
 5510:                         foreach my $field (@fields) {
 5511:                             if ($hashref->{$field}) {
 5512:                                 $checks{$field} = ' checked="checked" ';
 5513:                             }
 5514:                         }
 5515:                     }
 5516:                 }
 5517:             }
 5518:         }
 5519:     }
 5520:      
 5521:     for (my $i=0; $i<@fields; $i++) {
 5522:         my $rem = $i%($numinrow);
 5523:         if ($rem == 0) {
 5524:             if ($i > 0) {
 5525:                 $output .= '</tr>';
 5526:             }
 5527:             $output .= '<tr>';
 5528:         }
 5529:         my $check = ' ';
 5530:         unless ($role eq 'emailusername') {
 5531:             if (exists($checks{$fields[$i]})) {
 5532:                 $check = $checks{$fields[$i]}
 5533:             } else {
 5534:                 if ($role eq 'st') {
 5535:                     if (ref($settings) ne 'HASH') {
 5536:                         $check = ' checked="checked" '; 
 5537:                     }
 5538:                 }
 5539:             }
 5540:         }
 5541:         $output .= '<td class="LC_left_item">'.
 5542:                    '<span class="LC_nobreak">';
 5543:         if ($role eq 'emailusername') {
 5544:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5545:                 $checks{$fields[$i]} = 'omit';
 5546:             }
 5547:             foreach my $option ('required','optional','omit') {
 5548:                 my $checked='';
 5549:                 if ($checks{$fields[$i]} eq $option) {
 5550:                     $checked='checked="checked" ';
 5551:                 }
 5552:                 $output .= '<label>'.
 5553:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5554:                            &mt($option).'</label>'.('&nbsp;' x2);
 5555:             }
 5556:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5557:         } else {
 5558:             $output .= '<label>'.
 5559:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5560:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5561:                        '</label>';
 5562:         }
 5563:         $output .= '</span></td>';
 5564:         $rem = @fields%($numinrow);
 5565:     }
 5566:     my $colsleft = $numinrow - $rem;
 5567:     if ($colsleft > 1 ) {
 5568:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5569:                    '&nbsp;</td>';
 5570:     } elsif ($colsleft == 1) {
 5571:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5572:     }
 5573:     $output .= '</tr></table></td></tr>';
 5574:     return $output;
 5575: }
 5576: 
 5577: sub insttypes_row {
 5578:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5579:     my %lt = &Apache::lonlocal::texthash (
 5580:                       cansearch => 'Users allowed to search',
 5581:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5582:                       lockablenames => 'User preference to lock name',
 5583:              );
 5584:     my $showdom;
 5585:     if ($context eq 'cansearch') {
 5586:         $showdom = ' ('.$dom.')';
 5587:     }
 5588:     my $class = 'LC_left_item';
 5589:     if ($context eq 'statustocreate') {
 5590:         $class = 'LC_right_item';
 5591:     }
 5592:     my $css_class = ' class="LC_odd_row"';
 5593:     if ($rownum ne '') { 
 5594:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5595:     }
 5596:     my $output = '<tr'.$css_class.'>'.
 5597:                  '<td>'.$lt{$context}.$showdom.
 5598:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5599:     my $rem;
 5600:     if (ref($types) eq 'ARRAY') {
 5601:         for (my $i=0; $i<@{$types}; $i++) {
 5602:             if (defined($usertypes->{$types->[$i]})) {
 5603:                 my $rem = $i%($numinrow);
 5604:                 if ($rem == 0) {
 5605:                     if ($i > 0) {
 5606:                         $output .= '</tr>';
 5607:                     }
 5608:                     $output .= '<tr>';
 5609:                 }
 5610:                 my $check = ' ';
 5611:                 if (ref($settings) eq 'HASH') {
 5612:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5613:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5614:                             $check = ' checked="checked" ';
 5615:                         }
 5616:                     } elsif ($context eq 'statustocreate') {
 5617:                         $check = ' checked="checked" ';
 5618:                     }
 5619:                 }
 5620:                 $output .= '<td class="LC_left_item">'.
 5621:                            '<span class="LC_nobreak"><label>'.
 5622:                            '<input type="checkbox" name="'.$context.'" '.
 5623:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5624:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5625:             }
 5626:         }
 5627:         $rem = @{$types}%($numinrow);
 5628:     }
 5629:     my $colsleft = $numinrow - $rem;
 5630:     if (($rem == 0) && (@{$types} > 0)) {
 5631:         $output .= '<tr>';
 5632:     }
 5633:     if ($colsleft > 1) {
 5634:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5635:     } else {
 5636:         $output .= '<td class="LC_left_item">';
 5637:     }
 5638:     my $defcheck = ' ';
 5639:     if (ref($settings) eq 'HASH') {  
 5640:         if (ref($settings->{$context}) eq 'ARRAY') {
 5641:             if (grep(/^default$/,@{$settings->{$context}})) {
 5642:                 $defcheck = ' checked="checked" ';
 5643:             }
 5644:         } elsif ($context eq 'statustocreate') {
 5645:             $defcheck = ' checked="checked" ';
 5646:         }
 5647:     }
 5648:     $output .= '<span class="LC_nobreak"><label>'.
 5649:                '<input type="checkbox" name="'.$context.'" '.
 5650:                'value="default"'.$defcheck.'/>'.
 5651:                $othertitle.'</label></span></td>'.
 5652:                '</tr></table></td></tr>';
 5653:     return $output;
 5654: }
 5655: 
 5656: sub sorted_searchtitles {
 5657:     my %searchtitles = &Apache::lonlocal::texthash(
 5658:                          'uname' => 'username',
 5659:                          'lastname' => 'last name',
 5660:                          'lastfirst' => 'last name, first name',
 5661:                      );
 5662:     my @titleorder = ('uname','lastname','lastfirst');
 5663:     return (\%searchtitles,\@titleorder);
 5664: }
 5665: 
 5666: sub sorted_searchtypes {
 5667:     my %srchtypes_desc = (
 5668:                            exact    => 'is exact match',
 5669:                            contains => 'contains ..',
 5670:                            begins   => 'begins with ..',
 5671:                          );
 5672:     my @srchtypeorder = ('exact','begins','contains');
 5673:     return (\%srchtypes_desc,\@srchtypeorder);
 5674: }
 5675: 
 5676: sub usertype_update_row {
 5677:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5678:     my $datatable;
 5679:     my $numinrow = 4;
 5680:     foreach my $type (@{$types}) {
 5681:         if (defined($usertypes->{$type})) {
 5682:             $$rownums ++;
 5683:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5684:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5685:                           '</td><td class="LC_left_item"><table>';
 5686:             for (my $i=0; $i<@{$fields}; $i++) {
 5687:                 my $rem = $i%($numinrow);
 5688:                 if ($rem == 0) {
 5689:                     if ($i > 0) {
 5690:                         $datatable .= '</tr>';
 5691:                     }
 5692:                     $datatable .= '<tr>';
 5693:                 }
 5694:                 my $check = ' ';
 5695:                 if (ref($settings) eq 'HASH') {
 5696:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5697:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5698:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5699:                                 $check = ' checked="checked" ';
 5700:                             }
 5701:                         }
 5702:                     }
 5703:                 }
 5704: 
 5705:                 if ($i == @{$fields}-1) {
 5706:                     my $colsleft = $numinrow - $rem;
 5707:                     if ($colsleft > 1) {
 5708:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5709:                     } else {
 5710:                         $datatable .= '<td>';
 5711:                     }
 5712:                 } else {
 5713:                     $datatable .= '<td>';
 5714:                 }
 5715:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5716:                               '<input type="checkbox" name="updateable_'.$type.
 5717:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5718:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5719:             }
 5720:             $datatable .= '</tr></table></td></tr>';
 5721:         }
 5722:     }
 5723:     return $datatable;
 5724: }
 5725: 
 5726: sub modify_login {
 5727:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5728:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5729:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5730:     %title = ( coursecatalog => 'Display course catalog',
 5731:                adminmail => 'Display administrator E-mail address',
 5732:                helpdesk  => 'Display "Contact Helpdesk" link',
 5733:                newuser => 'Link for visitors to create a user account',
 5734:                loginheader => 'Log-in box header');
 5735:     @offon = ('off','on');
 5736:     if (ref($domconfig{login}) eq 'HASH') {
 5737:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5738:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5739:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5740:             }
 5741:         }
 5742:     }
 5743:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5744:                                            \%domconfig,\%loginhash);
 5745:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5746:     foreach my $item (@toggles) {
 5747:         $loginhash{login}{$item} = $env{'form.'.$item};
 5748:     }
 5749:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5750:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5751:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5752:                                          \%loginhash);
 5753:     }
 5754: 
 5755:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5756:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5757:     if (keys(%servers) > 1) {
 5758:         foreach my $lonhost (keys(%servers)) {
 5759:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5760:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5761:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5762:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5763:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5764:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5765:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5766:                         $changes{'loginvia'}{$lonhost} = 1;
 5767:                     } else {
 5768:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5769:                         $changes{'loginvia'}{$lonhost} = 1;
 5770:                     }
 5771:                 } else {
 5772:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5773:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5774:                         $changes{'loginvia'}{$lonhost} = 1;
 5775:                     }
 5776:                 }
 5777:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5778:                     foreach my $item (@loginvia_attribs) {
 5779:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5780:                     }
 5781:                 } else {
 5782:                     foreach my $item (@loginvia_attribs) {
 5783:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5784:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5785:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5786:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5787:                                 $new = '/';
 5788:                             }
 5789:                         }
 5790:                         if (($item eq 'custompath') && 
 5791:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5792:                             $new = '';
 5793:                         }
 5794:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5795:                             $changes{'loginvia'}{$lonhost} = 1;
 5796:                         }
 5797:                         if ($item eq 'exempt') {
 5798:                             $new =~ s/^\s+//;
 5799:                             $new =~ s/\s+$//;
 5800:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5801:                             my @okips;
 5802:                             foreach my $ip (@poss_ips) {
 5803:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5804:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5805:                                         push(@okips,$ip); 
 5806:                                     }
 5807:                                 }
 5808:                             }
 5809:                             if (@okips > 0) {
 5810:                                 $new = join(',',@okips); 
 5811:                             } else {
 5812:                                 $new = ''; 
 5813:                             }
 5814:                         }
 5815:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5816:                     }
 5817:                 }
 5818:             } else {
 5819:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5820:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5821:                     $changes{'loginvia'}{$lonhost} = 1;
 5822:                     foreach my $item (@loginvia_attribs) {
 5823:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5824:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5825:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5826:                                 $new = '/';
 5827:                             }
 5828:                         }
 5829:                         if (($item eq 'custompath') && 
 5830:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5831:                             $new = '';
 5832:                         }
 5833:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5834:                     }
 5835:                 }
 5836:             }
 5837:         }
 5838:     }
 5839: 
 5840:     my $servadm = $r->dir_config('lonAdmEMail');
 5841:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5842:     if (ref($domconfig{'login'}) eq 'HASH') {
 5843:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5844:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5845:                 if ($lang eq 'nolang') {
 5846:                     push(@currlangs,$lang);
 5847:                 } elsif (defined($langchoices{$lang})) {
 5848:                     push(@currlangs,$lang);
 5849:                 } else {
 5850:                     next;
 5851:                 }
 5852:             }
 5853:         }
 5854:     }
 5855:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5856:     if (@currlangs > 0) {
 5857:         foreach my $lang (@currlangs) {
 5858:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5859:                 $changes{'helpurl'}{$lang} = 1;
 5860:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5861:                 $changes{'helpurl'}{$lang} = 1;
 5862:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5863:                 push(@newlangs,$lang);
 5864:             } else {
 5865:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5866:             }
 5867:         }
 5868:     }
 5869:     unless (grep(/^nolang$/,@currlangs)) {
 5870:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5871:             $changes{'helpurl'}{'nolang'} = 1;
 5872:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5873:             push(@newlangs,'nolang');
 5874:         }
 5875:     }
 5876:     if ($env{'form.loginhelpurl_add_lang'}) {
 5877:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5878:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5879:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5880:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5881:         }
 5882:     }
 5883:     if ((@newlangs > 0) || ($addedfile)) {
 5884:         my $error;
 5885:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5886:         if ($configuserok eq 'ok') {
 5887:             if ($switchserver) {
 5888:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5889:             } elsif ($author_ok eq 'ok') {
 5890:                 my @allnew = @newlangs;
 5891:                 if ($addedfile ne '') {
 5892:                     push(@allnew,$addedfile);
 5893:                 }
 5894:                 foreach my $lang (@allnew) {
 5895:                     my $formelem = 'loginhelpurl_'.$lang;
 5896:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5897:                         $formelem = 'loginhelpurl_add_file';
 5898:                     }
 5899:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5900:                                                                "help/$lang",'','',$newfile{$lang});
 5901:                     if ($result eq 'ok') {
 5902:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5903:                         $changes{'helpurl'}{$lang} = 1;
 5904:                     } else {
 5905:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5906:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5907:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5908:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5909: 
 5910:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5911:                         }
 5912:                     }
 5913:                 }
 5914:             } else {
 5915:                 $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);
 5916:             }
 5917:         } else {
 5918:             $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);
 5919:         }
 5920:         if ($error) {
 5921:             &Apache::lonnet::logthis($error);
 5922:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5923:         }
 5924:     }
 5925:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5926: 
 5927:     my $defaulthelpfile = '/adm/loginproblems.html';
 5928:     my $defaulttext = &mt('Default in use');
 5929: 
 5930:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5931:                                              $dom);
 5932:     if ($putresult eq 'ok') {
 5933:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5934:         my %defaultchecked = (
 5935:                     'coursecatalog' => 'on',
 5936:                     'helpdesk'      => 'on',
 5937:                     'adminmail'     => 'off',
 5938:                     'newuser'       => 'off',
 5939:         );
 5940:         if (ref($domconfig{'login'}) eq 'HASH') {
 5941:             foreach my $item (@toggles) {
 5942:                 if ($defaultchecked{$item} eq 'on') { 
 5943:                     if (($domconfig{'login'}{$item} eq '0') &&
 5944:                         ($env{'form.'.$item} eq '1')) {
 5945:                         $changes{$item} = 1;
 5946:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5947:                               $domconfig{'login'}{$item} eq '1') &&
 5948:                              ($env{'form.'.$item} eq '0')) {
 5949:                         $changes{$item} = 1;
 5950:                     }
 5951:                 } elsif ($defaultchecked{$item} eq 'off') {
 5952:                     if (($domconfig{'login'}{$item} eq '1') &&
 5953:                         ($env{'form.'.$item} eq '0')) {
 5954:                         $changes{$item} = 1;
 5955:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5956:                               $domconfig{'login'}{$item} eq '0') &&
 5957:                              ($env{'form.'.$item} eq '1')) {
 5958:                         $changes{$item} = 1;
 5959:                     }
 5960:                 }
 5961:             }
 5962:         }
 5963:         if (keys(%changes) > 0 || $colchgtext) {
 5964:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5965:             if (ref($lastactref) eq 'HASH') {
 5966:                 $lastactref->{'domainconfig'} = 1;
 5967:             }
 5968:             $resulttext = &mt('Changes made:').'<ul>';
 5969:             foreach my $item (sort(keys(%changes))) {
 5970:                 if ($item eq 'loginvia') {
 5971:                     if (ref($changes{$item}) eq 'HASH') {
 5972:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5973:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5974:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5975:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5976:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5977:                                     $protocol = 'http' if ($protocol ne 'https');
 5978:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5979: 
 5980:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5981:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5982:                                     } else {
 5983:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5984:                                     }
 5985:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5986:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5987:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5988:                                     }
 5989:                                     $resulttext .= '</li>';
 5990:                                 } else {
 5991:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5992:                                 }
 5993:                             } else {
 5994:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5995:                             }
 5996:                         }
 5997:                         $resulttext .= '</ul></li>';
 5998:                     }
 5999:                 } elsif ($item eq 'helpurl') {
 6000:                     if (ref($changes{$item}) eq 'HASH') {
 6001:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 6002:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 6003:                                 my ($chg,$link);
 6004:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 6005:                                 if ($lang eq 'nolang') {
 6006:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 6007:                                 } else {
 6008:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 6009:                                 }
 6010:                                 $resulttext .= '<li>'.$chg.'</li>';
 6011:                             } else {
 6012:                                 my $chg;
 6013:                                 if ($lang eq 'nolang') {
 6014:                                     $chg = &mt('custom log-in help file for no preferred language');
 6015:                                 } else {
 6016:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 6017:                                 }
 6018:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 6019:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 6020:                                                       '?inhibitmenu=yes',$chg,600,500).
 6021:                                                '</li>';
 6022:                             }
 6023:                         }
 6024:                     }
 6025:                 } elsif ($item eq 'captcha') {
 6026:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6027:                         my $chgtxt;
 6028:                         if ($loginhash{'login'}{$item} eq 'notused') {
 6029:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 6030:                         } else {
 6031:                             my %captchas = &captcha_phrases();
 6032:                             if ($captchas{$loginhash{'login'}{$item}}) {
 6033:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 6034:                             } else {
 6035:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 6036:                             }
 6037:                         }
 6038:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6039:                     }
 6040:                 } elsif ($item eq 'recaptchakeys') {
 6041:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6042:                         my ($privkey,$pubkey);
 6043:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 6044:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 6045:                             $privkey = $loginhash{'login'}{$item}{'private'};
 6046:                         }
 6047:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 6048:                         if (!$pubkey) {
 6049:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 6050:                         } else {
 6051:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6052:                         }
 6053:                         if (!$privkey) {
 6054:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 6055:                         } else {
 6056:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6057:                         }
 6058:                         $chgtxt .= '</ul>';
 6059:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6060:                     }
 6061:                 } else {
 6062:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 6063:                 }
 6064:             }
 6065:             $resulttext .= $colchgtext.'</ul>';
 6066:         } else {
 6067:             $resulttext = &mt('No changes made to log-in page settings');
 6068:         }
 6069:     } else {
 6070:         $resulttext = '<span class="LC_error">'.
 6071: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6072:     }
 6073:     if ($errors) {
 6074:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 6075:                        $errors.'</ul>';
 6076:     }
 6077:     return $resulttext;
 6078: }
 6079: 
 6080: sub color_font_choices {
 6081:     my %choices =
 6082:         &Apache::lonlocal::texthash (
 6083:             img => "Header",
 6084:             bgs => "Background colors",
 6085:             links => "Link colors",
 6086:             images => "Images",
 6087:             font => "Font color",
 6088:             fontmenu => "Font menu",
 6089:             pgbg => "Page",
 6090:             tabbg => "Header",
 6091:             sidebg => "Border",
 6092:             link => "Link",
 6093:             alink => "Active link",
 6094:             vlink => "Visited link",
 6095:         );
 6096:     return %choices;
 6097: }
 6098: 
 6099: sub modify_rolecolors {
 6100:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 6101:     my ($resulttext,%rolehash);
 6102:     $rolehash{'rolecolors'} = {};
 6103:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 6104:         if ($domconfig{'rolecolors'} eq '') {
 6105:             $domconfig{'rolecolors'} = {};
 6106:         }
 6107:     }
 6108:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 6109:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 6110:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 6111:                                              $dom);
 6112:     if ($putresult eq 'ok') {
 6113:         if (keys(%changes) > 0) {
 6114:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6115:             if (ref($lastactref) eq 'HASH') {
 6116:                 $lastactref->{'domainconfig'} = 1;
 6117:             }
 6118:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 6119:                                              $rolehash{'rolecolors'});
 6120:         } else {
 6121:             $resulttext = &mt('No changes made to default color schemes');
 6122:         }
 6123:     } else {
 6124:         $resulttext = '<span class="LC_error">'.
 6125: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6126:     }
 6127:     if ($errors) {
 6128:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6129:                        $errors.'</ul>';
 6130:     }
 6131:     return $resulttext;
 6132: }
 6133: 
 6134: sub modify_colors {
 6135:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 6136:     my (%changes,%choices);
 6137:     my @bgs;
 6138:     my @links = ('link','alink','vlink');
 6139:     my @logintext;
 6140:     my @images;
 6141:     my $servadm = $r->dir_config('lonAdmEMail');
 6142:     my $errors;
 6143:     my %defaults;
 6144:     foreach my $role (@{$roles}) {
 6145:         if ($role eq 'login') {
 6146:             %choices = &login_choices();
 6147:             @logintext = ('textcol','bgcol');
 6148:         } else {
 6149:             %choices = &color_font_choices();
 6150:         }
 6151:         if ($role eq 'login') {
 6152:             @images = ('img','logo','domlogo','login');
 6153:             @bgs = ('pgbg','mainbg','sidebg');
 6154:         } else {
 6155:             @images = ('img');
 6156:             @bgs = ('pgbg','tabbg','sidebg');
 6157:         }
 6158:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 6159:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 6160:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 6161:         }
 6162:         if ($role eq 'login') {
 6163:             foreach my $item (@logintext) {
 6164:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6165:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6166:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6167:                 }
 6168:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 6169:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6170:                 }
 6171:             }
 6172:         } else {
 6173:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 6174:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 6175:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 6176:             }
 6177:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 6178:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 6179:             }
 6180:         }
 6181:         foreach my $item (@bgs) {
 6182:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6183:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6184:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6185:             }
 6186:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 6187:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6188:             }
 6189:         }
 6190:         foreach my $item (@links) {
 6191:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6192:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6193:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6194:             }
 6195:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 6196:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6197:             }
 6198:         }
 6199:         my ($configuserok,$author_ok,$switchserver) = 
 6200:             &config_check($dom,$confname,$servadm);
 6201:         my ($width,$height) = &thumb_dimensions();
 6202:         if (ref($domconfig->{$role}) ne 'HASH') {
 6203:             $domconfig->{$role} = {};
 6204:         }
 6205:         foreach my $img (@images) {
 6206:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 6207:                 if (defined($env{'form.login_showlogo_'.$img})) {
 6208:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 6209:                 } else { 
 6210:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 6211:                 }
 6212:             } 
 6213: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 6214: 		 && !defined($domconfig->{$role}{$img})
 6215: 		 && !$env{'form.'.$role.'_del_'.$img}
 6216: 		 && $env{'form.'.$role.'_import_'.$img}) {
 6217: 		# import the old configured image from the .tab setting
 6218: 		# if they haven't provided a new one 
 6219: 		$domconfig->{$role}{$img} = 
 6220: 		    $env{'form.'.$role.'_import_'.$img};
 6221: 	    }
 6222:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 6223:                 my $error;
 6224:                 if ($configuserok eq 'ok') {
 6225:                     if ($switchserver) {
 6226:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 6227:                     } else {
 6228:                         if ($author_ok eq 'ok') {
 6229:                             my ($result,$logourl) = 
 6230:                                 &publishlogo($r,'upload',$role.'_'.$img,
 6231:                                            $dom,$confname,$img,$width,$height);
 6232:                             if ($result eq 'ok') {
 6233:                                 $confhash->{$role}{$img} = $logourl;
 6234:                                 $changes{$role}{'images'}{$img} = 1;
 6235:                             } else {
 6236:                                 $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);
 6237:                             }
 6238:                         } else {
 6239:                             $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);
 6240:                         }
 6241:                     }
 6242:                 } else {
 6243:                     $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);
 6244:                 }
 6245:                 if ($error) {
 6246:                     &Apache::lonnet::logthis($error);
 6247:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6248:                 }
 6249:             } elsif ($domconfig->{$role}{$img} ne '') {
 6250:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 6251:                     my $error;
 6252:                     if ($configuserok eq 'ok') {
 6253: # is confname an author?
 6254:                         if ($switchserver eq '') {
 6255:                             if ($author_ok eq 'ok') {
 6256:                                 my ($result,$logourl) = 
 6257:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 6258:                                             $dom,$confname,$img,$width,$height);
 6259:                                 if ($result eq 'ok') {
 6260:                                     $confhash->{$role}{$img} = $logourl;
 6261: 				    $changes{$role}{'images'}{$img} = 1;
 6262:                                 }
 6263:                             }
 6264:                         }
 6265:                     }
 6266:                 }
 6267:             }
 6268:         }
 6269:         if (ref($domconfig) eq 'HASH') {
 6270:             if (ref($domconfig->{$role}) eq 'HASH') {
 6271:                 foreach my $img (@images) {
 6272:                     if ($domconfig->{$role}{$img} ne '') {
 6273:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6274:                             $confhash->{$role}{$img} = '';
 6275:                             $changes{$role}{'images'}{$img} = 1;
 6276:                         } else {
 6277:                             if ($confhash->{$role}{$img} eq '') {
 6278:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 6279:                             }
 6280:                         }
 6281:                     } else {
 6282:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6283:                             $confhash->{$role}{$img} = '';
 6284:                             $changes{$role}{'images'}{$img} = 1;
 6285:                         } 
 6286:                     }
 6287:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 6288:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 6289:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 6290:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 6291:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 6292:                             }
 6293:                         } else {
 6294:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6295:                                 $changes{$role}{'showlogo'}{$img} = 1;
 6296:                             }
 6297:                         }
 6298:                     }
 6299:                 }
 6300:                 if ($domconfig->{$role}{'font'} ne '') {
 6301:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 6302:                         $changes{$role}{'font'} = 1;
 6303:                     }
 6304:                 } else {
 6305:                     if ($confhash->{$role}{'font'}) {
 6306:                         $changes{$role}{'font'} = 1;
 6307:                     }
 6308:                 }
 6309:                 if ($role ne 'login') {
 6310:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 6311:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 6312:                             $changes{$role}{'fontmenu'} = 1;
 6313:                         }
 6314:                     } else {
 6315:                         if ($confhash->{$role}{'fontmenu'}) {
 6316:                             $changes{$role}{'fontmenu'} = 1;
 6317:                         }
 6318:                     }
 6319:                 }
 6320:                 foreach my $item (@bgs) {
 6321:                     if ($domconfig->{$role}{$item} ne '') {
 6322:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6323:                             $changes{$role}{'bgs'}{$item} = 1;
 6324:                         } 
 6325:                     } else {
 6326:                         if ($confhash->{$role}{$item}) {
 6327:                             $changes{$role}{'bgs'}{$item} = 1;
 6328:                         }
 6329:                     }
 6330:                 }
 6331:                 foreach my $item (@links) {
 6332:                     if ($domconfig->{$role}{$item} ne '') {
 6333:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6334:                             $changes{$role}{'links'}{$item} = 1;
 6335:                         }
 6336:                     } else {
 6337:                         if ($confhash->{$role}{$item}) {
 6338:                             $changes{$role}{'links'}{$item} = 1;
 6339:                         }
 6340:                     }
 6341:                 }
 6342:                 foreach my $item (@logintext) {
 6343:                     if ($domconfig->{$role}{$item} ne '') {
 6344:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6345:                             $changes{$role}{'logintext'}{$item} = 1;
 6346:                         }
 6347:                     } else {
 6348:                         if ($confhash->{$role}{$item}) {
 6349:                             $changes{$role}{'logintext'}{$item} = 1;
 6350:                         }
 6351:                     }
 6352:                 }
 6353:             } else {
 6354:                 &default_change_checker($role,\@images,\@links,\@bgs,
 6355:                                         \@logintext,$confhash,\%changes); 
 6356:             }
 6357:         } else {
 6358:             &default_change_checker($role,\@images,\@links,\@bgs,
 6359:                                     \@logintext,$confhash,\%changes); 
 6360:         }
 6361:     }
 6362:     return ($errors,%changes);
 6363: }
 6364: 
 6365: sub config_check {
 6366:     my ($dom,$confname,$servadm) = @_;
 6367:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6368:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6369:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6370:                                                    $confname,$servadm);
 6371:     if ($configuserok eq 'ok') {
 6372:         $switchserver = &check_switchserver($dom,$confname);
 6373:         if ($switchserver eq '') {
 6374:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6375:         }
 6376:     }
 6377:     return ($configuserok,$author_ok,$switchserver);
 6378: }
 6379: 
 6380: sub default_change_checker {
 6381:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6382:     foreach my $item (@{$links}) {
 6383:         if ($confhash->{$role}{$item}) {
 6384:             $changes->{$role}{'links'}{$item} = 1;
 6385:         }
 6386:     }
 6387:     foreach my $item (@{$bgs}) {
 6388:         if ($confhash->{$role}{$item}) {
 6389:             $changes->{$role}{'bgs'}{$item} = 1;
 6390:         }
 6391:     }
 6392:     foreach my $item (@{$logintext}) {
 6393:         if ($confhash->{$role}{$item}) {
 6394:             $changes->{$role}{'logintext'}{$item} = 1;
 6395:         }
 6396:     }
 6397:     foreach my $img (@{$images}) {
 6398:         if ($env{'form.'.$role.'_del_'.$img}) {
 6399:             $confhash->{$role}{$img} = '';
 6400:             $changes->{$role}{'images'}{$img} = 1;
 6401:         }
 6402:         if ($role eq 'login') {
 6403:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6404:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6405:             }
 6406:         }
 6407:     }
 6408:     if ($confhash->{$role}{'font'}) {
 6409:         $changes->{$role}{'font'} = 1;
 6410:     }
 6411: }
 6412: 
 6413: sub display_colorchgs {
 6414:     my ($dom,$changes,$roles,$confhash) = @_;
 6415:     my (%choices,$resulttext);
 6416:     if (!grep(/^login$/,@{$roles})) {
 6417:         $resulttext = &mt('Changes made:').'<br />';
 6418:     }
 6419:     foreach my $role (@{$roles}) {
 6420:         if ($role eq 'login') {
 6421:             %choices = &login_choices();
 6422:         } else {
 6423:             %choices = &color_font_choices();
 6424:         }
 6425:         if (ref($changes->{$role}) eq 'HASH') {
 6426:             if ($role ne 'login') {
 6427:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6428:             }
 6429:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6430:                 if ($role ne 'login') {
 6431:                     $resulttext .= '<ul>';
 6432:                 }
 6433:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6434:                     if ($role ne 'login') {
 6435:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6436:                     }
 6437:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6438:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6439:                             if ($confhash->{$role}{$key}{$item}) {
 6440:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6441:                             } else {
 6442:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6443:                             }
 6444:                         } elsif ($confhash->{$role}{$item} eq '') {
 6445:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6446:                         } else {
 6447:                             my $newitem = $confhash->{$role}{$item};
 6448:                             if ($key eq 'images') {
 6449:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6450:                             }
 6451:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6452:                         }
 6453:                     }
 6454:                     if ($role ne 'login') {
 6455:                         $resulttext .= '</ul></li>';
 6456:                     }
 6457:                 } else {
 6458:                     if ($confhash->{$role}{$key} eq '') {
 6459:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6460:                     } else {
 6461:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6462:                     }
 6463:                 }
 6464:                 if ($role ne 'login') {
 6465:                     $resulttext .= '</ul>';
 6466:                 }
 6467:             }
 6468:         }
 6469:     }
 6470:     return $resulttext;
 6471: }
 6472: 
 6473: sub thumb_dimensions {
 6474:     return ('200','50');
 6475: }
 6476: 
 6477: sub check_dimensions {
 6478:     my ($inputfile) = @_;
 6479:     my ($fullwidth,$fullheight);
 6480:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6481:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6482:             my $imageinfo = <PIPE>;
 6483:             if (!close(PIPE)) {
 6484:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6485:             }
 6486:             chomp($imageinfo);
 6487:             my ($fullsize) = 
 6488:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6489:             if ($fullsize) {
 6490:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6491:             }
 6492:         }
 6493:     }
 6494:     return ($fullwidth,$fullheight);
 6495: }
 6496: 
 6497: sub check_configuser {
 6498:     my ($uhome,$dom,$confname,$servadm) = @_;
 6499:     my ($configuserok,%currroles);
 6500:     if ($uhome eq 'no_host') {
 6501:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6502:         my $configpass = &LONCAPA::Enrollment::create_password();
 6503:         $configuserok = 
 6504:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6505:                              $configpass,'','','','','',undef,$servadm);
 6506:     } else {
 6507:         $configuserok = 'ok';
 6508:         %currroles = 
 6509:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6510:     }
 6511:     return ($configuserok,%currroles);
 6512: }
 6513: 
 6514: sub check_authorstatus {
 6515:     my ($dom,$confname,%currroles) = @_;
 6516:     my $author_ok;
 6517:     if (!$currroles{':'.$dom.':au'}) {
 6518:         my $start = time;
 6519:         my $end = 0;
 6520:         $author_ok = 
 6521:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6522:                                         'au',$end,$start,'','','domconfig');
 6523:     } else {
 6524:         $author_ok = 'ok';
 6525:     }
 6526:     return $author_ok;
 6527: }
 6528: 
 6529: sub publishlogo {
 6530:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6531:     my ($output,$fname,$logourl);
 6532:     if ($action eq 'upload') {
 6533:         $fname=$env{'form.'.$formname.'.filename'};
 6534:         chop($env{'form.'.$formname});
 6535:     } else {
 6536:         ($fname) = ($formname =~ /([^\/]+)$/);
 6537:     }
 6538:     if ($savefileas ne '') {
 6539:         $fname = $savefileas;
 6540:     }
 6541:     $fname=&Apache::lonnet::clean_filename($fname);
 6542: # See if there is anything left
 6543:     unless ($fname) { return ('error: no uploaded file'); }
 6544:     $fname="$subdir/$fname";
 6545:     my $docroot=$r->dir_config('lonDocRoot');
 6546:     my $filepath="$docroot/priv";
 6547:     my $relpath = "$dom/$confname";
 6548:     my ($fnamepath,$file,$fetchthumb);
 6549:     $file=$fname;
 6550:     if ($fname=~m|/|) {
 6551:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6552:     }
 6553:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6554:     my $count;
 6555:     for ($count=5;$count<=$#parts;$count++) {
 6556:         $filepath.="/$parts[$count]";
 6557:         if ((-e $filepath)!=1) {
 6558:             mkdir($filepath,02770);
 6559:         }
 6560:     }
 6561:     # Check for bad extension and disallow upload
 6562:     if ($file=~/\.(\w+)$/ &&
 6563:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6564:         $output = 
 6565:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6566:     } elsif ($file=~/\.(\w+)$/ &&
 6567:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6568:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6569:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6570:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6571:     } elsif (-d "$filepath/$file") {
 6572:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6573:     } else {
 6574:         my $source = $filepath.'/'.$file;
 6575:         my $logfile;
 6576:         if (!open($logfile,">>$source".'.log')) {
 6577:             return (&mt('No write permission to Authoring Space'));
 6578:         }
 6579:         print $logfile
 6580: "\n================= Publish ".localtime()." ================\n".
 6581: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6582: # Save the file
 6583:         if (!open(FH,'>'.$source)) {
 6584:             &Apache::lonnet::logthis('Failed to create '.$source);
 6585:             return (&mt('Failed to create file'));
 6586:         }
 6587:         if ($action eq 'upload') {
 6588:             if (!print FH ($env{'form.'.$formname})) {
 6589:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6590:                 return (&mt('Failed to write file'));
 6591:             }
 6592:         } else {
 6593:             my $original = &Apache::lonnet::filelocation('',$formname);
 6594:             if(!copy($original,$source)) {
 6595:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6596:                 return (&mt('Failed to write file'));
 6597:             }
 6598:         }
 6599:         close(FH);
 6600:         chmod(0660, $source); # Permissions to rw-rw---.
 6601: 
 6602:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6603:         my $copyfile=$targetdir.'/'.$file;
 6604: 
 6605:         my @parts=split(/\//,$targetdir);
 6606:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6607:         for (my $count=5;$count<=$#parts;$count++) {
 6608:             $path.="/$parts[$count]";
 6609:             if (!-e $path) {
 6610:                 print $logfile "\nCreating directory ".$path;
 6611:                 mkdir($path,02770);
 6612:             }
 6613:         }
 6614:         my $versionresult;
 6615:         if (-e $copyfile) {
 6616:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6617:         } else {
 6618:             $versionresult = 'ok';
 6619:         }
 6620:         if ($versionresult eq 'ok') {
 6621:             if (copy($source,$copyfile)) {
 6622:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6623:                 $output = 'ok';
 6624:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6625:                 push(@{$modified_urls},[$copyfile,$source]);
 6626:                 my $metaoutput = 
 6627:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6628:                 unless ($registered_cleanup) {
 6629:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6630:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6631:                     $registered_cleanup=1;
 6632:                 }
 6633:             } else {
 6634:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6635:                 $output = &mt('Failed to copy file to RES space').", $!";
 6636:             }
 6637:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6638:                 my $inputfile = $filepath.'/'.$file;
 6639:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6640:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6641:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6642:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6643:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6644:                         system("convert -sample $thumbsize $inputfile $outfile");
 6645:                         chmod(0660, $filepath.'/tn-'.$file);
 6646:                         if (-e $outfile) {
 6647:                             my $copyfile=$targetdir.'/tn-'.$file;
 6648:                             if (copy($outfile,$copyfile)) {
 6649:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6650:                                 my $thumb_metaoutput = 
 6651:                                     &write_metadata($dom,$confname,$formname,
 6652:                                                     $targetdir,'tn-'.$file,$logfile);
 6653:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6654:                                 unless ($registered_cleanup) {
 6655:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6656:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6657:                                     $registered_cleanup=1;
 6658:                                 }
 6659:                             } else {
 6660:                                 print $logfile "\nUnable to write ".$copyfile.
 6661:                                                ':'.$!."\n";
 6662:                             }
 6663:                         }
 6664:                     }
 6665:                 }
 6666:             }
 6667:         } else {
 6668:             $output = $versionresult;
 6669:         }
 6670:     }
 6671:     return ($output,$logourl);
 6672: }
 6673: 
 6674: sub logo_versioning {
 6675:     my ($targetdir,$file,$logfile) = @_;
 6676:     my $target = $targetdir.'/'.$file;
 6677:     my ($maxversion,$fn,$extn,$output);
 6678:     $maxversion = 0;
 6679:     if ($file =~ /^(.+)\.(\w+)$/) {
 6680:         $fn=$1;
 6681:         $extn=$2;
 6682:     }
 6683:     opendir(DIR,$targetdir);
 6684:     while (my $filename=readdir(DIR)) {
 6685:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6686:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6687:         }
 6688:     }
 6689:     $maxversion++;
 6690:     print $logfile "\nCreating old version ".$maxversion."\n";
 6691:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6692:     if (copy($target,$copyfile)) {
 6693:         print $logfile "Copied old target to ".$copyfile."\n";
 6694:         $copyfile=$copyfile.'.meta';
 6695:         if (copy($target.'.meta',$copyfile)) {
 6696:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6697:             $output = 'ok';
 6698:         } else {
 6699:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6700:             $output = &mt('Failed to copy old meta').", $!, ";
 6701:         }
 6702:     } else {
 6703:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6704:         $output = &mt('Failed to copy old target').", $!, ";
 6705:     }
 6706:     return $output;
 6707: }
 6708: 
 6709: sub write_metadata {
 6710:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6711:     my (%metadatafields,%metadatakeys,$output);
 6712:     $metadatafields{'title'}=$formname;
 6713:     $metadatafields{'creationdate'}=time;
 6714:     $metadatafields{'lastrevisiondate'}=time;
 6715:     $metadatafields{'copyright'}='public';
 6716:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6717:                                          $env{'user.domain'};
 6718:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6719:     $metadatafields{'domain'}=$dom;
 6720:     {
 6721:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6722:         my $mfh;
 6723:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6724:             foreach (sort(keys(%metadatafields))) {
 6725:                 unless ($_=~/\./) {
 6726:                     my $unikey=$_;
 6727:                     $unikey=~/^([A-Za-z]+)/;
 6728:                     my $tag=$1;
 6729:                     $tag=~tr/A-Z/a-z/;
 6730:                     print $mfh "\n\<$tag";
 6731:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6732:                         my $value=$metadatafields{$unikey.'.'.$_};
 6733:                         $value=~s/\"/\'\'/g;
 6734:                         print $mfh ' '.$_.'="'.$value.'"';
 6735:                     }
 6736:                     print $mfh '>'.
 6737:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6738:                             .'</'.$tag.'>';
 6739:                 }
 6740:             }
 6741:             $output = 'ok';
 6742:             print $logfile "\nWrote metadata";
 6743:             close($mfh);
 6744:         } else {
 6745:             print $logfile "\nFailed to open metadata file";
 6746:             $output = &mt('Could not write metadata');
 6747:         }
 6748:     }
 6749:     return $output;
 6750: }
 6751: 
 6752: sub notifysubscribed {
 6753:     foreach my $targetsource (@{$modified_urls}){
 6754:         next unless (ref($targetsource) eq 'ARRAY');
 6755:         my ($target,$source)=@{$targetsource};
 6756:         if ($source ne '') {
 6757:             if (open(my $logfh,'>>'.$source.'.log')) {
 6758:                 print $logfh "\nCleanup phase: Notifications\n";
 6759:                 my @subscribed=&subscribed_hosts($target);
 6760:                 foreach my $subhost (@subscribed) {
 6761:                     print $logfh "\nNotifying host ".$subhost.':';
 6762:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6763:                     print $logfh $reply;
 6764:                 }
 6765:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6766:                 foreach my $subhost (@subscribedmeta) {
 6767:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6768:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6769:                                                         $subhost);
 6770:                     print $logfh $reply;
 6771:                 }
 6772:                 print $logfh "\n============ Done ============\n";
 6773:                 close($logfh);
 6774:             }
 6775:         }
 6776:     }
 6777:     return OK;
 6778: }
 6779: 
 6780: sub subscribed_hosts {
 6781:     my ($target) = @_;
 6782:     my @subscribed;
 6783:     if (open(my $fh,"<$target.subscription")) {
 6784:         while (my $subline=<$fh>) {
 6785:             if ($subline =~ /^($match_lonid):/) {
 6786:                 my $host = $1;
 6787:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6788:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6789:                         push(@subscribed,$host);
 6790:                     }
 6791:                 }
 6792:             }
 6793:         }
 6794:     }
 6795:     return @subscribed;
 6796: }
 6797: 
 6798: sub check_switchserver {
 6799:     my ($dom,$confname) = @_;
 6800:     my ($allowed,$switchserver);
 6801:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6802:     if ($home eq 'no_host') {
 6803:         $home = &Apache::lonnet::domain($dom,'primary');
 6804:     }
 6805:     my @ids=&Apache::lonnet::current_machine_ids();
 6806:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6807:     if (!$allowed) {
 6808: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6809:     }
 6810:     return $switchserver;
 6811: }
 6812: 
 6813: sub modify_quotas {
 6814:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6815:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6816:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6817:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 6818:         $validationfieldsref);
 6819:     if ($action eq 'quotas') {
 6820:         $context = 'tools'; 
 6821:     } else {
 6822:         $context = $action;
 6823:     }
 6824:     if ($context eq 'requestcourses') {
 6825:         @usertools = ('official','unofficial','community','textbook');
 6826:         @options =('norequest','approval','validate','autolimit');
 6827:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6828:         %titles = &courserequest_titles();
 6829:         $toolregexp = join('|',@usertools);
 6830:         %conditions = &courserequest_conditions();
 6831:         $confname = $dom.'-domainconfig';
 6832:         my $servadm = $r->dir_config('lonAdmEMail');
 6833:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6834:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 6835:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6836:     } elsif ($context eq 'requestauthor') {
 6837:         @usertools = ('author');
 6838:         %titles = &authorrequest_titles();
 6839:     } else {
 6840:         @usertools = ('aboutme','blog','webdav','portfolio');
 6841:         %titles = &tool_titles();
 6842:     }
 6843:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6844:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6845:     foreach my $key (keys(%env)) {
 6846:         if ($context eq 'requestcourses') {
 6847:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6848:                 my $item = $1;
 6849:                 my $type = $2;
 6850:                 if ($type =~ /^limit_(.+)/) {
 6851:                     $limithash{$item}{$1} = $env{$key};
 6852:                 } else {
 6853:                     $confhash{$item}{$type} = $env{$key};
 6854:                 }
 6855:             }
 6856:         } elsif ($context eq 'requestauthor') {
 6857:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6858:                 $confhash{$1} = $env{$key};
 6859:             }
 6860:         } else {
 6861:             if ($key =~ /^form\.quota_(.+)$/) {
 6862:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6863:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6864:                 $confhash{'authorquota'}{$1} = $env{$key};
 6865:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6866:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6867:             }
 6868:         }
 6869:     }
 6870:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6871:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6872:         @approvalnotify = sort(@approvalnotify);
 6873:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6874:         my @crstypes = ('official','unofficial','community','textbook');
 6875:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6876:         foreach my $type (@hasuniquecode) {
 6877:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6878:                 $confhash{'uniquecode'}{$type} = 1;
 6879:             }
 6880:         }
 6881:         my (%newbook,%allpos);
 6882:         if ($context eq 'requestcourses') {
 6883:             foreach my $type ('textbooks','templates') {
 6884:                 @{$allpos{$type}} = (); 
 6885:                 my $invalid;
 6886:                 if ($type eq 'textbooks') {
 6887:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 6888:                 } else {
 6889:                     $invalid = &mt('Invalid LON-CAPA course for template');
 6890:                 }
 6891:                 if ($env{'form.'.$type.'_addbook'}) {
 6892:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 6893:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 6894:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 6895:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 6896:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 6897:                         } else {
 6898:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 6899:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 6900:                             $position =~ s/\D+//g;
 6901:                             if ($position ne '') {
 6902:                                 $allpos{$type}[$position] = $newbook{$type};
 6903:                             }
 6904:                         }
 6905:                     } else {
 6906:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 6907:                     }
 6908:                 }
 6909:             } 
 6910:         }
 6911:         if (ref($domconfig{$action}) eq 'HASH') {
 6912:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6913:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6914:                     $changes{'notify'}{'approval'} = 1;
 6915:                 }
 6916:             } else {
 6917:                 if ($confhash{'notify'}{'approval'}) {
 6918:                     $changes{'notify'}{'approval'} = 1;
 6919:                 }
 6920:             }
 6921:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6922:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6923:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6924:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6925:                             $changes{'uniquecode'} = 1;
 6926:                         }
 6927:                     }
 6928:                     unless ($changes{'uniquecode'}) {
 6929:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6930:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6931:                                 $changes{'uniquecode'} = 1;
 6932:                             }
 6933:                         }
 6934:                     }
 6935:                } else {
 6936:                    $changes{'uniquecode'} = 1;
 6937:                }
 6938:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6939:                 $changes{'uniquecode'} = 1;
 6940:             }
 6941:             if ($context eq 'requestcourses') {
 6942:                 foreach my $type ('textbooks','templates') {
 6943:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 6944:                         my %deletions;
 6945:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 6946:                         if (@todelete) {
 6947:                             map { $deletions{$_} = 1; } @todelete;
 6948:                         }
 6949:                         my %imgdeletions;
 6950:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 6951:                         if (@todeleteimages) {
 6952:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 6953:                         }
 6954:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 6955:                         for (my $i=0; $i<=$maxnum; $i++) {
 6956:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 6957:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 6958:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 6959:                                 if ($deletions{$key}) {
 6960:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 6961:                                         #FIXME need to obsolete item in RES space
 6962:                                     }
 6963:                                     next;
 6964:                                 } else {
 6965:                                     my $newpos = $env{'form.'.$itemid};
 6966:                                     $newpos =~ s/\D+//g;
 6967:                                     foreach my $item ('subject','title','publisher','author') {
 6968:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 6969:                                                  ($type eq 'templates'));
 6970:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 6971:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 6972:                                             $changes{$type}{$key} = 1;
 6973:                                         }
 6974:                                     }
 6975:                                     $allpos{$type}[$newpos] = $key;
 6976:                                 }
 6977:                                 if ($imgdeletions{$key}) {
 6978:                                     $changes{$type}{$key} = 1;
 6979:                                     #FIXME need to obsolete item in RES space
 6980:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 6981:                                     my ($cdom,$cnum) = split(/_/,$key);
 6982:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 6983:                                                                                   $cdom,$cnum,$type,$configuserok,
 6984:                                                                                   $switchserver,$author_ok);
 6985:                                     if ($imgurl) {
 6986:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 6987:                                         $changes{$type}{$key} = 1; 
 6988:                                     }
 6989:                                     if ($error) {
 6990:                                         &Apache::lonnet::logthis($error);
 6991:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6992:                                     } 
 6993:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 6994:                                     $confhash{$type}{$key}{'image'} = 
 6995:                                         $domconfig{$action}{$type}{$key}{'image'};
 6996:                                 }
 6997:                             }
 6998:                         }
 6999:                     }
 7000:                 }
 7001:             }
 7002:         } else {
 7003:             if ($confhash{'notify'}{'approval'}) {
 7004:                 $changes{'notify'}{'approval'} = 1;
 7005:             }
 7006:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 7007:                 $changes{'uniquecode'} = 1;
 7008:             }
 7009:         }
 7010:         if ($context eq 'requestcourses') {
 7011:             foreach my $type ('textbooks','templates') {
 7012:                 if ($newbook{$type}) {
 7013:                     $changes{$type}{$newbook{$type}} = 1;
 7014:                     foreach my $item ('subject','title','publisher','author') {
 7015:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7016:                                  ($type eq 'template'));
 7017:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 7018:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 7019:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 7020:                         }
 7021:                     }
 7022:                     if ($type eq 'textbooks') {
 7023:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 7024:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 7025:                             my ($imageurl,$error) =
 7026:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 7027:                                                         $configuserok,$switchserver,$author_ok);
 7028:                             if ($imageurl) {
 7029:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 7030:                             }
 7031:                             if ($error) {
 7032:                                 &Apache::lonnet::logthis($error);
 7033:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7034:                             }
 7035:                         }
 7036:                     }
 7037:                 }
 7038:                 if (@{$allpos{$type}} > 0) {
 7039:                     my $idx = 0;
 7040:                     foreach my $item (@{$allpos{$type}}) {
 7041:                         if ($item ne '') {
 7042:                             $confhash{$type}{$item}{'order'} = $idx;
 7043:                             if (ref($domconfig{$action}) eq 'HASH') {
 7044:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7045:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 7046:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 7047:                                             $changes{$type}{$item} = 1;
 7048:                                         }
 7049:                                     }
 7050:                                 }
 7051:                             }
 7052:                             $idx ++;
 7053:                         }
 7054:                     }
 7055:                 }
 7056:             }
 7057:             if (ref($validationitemsref) eq 'ARRAY') {
 7058:                 foreach my $item (@{$validationitemsref}) {
 7059:                     if ($item eq 'fields') {
 7060:                         my @changed;
 7061:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 7062:                         if (@{$confhash{'validation'}{$item}} > 0) {
 7063:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 7064:                         }
 7065:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7066:                             if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 7067:                                 @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 7068:                                                                               $domconfig{'requestcourses'}{'validation'}{$item});
 7069:                             } else {
 7070:                                 @changed = @{$confhash{'validation'}{$item}};
 7071:                             }
 7072:                         } else {
 7073:                             @changed = @{$confhash{'validation'}{$item}};
 7074:                         }
 7075:                         if (@changed) {
 7076:                             if ($confhash{'validation'}{$item}) {
 7077:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 7078:                             } else {
 7079:                                 $changes{'validation'}{$item} = &mt('None');
 7080:                             }
 7081:                         }
 7082:                     } else {
 7083:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 7084:                         if ($item eq 'markup') {
 7085:                             if ($env{'form.requestcourses_validation_'.$item}) {
 7086:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 7087:                             }
 7088:                         }
 7089:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7090:                             if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 7091:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7092:                             }
 7093:                         } else {
 7094:                             if ($confhash{'validation'}{$item} ne '') {
 7095:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7096:                             }
 7097:                         }
 7098:                     }
 7099:                 }
 7100:             }
 7101:             if ($env{'form.validationdc'}) {
 7102:                 my $newval = $env{'form.validationdc'};
 7103:                 my %domcoords = &get_active_dcs($dom);
 7104:                 if (exists($domcoords{$newval})) {
 7105:                     $confhash{'validation'}{'dc'} = $newval;
 7106:                 }
 7107:             }
 7108:             if (ref($confhash{'validation'}) eq 'HASH') {
 7109:                 if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7110:                     if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7111:                         unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7112:                             if ($confhash{'validation'}{'dc'} eq '') {
 7113:                                 $changes{'validation'}{'dc'} = &mt('None');
 7114:                             } else {
 7115:                                 $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7116:                             }
 7117:                         }
 7118:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 7119:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7120:                     }
 7121:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 7122:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7123:                 }
 7124:             } elsif (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7125:                 if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7126:                     $changes{'validation'}{'dc'} = &mt('None');
 7127:                 }
 7128:             }
 7129:         }
 7130:     } else {
 7131:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 7132:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 7133:     }
 7134:     foreach my $item (@usertools) {
 7135:         foreach my $type (@{$types},'default','_LC_adv') {
 7136:             my $unset; 
 7137:             if ($context eq 'requestcourses') {
 7138:                 $unset = '0';
 7139:                 if ($type eq '_LC_adv') {
 7140:                     $unset = '';
 7141:                 }
 7142:                 if ($confhash{$item}{$type} eq 'autolimit') {
 7143:                     $confhash{$item}{$type} .= '=';
 7144:                     unless ($limithash{$item}{$type} =~ /\D/) {
 7145:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 7146:                     }
 7147:                 }
 7148:             } elsif ($context eq 'requestauthor') {
 7149:                 $unset = '0';
 7150:                 if ($type eq '_LC_adv') {
 7151:                     $unset = '';
 7152:                 }
 7153:             } else {
 7154:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 7155:                     $confhash{$item}{$type} = 1;
 7156:                 } else {
 7157:                     $confhash{$item}{$type} = 0;
 7158:                 }
 7159:             }
 7160:             if (ref($domconfig{$action}) eq 'HASH') {
 7161:                 if ($action eq 'requestauthor') {
 7162:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 7163:                         $changes{$type} = 1;
 7164:                     }
 7165:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 7166:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 7167:                         $changes{$item}{$type} = 1;
 7168:                     }
 7169:                 } else {
 7170:                     if ($context eq 'requestcourses') {
 7171:                         if ($confhash{$item}{$type} ne $unset) {
 7172:                             $changes{$item}{$type} = 1;
 7173:                         }
 7174:                     } else {
 7175:                         if (!$confhash{$item}{$type}) {
 7176:                             $changes{$item}{$type} = 1;
 7177:                         }
 7178:                     }
 7179:                 }
 7180:             } else {
 7181:                 if ($context eq 'requestcourses') {
 7182:                     if ($confhash{$item}{$type} ne $unset) {
 7183:                         $changes{$item}{$type} = 1;
 7184:                     }
 7185:                 } elsif ($context eq 'requestauthor') {
 7186:                     if ($confhash{$type} ne $unset) {
 7187:                         $changes{$type} = 1;
 7188:                     }
 7189:                 } else {
 7190:                     if (!$confhash{$item}{$type}) {
 7191:                         $changes{$item}{$type} = 1;
 7192:                     }
 7193:                 }
 7194:             }
 7195:         }
 7196:     }
 7197:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7198:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 7199:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7200:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 7201:                     if (exists($confhash{'defaultquota'}{$key})) {
 7202:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 7203:                             $changes{'defaultquota'}{$key} = 1;
 7204:                         }
 7205:                     } else {
 7206:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 7207:                     }
 7208:                 }
 7209:             } else {
 7210:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 7211:                     if (exists($confhash{'defaultquota'}{$key})) {
 7212:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 7213:                             $changes{'defaultquota'}{$key} = 1;
 7214:                         }
 7215:                     } else {
 7216:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 7217:                     }
 7218:                 }
 7219:             }
 7220:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7221:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 7222:                     if (exists($confhash{'authorquota'}{$key})) {
 7223:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 7224:                             $changes{'authorquota'}{$key} = 1;
 7225:                         }
 7226:                     } else {
 7227:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 7228:                     }
 7229:                 }
 7230:             }
 7231:         }
 7232:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 7233:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 7234:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7235:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7236:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 7237:                             $changes{'defaultquota'}{$key} = 1;
 7238:                         }
 7239:                     } else {
 7240:                         if (!exists($domconfig{'quotas'}{$key})) {
 7241:                             $changes{'defaultquota'}{$key} = 1;
 7242:                         }
 7243:                     }
 7244:                 } else {
 7245:                     $changes{'defaultquota'}{$key} = 1;
 7246:                 }
 7247:             }
 7248:         }
 7249:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 7250:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 7251:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7252:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7253:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 7254:                             $changes{'authorquota'}{$key} = 1;
 7255:                         }
 7256:                     } else {
 7257:                         $changes{'authorquota'}{$key} = 1;
 7258:                     }
 7259:                 } else {
 7260:                     $changes{'authorquota'}{$key} = 1;
 7261:                 }
 7262:             }
 7263:         }
 7264:     }
 7265: 
 7266:     if ($context eq 'requestauthor') {
 7267:         $domdefaults{'requestauthor'} = \%confhash;
 7268:     } else {
 7269:         foreach my $key (keys(%confhash)) {
 7270:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 7271:                 $domdefaults{$key} = $confhash{$key};
 7272:             }
 7273:         }
 7274:     }
 7275: 
 7276:     my %quotahash = (
 7277:                       $action => { %confhash }
 7278:                     );
 7279:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 7280:                                              $dom);
 7281:     if ($putresult eq 'ok') {
 7282:         if (keys(%changes) > 0) {
 7283:             my $cachetime = 24*60*60;
 7284:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7285:             if (ref($lastactref) eq 'HASH') {
 7286:                 $lastactref->{'domdefaults'} = 1;
 7287:             }
 7288:             $resulttext = &mt('Changes made:').'<ul>';
 7289:             unless (($context eq 'requestcourses') ||
 7290:                     ($context eq 'requestauthor')) {
 7291:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 7292:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 7293:                     foreach my $type (@{$types},'default') {
 7294:                         if (defined($changes{'defaultquota'}{$type})) {
 7295:                             my $typetitle = $usertypes->{$type};
 7296:                             if ($type eq 'default') {
 7297:                                 $typetitle = $othertitle;
 7298:                             }
 7299:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 7300:                         }
 7301:                     }
 7302:                     $resulttext .= '</ul></li>';
 7303:                 }
 7304:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 7305:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 7306:                     foreach my $type (@{$types},'default') {
 7307:                         if (defined($changes{'authorquota'}{$type})) {
 7308:                             my $typetitle = $usertypes->{$type};
 7309:                             if ($type eq 'default') {
 7310:                                 $typetitle = $othertitle;
 7311:                             }
 7312:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 7313:                         }
 7314:                     }
 7315:                     $resulttext .= '</ul></li>';
 7316:                 }
 7317:             }
 7318:             my %newenv;
 7319:             foreach my $item (@usertools) {
 7320:                 my (%haschgs,%inconf);
 7321:                 if ($context eq 'requestauthor') {
 7322:                     %haschgs = %changes;
 7323:                     %inconf = %confhash;
 7324:                 } else {
 7325:                     if (ref($changes{$item}) eq 'HASH') {
 7326:                         %haschgs = %{$changes{$item}};
 7327:                     }
 7328:                     if (ref($confhash{$item}) eq 'HASH') {
 7329:                         %inconf = %{$confhash{$item}};
 7330:                     }
 7331:                 }
 7332:                 if (keys(%haschgs) > 0) {
 7333:                     my $newacc = 
 7334:                         &Apache::lonnet::usertools_access($env{'user.name'},
 7335:                                                           $env{'user.domain'},
 7336:                                                           $item,'reload',$context);
 7337:                     if (($context eq 'requestcourses') ||
 7338:                         ($context eq 'requestauthor')) {
 7339:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 7340:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 7341:                         }
 7342:                     } else {
 7343:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 7344:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 7345:                         }
 7346:                     }
 7347:                     unless ($context eq 'requestauthor') {
 7348:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 7349:                     }
 7350:                     foreach my $type (@{$types},'default','_LC_adv') {
 7351:                         if ($haschgs{$type}) {
 7352:                             my $typetitle = $usertypes->{$type};
 7353:                             if ($type eq 'default') {
 7354:                                 $typetitle = $othertitle;
 7355:                             } elsif ($type eq '_LC_adv') {
 7356:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 7357:                             }
 7358:                             if ($inconf{$type}) {
 7359:                                 if ($context eq 'requestcourses') {
 7360:                                     my $cond;
 7361:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 7362:                                         if ($1 eq '') {
 7363:                                             $cond = &mt('(Automatic processing of any request).');
 7364:                                         } else {
 7365:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 7366:                                         }
 7367:                                     } else { 
 7368:                                         $cond = $conditions{$inconf{$type}};
 7369:                                     }
 7370:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 7371:                                 } elsif ($context eq 'requestauthor') {
 7372:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 7373:                                                              $titles{$inconf{$type}},$typetitle);
 7374: 
 7375:                                 } else {
 7376:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7377:                                 }
 7378:                             } else {
 7379:                                 if ($type eq '_LC_adv') {
 7380:                                     if ($inconf{$type} eq '0') {
 7381:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7382:                                     } else { 
 7383:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7384:                                     }
 7385:                                 } else {
 7386:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7387:                                 }
 7388:                             }
 7389:                         }
 7390:                     }
 7391:                     unless ($context eq 'requestauthor') {
 7392:                         $resulttext .= '</ul></li>';
 7393:                     }
 7394:                 }
 7395:             }
 7396:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7397:                 if (ref($changes{'notify'}) eq 'HASH') {
 7398:                     if ($changes{'notify'}{'approval'}) {
 7399:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7400:                             if ($confhash{'notify'}{'approval'}) {
 7401:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7402:                             } else {
 7403:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7404:                             }
 7405:                         }
 7406:                     }
 7407:                 }
 7408:             }
 7409:             if ($action eq 'requestcourses') {
 7410:                 my @offon = ('off','on');
 7411:                 if ($changes{'uniquecode'}) {
 7412:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7413:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7414:                         $resulttext .= '<li>'.
 7415:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7416:                                        '</li>';
 7417:                     } else {
 7418:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7419:                                        '</li>';
 7420:                     }
 7421:                 }
 7422:                 foreach my $type ('textbooks','templates') {
 7423:                     if (ref($changes{$type}) eq 'HASH') {
 7424:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 7425:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 7426:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 7427:                             my $coursetitle = $coursehash{'description'};
 7428:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 7429:                             $resulttext .= '<li>';
 7430:                             foreach my $item ('subject','title','publisher','author') {
 7431:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7432:                                          ($type eq 'templates'));
 7433:                                 my $name = $item.':';
 7434:                                 $name =~ s/^(\w)/\U$1/;
 7435:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 7436:                             }
 7437:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7438:                             if ($type eq 'textbooks') {
 7439:                                 if ($confhash{$type}{$key}{'image'}) {
 7440:                                     $resulttext .= ' '.&mt('Image: [_1]',
 7441:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 7442:                                                    ' alt="Textbook cover" />').'<br />';
 7443:                                 }
 7444:                             }
 7445:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7446:                         }
 7447:                         $resulttext .= '</ul></li>';
 7448:                     }
 7449:                 }
 7450:                 if (ref($changes{'validation'}) eq 'HASH') {
 7451:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7452:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7453:                         foreach my $item (@{$validationitemsref}) {
 7454:                             if (exists($changes{'validation'}{$item})) {
 7455:                                 if ($item eq 'markup') {
 7456:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7457:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7458:                                 } else {
 7459:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7460:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7461:                                 }
 7462:                             }
 7463:                         }
 7464:                         if (exists($changes{'validation'}{'dc'})) {
 7465:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7466:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7467:                         }
 7468:                     }
 7469:                 }
 7470:             }
 7471:             $resulttext .= '</ul>';
 7472:             if (keys(%newenv)) {
 7473:                 &Apache::lonnet::appenv(\%newenv);
 7474:             }
 7475:         } else {
 7476:             if ($context eq 'requestcourses') {
 7477:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7478:             } elsif ($context eq 'requestauthor') {
 7479:                 $resulttext = &mt('No changes made to rights to request author space.');
 7480:             } else {
 7481:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7482:             }
 7483:         }
 7484:     } else {
 7485:         $resulttext = '<span class="LC_error">'.
 7486: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7487:     }
 7488:     if ($errors) {
 7489:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7490:                        '<ul>'.$errors.'</ul></p>';
 7491:     }
 7492:     return $resulttext;
 7493: }
 7494: 
 7495: sub process_textbook_image {
 7496:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 7497:     my $filename = $env{'form.'.$caller.'.filename'};
 7498:     my ($error,$url);
 7499:     my ($width,$height) = (50,50);
 7500:     if ($configuserok eq 'ok') {
 7501:         if ($switchserver) {
 7502:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7503:                          $switchserver);
 7504:         } elsif ($author_ok eq 'ok') {
 7505:             my ($result,$imageurl) =
 7506:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7507:                              "$type/$dom/$cnum/cover",$width,$height);
 7508:             if ($result eq 'ok') {
 7509:                 $url = $imageurl;
 7510:             } else {
 7511:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7512:             }
 7513:         } else {
 7514:             $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);
 7515:         }
 7516:     } else {
 7517:         $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);
 7518:     }
 7519:     return ($url,$error);
 7520: }
 7521: 
 7522: sub modify_autoenroll {
 7523:     my ($dom,$lastactref,%domconfig) = @_;
 7524:     my ($resulttext,%changes);
 7525:     my %currautoenroll;
 7526:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7527:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7528:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7529:         }
 7530:     }
 7531:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7532:     my %title = ( run => 'Auto-enrollment active',
 7533:                   sender => 'Sender for notification messages',
 7534:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7535:     my @offon = ('off','on');
 7536:     my $sender_uname = $env{'form.sender_uname'};
 7537:     my $sender_domain = $env{'form.sender_domain'};
 7538:     if ($sender_domain eq '') {
 7539:         $sender_uname = '';
 7540:     } elsif ($sender_uname eq '') {
 7541:         $sender_domain = '';
 7542:     }
 7543:     my $coowners = $env{'form.autoassign_coowners'};
 7544:     my %autoenrollhash =  (
 7545:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7546:                                        'sender_uname' => $sender_uname,
 7547:                                        'sender_domain' => $sender_domain,
 7548:                                        'co-owners' => $coowners,
 7549:                                 }
 7550:                      );
 7551:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7552:                                              $dom);
 7553:     if ($putresult eq 'ok') {
 7554:         if (exists($currautoenroll{'run'})) {
 7555:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7556:                  $changes{'run'} = 1;
 7557:              }
 7558:         } elsif ($autorun) {
 7559:             if ($env{'form.autoenroll_run'} ne '1') {
 7560:                  $changes{'run'} = 1;
 7561:             }
 7562:         }
 7563:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7564:             $changes{'sender'} = 1;
 7565:         }
 7566:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7567:             $changes{'sender'} = 1;
 7568:         }
 7569:         if ($currautoenroll{'co-owners'} ne '') {
 7570:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7571:                 $changes{'coowners'} = 1;
 7572:             }
 7573:         } elsif ($coowners) {
 7574:             $changes{'coowners'} = 1;
 7575:         }      
 7576:         if (keys(%changes) > 0) {
 7577:             $resulttext = &mt('Changes made:').'<ul>';
 7578:             if ($changes{'run'}) {
 7579:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7580:             }
 7581:             if ($changes{'sender'}) {
 7582:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7583:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7584:                 } else {
 7585:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7586:                 }
 7587:             }
 7588:             if ($changes{'coowners'}) {
 7589:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7590:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7591:                 if (ref($lastactref) eq 'HASH') {
 7592:                     $lastactref->{'domainconfig'} = 1;
 7593:                 }
 7594:             }
 7595:             $resulttext .= '</ul>';
 7596:         } else {
 7597:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7598:         }
 7599:     } else {
 7600:         $resulttext = '<span class="LC_error">'.
 7601: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7602:     }
 7603:     return $resulttext;
 7604: }
 7605: 
 7606: sub modify_autoupdate {
 7607:     my ($dom,%domconfig) = @_;
 7608:     my ($resulttext,%currautoupdate,%fields,%changes);
 7609:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7610:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7611:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7612:         }
 7613:     }
 7614:     my @offon = ('off','on');
 7615:     my %title = &Apache::lonlocal::texthash (
 7616:                    run => 'Auto-update:',
 7617:                    classlists => 'Updates to user information in classlists?'
 7618:                 );
 7619:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7620:     my %fieldtitles = &Apache::lonlocal::texthash (
 7621:                         id => 'Student/Employee ID',
 7622:                         permanentemail => 'E-mail address',
 7623:                         lastname => 'Last Name',
 7624:                         firstname => 'First Name',
 7625:                         middlename => 'Middle Name',
 7626:                         generation => 'Generation',
 7627:                       );
 7628:     $othertitle = &mt('All users');
 7629:     if (keys(%{$usertypes}) >  0) {
 7630:         $othertitle = &mt('Other users');
 7631:     }
 7632:     foreach my $key (keys(%env)) {
 7633:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7634:             my ($usertype,$item) = ($1,$2);
 7635:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7636:                 if ($usertype eq 'default') {   
 7637:                     push(@{$fields{$1}},$2);
 7638:                 } elsif (ref($types) eq 'ARRAY') {
 7639:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7640:                         push(@{$fields{$1}},$2);
 7641:                     }
 7642:                 }
 7643:             }
 7644:         }
 7645:     }
 7646:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 7647:     @lockablenames = sort(@lockablenames);
 7648:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 7649:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7650:         if (@changed) {
 7651:             $changes{'lockablenames'} = 1;
 7652:         }
 7653:     } else {
 7654:         if (@lockablenames) {
 7655:             $changes{'lockablenames'} = 1;
 7656:         }
 7657:     }
 7658:     my %updatehash = (
 7659:                       autoupdate => { run => $env{'form.autoupdate_run'},
 7660:                                       classlists => $env{'form.classlists'},
 7661:                                       fields => {%fields},
 7662:                                       lockablenames => \@lockablenames,
 7663:                                     }
 7664:                      );
 7665:     foreach my $key (keys(%currautoupdate)) {
 7666:         if (($key eq 'run') || ($key eq 'classlists')) {
 7667:             if (exists($updatehash{autoupdate}{$key})) {
 7668:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 7669:                     $changes{$key} = 1;
 7670:                 }
 7671:             }
 7672:         } elsif ($key eq 'fields') {
 7673:             if (ref($currautoupdate{$key}) eq 'HASH') {
 7674:                 foreach my $item (@{$types},'default') {
 7675:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 7676:                         my $change = 0;
 7677:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 7678:                             if (!exists($fields{$item})) {
 7679:                                 $change = 1;
 7680:                                 last;
 7681:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 7682:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 7683:                                     $change = 1;
 7684:                                     last;
 7685:                                 }
 7686:                             }
 7687:                         }
 7688:                         if ($change) {
 7689:                             push(@{$changes{$key}},$item);
 7690:                         }
 7691:                     } 
 7692:                 }
 7693:             }
 7694:         } elsif ($key eq 'lockablenames') {
 7695:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 7696:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7697:                 if (@changed) {
 7698:                     $changes{'lockablenames'} = 1;
 7699:                 }
 7700:             } else {
 7701:                 if (@lockablenames) {
 7702:                     $changes{'lockablenames'} = 1;
 7703:                 }
 7704:             }
 7705:         }
 7706:     }
 7707:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 7708:         if (@lockablenames) {
 7709:             $changes{'lockablenames'} = 1;
 7710:         }
 7711:     }
 7712:     foreach my $item (@{$types},'default') {
 7713:         if (defined($fields{$item})) {
 7714:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 7715:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 7716:                     my $change = 0;
 7717:                     if (ref($fields{$item}) eq 'ARRAY') {
 7718:                         foreach my $type (@{$fields{$item}}) {
 7719:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 7720:                                 $change = 1;
 7721:                                 last;
 7722:                             }
 7723:                         }
 7724:                     }
 7725:                     if ($change) {
 7726:                         push(@{$changes{'fields'}},$item);
 7727:                     }
 7728:                 } else {
 7729:                     push(@{$changes{'fields'}},$item);
 7730:                 }
 7731:             } else {
 7732:                 push(@{$changes{'fields'}},$item);
 7733:             }
 7734:         }
 7735:     }
 7736:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7737:                                              $dom);
 7738:     if ($putresult eq 'ok') {
 7739:         if (keys(%changes) > 0) {
 7740:             $resulttext = &mt('Changes made:').'<ul>';
 7741:             foreach my $key (sort(keys(%changes))) {
 7742:                 if ($key eq 'lockablenames') {
 7743:                     $resulttext .= '<li>';
 7744:                     if (@lockablenames) {
 7745:                         $usertypes->{'default'} = $othertitle;
 7746:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7747:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7748:                     } else {
 7749:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7750:                     }
 7751:                     $resulttext .= '</li>';
 7752:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7753:                     foreach my $item (@{$changes{$key}}) {
 7754:                         my @newvalues;
 7755:                         foreach my $type (@{$fields{$item}}) {
 7756:                             push(@newvalues,$fieldtitles{$type});
 7757:                         }
 7758:                         my $newvaluestr;
 7759:                         if (@newvalues > 0) {
 7760:                             $newvaluestr = join(', ',@newvalues);
 7761:                         } else {
 7762:                             $newvaluestr = &mt('none');
 7763:                         }
 7764:                         if ($item eq 'default') {
 7765:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7766:                         } else {
 7767:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7768:                         }
 7769:                     }
 7770:                 } else {
 7771:                     my $newvalue;
 7772:                     if ($key eq 'run') {
 7773:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7774:                     } else {
 7775:                         $newvalue = $offon[$env{'form.'.$key}];
 7776:                     }
 7777:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7778:                 }
 7779:             }
 7780:             $resulttext .= '</ul>';
 7781:         } else {
 7782:             $resulttext = &mt('No changes made to autoupdates');
 7783:         }
 7784:     } else {
 7785:         $resulttext = '<span class="LC_error">'.
 7786: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7787:     }
 7788:     return $resulttext;
 7789: }
 7790: 
 7791: sub modify_autocreate {
 7792:     my ($dom,%domconfig) = @_;
 7793:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7794:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7795:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7796:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7797:         }
 7798:     }
 7799:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7800:                  req => 'Auto-creation of validated requests for official courses',
 7801:                  xmldc => 'Identity of course creator of courses from XML files',
 7802:                );
 7803:     my @types = ('xml','req');
 7804:     foreach my $item (@types) {
 7805:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7806:         $newvals{$item} =~ s/\D//g;
 7807:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7808:     }
 7809:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7810:     my %domcoords = &get_active_dcs($dom);
 7811:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7812:         $newvals{'xmldc'} = '';
 7813:     } 
 7814:     %autocreatehash =  (
 7815:                         autocreate => { xml => $newvals{'xml'},
 7816:                                         req => $newvals{'req'},
 7817:                                       }
 7818:                        );
 7819:     if ($newvals{'xmldc'} ne '') {
 7820:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7821:     }
 7822:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7823:                                              $dom);
 7824:     if ($putresult eq 'ok') {
 7825:         my @items = @types;
 7826:         if ($newvals{'xml'}) {
 7827:             push(@items,'xmldc');
 7828:         }
 7829:         foreach my $item (@items) {
 7830:             if (exists($currautocreate{$item})) {
 7831:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7832:                     $changes{$item} = 1;
 7833:                 }
 7834:             } elsif ($newvals{$item}) {
 7835:                 $changes{$item} = 1;
 7836:             }
 7837:         }
 7838:         if (keys(%changes) > 0) {
 7839:             my @offon = ('off','on'); 
 7840:             $resulttext = &mt('Changes made:').'<ul>';
 7841:             foreach my $item (@types) {
 7842:                 if ($changes{$item}) {
 7843:                     my $newtxt = $offon[$newvals{$item}];
 7844:                     $resulttext .= '<li>'.
 7845:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7846:                                        '<b>','</b>').
 7847:                                    '</li>';
 7848:                 }
 7849:             }
 7850:             if ($changes{'xmldc'}) {
 7851:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7852:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7853:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7854:             }
 7855:             $resulttext .= '</ul>';
 7856:         } else {
 7857:             $resulttext = &mt('No changes made to auto-creation settings');
 7858:         }
 7859:     } else {
 7860:         $resulttext = '<span class="LC_error">'.
 7861:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7862:     }
 7863:     return $resulttext;
 7864: }
 7865: 
 7866: sub modify_directorysrch {
 7867:     my ($dom,%domconfig) = @_;
 7868:     my ($resulttext,%changes);
 7869:     my %currdirsrch;
 7870:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7871:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7872:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7873:         }
 7874:     }
 7875:     my %title = ( available => 'Directory search available',
 7876:                   localonly => 'Other domains can search',
 7877:                   searchby => 'Search types',
 7878:                   searchtypes => 'Search latitude');
 7879:     my @offon = ('off','on');
 7880:     my @otherdoms = ('Yes','No');
 7881: 
 7882:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7883:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7884:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7885: 
 7886:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7887:     if (keys(%{$usertypes}) == 0) {
 7888:         @cansearch = ('default');
 7889:     } else {
 7890:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7891:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7892:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7893:                     push(@{$changes{'cansearch'}},$type);
 7894:                 }
 7895:             }
 7896:             foreach my $type (@cansearch) {
 7897:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7898:                     push(@{$changes{'cansearch'}},$type);
 7899:                 }
 7900:             }
 7901:         } else {
 7902:             push(@{$changes{'cansearch'}},@cansearch);
 7903:         }
 7904:     }
 7905: 
 7906:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7907:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7908:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7909:                 push(@{$changes{'searchby'}},$by);
 7910:             }
 7911:         }
 7912:         foreach my $by (@searchby) {
 7913:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7914:                 push(@{$changes{'searchby'}},$by);
 7915:             }
 7916:         }
 7917:     } else {
 7918:         push(@{$changes{'searchby'}},@searchby);
 7919:     }
 7920: 
 7921:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7922:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7923:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7924:                 push(@{$changes{'searchtypes'}},$type);
 7925:             }
 7926:         }
 7927:         foreach my $type (@searchtypes) {
 7928:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7929:                 push(@{$changes{'searchtypes'}},$type);
 7930:             }
 7931:         }
 7932:     } else {
 7933:         if (exists($currdirsrch{'searchtypes'})) {
 7934:             foreach my $type (@searchtypes) {  
 7935:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7936:                     push(@{$changes{'searchtypes'}},$type);
 7937:                 }
 7938:             }
 7939:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7940:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7941:             }   
 7942:         } else {
 7943:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7944:         }
 7945:     }
 7946: 
 7947:     my %dirsrch_hash =  (
 7948:             directorysrch => { available => $env{'form.dirsrch_available'},
 7949:                                cansearch => \@cansearch,
 7950:                                localonly => $env{'form.dirsrch_localonly'},
 7951:                                searchby => \@searchby,
 7952:                                searchtypes => \@searchtypes,
 7953:                              }
 7954:             );
 7955:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7956:                                              $dom);
 7957:     if ($putresult eq 'ok') {
 7958:         if (exists($currdirsrch{'available'})) {
 7959:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7960:                  $changes{'available'} = 1;
 7961:              }
 7962:         } else {
 7963:             if ($env{'form.dirsrch_available'} eq '1') {
 7964:                 $changes{'available'} = 1;
 7965:             }
 7966:         }
 7967:         if (exists($currdirsrch{'localonly'})) {
 7968:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7969:                  $changes{'localonly'} = 1;
 7970:              }
 7971:         } else {
 7972:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7973:                 $changes{'localonly'} = 1;
 7974:             }
 7975:         }
 7976:         if (keys(%changes) > 0) {
 7977:             $resulttext = &mt('Changes made:').'<ul>';
 7978:             if ($changes{'available'}) {
 7979:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7980:             }
 7981:             if ($changes{'localonly'}) {
 7982:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7983:             }
 7984: 
 7985:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7986:                 my $chgtext;
 7987:                 if (ref($usertypes) eq 'HASH') {
 7988:                     if (keys(%{$usertypes}) > 0) {
 7989:                         foreach my $type (@{$types}) {
 7990:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7991:                                 $chgtext .= $usertypes->{$type}.'; ';
 7992:                             }
 7993:                         }
 7994:                         if (grep(/^default$/,@cansearch)) {
 7995:                             $chgtext .= $othertitle;
 7996:                         } else {
 7997:                             $chgtext =~ s/\; $//;
 7998:                         }
 7999:                         $resulttext .=
 8000:                             '<li>'.
 8001:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 8002:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 8003:                             '</li>';
 8004:                     }
 8005:                 }
 8006:             }
 8007:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 8008:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 8009:                 my $chgtext;
 8010:                 foreach my $type (@{$titleorder}) {
 8011:                     if (grep(/^\Q$type\E$/,@searchby)) {
 8012:                         if (defined($searchtitles->{$type})) {
 8013:                             $chgtext .= $searchtitles->{$type}.'; ';
 8014:                         }
 8015:                     }
 8016:                 }
 8017:                 $chgtext =~ s/\; $//;
 8018:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 8019:             }
 8020:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 8021:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 8022:                 my $chgtext;
 8023:                 foreach my $type (@{$srchtypeorder}) {
 8024:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 8025:                         if (defined($srchtypes_desc->{$type})) {
 8026:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 8027:                         }
 8028:                     }
 8029:                 }
 8030:                 $chgtext =~ s/\; $//;
 8031:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 8032:             }
 8033:             $resulttext .= '</ul>';
 8034:         } else {
 8035:             $resulttext = &mt('No changes made to institution directory search settings');
 8036:         }
 8037:     } else {
 8038:         $resulttext = '<span class="LC_error">'.
 8039:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 8040:     }
 8041:     return $resulttext;
 8042: }
 8043: 
 8044: sub modify_contacts {
 8045:     my ($dom,$lastactref,%domconfig) = @_;
 8046:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 8047:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 8048:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 8049:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 8050:         }
 8051:     }
 8052:     my (%others,%to,%bcc);
 8053:     my @contacts = ('supportemail','adminemail');
 8054:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 8055:                     'requestsmail','updatesmail','idconflictsmail');
 8056:     my @toggles = ('reporterrors','reportupdates');
 8057:     foreach my $type (@mailings) {
 8058:         @{$newsetting{$type}} = 
 8059:             &Apache::loncommon::get_env_multiple('form.'.$type);
 8060:         foreach my $item (@contacts) {
 8061:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 8062:                 $contacts_hash{contacts}{$type}{$item} = 1;
 8063:             } else {
 8064:                 $contacts_hash{contacts}{$type}{$item} = 0;
 8065:             }
 8066:         }  
 8067:         $others{$type} = $env{'form.'.$type.'_others'};
 8068:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 8069:         if ($type eq 'helpdeskmail') {
 8070:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 8071:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 8072:         }
 8073:     }
 8074:     foreach my $item (@contacts) {
 8075:         $to{$item} = $env{'form.'.$item};
 8076:         $contacts_hash{'contacts'}{$item} = $to{$item};
 8077:     }
 8078:     foreach my $item (@toggles) {
 8079:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 8080:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 8081:         }
 8082:     }
 8083:     if (keys(%currsetting) > 0) {
 8084:         foreach my $item (@contacts) {
 8085:             if ($to{$item} ne $currsetting{$item}) {
 8086:                 $changes{$item} = 1;
 8087:             }
 8088:         }
 8089:         foreach my $type (@mailings) {
 8090:             foreach my $item (@contacts) {
 8091:                 if (ref($currsetting{$type}) eq 'HASH') {
 8092:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 8093:                         push(@{$changes{$type}},$item);
 8094:                     }
 8095:                 } else {
 8096:                     push(@{$changes{$type}},@{$newsetting{$type}});
 8097:                 }
 8098:             }
 8099:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 8100:                 push(@{$changes{$type}},'others');
 8101:             }
 8102:             if ($type eq 'helpdeskmail') {   
 8103:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 8104:                     push(@{$changes{$type}},'bcc'); 
 8105:                 }
 8106:             }
 8107:         }
 8108:     } else {
 8109:         my %default;
 8110:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 8111:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 8112:         $default{'errormail'} = 'adminemail';
 8113:         $default{'packagesmail'} = 'adminemail';
 8114:         $default{'helpdeskmail'} = 'supportemail';
 8115:         $default{'lonstatusmail'} = 'adminemail';
 8116:         $default{'requestsmail'} = 'adminemail';
 8117:         $default{'updatesmail'} = 'adminemail';
 8118:         foreach my $item (@contacts) {
 8119:            if ($to{$item} ne $default{$item}) {
 8120:               $changes{$item} = 1;
 8121:            }
 8122:         }
 8123:         foreach my $type (@mailings) {
 8124:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 8125:                
 8126:                 push(@{$changes{$type}},@{$newsetting{$type}});
 8127:             }
 8128:             if ($others{$type} ne '') {
 8129:                 push(@{$changes{$type}},'others');
 8130:             }
 8131:             if ($type eq 'helpdeskmail') {
 8132:                 if ($bcc{$type} ne '') {
 8133:                     push(@{$changes{$type}},'bcc');
 8134:                 }
 8135:             }
 8136:         }
 8137:     }
 8138:     foreach my $item (@toggles) {
 8139:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 8140:             $changes{$item} = 1;
 8141:         } elsif ((!$env{'form.'.$item}) &&
 8142:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 8143:             $changes{$item} = 1;
 8144:         }
 8145:     }
 8146:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 8147:                                              $dom);
 8148:     if ($putresult eq 'ok') {
 8149:         if (keys(%changes) > 0) {
 8150:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8151:             if (ref($lastactref) eq 'HASH') {
 8152:                 $lastactref->{'domainconfig'} = 1;
 8153:             }
 8154:             my ($titles,$short_titles)  = &contact_titles();
 8155:             $resulttext = &mt('Changes made:').'<ul>';
 8156:             foreach my $item (@contacts) {
 8157:                 if ($changes{$item}) {
 8158:                     $resulttext .= '<li>'.$titles->{$item}.
 8159:                                     &mt(' set to: ').
 8160:                                     '<span class="LC_cusr_emph">'.
 8161:                                     $to{$item}.'</span></li>';
 8162:                 }
 8163:             }
 8164:             foreach my $type (@mailings) {
 8165:                 if (ref($changes{$type}) eq 'ARRAY') {
 8166:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 8167:                     my @text;
 8168:                     foreach my $item (@{$newsetting{$type}}) {
 8169:                         push(@text,$short_titles->{$item});
 8170:                     }
 8171:                     if ($others{$type} ne '') {
 8172:                         push(@text,$others{$type});
 8173:                     }
 8174:                     $resulttext .= '<span class="LC_cusr_emph">'.
 8175:                                    join(', ',@text).'</span>';
 8176:                     if ($type eq 'helpdeskmail') {
 8177:                         if ($bcc{$type} ne '') {
 8178:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 8179:                         }
 8180:                     }
 8181:                     $resulttext .= '</li>';
 8182:                 }
 8183:             }
 8184:             my @offon = ('off','on');
 8185:             if ($changes{'reporterrors'}) {
 8186:                 $resulttext .= '<li>'.
 8187:                                &mt('E-mail error reports to [_1] set to "'.
 8188:                                    $offon[$env{'form.reporterrors'}].'".',
 8189:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8190:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 8191:                                '</li>';
 8192:             }
 8193:             if ($changes{'reportupdates'}) {
 8194:                 $resulttext .= '<li>'.
 8195:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 8196:                                     $offon[$env{'form.reportupdates'}].'".',
 8197:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8198:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 8199:                                 '</li>';
 8200:             }
 8201:             $resulttext .= '</ul>';
 8202:         } else {
 8203:             $resulttext = &mt('No changes made to contact information');
 8204:         }
 8205:     } else {
 8206:         $resulttext = '<span class="LC_error">'.
 8207:             &mt('An error occurred: [_1].',$putresult).'</span>';
 8208:     }
 8209:     return $resulttext;
 8210: }
 8211: 
 8212: sub modify_usercreation {
 8213:     my ($dom,%domconfig) = @_;
 8214:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 8215:     my $warningmsg;
 8216:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8217:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8218:             if ($key eq 'cancreate') {
 8219:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8220:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8221:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8222:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 8223:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8224:                         } else {
 8225:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8226:                         }
 8227:                     }
 8228:                 }
 8229:             } elsif ($key eq 'email_rule') {
 8230:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8231:             } else {
 8232:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8233:             }
 8234:         }
 8235:     }
 8236:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 8237:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 8238:     my @contexts = ('author','course','requestcrs');
 8239:     foreach my $item(@contexts) {
 8240:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 8241:     }
 8242:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8243:         foreach my $item (@contexts) {
 8244:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 8245:                 push(@{$changes{'cancreate'}},$item);
 8246:             }
 8247:         }
 8248:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 8249:         foreach my $item (@contexts) {
 8250:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 8251:                 if ($cancreate{$item} ne 'any') {
 8252:                     push(@{$changes{'cancreate'}},$item);
 8253:                 }
 8254:             } else {
 8255:                 if ($cancreate{$item} ne 'none') {
 8256:                     push(@{$changes{'cancreate'}},$item);
 8257:                 }
 8258:             }
 8259:         }
 8260:     } else {
 8261:         foreach my $item (@contexts)  {
 8262:             push(@{$changes{'cancreate'}},$item);
 8263:         }
 8264:     }
 8265: 
 8266:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 8267:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 8268:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 8269:                 push(@{$changes{'username_rule'}},$type);
 8270:             }
 8271:         }
 8272:         foreach my $type (@username_rule) {
 8273:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 8274:                 push(@{$changes{'username_rule'}},$type);
 8275:             }
 8276:         }
 8277:     } else {
 8278:         push(@{$changes{'username_rule'}},@username_rule);
 8279:     }
 8280: 
 8281:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 8282:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 8283:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 8284:                 push(@{$changes{'id_rule'}},$type);
 8285:             }
 8286:         }
 8287:         foreach my $type (@id_rule) {
 8288:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 8289:                 push(@{$changes{'id_rule'}},$type);
 8290:             }
 8291:         }
 8292:     } else {
 8293:         push(@{$changes{'id_rule'}},@id_rule);
 8294:     }
 8295: 
 8296:     my @authen_contexts = ('author','course','domain');
 8297:     my @authtypes = ('int','krb4','krb5','loc');
 8298:     my %authhash;
 8299:     foreach my $item (@authen_contexts) {
 8300:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 8301:         foreach my $auth (@authtypes) {
 8302:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 8303:                 $authhash{$item}{$auth} = 1;
 8304:             } else {
 8305:                 $authhash{$item}{$auth} = 0;
 8306:             }
 8307:         }
 8308:     }
 8309:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 8310:         foreach my $item (@authen_contexts) {
 8311:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 8312:                 foreach my $auth (@authtypes) {
 8313:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 8314:                         push(@{$changes{'authtypes'}},$item);
 8315:                         last;
 8316:                     }
 8317:                 }
 8318:             }
 8319:         }
 8320:     } else {
 8321:         foreach my $item (@authen_contexts) {
 8322:             push(@{$changes{'authtypes'}},$item);
 8323:         }
 8324:     }
 8325: 
 8326:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 8327:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 8328:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 8329:     $save_usercreate{'id_rule'} = \@id_rule;
 8330:     $save_usercreate{'username_rule'} = \@username_rule,
 8331:     $save_usercreate{'authtypes'} = \%authhash;
 8332: 
 8333:     my %usercreation_hash =  (
 8334:         usercreation     => \%save_usercreate,
 8335:     );
 8336: 
 8337:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 8338:                                              $dom);
 8339: 
 8340:     if ($putresult eq 'ok') {
 8341:         if (keys(%changes) > 0) {
 8342:             $resulttext = &mt('Changes made:').'<ul>';
 8343:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8344:                 my %lt = &usercreation_types();
 8345:                 foreach my $type (@{$changes{'cancreate'}}) {
 8346:                     my $chgtext = $lt{$type}.', ';
 8347:                     if ($cancreate{$type} eq 'none') {
 8348:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 8349:                     } elsif ($cancreate{$type} eq 'any') {
 8350:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 8351:                     } elsif ($cancreate{$type} eq 'official') {
 8352:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 8353:                     } elsif ($cancreate{$type} eq 'unofficial') {
 8354:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 8355:                     }
 8356:                     $resulttext .= '<li>'.$chgtext.'</li>';
 8357:                 }
 8358:             }
 8359:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 8360:                 my ($rules,$ruleorder) = 
 8361:                     &Apache::lonnet::inst_userrules($dom,'username');
 8362:                 my $chgtext = '<ul>';
 8363:                 foreach my $type (@username_rule) {
 8364:                     if (ref($rules->{$type}) eq 'HASH') {
 8365:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 8366:                     }
 8367:                 }
 8368:                 $chgtext .= '</ul>';
 8369:                 if (@username_rule > 0) {
 8370:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 8371:                 } else {
 8372:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 8373:                 }
 8374:             }
 8375:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 8376:                 my ($idrules,$idruleorder) = 
 8377:                     &Apache::lonnet::inst_userrules($dom,'id');
 8378:                 my $chgtext = '<ul>';
 8379:                 foreach my $type (@id_rule) {
 8380:                     if (ref($idrules->{$type}) eq 'HASH') {
 8381:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8382:                     }
 8383:                 }
 8384:                 $chgtext .= '</ul>';
 8385:                 if (@id_rule > 0) {
 8386:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8387:                 } else {
 8388:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8389:                 }
 8390:             }
 8391:             my %authname = &authtype_names();
 8392:             my %context_title = &context_names();
 8393:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8394:                 my $chgtext = '<ul>';
 8395:                 foreach my $type (@{$changes{'authtypes'}}) {
 8396:                     my @allowed;
 8397:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8398:                     foreach my $auth (@authtypes) {
 8399:                         if ($authhash{$type}{$auth}) {
 8400:                             push(@allowed,$authname{$auth});
 8401:                         }
 8402:                     }
 8403:                     if (@allowed > 0) {
 8404:                         $chgtext .= join(', ',@allowed).'</li>';
 8405:                     } else {
 8406:                         $chgtext .= &mt('none').'</li>';
 8407:                     }
 8408:                 }
 8409:                 $chgtext .= '</ul>';
 8410:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8411:                 $resulttext .= '</li>';
 8412:             }
 8413:             $resulttext .= '</ul>';
 8414:         } else {
 8415:             $resulttext = &mt('No changes made to user creation settings');
 8416:         }
 8417:     } else {
 8418:         $resulttext = '<span class="LC_error">'.
 8419:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8420:     }
 8421:     if ($warningmsg ne '') {
 8422:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8423:     }
 8424:     return $resulttext;
 8425: }
 8426: 
 8427: sub modify_selfcreation {
 8428:     my ($dom,%domconfig) = @_;
 8429:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8430:     my (%save_usercreate,%save_usermodify);
 8431:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8432:     if (ref($types) eq 'ARRAY') {
 8433:         $usertypes->{'default'} = $othertitle;
 8434:         push(@{$types},'default');
 8435:     }
 8436: #
 8437: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8438: #
 8439:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8440:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8441:             if ($key eq 'cancreate') {
 8442:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8443:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8444:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8445:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8446:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 8447:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 8448:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8449:                         } else {
 8450:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8451:                         }
 8452:                     }
 8453:                 }
 8454:             } elsif ($key eq 'email_rule') {
 8455:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8456:             } else {
 8457:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8458:             }
 8459:         }
 8460:     }
 8461: #
 8462: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8463: #
 8464:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8465:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8466:             if ($key eq 'selfcreate') {
 8467:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8468:             } else {
 8469:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8470:             }
 8471:         }
 8472:     }
 8473: 
 8474:     my @contexts = ('selfcreate');
 8475:     @{$cancreate{'selfcreate'}} = ();
 8476:     %{$cancreate{'emailusername'}} = ();
 8477:     @{$cancreate{'statustocreate'}} = ();
 8478:     %{$cancreate{'selfcreateprocessing'}} = ();
 8479:     %{$cancreate{'shibenv'}} = ();
 8480:     my %selfcreatetypes = (
 8481:                              sso   => 'users authenticated by institutional single sign on',
 8482:                              login => 'users authenticated by institutional log-in',
 8483:                              email => 'users who provide a valid e-mail address for use as username',
 8484:                           );
 8485: #
 8486: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8487: # is permitted.
 8488: #
 8489: 
 8490:     my @statuses;
 8491:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 8492:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 8493:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 8494:         }
 8495:     }
 8496:     push(@statuses,'default');
 8497: 
 8498:     foreach my $item ('login','sso','email') {
 8499:         if ($item eq 'email') {
 8500:             if ($env{'form.cancreate_email'}) {
 8501:                 push(@{$cancreate{'selfcreate'}},'email');
 8502:                 push(@contexts,'selfcreateprocessing');
 8503:                 foreach my $type (@statuses) {
 8504:                     if ($type eq 'default') {
 8505:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 8506:                     } else { 
 8507:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 8508:                     }
 8509:                 }
 8510:             }
 8511:         } else {
 8512:             if ($env{'form.cancreate_'.$item}) {
 8513:                 push(@{$cancreate{'selfcreate'}},$item);
 8514:             }
 8515:         }
 8516:     }
 8517:     my (@email_rule,%userinfo,%savecaptcha);
 8518:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8519: #
 8520: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8521: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8522: #
 8523: 
 8524:     if ($env{'form.cancreate_email'}) {
 8525:         push(@contexts,'emailusername');
 8526:         if (ref($types) eq 'ARRAY') {
 8527:             foreach my $type (@{$types}) {
 8528:                 if (ref($infofields) eq 'ARRAY') {
 8529:                     foreach my $field (@{$infofields}) {
 8530:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8531:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8532:                         }
 8533:                     }
 8534:                 }
 8535:             }
 8536:         }
 8537: #
 8538: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8539: # queued requests for self-creation of account using e-mail address as username
 8540: #
 8541: 
 8542:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8543:         @approvalnotify = sort(@approvalnotify);
 8544:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8545:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8546:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8547:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8548:                     push(@{$changes{'cancreate'}},'notify');
 8549:                 }
 8550:             } else {
 8551:                 if ($cancreate{'notify'}{'approval'}) {
 8552:                     push(@{$changes{'cancreate'}},'notify');
 8553:                 }
 8554:             }
 8555:         } elsif ($cancreate{'notify'}{'approval'}) {
 8556:             push(@{$changes{'cancreate'}},'notify');
 8557:         }
 8558: 
 8559: #
 8560: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8561: #
 8562:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8563:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8564:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8565:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8566:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8567:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8568:                         push(@{$changes{'email_rule'}},$type);
 8569:                     }
 8570:                 }
 8571:             }
 8572:             if (@email_rule > 0) {
 8573:                 foreach my $type (@email_rule) {
 8574:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8575:                         push(@{$changes{'email_rule'}},$type);
 8576:                     }
 8577:                 }
 8578:             }
 8579:         } elsif (@email_rule > 0) {
 8580:             push(@{$changes{'email_rule'}},@email_rule);
 8581:         }
 8582:     }
 8583: #  
 8584: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 8585: # institutional log-in.
 8586: #
 8587:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8588:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8589:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8590:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8591:             $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.').' '.
 8592:                           &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.');
 8593:         }
 8594:     }
 8595:     my @fields = ('lastname','firstname','middlename','generation',
 8596:                   'permanentemail','id');
 8597:     my @shibfields = (@fields,'inststatus');
 8598:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8599: #
 8600: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8601: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8602: # may self-create accounts 
 8603: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8604: # which the user may supply, if institutional data is unavailable.
 8605: #
 8606:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8607:         if (ref($types) eq 'ARRAY') {
 8608:             if (@{$types} > 1) {
 8609:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8610:                 push(@contexts,'statustocreate');
 8611:             } else {
 8612:                 undef($cancreate{'statustocreate'});
 8613:             } 
 8614:             foreach my $type (@{$types}) {
 8615:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8616:                 foreach my $field (@fields) {
 8617:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8618:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8619:                     } else {
 8620:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8621:                     }
 8622:                 }
 8623:             }
 8624:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8625:                 foreach my $type (@{$types}) {
 8626:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8627:                         foreach my $field (@fields) {
 8628:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8629:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8630:                                 push(@{$changes{'selfcreate'}},$type);
 8631:                                 last;
 8632:                             }
 8633:                         }
 8634:                     }
 8635:                 }
 8636:             } else {
 8637:                 foreach my $type (@{$types}) {
 8638:                     push(@{$changes{'selfcreate'}},$type);
 8639:                 }
 8640:             }
 8641:         }
 8642:         foreach my $field (@shibfields) {
 8643:             if ($env{'form.shibenv_'.$field} ne '') {
 8644:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 8645:             }
 8646:         }
 8647:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8648:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 8649:                 foreach my $field (@shibfields) {
 8650:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 8651:                         push(@{$changes{'cancreate'}},'shibenv');
 8652:                     }
 8653:                 }
 8654:             } else {
 8655:                 foreach my $field (@shibfields) {
 8656:                     if ($env{'form.shibenv_'.$field}) {
 8657:                         push(@{$changes{'cancreate'}},'shibenv');
 8658:                         last;
 8659:                     }
 8660:                 }
 8661:             }
 8662:         }
 8663:     }
 8664:     foreach my $item (@contexts) {
 8665:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 8666:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 8667:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 8668:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 8669:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8670:                             push(@{$changes{'cancreate'}},$item);
 8671:                         }
 8672:                     }
 8673:                 }
 8674:             }
 8675:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8676:                 foreach my $type (@{$cancreate{$item}}) {
 8677:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 8678:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8679:                             push(@{$changes{'cancreate'}},$item);
 8680:                         }
 8681:                     }
 8682:                 }
 8683:             }
 8684:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 8685:             if (ref($cancreate{$item}) eq 'HASH') {
 8686:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 8687:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 8688:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 8689:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 8690:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8691:                                     push(@{$changes{'cancreate'}},$item);
 8692:                                 }
 8693:                             }
 8694:                         }
 8695:                     } elsif ($item eq 'selfcreateprocessing') {
 8696:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 8697:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8698:                                 push(@{$changes{'cancreate'}},$item);
 8699:                             }
 8700:                         }
 8701:                     } else {
 8702:                         if (!$cancreate{$item}{$curr}) {
 8703:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8704:                                 push(@{$changes{'cancreate'}},$item);
 8705:                             }
 8706:                         }
 8707:                     }
 8708:                 }
 8709:                 foreach my $field (keys(%{$cancreate{$item}})) {
 8710:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 8711:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 8712:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 8713:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 8714:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8715:                                         push(@{$changes{'cancreate'}},$item);
 8716:                                     }
 8717:                                 }
 8718:                             } else {
 8719:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8720:                                     push(@{$changes{'cancreate'}},$item);
 8721:                                 }
 8722:                             }
 8723:                         }
 8724:                     } elsif ($item eq 'selfcreateprocessing') {
 8725:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 8726:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8727:                                 push(@{$changes{'cancreate'}},$item);
 8728:                             }
 8729:                         }
 8730:                     } else {
 8731:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 8732:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8733:                                 push(@{$changes{'cancreate'}},$item);
 8734:                             }
 8735:                         }
 8736:                     }
 8737:                 }
 8738:             }
 8739:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 8740:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8741:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 8742:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8743:                         push(@{$changes{'cancreate'}},$item);
 8744:                     }
 8745:                 }
 8746:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 8747:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 8748:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8749:                         push(@{$changes{'cancreate'}},$item);
 8750:                     }
 8751:                 }
 8752:             }
 8753:         } elsif ($item eq 'emailusername') {
 8754:             if (ref($cancreate{$item}) eq 'HASH') {
 8755:                 foreach my $type (keys(%{$cancreate{$item}})) {
 8756:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 8757:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 8758:                             if ($cancreate{$item}{$type}{$field}) {
 8759:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8760:                                     push(@{$changes{'cancreate'}},$item);
 8761:                                 }
 8762:                                 last;
 8763:                             }
 8764:                         }
 8765:                     }
 8766:                 }
 8767:             }
 8768:         }
 8769:     }
 8770: #
 8771: # Populate %save_usercreate hash with updates to self-creation configuration.
 8772: #
 8773:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 8774:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 8775:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 8776:     if (ref($cancreate{'notify'}) eq 'HASH') {
 8777:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 8778:     }
 8779:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
 8780:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
 8781:     }
 8782:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8783:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 8784:     }
 8785:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
 8786:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
 8787:     }
 8788:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 8789:     $save_usercreate{'emailrule'} = \@email_rule;
 8790: 
 8791:     my %userconfig_hash = (
 8792:             usercreation     => \%save_usercreate,
 8793:             usermodification => \%save_usermodify,
 8794:     );
 8795:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8796:                                              $dom);
 8797: #
 8798: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8799: #
 8800:     if ($putresult eq 'ok') {
 8801:         if (keys(%changes) > 0) {
 8802:             $resulttext = &mt('Changes made:').'<ul>';
 8803:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8804:                 my %lt = &selfcreation_types();
 8805:                 foreach my $type (@{$changes{'cancreate'}}) {
 8806:                     my $chgtext;
 8807:                     if ($type eq 'selfcreate') {
 8808:                         if (@{$cancreate{$type}} == 0) {
 8809:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 8810:                         } else {
 8811:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 8812:                                         '<ul>';
 8813:                             foreach my $case (@{$cancreate{$type}}) {
 8814:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 8815:                             }
 8816:                             $chgtext .= '</ul>';
 8817:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 8818:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 8819:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8820:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 8821:                                             $chgtext .= '<br />'.
 8822:                                                         '<span class="LC_warning">'.
 8823:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8824:                                                         '</span>';
 8825:                                         }
 8826:                                     }
 8827:                                 }
 8828:                             }
 8829:                         }
 8830:                     } elsif ($type eq 'shibenv') {
 8831:                         if (keys(%{$cancreate{$type}}) == 0) {
 8832:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
 8833:                         } else {
 8834:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
 8835:                                         '<ul>';
 8836:                             foreach my $field (@shibfields) {
 8837:                                 next if ($cancreate{$type}{$field} eq '');
 8838:                                 if ($field eq 'inststatus') {
 8839:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
 8840:                                 } else {
 8841:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
 8842:                                 }
 8843:                             }
 8844:                             $chgtext .= '</ul>';
 8845:                         }  
 8846:                     } elsif ($type eq 'statustocreate') {
 8847:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 8848:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 8849:                             if (@{$cancreate{'selfcreate'}} > 0) {
 8850:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 8851:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 8852:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 8853:                                         $chgtext .= '<br />'.
 8854:                                                     '<span class="LC_warning">'.
 8855:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8856:                                                     '</span>';
 8857:                                     }
 8858:                                 } elsif (ref($usertypes) eq 'HASH') {
 8859:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8860:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 8861:                                     } else {
 8862:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 8863:                                     }
 8864:                                     $chgtext .= '<ul>';
 8865:                                     foreach my $case (@{$cancreate{$type}}) {
 8866:                                         if ($case eq 'default') {
 8867:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 8868:                                         } else {
 8869:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 8870:                                         }
 8871:                                     }
 8872:                                     $chgtext .= '</ul>';
 8873:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8874:                                         $chgtext .= '<br /><span class="LC_warning">'.
 8875:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 8876:                                                     '</span>';
 8877:                                     }
 8878:                                 }
 8879:                             } else {
 8880:                                 if (@{$cancreate{$type}} == 0) {
 8881:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8882:                                 } else {
 8883:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8884:                                 }
 8885:                             }
 8886:                         }
 8887:                     } elsif ($type eq 'selfcreateprocessing') {
 8888:                         my %choices = &Apache::lonlocal::texthash (
 8889:                                                                     automatic => 'Automatic approval',
 8890:                                                                     approval  => 'Queued for approval',
 8891:                                                                   );
 8892:                         if (@statuses > 1) {
 8893:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
 8894:                                         '<ul>';
 8895:                            foreach my $type (@statuses) {
 8896:                                if ($type eq 'default') {
 8897:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8898:                                } else {
 8899:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8900:                                }
 8901:                            }
 8902:                            $chgtext .= '</ul>';
 8903:                         } else {
 8904:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
 8905:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
 8906:                         }
 8907:                     } elsif ($type eq 'captcha') {
 8908:                         if ($savecaptcha{$type} eq 'notused') {
 8909:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8910:                         } else {
 8911:                             my %captchas = &captcha_phrases();
 8912:                             if ($captchas{$savecaptcha{$type}}) {
 8913:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8914:                             } else {
 8915:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8916:                             }
 8917:                         }
 8918:                     } elsif ($type eq 'recaptchakeys') {
 8919:                         my ($privkey,$pubkey);
 8920:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8921:                             $pubkey = $savecaptcha{$type}{'public'};
 8922:                             $privkey = $savecaptcha{$type}{'private'};
 8923:                         }
 8924:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8925:                         if (!$pubkey) {
 8926:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8927:                         } else {
 8928:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8929:                         }
 8930:                         if (!$privkey) {
 8931:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8932:                         } else {
 8933:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8934:                         }
 8935:                         $chgtext .= '</ul>';
 8936:                     } elsif ($type eq 'emailusername') {
 8937:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8938:                             if (ref($types) eq 'ARRAY') {
 8939:                                 foreach my $type (@{$types}) {
 8940:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 8941:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 8942:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
 8943:                                                     '<ul>';
 8944:                                             foreach my $field (@{$infofields}) {
 8945:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 8946:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8947:                                                 }
 8948:                                             }
 8949:                                             $chgtext .= '</ul>';
 8950:                                         } else {
 8951:                                             $chgtext .= &mt('When self creating account with e-mail as username, no information besides e-mail address will be provided by [_1].',"'$usertypes->{$type}'").'<br />';
 8952:                                         }
 8953:                                     } else {
 8954:                                         $chgtext .= &mt('When self creating account with e-mail as username, no information besides e-mail address will be provided by [_1].',"'$usertypes->{$type}'").'<br />';
 8955:                                     }
 8956:                                 }
 8957:                             }
 8958:                         }
 8959:                     } elsif ($type eq 'notify') {
 8960:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8961:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8962:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8963:                                 if ($cancreate{'notify'}{'approval'}) {
 8964:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8965:                                 }
 8966:                             }
 8967:                         }
 8968:                     }
 8969:                     if ($chgtext) {
 8970:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8971:                     }
 8972:                 }
 8973:             }
 8974:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8975:                 my ($emailrules,$emailruleorder) =
 8976:                     &Apache::lonnet::inst_userrules($dom,'email');
 8977:                 my $chgtext = '<ul>';
 8978:                 foreach my $type (@email_rule) {
 8979:                     if (ref($emailrules->{$type}) eq 'HASH') {
 8980:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 8981:                     }
 8982:                 }
 8983:                 $chgtext .= '</ul>';
 8984:                 if (@email_rule > 0) {
 8985:                     $resulttext .= '<li>'.
 8986:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 8987:                                        $chgtext.
 8988:                                    '</li>';
 8989:                 } else {
 8990:                     $resulttext .= '<li>'.
 8991:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 8992:                                    '</li>';
 8993:                 }
 8994:             }
 8995:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 8996:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 8997:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8998:                 foreach my $type (@{$changes{'selfcreate'}}) {
 8999:                     my $typename = $type;
 9000:                     if (ref($usertypes) eq 'HASH') {
 9001:                         if ($usertypes->{$type} ne '') {
 9002:                             $typename = $usertypes->{$type};
 9003:                         }
 9004:                     }
 9005:                     my @modifiable;
 9006:                     $resulttext .= '<li>'.
 9007:                                     &mt('Self-creation of account by users with status: [_1]',
 9008:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 9009:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 9010:                     foreach my $field (@fields) {
 9011:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 9012:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 9013:                         }
 9014:                     }
 9015:                     if (@modifiable > 0) {
 9016:                         $resulttext .= join(', ',@modifiable);
 9017:                     } else {
 9018:                         $resulttext .= &mt('none');
 9019:                     }
 9020:                     $resulttext .= '</li>';
 9021:                 }
 9022:                 $resulttext .= '</ul></li>';
 9023:             }
 9024:             $resulttext .= '</ul>';
 9025:         } else {
 9026:             $resulttext = &mt('No changes made to self-creation settings');
 9027:         }
 9028:     } else {
 9029:         $resulttext = '<span class="LC_error">'.
 9030:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9031:     }
 9032:     if ($warningmsg ne '') {
 9033:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9034:     }
 9035:     return $resulttext;
 9036: }
 9037: 
 9038: sub process_captcha {
 9039:     my ($container,$changes,$newsettings,$current) = @_;
 9040:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 9041:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 9042:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 9043:         $newsettings->{'captcha'} = 'original';
 9044:     }
 9045:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 9046:         if ($container eq 'cancreate') {
 9047:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9048:                 push(@{$changes->{'cancreate'}},'captcha');
 9049:             } elsif (!defined($changes->{'cancreate'})) {
 9050:                 $changes->{'cancreate'} = ['captcha'];
 9051:             }
 9052:         } else {
 9053:             $changes->{'captcha'} = 1;
 9054:         }
 9055:     }
 9056:     my ($newpub,$newpriv,$currpub,$currpriv);
 9057:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 9058:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 9059:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 9060:         $newpub =~ s/[^\w\-]//g;
 9061:         $newpriv =~ s/[^\w\-]//g;
 9062:         $newsettings->{'recaptchakeys'} = {
 9063:                                              public  => $newpub,
 9064:                                              private => $newpriv,
 9065:                                           };
 9066:     }
 9067:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 9068:         $currpub = $current->{'recaptchakeys'}{'public'};
 9069:         $currpriv = $current->{'recaptchakeys'}{'private'};
 9070:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 9071:             $newsettings->{'recaptchakeys'} = {
 9072:                                                  public  => '',
 9073:                                                  private => '',
 9074:                                               }
 9075:         }
 9076:     }
 9077:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 9078:         if ($container eq 'cancreate') {
 9079:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9080:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 9081:             } elsif (!defined($changes->{'cancreate'})) {
 9082:                 $changes->{'cancreate'} = ['recaptchakeys'];
 9083:             }
 9084:         } else {
 9085:             $changes->{'recaptchakeys'} = 1;
 9086:         }
 9087:     }
 9088:     return;
 9089: }
 9090: 
 9091: sub modify_usermodification {
 9092:     my ($dom,%domconfig) = @_;
 9093:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 9094:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9095:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9096:             if ($key eq 'selfcreate') {
 9097:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 9098:             } else {  
 9099:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 9100:             }
 9101:         }
 9102:     }
 9103:     my @contexts = ('author','course');
 9104:     my %context_title = (
 9105:                            author => 'In author context',
 9106:                            course => 'In course context',
 9107:                         );
 9108:     my @fields = ('lastname','firstname','middlename','generation',
 9109:                   'permanentemail','id');
 9110:     my %roles = (
 9111:                   author => ['ca','aa'],
 9112:                   course => ['st','ep','ta','in','cr'],
 9113:                 );
 9114:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9115:     foreach my $context (@contexts) {
 9116:         foreach my $role (@{$roles{$context}}) {
 9117:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 9118:             foreach my $item (@fields) {
 9119:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 9120:                     $modifyhash{$context}{$role}{$item} = 1;
 9121:                 } else {
 9122:                     $modifyhash{$context}{$role}{$item} = 0;
 9123:                 }
 9124:             }
 9125:         }
 9126:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 9127:             foreach my $role (@{$roles{$context}}) {
 9128:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 9129:                     foreach my $field (@fields) {
 9130:                         if ($modifyhash{$context}{$role}{$field} ne 
 9131:                                 $curr_usermodification{$context}{$role}{$field}) {
 9132:                             push(@{$changes{$context}},$role);
 9133:                             last;
 9134:                         }
 9135:                     }
 9136:                 }
 9137:             }
 9138:         } else {
 9139:             foreach my $context (@contexts) {
 9140:                 foreach my $role (@{$roles{$context}}) {
 9141:                     push(@{$changes{$context}},$role);
 9142:                 }
 9143:             }
 9144:         }
 9145:     }
 9146:     my %usermodification_hash =  (
 9147:                                    usermodification => \%modifyhash,
 9148:                                  );
 9149:     my $putresult = &Apache::lonnet::put_dom('configuration',
 9150:                                              \%usermodification_hash,$dom);
 9151:     if ($putresult eq 'ok') {
 9152:         if (keys(%changes) > 0) {
 9153:             $resulttext = &mt('Changes made: ').'<ul>';
 9154:             foreach my $context (@contexts) {
 9155:                 if (ref($changes{$context}) eq 'ARRAY') {
 9156:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 9157:                     if (ref($changes{$context}) eq 'ARRAY') {
 9158:                         foreach my $role (@{$changes{$context}}) {
 9159:                             my $rolename;
 9160:                             if ($role eq 'cr') {
 9161:                                 $rolename = &mt('Custom');
 9162:                             } else {
 9163:                                 $rolename = &Apache::lonnet::plaintext($role);
 9164:                             }
 9165:                             my @modifiable;
 9166:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 9167:                             foreach my $field (@fields) {
 9168:                                 if ($modifyhash{$context}{$role}{$field}) {
 9169:                                     push(@modifiable,$fieldtitles{$field});
 9170:                                 }
 9171:                             }
 9172:                             if (@modifiable > 0) {
 9173:                                 $resulttext .= join(', ',@modifiable);
 9174:                             } else {
 9175:                                 $resulttext .= &mt('none'); 
 9176:                             }
 9177:                             $resulttext .= '</li>';
 9178:                         }
 9179:                         $resulttext .= '</ul></li>';
 9180:                     }
 9181:                 }
 9182:             }
 9183:             $resulttext .= '</ul>';
 9184:         } else {
 9185:             $resulttext = &mt('No changes made to user modification settings');
 9186:         }
 9187:     } else {
 9188:         $resulttext = '<span class="LC_error">'.
 9189:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9190:     }
 9191:     return $resulttext;
 9192: }
 9193: 
 9194: sub modify_defaults {
 9195:     my ($dom,$lastactref,%domconfig) = @_;
 9196:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 9197:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9198:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 9199:     my @authtypes = ('internal','krb4','krb5','localauth');
 9200:     foreach my $item (@items) {
 9201:         $newvalues{$item} = $env{'form.'.$item};
 9202:         if ($item eq 'auth_def') {
 9203:             if ($newvalues{$item} ne '') {
 9204:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 9205:                     push(@errors,$item);
 9206:                 }
 9207:             }
 9208:         } elsif ($item eq 'lang_def') {
 9209:             if ($newvalues{$item} ne '') {
 9210:                 if ($newvalues{$item} =~ /^(\w+)/) {
 9211:                     my $langcode = $1;
 9212:                     if ($langcode ne 'x_chef') {
 9213:                         if (code2language($langcode) eq '') {
 9214:                             push(@errors,$item);
 9215:                         }
 9216:                     }
 9217:                 } else {
 9218:                     push(@errors,$item);
 9219:                 }
 9220:             }
 9221:         } elsif ($item eq 'timezone_def') {
 9222:             if ($newvalues{$item} ne '') {
 9223:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 9224:                     push(@errors,$item);   
 9225:                 }
 9226:             }
 9227:         } elsif ($item eq 'datelocale_def') {
 9228:             if ($newvalues{$item} ne '') {
 9229:                 my @datelocale_ids = DateTime::Locale->ids();
 9230:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 9231:                     push(@errors,$item);
 9232:                 }
 9233:             }
 9234:         } elsif ($item eq 'portal_def') {
 9235:             if ($newvalues{$item} ne '') {
 9236:                 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])\/?$/) {
 9237:                     push(@errors,$item);
 9238:                 }
 9239:             }
 9240:         }
 9241:         if (grep(/^\Q$item\E$/,@errors)) {
 9242:             $newvalues{$item} = $domdefaults{$item};
 9243:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 9244:             $changes{$item} = 1;
 9245:         }
 9246:         $domdefaults{$item} = $newvalues{$item};
 9247:     }
 9248:     my %defaults_hash = (
 9249:                          defaults => \%newvalues,
 9250:                         );
 9251:     my $title = &defaults_titles();
 9252: 
 9253:     my $currinststatus;
 9254:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9255:         $currinststatus = $domconfig{'inststatus'};
 9256:     } else {
 9257:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9258:         $currinststatus = {
 9259:                              inststatustypes => $usertypes,
 9260:                              inststatusorder => $types,
 9261:                              inststatusguest => [],
 9262:                           };
 9263:     }
 9264:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
 9265:     my @allpos;
 9266:     my %guests;
 9267:     my %alltypes;
 9268:     my ($currtitles,$currguests,$currorder);
 9269:     if (ref($currinststatus) eq 'HASH') {
 9270:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
 9271:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
 9272:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
 9273:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
 9274:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
 9275:                     }
 9276:                 }
 9277:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
 9278:                     my $position = $env{'form.inststatus_pos_'.$type};
 9279:                     $position =~ s/\D+//g;
 9280:                     $allpos[$position] = $type;
 9281:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
 9282:                     $alltypes{$type} =~ s/`//g;
 9283:                     if ($env{'form.inststatus_guest_'.$type}) {
 9284:                         $guests{$type} = 1;
 9285:                     }
 9286:                 }
 9287:             }
 9288:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
 9289:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
 9290:             }
 9291:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
 9292:             $currtitles =~ s/,$//;
 9293:         }
 9294:     }
 9295:     if ($env{'form.addinststatus'}) {
 9296:         my $newtype = $env{'form.addinststatus'};
 9297:         $newtype =~ s/\W//g;
 9298:         unless (exists($alltypes{$newtype})) {
 9299:             if ($env{'form.addinststatus_guest'}) {
 9300:                 $guests{$newtype} = 1;
 9301:             }
 9302:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
 9303:             $alltypes{$newtype} =~ s/`//g; 
 9304:             my $position = $env{'form.addinststatus_pos'};
 9305:             $position =~ s/\D+//g;
 9306:             if ($position ne '') {
 9307:                 $allpos[$position] = $newtype;
 9308:             }
 9309:         }
 9310:     }
 9311:     my (@orderedstatus,@orderedguests);
 9312:     foreach my $type (@allpos) {
 9313:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
 9314:             push(@orderedstatus,$type);
 9315:             if ($guests{$type}) {
 9316:                 push(@orderedguests,$type);
 9317:             }
 9318:         }
 9319:     }
 9320:     foreach my $type (keys(%alltypes)) {
 9321:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
 9322:             delete($alltypes{$type});
 9323:         }
 9324:     }
 9325:     $defaults_hash{'inststatus'} = {
 9326:                                      inststatustypes => \%alltypes,
 9327:                                      inststatusorder => \@orderedstatus,
 9328:                                      inststatusguest => \@orderedguests,
 9329:                                    };
 9330:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
 9331:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 9332:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
 9333:         }
 9334:     }
 9335:     if ($currorder ne join(',',@orderedstatus)) {
 9336:         $changes{'inststatus'}{'inststatusorder'} = 1;
 9337:     }
 9338:     if ($currguests ne join(',',@orderedguests)) {
 9339:         $changes{'inststatus'}{'inststatusguest'} = 1;
 9340:     }
 9341:     my $newtitles;
 9342:     foreach my $item (@orderedstatus) {
 9343:         $newtitles .= $alltypes{$item}.',';
 9344:     }
 9345:     $newtitles =~ s/,$//;
 9346:     if ($currtitles ne $newtitles) {
 9347:         $changes{'inststatus'}{'inststatustypes'} = 1;
 9348:     }
 9349:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 9350:                                              $dom);
 9351:     if ($putresult eq 'ok') {
 9352:         if (keys(%changes) > 0) {
 9353:             $resulttext = &mt('Changes made:').'<ul>';
 9354:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 9355:             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";
 9356:             foreach my $item (sort(keys(%changes))) {
 9357:                 if ($item eq 'inststatus') {
 9358:                     if (ref($changes{'inststatus'}) eq 'HASH') {
 9359:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
 9360:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
 9361:                             foreach my $type (@orderedstatus) { 
 9362:                                 $resulttext .= $alltypes{$type}.', ';
 9363:                             }
 9364:                             $resulttext =~ s/, $//;
 9365:                             $resulttext .= '</li>';
 9366:                         }
 9367:                         if ($changes{'inststatus'}{'inststatusguest'}) {
 9368:                             $resulttext .= '<li>'; 
 9369:                             if (@orderedguests) {
 9370:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
 9371:                                 foreach my $type (@orderedguests) {
 9372:                                     $resulttext .= $alltypes{$type}.', ';
 9373:                                 }
 9374:                                 $resulttext =~ s/, $//;
 9375:                             } else {
 9376:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
 9377:                             }
 9378:                             $resulttext .= '</li>';
 9379:                         }
 9380:                     }
 9381:                 } else {
 9382:                     my $value = $env{'form.'.$item};
 9383:                     if ($value eq '') {
 9384:                         $value = &mt('none');
 9385:                     } elsif ($item eq 'auth_def') {
 9386:                         my %authnames = &authtype_names();
 9387:                         my %shortauth = (
 9388:                                           internal   => 'int',
 9389:                                           krb4       => 'krb4',
 9390:                                           krb5       => 'krb5',
 9391:                                           localauth  => 'loc',
 9392:                         );
 9393:                         $value = $authnames{$shortauth{$value}};
 9394:                     }
 9395:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 9396:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
 9397:                 }
 9398:             }
 9399:             $resulttext .= '</ul>';
 9400:             $mailmsgtext .= "\n";
 9401:             my $cachetime = 24*60*60;
 9402:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9403:             if (ref($lastactref) eq 'HASH') {
 9404:                 $lastactref->{'domdefaults'} = 1;
 9405:             }
 9406:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 9407:                 my $notify = 1;
 9408:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 9409:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 9410:                         $notify = 0;
 9411:                     }
 9412:                 }
 9413:                 if ($notify) {
 9414:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 9415:                                                "LON-CAPA Domain Settings Change - $dom",
 9416:                                                $mailmsgtext);
 9417:                 }
 9418:             }
 9419:         } else {
 9420:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 9421:         }
 9422:     } else {
 9423:         $resulttext = '<span class="LC_error">'.
 9424:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9425:     }
 9426:     if (@errors > 0) {
 9427:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 9428:         foreach my $item (@errors) {
 9429:             $resulttext .= ' "'.$title->{$item}.'",';
 9430:         }
 9431:         $resulttext =~ s/,$//;
 9432:     }
 9433:     return $resulttext;
 9434: }
 9435: 
 9436: sub modify_scantron {
 9437:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9438:     my ($resulttext,%confhash,%changes,$errors);
 9439:     my $custom = 'custom.tab';
 9440:     my $default = 'default.tab';
 9441:     my $servadm = $r->dir_config('lonAdmEMail');
 9442:     my ($configuserok,$author_ok,$switchserver) = 
 9443:         &config_check($dom,$confname,$servadm);
 9444:     if ($env{'form.scantronformat.filename'} ne '') {
 9445:         my $error;
 9446:         if ($configuserok eq 'ok') {
 9447:             if ($switchserver) {
 9448:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 9449:             } else {
 9450:                 if ($author_ok eq 'ok') {
 9451:                     my ($result,$scantronurl) =
 9452:                         &publishlogo($r,'upload','scantronformat',$dom,
 9453:                                      $confname,'scantron','','',$custom);
 9454:                     if ($result eq 'ok') {
 9455:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 9456:                         $changes{'scantronformat'} = 1;
 9457:                     } else {
 9458:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 9459:                     }
 9460:                 } else {
 9461:                     $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);
 9462:                 }
 9463:             }
 9464:         } else {
 9465:             $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);
 9466:         }
 9467:         if ($error) {
 9468:             &Apache::lonnet::logthis($error);
 9469:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9470:         }
 9471:     }
 9472:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 9473:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 9474:             if ($env{'form.scantronformat_del'}) {
 9475:                 $confhash{'scantron'}{'scantronformat'} = '';
 9476:                 $changes{'scantronformat'} = 1;
 9477:             }
 9478:         }
 9479:     }
 9480:     if (keys(%confhash) > 0) {
 9481:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 9482:                                                  $dom);
 9483:         if ($putresult eq 'ok') {
 9484:             if (keys(%changes) > 0) {
 9485:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 9486:                     $resulttext = &mt('Changes made:').'<ul>';
 9487:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 9488:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 9489:                     } else {
 9490:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 9491:                     }
 9492:                     $resulttext .= '</ul>';
 9493:                 } else {
 9494:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 9495:                 }
 9496:                 $resulttext .= '</ul>';
 9497:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9498:                 if (ref($lastactref) eq 'HASH') {
 9499:                     $lastactref->{'domainconfig'} = 1;
 9500:                 }
 9501:             } else {
 9502:                 $resulttext = &mt('No changes made to bubblesheet format file');
 9503:             }
 9504:         } else {
 9505:             $resulttext = '<span class="LC_error">'.
 9506:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 9507:         }
 9508:     } else {
 9509:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 9510:     }
 9511:     if ($errors) {
 9512:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9513:                        $errors.'</ul>';
 9514:     }
 9515:     return $resulttext;
 9516: }
 9517: 
 9518: sub modify_coursecategories {
 9519:     my ($dom,$lastactref,%domconfig) = @_;
 9520:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 9521:         $cathash);
 9522:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 9523:     my @catitems = ('unauth','auth');
 9524:     my @cattypes = ('std','domonly','codesrch','none');
 9525:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9526:         $cathash = $domconfig{'coursecategories'}{'cats'};
 9527:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 9528:             $changes{'togglecats'} = 1;
 9529:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 9530:         }
 9531:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 9532:             $changes{'categorize'} = 1;
 9533:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 9534:         }
 9535:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 9536:             $changes{'togglecatscomm'} = 1;
 9537:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 9538:         }
 9539:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 9540:             $changes{'categorizecomm'} = 1;
 9541:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 9542:         }
 9543:         foreach my $item (@catitems) {
 9544:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9545:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
 9546:                     $changes{$item} = 1;
 9547:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9548:                 }
 9549:             }
 9550:         }
 9551:     } else {
 9552:         $changes{'togglecats'} = 1;
 9553:         $changes{'categorize'} = 1;
 9554:         $changes{'togglecatscomm'} = 1;
 9555:         $changes{'categorizecomm'} = 1;
 9556:         $domconfig{'coursecategories'} = {
 9557:                                              togglecats => $env{'form.togglecats'},
 9558:                                              categorize => $env{'form.categorize'},
 9559:                                              togglecatscomm => $env{'form.togglecatscomm'},
 9560:                                              categorizecomm => $env{'form.categorizecomm'},
 9561:                                          };
 9562:         foreach my $item (@catitems) {
 9563:             if ($env{'form.coursecat_'.$item} ne 'std') {
 9564:                 $changes{$item} = 1;
 9565:             }
 9566:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9567:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9568:             }
 9569:         }
 9570:     }
 9571:     if (ref($cathash) eq 'HASH') {
 9572:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 9573:             push (@deletecategory,'instcode::0');
 9574:         }
 9575:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 9576:             push(@deletecategory,'communities::0');
 9577:         }
 9578:     }
 9579:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 9580:     if (ref($cathash) eq 'HASH') {
 9581:         if (@deletecategory > 0) {
 9582:             #FIXME Need to remove category from all courses using a deleted category 
 9583:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 9584:             foreach my $item (@deletecategory) {
 9585:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 9586:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 9587:                     $deletions{$item} = 1;
 9588:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 9589:                 }
 9590:             }
 9591:         }
 9592:         foreach my $item (keys(%{$cathash})) {
 9593:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9594:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 9595:                 $reorderings{$item} = 1;
 9596:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 9597:             }
 9598:             if ($env{'form.addcategory_name_'.$item} ne '') {
 9599:                 my $newcat = $env{'form.addcategory_name_'.$item};
 9600:                 my $newdepth = $depth+1;
 9601:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9602:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 9603:                 $adds{$newitem} = 1; 
 9604:             }
 9605:             if ($env{'form.subcat_'.$item} ne '') {
 9606:                 my $newcat = $env{'form.subcat_'.$item};
 9607:                 my $newdepth = $depth+1;
 9608:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9609:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 9610:                 $adds{$newitem} = 1;
 9611:             }
 9612:         }
 9613:     }
 9614:     if ($env{'form.instcode'} eq '1') {
 9615:         if (ref($cathash) eq 'HASH') {
 9616:             my $newitem = 'instcode::0';
 9617:             if ($cathash->{$newitem} eq '') {  
 9618:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9619:                 $adds{$newitem} = 1;
 9620:             }
 9621:         } else {
 9622:             my $newitem = 'instcode::0';
 9623:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9624:             $adds{$newitem} = 1;
 9625:         }
 9626:     }
 9627:     if ($env{'form.communities'} eq '1') {
 9628:         if (ref($cathash) eq 'HASH') {
 9629:             my $newitem = 'communities::0';
 9630:             if ($cathash->{$newitem} eq '') {
 9631:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9632:                 $adds{$newitem} = 1;
 9633:             }
 9634:         } else {
 9635:             my $newitem = 'communities::0';
 9636:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9637:             $adds{$newitem} = 1;
 9638:         }
 9639:     }
 9640:     if ($env{'form.addcategory_name'} ne '') {
 9641:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 9642:             ($env{'form.addcategory_name'} ne 'communities')) {
 9643:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 9644:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 9645:             $adds{$newitem} = 1;
 9646:         }
 9647:     }
 9648:     my $putresult;
 9649:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9650:         if (keys(%deletions) > 0) {
 9651:             foreach my $key (keys(%deletions)) {
 9652:                 if ($predelallitems{$key} ne '') {
 9653:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 9654:                 }
 9655:             }
 9656:         }
 9657:         my (@chkcats,@chktrails,%chkallitems);
 9658:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 9659:         if (ref($chkcats[0]) eq 'ARRAY') {
 9660:             my $depth = 0;
 9661:             my $chg = 0;
 9662:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 9663:                 my $name = $chkcats[0][$i];
 9664:                 my $item;
 9665:                 if ($name eq '') {
 9666:                     $chg ++;
 9667:                 } else {
 9668:                     $item = &escape($name).'::0';
 9669:                     if ($chg) {
 9670:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 9671:                     }
 9672:                     $depth ++; 
 9673:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 9674:                     $depth --;
 9675:                 }
 9676:             }
 9677:         }
 9678:     }
 9679:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9680:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 9681:         if ($putresult eq 'ok') {
 9682:             my %title = (
 9683:                          togglecats     => 'Show/Hide a course in catalog',
 9684:                          categorize     => 'Assign a category to a course',
 9685:                          togglecatscomm => 'Show/Hide a community in catalog',
 9686:                          categorizecomm => 'Assign a category to a community',
 9687:                         );
 9688:             my %level = (
 9689:                          dom  => 'set in Domain ("Modify Course/Community")',
 9690:                          crs  => 'set in Course ("Course Configuration")',
 9691:                          comm => 'set in Community ("Community Configuration")',
 9692:                          none     => 'No catalog',
 9693:                          std      => 'Standard catalog',
 9694:                          domonly  => 'Domain-only catalog',
 9695:                          codesrch => 'Code search form',
 9696:                         );
 9697:             $resulttext = &mt('Changes made:').'<ul>';
 9698:             if ($changes{'togglecats'}) {
 9699:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 9700:             }
 9701:             if ($changes{'categorize'}) {
 9702:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 9703:             }
 9704:             if ($changes{'togglecatscomm'}) {
 9705:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 9706:             }
 9707:             if ($changes{'categorizecomm'}) {
 9708:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 9709:             }
 9710:             if ($changes{'unauth'}) {
 9711:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
 9712:             }
 9713:             if ($changes{'auth'}) {
 9714:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
 9715:             }
 9716:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9717:                 my $cathash;
 9718:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9719:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 9720:                 } else {
 9721:                     $cathash = {};
 9722:                 } 
 9723:                 my (@cats,@trails,%allitems);
 9724:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 9725:                 if (keys(%deletions) > 0) {
 9726:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 9727:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 9728:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 9729:                     }
 9730:                     $resulttext .= '</ul></li>';
 9731:                 }
 9732:                 if (keys(%reorderings) > 0) {
 9733:                     my %sort_by_trail;
 9734:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 9735:                     foreach my $key (keys(%reorderings)) {
 9736:                         if ($allitems{$key} ne '') {
 9737:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9738:                         }
 9739:                     }
 9740:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9741:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9742:                     }
 9743:                     $resulttext .= '</ul></li>';
 9744:                 }
 9745:                 if (keys(%adds) > 0) {
 9746:                     my %sort_by_trail;
 9747:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 9748:                     foreach my $key (keys(%adds)) {
 9749:                         if ($allitems{$key} ne '') {
 9750:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9751:                         }
 9752:                     }
 9753:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9754:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9755:                     }
 9756:                     $resulttext .= '</ul></li>';
 9757:                 }
 9758:             }
 9759:             $resulttext .= '</ul>';
 9760:             if ($changes{'unauth'} || $changes{'auth'}) {
 9761:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 9762:                 if ($changes{'auth'}) {
 9763:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 9764:                 }
 9765:                 if ($changes{'unauth'}) {
 9766:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 9767:                 }
 9768:                 my $cachetime = 24*60*60;
 9769:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9770:                 if (ref($lastactref) eq 'HASH') {
 9771:                     $lastactref->{'domdefaults'} = 1;
 9772:                 }
 9773:             }
 9774:         } else {
 9775:             $resulttext = '<span class="LC_error">'.
 9776:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9777:         }
 9778:     } else {
 9779:         $resulttext = &mt('No changes made to course and community categories');
 9780:     }
 9781:     return $resulttext;
 9782: }
 9783: 
 9784: sub modify_serverstatuses {
 9785:     my ($dom,%domconfig) = @_;
 9786:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 9787:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 9788:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 9789:     }
 9790:     my @pages = &serverstatus_pages();
 9791:     foreach my $type (@pages) {
 9792:         $newserverstatus{$type}{'namedusers'} = '';
 9793:         $newserverstatus{$type}{'machines'} = '';
 9794:         if (defined($env{'form.'.$type.'_namedusers'})) {
 9795:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 9796:             my @okusers;
 9797:             foreach my $user (@users) {
 9798:                 my ($uname,$udom) = split(/:/,$user);
 9799:                 if (($udom =~ /^$match_domain$/) &&   
 9800:                     (&Apache::lonnet::domain($udom)) &&
 9801:                     ($uname =~ /^$match_username$/)) {
 9802:                     if (!grep(/^\Q$user\E/,@okusers)) {
 9803:                         push(@okusers,$user);
 9804:                     }
 9805:                 }
 9806:             }
 9807:             if (@okusers > 0) {
 9808:                  @okusers = sort(@okusers);
 9809:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 9810:             }
 9811:         }
 9812:         if (defined($env{'form.'.$type.'_machines'})) {
 9813:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 9814:             my @okmachines;
 9815:             foreach my $ip (@machines) {
 9816:                 my @parts = split(/\./,$ip);
 9817:                 next if (@parts < 4);
 9818:                 my $badip = 0;
 9819:                 for (my $i=0; $i<4; $i++) {
 9820:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 9821:                         $badip = 1;
 9822:                         last;
 9823:                     }
 9824:                 }
 9825:                 if (!$badip) {
 9826:                     push(@okmachines,$ip);     
 9827:                 }
 9828:             }
 9829:             @okmachines = sort(@okmachines);
 9830:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 9831:         }
 9832:     }
 9833:     my %serverstatushash =  (
 9834:                                 serverstatuses => \%newserverstatus,
 9835:                             );
 9836:     foreach my $type (@pages) {
 9837:         foreach my $setting ('namedusers','machines') {
 9838:             my (@current,@new);
 9839:             if (ref($currserverstatus{$type}) eq 'HASH') {
 9840:                 if ($currserverstatus{$type}{$setting} ne '') { 
 9841:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 9842:                 }
 9843:             }
 9844:             if ($newserverstatus{$type}{$setting} ne '') {
 9845:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 9846:             }
 9847:             if (@current > 0) {
 9848:                 if (@new > 0) {
 9849:                     foreach my $item (@current) {
 9850:                         if (!grep(/^\Q$item\E$/,@new)) {
 9851:                             $changes{$type}{$setting} = 1;
 9852:                             last;
 9853:                         }
 9854:                     }
 9855:                     foreach my $item (@new) {
 9856:                         if (!grep(/^\Q$item\E$/,@current)) {
 9857:                             $changes{$type}{$setting} = 1;
 9858:                             last;
 9859:                         }
 9860:                     }
 9861:                 } else {
 9862:                     $changes{$type}{$setting} = 1;
 9863:                 }
 9864:             } elsif (@new > 0) {
 9865:                 $changes{$type}{$setting} = 1;
 9866:             }
 9867:         }
 9868:     }
 9869:     if (keys(%changes) > 0) {
 9870:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9871:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9872:                                                  \%serverstatushash,$dom);
 9873:         if ($putresult eq 'ok') {
 9874:             $resulttext .= &mt('Changes made:').'<ul>';
 9875:             foreach my $type (@pages) {
 9876:                 if (ref($changes{$type}) eq 'HASH') {
 9877:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 9878:                     if ($changes{$type}{'namedusers'}) {
 9879:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 9880:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 9881:                         } else {
 9882:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 9883:                         }
 9884:                     }
 9885:                     if ($changes{$type}{'machines'}) {
 9886:                         if ($newserverstatus{$type}{'machines'} eq '') {
 9887:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 9888:                         } else {
 9889:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 9890:                         }
 9891: 
 9892:                     }
 9893:                     $resulttext .= '</ul></li>';
 9894:                 }
 9895:             }
 9896:             $resulttext .= '</ul>';
 9897:         } else {
 9898:             $resulttext = '<span class="LC_error">'.
 9899:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 9900: 
 9901:         }
 9902:     } else {
 9903:         $resulttext = &mt('No changes made to access to server status pages');
 9904:     }
 9905:     return $resulttext;
 9906: }
 9907: 
 9908: sub modify_helpsettings {
 9909:     my ($r,$dom,$confname,%domconfig) = @_;
 9910:     my ($resulttext,$errors,%changes,%helphash);
 9911:     my %defaultchecked = ('submitbugs' => 'on');
 9912:     my @offon = ('off','on');
 9913:     my @toggles = ('submitbugs');
 9914:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 9915:         foreach my $item (@toggles) {
 9916:             if ($defaultchecked{$item} eq 'on') { 
 9917:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9918:                     if ($env{'form.'.$item} eq '0') {
 9919:                         $changes{$item} = 1;
 9920:                     }
 9921:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9922:                     $changes{$item} = 1;
 9923:                 }
 9924:             } elsif ($defaultchecked{$item} eq 'off') {
 9925:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9926:                     if ($env{'form.'.$item} eq '1') {
 9927:                         $changes{$item} = 1;
 9928:                     }
 9929:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9930:                     $changes{$item} = 1;
 9931:                 }
 9932:             }
 9933:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 9934:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 9935:             }
 9936:         }
 9937:     }
 9938:     my $putresult;
 9939:     if (keys(%changes) > 0) {
 9940:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 9941:         if ($putresult eq 'ok') {
 9942:             $resulttext = &mt('Changes made:').'<ul>';
 9943:             foreach my $item (sort(keys(%changes))) {
 9944:                 if ($item eq 'submitbugs') {
 9945:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 9946:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 9947:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 9948:                 }
 9949:             }
 9950:             $resulttext .= '</ul>';
 9951:         } else {
 9952:             $resulttext = &mt('No changes made to help settings');
 9953:             $errors .= '<li><span class="LC_error">'.
 9954:                        &mt('An error occurred storing the settings: [_1]',
 9955:                            $putresult).'</span></li>';
 9956:         }
 9957:     }
 9958:     if ($errors) {
 9959:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9960:                        $errors.'</ul>';
 9961:     }
 9962:     return $resulttext;
 9963: }
 9964: 
 9965: sub modify_coursedefaults {
 9966:     my ($dom,$lastactref,%domconfig) = @_;
 9967:     my ($resulttext,$errors,%changes,%defaultshash);
 9968:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 9969:     my @toggles = ('canuse_pdfforms');
 9970:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 9971:                    'uploadquota_community','uploadquota_textbook');
 9972:     my @types = ('official','unofficial','community','textbook');
 9973:     my %staticdefaults = (
 9974:                            anonsurvey_threshold => 10,
 9975:                            uploadquota          => 500,
 9976:                          );
 9977: 
 9978:     $defaultshash{'coursedefaults'} = {};
 9979: 
 9980:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 9981:         if ($domconfig{'coursedefaults'} eq '') {
 9982:             $domconfig{'coursedefaults'} = {};
 9983:         }
 9984:     }
 9985: 
 9986:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 9987:         foreach my $item (@toggles) {
 9988:             if ($defaultchecked{$item} eq 'on') {
 9989:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9990:                     ($env{'form.'.$item} eq '0')) {
 9991:                     $changes{$item} = 1;
 9992:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9993:                     $changes{$item} = 1;
 9994:                 }
 9995:             } elsif ($defaultchecked{$item} eq 'off') {
 9996:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9997:                     ($env{'form.'.$item} eq '1')) {
 9998:                     $changes{$item} = 1;
 9999:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10000:                     $changes{$item} = 1;
10001:                 }
10002:             }
10003:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
10004:         }
10005:         foreach my $item (@numbers) {
10006:             my ($currdef,$newdef);
10007:             $newdef = $env{'form.'.$item};
10008:             if ($item eq 'anonsurvey_threshold') {
10009:                 $currdef = $domconfig{'coursedefaults'}{$item};
10010:                 $newdef =~ s/\D//g;
10011:                 if ($newdef eq '' || $newdef < 1) {
10012:                     $newdef = 1;
10013:                 }
10014:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
10015:             } else {
10016:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
10017:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10018:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
10019:                 }
10020:                 $newdef =~ s/[^\w.\-]//g;
10021:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
10022:             }
10023:             if ($currdef ne $newdef) {
10024:                 my $staticdef;
10025:                 if ($item eq 'anonsurvey_threshold') {
10026:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
10027:                         $changes{$item} = 1;
10028:                     }
10029:                 } else {
10030:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
10031:                         $changes{'uploadquota'} = 1;
10032:                     }
10033:                 }
10034:             }
10035:         }
10036: 
10037:         my $officialcreds = $env{'form.official_credits'};
10038:         $officialcreds =~ s/[^\d.]+//g;
10039:         my $unofficialcreds = $env{'form.unofficial_credits'};
10040:         $unofficialcreds =~ s/[^\d.]+//g;
10041:         my $textbookcreds = $env{'form.textbook_credits'};
10042:         $textbookcreds =~ s/[^\d.]+//g;
10043:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
10044:                 ($env{'form.coursecredits'} eq '1')) {
10045:                 $changes{'coursecredits'} = 1;
10046:         } else {
10047:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
10048:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
10049:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
10050:                 $changes{'coursecredits'} = 1;
10051:             }
10052:         }
10053:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
10054:             official   => $officialcreds,
10055:             unofficial => $unofficialcreds,
10056:             textbook   => $textbookcreds,
10057:         }
10058:     }
10059:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10060:                                              $dom);
10061:     if ($putresult eq 'ok') {
10062:         if (keys(%changes) > 0) {
10063:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10064:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || 
10065:                 ($changes{'uploadquota'})) { 
10066:                 if ($changes{'canuse_pdfforms'}) {
10067:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
10068:                 }
10069:                 if ($changes{'coursecredits'}) {
10070:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10071:                         $domdefaults{'officialcredits'} =
10072:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
10073:                         $domdefaults{'unofficialcredits'} =
10074:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
10075:                         $domdefaults{'textbookcredits'} =
10076:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
10077:                     }
10078:                 }
10079:                 if ($changes{'uploadquota'}) {
10080:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10081:                         foreach my $type (@types) {
10082:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
10083:                         }
10084:                     }
10085:                 }
10086:                 my $cachetime = 24*60*60;
10087:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10088:                 if (ref($lastactref) eq 'HASH') {
10089:                     $lastactref->{'domdefaults'} = 1;
10090:                 }
10091:             }
10092:             $resulttext = &mt('Changes made:').'<ul>';
10093:             foreach my $item (sort(keys(%changes))) {
10094:                 if ($item eq 'canuse_pdfforms') {
10095:                     if ($env{'form.'.$item} eq '1') {
10096:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
10097:                     } else {
10098:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
10099:                     }
10100:                 } elsif ($item eq 'anonsurvey_threshold') {
10101:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
10102:                 } elsif ($item eq 'uploadquota') {
10103:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10104:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
10105:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
10106:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
10107:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
10108: 
10109:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
10110:                                        '</ul>'.
10111:                                        '</li>';
10112:                     } else {
10113:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
10114:                     }
10115:                 } elsif ($item eq 'coursecredits') {
10116:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10117:                         if (($domdefaults{'officialcredits'} eq '') &&
10118:                             ($domdefaults{'unofficialcredits'} eq '') &&
10119:                             ($domdefaults{'textbookcredits'} eq '')) {
10120:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10121:                         } else {
10122:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
10123:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
10124:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
10125:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
10126:                                            '</ul>'.
10127:                                            '</li>';
10128:                         }
10129:                     } else {
10130:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10131:                     }
10132:                 }
10133:             }
10134:             $resulttext .= '</ul>';
10135:         } else {
10136:             $resulttext = &mt('No changes made to course defaults');
10137:         }
10138:     } else {
10139:         $resulttext = '<span class="LC_error">'.
10140:             &mt('An error occurred: [_1]',$putresult).'</span>';
10141:     }
10142:     return $resulttext;
10143: }
10144: 
10145: sub modify_selfenrollment {
10146:     my ($dom,$lastactref,%domconfig) = @_;
10147:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
10148:     my @types = ('official','unofficial','community','textbook');
10149:     my %titles = &tool_titles();
10150:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
10151:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
10152:     $ordered{'default'} = ['types','registered','approval','limit'];
10153: 
10154:     my (%roles,%shown,%toplevel);
10155:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
10156: 
10157:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
10158:         if ($domconfig{'selfenrollment'} eq '') {
10159:             $domconfig{'selfenrollment'} = {};
10160:         }
10161:     }
10162:     %toplevel = (
10163:                   admin      => 'Configuration Rights',
10164:                   default    => 'Default settings',
10165:                   validation => 'Validation of self-enrollment requests',
10166:                 );
10167:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
10168: 
10169:     if (ref($ordered{'admin'}) eq 'ARRAY') {
10170:         foreach my $item (@{$ordered{'admin'}}) {
10171:             foreach my $type (@types) {
10172:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
10173:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
10174:                 } else {
10175:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
10176:                 }
10177:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
10178:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
10179:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
10180:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
10181:                             push(@{$changes{'admin'}{$type}},$item);
10182:                         }
10183:                     } else {
10184:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
10185:                             push(@{$changes{'admin'}{$type}},$item);
10186:                         }
10187:                     }
10188:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
10189:                     push(@{$changes{'admin'}{$type}},$item);
10190:                 }
10191:             }
10192:         }
10193:     }
10194: 
10195:     foreach my $item (@{$ordered{'default'}}) {
10196:         foreach my $type (@types) {
10197:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
10198:             if ($item eq 'types') {
10199:                 unless (($value eq 'all') || ($value eq 'dom')) {
10200:                     $value = '';
10201:                 }
10202:             } elsif ($item eq 'registered') {
10203:                 unless ($value eq '1') {
10204:                     $value = 0;
10205:                 }
10206:             } elsif ($item eq 'approval') {
10207:                 unless ($value =~ /^[012]$/) {
10208:                     $value = 0;
10209:                 }
10210:             } else {
10211:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10212:                     $value = 'none';
10213:                 }
10214:             }
10215:             $selfenrollhash{'default'}{$type}{$item} = $value;
10216:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
10217:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10218:                     if ($selfenrollhash{'default'}{$type}{$item} ne
10219:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
10220:                          push(@{$changes{'default'}{$type}},$item);
10221:                     }
10222:                 } else {
10223:                     push(@{$changes{'default'}{$type}},$item);
10224:                 }
10225:             } else {
10226:                 push(@{$changes{'default'}{$type}},$item);
10227:             }
10228:             if ($item eq 'limit') {
10229:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10230:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
10231:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
10232:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
10233:                     }
10234:                 } else {
10235:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
10236:                 }
10237:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10238:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
10239:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
10240:                          push(@{$changes{'default'}{$type}},'cap');
10241:                     }
10242:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
10243:                     push(@{$changes{'default'}{$type}},'cap');
10244:                 }
10245:             }
10246:         }
10247:     }
10248: 
10249:     foreach my $item (@{$itemsref}) {
10250:         if ($item eq 'fields') {
10251:             my @changed;
10252:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
10253:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
10254:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
10255:             }
10256:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10257:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
10258:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
10259:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
10260:                 } else {
10261:                     @changed = @{$selfenrollhash{'validation'}{$item}};
10262:                 }
10263:             } else {
10264:                 @changed = @{$selfenrollhash{'validation'}{$item}};
10265:             }
10266:             if (@changed) {
10267:                 if ($selfenrollhash{'validation'}{$item}) { 
10268:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
10269:                 } else {
10270:                     $changes{'validation'}{$item} = &mt('None');
10271:                 }
10272:             }
10273:         } else {
10274:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
10275:             if ($item eq 'markup') {
10276:                if ($env{'form.selfenroll_validation_'.$item}) {
10277:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10278:                }
10279:             }
10280:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10281:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
10282:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
10283:                 }
10284:             }
10285:         }
10286:     }
10287: 
10288:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
10289:                                              $dom);
10290:     if ($putresult eq 'ok') {
10291:         if (keys(%changes) > 0) {
10292:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10293:             $resulttext = &mt('Changes made:').'<ul>';
10294:             foreach my $key ('admin','default','validation') {
10295:                 if (ref($changes{$key}) eq 'HASH') {
10296:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
10297:                     if ($key eq 'validation') {
10298:                         foreach my $item (@{$itemsref}) {
10299:                             if (exists($changes{$key}{$item})) {
10300:                                 if ($item eq 'markup') {
10301:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10302:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
10303:                                 } else {  
10304:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10305:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
10306:                                 }
10307:                             }
10308:                         }
10309:                     } else {
10310:                         foreach my $type (@types) {
10311:                             if ($type eq 'community') {
10312:                                 $roles{'1'} = &mt('Community personnel');
10313:                             } else {
10314:                                 $roles{'1'} = &mt('Course personnel');
10315:                             }
10316:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
10317:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
10318:                                     if ($key eq 'admin') {
10319:                                         my @mgrdc = ();
10320:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10321:                                             foreach my $item (@{$ordered{'admin'}}) {
10322:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
10323:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
10324:                                                         push(@mgrdc,$item);
10325:                                                     }
10326:                                                 }
10327:                                             }
10328:                                             if (@mgrdc) {
10329:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
10330:                                             } else {
10331:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
10332:                                             }
10333:                                         }
10334:                                     } else {
10335:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10336:                                             foreach my $item (@{$ordered{$key}}) {
10337:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10338:                                                     $domdefaults{$type.'selfenroll'.$item} =
10339:                                                         $selfenrollhash{$key}{$type}{$item};
10340:                                                 }
10341:                                             }
10342:                                         }
10343:                                     }
10344:                                 }
10345:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
10346:                                 foreach my $item (@{$ordered{$key}}) {
10347:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10348:                                         $resulttext .= '<li>';
10349:                                         if ($key eq 'admin') {
10350:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
10351:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
10352:                                         } else {
10353:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
10354:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
10355:                                         }
10356:                                         $resulttext .= '</li>';
10357:                                     }
10358:                                 }
10359:                                 $resulttext .= '</ul></li>';
10360:                             }
10361:                         }
10362:                         $resulttext .= '</ul></li>'; 
10363:                     }
10364:                 }
10365:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
10366:                     my $cachetime = 24*60*60;
10367:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10368:                     if (ref($lastactref) eq 'HASH') {
10369:                         $lastactref->{'domdefaults'} = 1;
10370:                     }
10371:                 }
10372:             }
10373:             $resulttext .= '</ul>';
10374:         } else {
10375:             $resulttext = &mt('No changes made to self-enrollment settings');
10376:         }
10377:     } else {
10378:         $resulttext = '<span class="LC_error">'.
10379:             &mt('An error occurred: [_1]',$putresult).'</span>';
10380:     }
10381:     return $resulttext;
10382: }
10383: 
10384: sub modify_usersessions {
10385:     my ($dom,$lastactref,%domconfig) = @_;
10386:     my @hostingtypes = ('version','excludedomain','includedomain');
10387:     my @offloadtypes = ('primary','default');
10388:     my %types = (
10389:                   remote => \@hostingtypes,
10390:                   hosted => \@hostingtypes,
10391:                   spares => \@offloadtypes,
10392:                 );
10393:     my @prefixes = ('remote','hosted','spares');
10394:     my @lcversions = &Apache::lonnet::all_loncaparevs();
10395:     my (%by_ip,%by_location,@intdoms);
10396:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
10397:     my @locations = sort(keys(%by_location));
10398:     my (%defaultshash,%changes);
10399:     foreach my $prefix (@prefixes) {
10400:         $defaultshash{'usersessions'}{$prefix} = {};
10401:     }
10402:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10403:     my $resulttext;
10404:     my %iphost = &Apache::lonnet::get_iphost();
10405:     foreach my $prefix (@prefixes) {
10406:         next if ($prefix eq 'spares');
10407:         foreach my $type (@{$types{$prefix}}) {
10408:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
10409:             if ($type eq 'version') {
10410:                 my $value = $env{'form.'.$prefix.'_'.$type};
10411:                 my $okvalue;
10412:                 if ($value ne '') {
10413:                     if (grep(/^\Q$value\E$/,@lcversions)) {
10414:                         $okvalue = $value;
10415:                     }
10416:                 }
10417:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10418:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10419:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
10420:                             if ($inuse == 0) {
10421:                                 $changes{$prefix}{$type} = 1;
10422:                             } else {
10423:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
10424:                                     $changes{$prefix}{$type} = 1;
10425:                                 }
10426:                                 if ($okvalue ne '') {
10427:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10428:                                 } 
10429:                             }
10430:                         } else {
10431:                             if (($inuse == 1) && ($okvalue ne '')) {
10432:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10433:                                 $changes{$prefix}{$type} = 1;
10434:                             }
10435:                         }
10436:                     } else {
10437:                         if (($inuse == 1) && ($okvalue ne '')) {
10438:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10439:                             $changes{$prefix}{$type} = 1;
10440:                         }
10441:                     }
10442:                 } else {
10443:                     if (($inuse == 1) && ($okvalue ne '')) {
10444:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10445:                         $changes{$prefix}{$type} = 1;
10446:                     }
10447:                 }
10448:             } else {
10449:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
10450:                 my @okvals;
10451:                 foreach my $val (@vals) {
10452:                     if ($val =~ /:/) {
10453:                         my @items = split(/:/,$val);
10454:                         foreach my $item (@items) {
10455:                             if (ref($by_location{$item}) eq 'ARRAY') {
10456:                                 push(@okvals,$item);
10457:                             }
10458:                         }
10459:                     } else {
10460:                         if (ref($by_location{$val}) eq 'ARRAY') {
10461:                             push(@okvals,$val);
10462:                         }
10463:                     }
10464:                 }
10465:                 @okvals = sort(@okvals);
10466:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10467:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10468:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10469:                             if ($inuse == 0) {
10470:                                 $changes{$prefix}{$type} = 1; 
10471:                             } else {
10472:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10473:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
10474:                                 if (@changed > 0) {
10475:                                     $changes{$prefix}{$type} = 1;
10476:                                 }
10477:                             }
10478:                         } else {
10479:                             if ($inuse == 1) {
10480:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10481:                                 $changes{$prefix}{$type} = 1;
10482:                             }
10483:                         } 
10484:                     } else {
10485:                         if ($inuse == 1) {
10486:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10487:                             $changes{$prefix}{$type} = 1;
10488:                         }
10489:                     }
10490:                 } else {
10491:                     if ($inuse == 1) {
10492:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10493:                         $changes{$prefix}{$type} = 1;
10494:                     }
10495:                 }
10496:             }
10497:         }
10498:     }
10499: 
10500:     my @alldoms = &Apache::lonnet::all_domains();
10501:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10502:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
10503:     my $savespares;
10504: 
10505:     foreach my $lonhost (sort(keys(%servers))) {
10506:         my $serverhomeID =
10507:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
10508:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
10509:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
10510:         my %spareschg;
10511:         foreach my $type (@{$types{'spares'}}) {
10512:             my @okspares;
10513:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
10514:             foreach my $server (@checked) {
10515:                 if (&Apache::lonnet::hostname($server) ne '') {
10516:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
10517:                         unless (grep(/^\Q$server\E$/,@okspares)) {
10518:                             push(@okspares,$server);
10519:                         }
10520:                     }
10521:                 }
10522:             }
10523:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
10524:             my $newspare;
10525:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
10526:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
10527:                     $newspare = $new;
10528:                 }
10529:             }
10530:             my @spares;
10531:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
10532:                 @spares = sort(@okspares,$newspare);
10533:             } else {
10534:                 @spares = sort(@okspares);
10535:             }
10536:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
10537:             if (ref($spareid{$lonhost}) eq 'HASH') {
10538:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
10539:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
10540:                     if (@diffs > 0) {
10541:                         $spareschg{$type} = 1;
10542:                     }
10543:                 }
10544:             }
10545:         }
10546:         if (keys(%spareschg) > 0) {
10547:             $changes{'spares'}{$lonhost} = \%spareschg;
10548:         }
10549:     }
10550: 
10551:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
10552:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
10553:             if (ref($changes{'spares'}) eq 'HASH') {
10554:                 if (keys(%{$changes{'spares'}}) > 0) {
10555:                     $savespares = 1;
10556:                 }
10557:             }
10558:         } else {
10559:             $savespares = 1;
10560:         }
10561:     }
10562: 
10563:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
10564:     if ((keys(%changes) > 0) || ($savespares)) {
10565:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10566:                                                  $dom);
10567:         if ($putresult eq 'ok') {
10568:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10569:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
10570:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
10571:                 }
10572:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
10573:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
10574:                 }
10575:             }
10576:             my $cachetime = 24*60*60;
10577:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10578:             if (ref($lastactref) eq 'HASH') {
10579:                 $lastactref->{'domdefaults'} = 1;
10580:             }
10581:             if (keys(%changes) > 0) {
10582:                 my %lt = &usersession_titles();
10583:                 $resulttext = &mt('Changes made:').'<ul>';
10584:                 foreach my $prefix (@prefixes) {
10585:                     if (ref($changes{$prefix}) eq 'HASH') {
10586:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
10587:                         if ($prefix eq 'spares') {
10588:                             if (ref($changes{$prefix}) eq 'HASH') {
10589:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
10590:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
10591:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
10592:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
10593:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
10594:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
10595:                                         foreach my $type (@{$types{$prefix}}) {
10596:                                             if ($changes{$prefix}{$lonhost}{$type}) {
10597:                                                 my $offloadto = &mt('None');
10598:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
10599:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
10600:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
10601:                                                     }
10602:                                                 }
10603:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
10604:                                             }
10605:                                         }
10606:                                     }
10607:                                     $resulttext .= '</li>';
10608:                                 }
10609:                             }
10610:                         } else {
10611:                             foreach my $type (@{$types{$prefix}}) {
10612:                                 if (defined($changes{$prefix}{$type})) {
10613:                                     my $newvalue;
10614:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10615:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
10616:                                             if ($type eq 'version') {
10617:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
10618:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10619:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
10620:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
10621:                                                 }
10622:                                             }
10623:                                         }
10624:                                     }
10625:                                     if ($newvalue eq '') {
10626:                                         if ($type eq 'version') {
10627:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
10628:                                         } else {
10629:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
10630:                                         }
10631:                                     } else {
10632:                                         if ($type eq 'version') {
10633:                                             $newvalue .= ' '.&mt('(or later)'); 
10634:                                         }
10635:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
10636:                                     }
10637:                                 }
10638:                             }
10639:                         }
10640:                         $resulttext .= '</ul>';
10641:                     }
10642:                 }
10643:                 $resulttext .= '</ul>';
10644:             } else {
10645:                 $resulttext = $nochgmsg;
10646:             }
10647:         } else {
10648:             $resulttext = '<span class="LC_error">'.
10649:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10650:         }
10651:     } else {
10652:         $resulttext = $nochgmsg;
10653:     }
10654:     return $resulttext;
10655: }
10656: 
10657: sub modify_loadbalancing {
10658:     my ($dom,%domconfig) = @_;
10659:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
10660:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
10661:     my ($othertitle,$usertypes,$types) =
10662:         &Apache::loncommon::sorted_inst_types($dom);
10663:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10664:     my @sparestypes = ('primary','default');
10665:     my %typetitles = &sparestype_titles();
10666:     my $resulttext;
10667:     my (%currbalancer,%currtargets,%currrules,%existing);
10668:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10669:         %existing = %{$domconfig{'loadbalancing'}};
10670:     }
10671:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
10672:                               \%currtargets,\%currrules);
10673:     my ($saveloadbalancing,%defaultshash,%changes);
10674:     my ($alltypes,$othertypes,$titles) =
10675:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
10676:     my %ruletitles = &offloadtype_text();
10677:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
10678:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
10679:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
10680:         if ($balancer eq '') {
10681:             next;
10682:         }
10683:         if (!exists($servers{$balancer})) {
10684:             if (exists($currbalancer{$balancer})) {
10685:                 push(@{$changes{'delete'}},$balancer);
10686:             }
10687:             next;
10688:         }
10689:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
10690:             push(@{$changes{'delete'}},$balancer);
10691:             next;
10692:         }
10693:         if (!exists($currbalancer{$balancer})) {
10694:             push(@{$changes{'add'}},$balancer);
10695:         }
10696:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
10697:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
10698:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
10699:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10700:             $saveloadbalancing = 1;
10701:         }
10702:         foreach my $sparetype (@sparestypes) {
10703:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
10704:             my @offloadto;
10705:             foreach my $target (@targets) {
10706:                 if (($servers{$target}) && ($target ne $balancer)) {
10707:                     if ($sparetype eq 'default') {
10708:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
10709:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
10710:                         }
10711:                     }
10712:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
10713:                         push(@offloadto,$target);
10714:                     }
10715:                 }
10716:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
10717:             }
10718:         }
10719:         if (ref($currtargets{$balancer}) eq 'HASH') {
10720:             foreach my $sparetype (@sparestypes) {
10721:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
10722:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
10723:                     if (@targetdiffs > 0) {
10724:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10725:                     }
10726:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10727:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10728:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10729:                     }
10730:                 }
10731:             }
10732:         } else {
10733:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
10734:                 foreach my $sparetype (@sparestypes) {
10735:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10736:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10737:                             $changes{'curr'}{$balancer}{'targets'} = 1;
10738:                         }
10739:                     }
10740:                 }
10741:             }
10742:         }
10743:         my $ishomedom;
10744:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
10745:             $ishomedom = 1;
10746:         }
10747:         if (ref($alltypes) eq 'ARRAY') {
10748:             foreach my $type (@{$alltypes}) {
10749:                 my $rule;
10750:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
10751:                          (!$ishomedom)) {
10752:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
10753:                 }
10754:                 if ($rule eq 'specific') {
10755:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
10756:                 }
10757:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
10758:                 if (ref($currrules{$balancer}) eq 'HASH') {
10759:                     if ($rule ne $currrules{$balancer}{$type}) {
10760:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10761:                     }
10762:                 } elsif ($rule ne '') {
10763:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10764:                 }
10765:             }
10766:         }
10767:     }
10768:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
10769:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
10770:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
10771:             $defaultshash{'loadbalancing'} = {};
10772:         }
10773:         my $putresult = &Apache::lonnet::put_dom('configuration',
10774:                                                  \%defaultshash,$dom);
10775:         if ($putresult eq 'ok') {
10776:             if (keys(%changes) > 0) {
10777:                 if (ref($changes{'delete'}) eq 'ARRAY') {
10778:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
10779:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
10780:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10781:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10782:                     }
10783:                 }
10784:                 if (ref($changes{'add'}) eq 'ARRAY') {
10785:                     foreach my $balancer (sort(@{$changes{'add'}})) {
10786:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
10787:                     }
10788:                 }
10789:                 if (ref($changes{'curr'}) eq 'HASH') {
10790:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
10791:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
10792:                             if ($changes{'curr'}{$balancer}{'targets'}) {
10793:                                 my %offloadstr;
10794:                                 foreach my $sparetype (@sparestypes) {
10795:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10796:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10797:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10798:                                         }
10799:                                     }
10800:                                 }
10801:                                 if (keys(%offloadstr) == 0) {
10802:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
10803:                                 } else {
10804:                                     my $showoffload;
10805:                                     foreach my $sparetype (@sparestypes) {
10806:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
10807:                                         if (defined($offloadstr{$sparetype})) {
10808:                                             $showoffload .= $offloadstr{$sparetype};
10809:                                         } else {
10810:                                             $showoffload .= &mt('None');
10811:                                         }
10812:                                         $showoffload .= ('&nbsp;'x3);
10813:                                     }
10814:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
10815:                                 }
10816:                             }
10817:                         }
10818:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
10819:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
10820:                                 foreach my $type (@{$alltypes}) {
10821:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
10822:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
10823:                                         my $balancetext;
10824:                                         if ($rule eq '') {
10825:                                             $balancetext =  $ruletitles{'default'};
10826:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
10827:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
10828:                                             $balancetext =  $ruletitles{$rule};
10829:                                         } else {
10830:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
10831:                                         }
10832:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
10833:                                     }
10834:                                 }
10835:                             }
10836:                         }
10837:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10838:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10839:                     }
10840:                 }
10841:                 if ($resulttext ne '') {
10842:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
10843:                 } else {
10844:                     $resulttext = $nochgmsg;
10845:                 }
10846:             } else {
10847:                 $resulttext = $nochgmsg;
10848:             }
10849:         } else {
10850:             $resulttext = '<span class="LC_error">'.
10851:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10852:         }
10853:     } else {
10854:         $resulttext = $nochgmsg;
10855:     }
10856:     return $resulttext;
10857: }
10858: 
10859: sub recurse_check {
10860:     my ($chkcats,$categories,$depth,$name) = @_;
10861:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
10862:         my $chg = 0;
10863:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
10864:             my $category = $chkcats->[$depth]{$name}[$j];
10865:             my $item;
10866:             if ($category eq '') {
10867:                 $chg ++;
10868:             } else {
10869:                 my $deeper = $depth + 1;
10870:                 $item = &escape($category).':'.&escape($name).':'.$depth;
10871:                 if ($chg) {
10872:                     $categories->{$item} -= $chg;
10873:                 }
10874:                 &recurse_check($chkcats,$categories,$deeper,$category);
10875:                 $deeper --;
10876:             }
10877:         }
10878:     }
10879:     return;
10880: }
10881: 
10882: sub recurse_cat_deletes {
10883:     my ($item,$coursecategories,$deletions) = @_;
10884:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10885:     my $subdepth = $depth + 1;
10886:     if (ref($coursecategories) eq 'HASH') {
10887:         foreach my $subitem (keys(%{$coursecategories})) {
10888:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
10889:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
10890:                 delete($coursecategories->{$subitem});
10891:                 $deletions->{$subitem} = 1;
10892:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
10893:             }
10894:         }
10895:     }
10896:     return;
10897: }
10898: 
10899: sub get_active_dcs {
10900:     my ($dom) = @_;
10901:     my $now = time;
10902:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
10903:     my %domcoords;
10904:     my $numdcs = 0;
10905:     foreach my $server (keys(%dompersonnel)) {
10906:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
10907:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
10908:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
10909:         }
10910:     }
10911:     return %domcoords;
10912: }
10913: 
10914: sub active_dc_picker {
10915:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
10916:     my %domcoords = &get_active_dcs($dom);
10917:     my @domcoord = keys(%domcoords);
10918:     if (keys(%currhash)) {
10919:         foreach my $dc (keys(%currhash)) {
10920:             unless (exists($domcoords{$dc})) {
10921:                 push(@domcoord,$dc);
10922:             }
10923:         }
10924:     }
10925:     @domcoord = sort(@domcoord);
10926:     my $numdcs = scalar(@domcoord);
10927:     my $rows = 0;
10928:     my $table;
10929:     if ($numdcs > 1) {
10930:         $table = '<table>';
10931:         for (my $i=0; $i<@domcoord; $i++) {
10932:             my $rem = $i%($numinrow);
10933:             if ($rem == 0) {
10934:                 if ($i > 0) {
10935:                     $table .= '</tr>';
10936:                 }
10937:                 $table .= '<tr>';
10938:                 $rows ++;
10939:             }
10940:             my $check = '';
10941:             if ($inputtype eq 'radio') {
10942:                 if (keys(%currhash) == 0) {
10943:                     if (!$i) {
10944:                         $check = ' checked="checked"';
10945:                     }
10946:                 } elsif (exists($currhash{$domcoord[$i]})) {
10947:                     $check = ' checked="checked"';
10948:                 }
10949:             } else {
10950:                 if (exists($currhash{$domcoord[$i]})) {
10951:                     $check = ' checked="checked"';
10952:                 }
10953:             }
10954:             if ($i == @domcoord - 1) {
10955:                 my $colsleft = $numinrow - $rem;
10956:                 if ($colsleft > 1) {
10957:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
10958:                 } else {
10959:                     $table .= '<td class="LC_left_item">';
10960:                 }
10961:             } else {
10962:                 $table .= '<td class="LC_left_item">';
10963:             }
10964:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
10965:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10966:             $table .= '<span class="LC_nobreak"><label>'.
10967:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
10968:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
10969:             if ($user ne $dcname.':'.$dcdom) {
10970:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10971:             }
10972:             $table .= '</label></span></td>';
10973:         }
10974:         $table .= '</tr></table>';
10975:     } elsif ($numdcs == 1) {
10976:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
10977:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10978:         if ($inputtype eq 'radio') {
10979:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
10980:             if ($user ne $dcname.':'.$dcdom) {
10981:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10982:             }
10983:         } else {
10984:             my $check;
10985:             if (exists($currhash{$domcoord[0]})) {
10986:                 $check = ' checked="checked"';
10987:             }
10988:             $table = '<span class="LC_nobreak"><label>'.
10989:                      '<input type="checkbox" name="'.$name.'" '.
10990:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
10991:             if ($user ne $dcname.':'.$dcdom) {
10992:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10993:             }
10994:             $table .= '</label></span>';
10995:             $rows ++;
10996:         }
10997:     }
10998:     return ($numdcs,$table,$rows);
10999: }
11000: 
11001: sub usersession_titles {
11002:     return &Apache::lonlocal::texthash(
11003:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
11004:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
11005:                spares => 'Servers offloaded to, when busy',
11006:                version => 'LON-CAPA version requirement',
11007:                excludedomain => 'Allow all, but exclude specific domains',
11008:                includedomain => 'Deny all, but include specific domains',
11009:                primary => 'Primary (checked first)',
11010:                default => 'Default',
11011:            );
11012: }
11013: 
11014: sub id_for_thisdom {
11015:     my (%servers) = @_;
11016:     my %altids;
11017:     foreach my $server (keys(%servers)) {
11018:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
11019:         if ($serverhome ne $server) {
11020:             $altids{$serverhome} = $server;
11021:         }
11022:     }
11023:     return %altids;
11024: }
11025: 
11026: sub count_servers {
11027:     my ($currbalancer,%servers) = @_;
11028:     my (@spares,$numspares);
11029:     foreach my $lonhost (sort(keys(%servers))) {
11030:         next if ($currbalancer eq $lonhost);
11031:         push(@spares,$lonhost);
11032:     }
11033:     if ($currbalancer) {
11034:         $numspares = scalar(@spares);
11035:     } else {
11036:         $numspares = scalar(@spares) - 1;
11037:     }
11038:     return ($numspares,@spares);
11039: }
11040: 
11041: sub lonbalance_targets_js {
11042:     my ($dom,$types,$servers,$settings) = @_;
11043:     my $select = &mt('Select');
11044:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
11045:     if (ref($servers) eq 'HASH') {
11046:         $alltargets = join("','",sort(keys(%{$servers})));
11047:         my @homedoms;
11048:         foreach my $server (sort(keys(%{$servers}))) {
11049:             if (&Apache::lonnet::host_domain($server) eq $dom) {
11050:                 push(@homedoms,'1');
11051:             } else {
11052:                 push(@homedoms,'0');
11053:             }
11054:         }
11055:         $allishome = join("','",@homedoms);
11056:     }
11057:     if (ref($types) eq 'ARRAY') {
11058:         if (@{$types} > 0) {
11059:             @alltypes = @{$types};
11060:         }
11061:     }
11062:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
11063:     $allinsttypes = join("','",@alltypes);
11064:     my (%currbalancer,%currtargets,%currrules,%existing);
11065:     if (ref($settings) eq 'HASH') {
11066:         %existing = %{$settings};
11067:     }
11068:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
11069:                               \%currtargets,\%currrules);
11070:     my $balancers = join("','",sort(keys(%currbalancer)));
11071:     return <<"END";
11072: 
11073: <script type="text/javascript">
11074: // <![CDATA[
11075: 
11076: currBalancers = new Array('$balancers');
11077: 
11078: function toggleTargets(balnum) {
11079:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11080:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
11081:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
11082:     var prevbalancer = prevhostitem.value;
11083:     var baltotal = document.getElementById('loadbalancing_total').value;
11084:     prevhostitem.value = balancer;
11085:     if (prevbalancer != '') {
11086:         var prevIdx = currBalancers.indexOf(prevbalancer);
11087:         if (prevIdx != -1) {
11088:             currBalancers.splice(prevIdx,1);
11089:         }
11090:     }
11091:     if (balancer == '') {
11092:         hideSpares(balnum);
11093:     } else {
11094:         var currIdx = currBalancers.indexOf(balancer);
11095:         if (currIdx == -1) {
11096:             currBalancers.push(balancer);
11097:         }
11098:         var homedoms = new Array('$allishome');
11099:         var ishomedom = homedoms[lonhostitem.selectedIndex];
11100:         showSpares(balancer,ishomedom,balnum);
11101:     }
11102:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
11103:     return;
11104: }
11105: 
11106: function showSpares(balancer,ishomedom,balnum) {
11107:     var alltargets = new Array('$alltargets');
11108:     var insttypes = new Array('$allinsttypes');
11109:     var offloadtypes = new Array('primary','default');
11110: 
11111:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
11112:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
11113:  
11114:     for (var i=0; i<offloadtypes.length; i++) {
11115:         var count = 0;
11116:         for (var j=0; j<alltargets.length; j++) {
11117:             if (alltargets[j] != balancer) {
11118:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
11119:                 item.value = alltargets[j];
11120:                 item.style.textAlign='left';
11121:                 item.style.textFace='normal';
11122:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
11123:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
11124:                     item.disabled = '';
11125:                 } else {
11126:                     item.disabled = 'disabled';
11127:                     item.checked = false;
11128:                 }
11129:                 count ++;
11130:             }
11131:         }
11132:     }
11133:     for (var k=0; k<insttypes.length; k++) {
11134:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
11135:             if (ishomedom == 1) {
11136:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11137:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11138:             } else {
11139:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11140:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11141:             }
11142:         } else {
11143:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11144:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11145:         }
11146:         if ((insttypes[k] != '_LC_external') && 
11147:             ((insttypes[k] != '_LC_internetdom') ||
11148:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
11149:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
11150:             item.options.length = 0;
11151:             item.options[0] = new Option("","",true,true);
11152:             var idx = 0;
11153:             for (var m=0; m<alltargets.length; m++) {
11154:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
11155:                     idx ++;
11156:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11157:                 }
11158:             }
11159:         }
11160:     }
11161:     return;
11162: }
11163: 
11164: function hideSpares(balnum) {
11165:     var alltargets = new Array('$alltargets');
11166:     var insttypes = new Array('$allinsttypes');
11167:     var offloadtypes = new Array('primary','default');
11168: 
11169:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
11170:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
11171: 
11172:     var total = alltargets.length - 1;
11173:     for (var i=0; i<offloadtypes; i++) {
11174:         for (var j=0; j<total; j++) {
11175:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
11176:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
11177:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
11178:         }
11179:     }
11180:     for (var k=0; k<insttypes.length; k++) {
11181:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11182:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11183:         if (insttypes[k] != '_LC_external') {
11184:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
11185:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
11186:         }
11187:     }
11188:     return;
11189: }
11190: 
11191: function checkOffloads(item,balnum,type) {
11192:     var alltargets = new Array('$alltargets');
11193:     var offloadtypes = new Array('primary','default');
11194:     if (item.checked) {
11195:         var total = alltargets.length - 1;
11196:         var other;
11197:         if (type == offloadtypes[0]) {
11198:             other = offloadtypes[1];
11199:         } else {
11200:             other = offloadtypes[0];
11201:         }
11202:         for (var i=0; i<total; i++) {
11203:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
11204:             if (server == item.value) {
11205:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
11206:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
11207:                 }
11208:             }
11209:         }
11210:     }
11211:     return;
11212: }
11213: 
11214: function singleServerToggle(balnum,type) {
11215:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
11216:     if (offloadtoSelIdx == 0) {
11217:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
11218:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11219: 
11220:     } else {
11221:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
11222:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11223:     }
11224:     return;
11225: }
11226: 
11227: function balanceruleChange(formname,balnum,type) {
11228:     if (type == '_LC_external') {
11229:         return;
11230:     }
11231:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
11232:     for (var i=0; i<typesRules.length; i++) {
11233:         if (formname.elements[typesRules[i]].checked) {
11234:             if (formname.elements[typesRules[i]].value != 'specific') {
11235:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
11236:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11237:             } else {
11238:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11239:             }
11240:         }
11241:     }
11242:     return;
11243: }
11244: 
11245: function balancerDeleteChange(balnum) {
11246:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11247:     var baltotal = document.getElementById('loadbalancing_total').value;
11248:     var addtarget;
11249:     var removetarget;
11250:     var action = 'delete';
11251:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
11252:         var lonhost = hostitem.value;
11253:         var currIdx = currBalancers.indexOf(lonhost);
11254:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
11255:             if (currIdx != -1) {
11256:                 currBalancers.splice(currIdx,1);
11257:             }
11258:             addtarget = lonhost;
11259:         } else {
11260:             if (currIdx == -1) {
11261:                 currBalancers.push(lonhost);
11262:             }
11263:             removetarget = lonhost;
11264:             action = 'undelete';
11265:         }
11266:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
11267:     }
11268:     return;
11269: }
11270: 
11271: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
11272:     if (baltotal > 1) {
11273:         var offloadtypes = new Array('primary','default');
11274:         var alltargets = new Array('$alltargets');
11275:         var insttypes = new Array('$allinsttypes');
11276:         for (var i=0; i<baltotal; i++) {
11277:             if (i != balnum) {
11278:                 for (var j=0; j<offloadtypes.length; j++) {
11279:                     var total = alltargets.length - 1;
11280:                     for (var k=0; k<total; k++) {
11281:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
11282:                         var server = serveritem.value;
11283:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
11284:                             if (server == addtarget) {
11285:                                 serveritem.disabled = '';
11286:                             }
11287:                         }
11288:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11289:                             if (server == removetarget) {
11290:                                 serveritem.disabled = 'disabled';
11291:                                 serveritem.checked = false;
11292:                             }
11293:                         }
11294:                     }
11295:                 }
11296:                 for (var j=0; j<insttypes.length; j++) {
11297:                     if (insttypes[j] != '_LC_external') {
11298:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
11299:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
11300:                             var currSel = singleserver.selectedIndex;
11301:                             var currVal = singleserver.options[currSel].value;
11302:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
11303:                                 var numoptions = singleserver.options.length;
11304:                                 var needsnew = 1;
11305:                                 for (var k=0; k<numoptions; k++) {
11306:                                     if (singleserver.options[k] == addtarget) {
11307:                                         needsnew = 0;
11308:                                         break;
11309:                                     }
11310:                                 }
11311:                                 if (needsnew == 1) {
11312:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
11313:                                 }
11314:                             }
11315:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11316:                                 singleserver.options.length = 0;
11317:                                 if ((currVal) && (currVal != removetarget)) {
11318:                                     singleserver.options[0] = new Option("","",false,false);
11319:                                 } else {
11320:                                     singleserver.options[0] = new Option("","",true,true);
11321:                                 }
11322:                                 var idx = 0;
11323:                                 for (var m=0; m<alltargets.length; m++) {
11324:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
11325:                                         idx ++;
11326:                                         if (currVal == alltargets[m]) {
11327:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
11328:                                         } else {
11329:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11330:                                         }
11331:                                     }
11332:                                 }
11333:                             }
11334:                         }
11335:                     }
11336:                 }
11337:             }
11338:         }
11339:     }
11340:     return;
11341: }
11342: 
11343: // ]]>
11344: </script>
11345: 
11346: END
11347: }
11348: 
11349: sub new_spares_js {
11350:     my @sparestypes = ('primary','default');
11351:     my $types = join("','",@sparestypes);
11352:     my $select = &mt('Select');
11353:     return <<"END";
11354: 
11355: <script type="text/javascript">
11356: // <![CDATA[
11357: 
11358: function updateNewSpares(formname,lonhost) {
11359:     var types = new Array('$types');
11360:     var include = new Array();
11361:     var exclude = new Array();
11362:     for (var i=0; i<types.length; i++) {
11363:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
11364:         for (var j=0; j<spareboxes.length; j++) {
11365:             if (formname.elements[spareboxes[j]].checked) {
11366:                 exclude.push(formname.elements[spareboxes[j]].value);
11367:             } else {
11368:                 include.push(formname.elements[spareboxes[j]].value);
11369:             }
11370:         }
11371:     }
11372:     for (var i=0; i<types.length; i++) {
11373:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
11374:         var selIdx = newSpare.selectedIndex;
11375:         var currnew = newSpare.options[selIdx].value;
11376:         var okSpares = new Array();
11377:         for (var j=0; j<newSpare.options.length; j++) {
11378:             var possible = newSpare.options[j].value;
11379:             if (possible != '') {
11380:                 if (exclude.indexOf(possible) == -1) {
11381:                     okSpares.push(possible);
11382:                 } else {
11383:                     if (currnew == possible) {
11384:                         selIdx = 0;
11385:                     }
11386:                 }
11387:             }
11388:         }
11389:         for (var k=0; k<include.length; k++) {
11390:             if (okSpares.indexOf(include[k]) == -1) {
11391:                 okSpares.push(include[k]);
11392:             }
11393:         }
11394:         okSpares.sort();
11395:         newSpare.options.length = 0;
11396:         if (selIdx == 0) {
11397:             newSpare.options[0] = new Option("$select","",true,true);
11398:         } else {
11399:             newSpare.options[0] = new Option("$select","",false,false);
11400:         }
11401:         for (var m=0; m<okSpares.length; m++) {
11402:             var idx = m+1;
11403:             var selThis = 0;
11404:             if (selIdx != 0) {
11405:                 if (okSpares[m] == currnew) {
11406:                     selThis = 1;
11407:                 }
11408:             }
11409:             if (selThis == 1) {
11410:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
11411:             } else {
11412:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
11413:             }
11414:         }
11415:     }
11416:     return;
11417: }
11418: 
11419: function checkNewSpares(lonhost,type) {
11420:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
11421:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
11422:     if (chosen != '') { 
11423:         var othertype;
11424:         var othernewSpare;
11425:         if (type == 'primary') {
11426:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
11427:         }
11428:         if (type == 'default') {
11429:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
11430:         }
11431:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
11432:             othernewSpare.selectedIndex = 0;
11433:         }
11434:     }
11435:     return;
11436: }
11437: 
11438: // ]]>
11439: </script>
11440: 
11441: END
11442: 
11443: }
11444: 
11445: sub common_domprefs_js {
11446:     return <<"END";
11447: 
11448: <script type="text/javascript">
11449: // <![CDATA[
11450: 
11451: function getIndicesByName(formname,item) {
11452:     var group = new Array();
11453:     for (var i=0;i<formname.elements.length;i++) {
11454:         if (formname.elements[i].name == item) {
11455:             group.push(formname.elements[i].id);
11456:         }
11457:     }
11458:     return group;
11459: }
11460: 
11461: // ]]>
11462: </script>
11463: 
11464: END
11465: 
11466: }
11467: 
11468: sub recaptcha_js {
11469:     my %lt = &captcha_phrases();
11470:     return <<"END";
11471: 
11472: <script type="text/javascript">
11473: // <![CDATA[
11474: 
11475: function updateCaptcha(caller,context) {
11476:     var privitem;
11477:     var pubitem;
11478:     var privtext;
11479:     var pubtext;
11480:     if (document.getElementById(context+'_recaptchapub')) {
11481:         pubitem = document.getElementById(context+'_recaptchapub');
11482:     } else {
11483:         return;
11484:     }
11485:     if (document.getElementById(context+'_recaptchapriv')) {
11486:         privitem = document.getElementById(context+'_recaptchapriv');
11487:     } else {
11488:         return;
11489:     }
11490:     if (document.getElementById(context+'_recaptchapubtxt')) {
11491:         pubtext = document.getElementById(context+'_recaptchapubtxt');
11492:     } else {
11493:         return;
11494:     }
11495:     if (document.getElementById(context+'_recaptchaprivtxt')) {
11496:         privtext = document.getElementById(context+'_recaptchaprivtxt');
11497:     } else {
11498:         return;
11499:     }
11500:     if (caller.checked) {
11501:         if (caller.value == 'recaptcha') {
11502:             pubitem.type = 'text';
11503:             privitem.type = 'text';
11504:             pubitem.size = '40';
11505:             privitem.size = '40';
11506:             pubtext.innerHTML = "$lt{'pub'}";
11507:             privtext.innerHTML = "$lt{'priv'}";
11508:         } else {
11509:             pubitem.type = 'hidden';
11510:             privitem.type = 'hidden';
11511:             pubtext.innerHTML = '';
11512:             privtext.innerHTML = '';
11513:         }
11514:     }
11515:     return;
11516: }
11517: 
11518: // ]]>
11519: </script>
11520: 
11521: END
11522: 
11523: }
11524: 
11525: sub toggle_display_js {
11526:     return <<"END";
11527: 
11528: <script type="text/javascript">
11529: // <![CDATA[
11530: 
11531: function toggleDisplay(domForm,caller) {
11532:     if (document.getElementById(caller)) {
11533:         var divitem = document.getElementById(caller);
11534:         var optionsElement = domForm.coursecredits;
11535:         if (caller == 'emailoptions') {
11536:             optionsElement = domForm.cancreate_email; 
11537:         }
11538:         if (optionsElement.length) {
11539:             var currval;
11540:             for (var i=0; i<optionsElement.length; i++) {
11541:                 if (optionsElement[i].checked) {
11542:                    currval = optionsElement[i].value;
11543:                 }
11544:             }
11545:             if (currval == 1) {
11546:                 divitem.style.display = 'block';
11547:             } else {
11548:                 divitem.style.display = 'none';
11549:             }
11550:         }
11551:     }
11552:     return;
11553: }
11554: 
11555: // ]]>
11556: </script>
11557: 
11558: END
11559: 
11560: }
11561: 
11562: sub captcha_phrases {
11563:     return &Apache::lonlocal::texthash (
11564:                  priv => 'Private key',
11565:                  pub  => 'Public key',
11566:                  original  => 'original (CAPTCHA)',
11567:                  recaptcha => 'successor (ReCAPTCHA)',
11568:                  notused   => 'unused',
11569:     );
11570: }
11571: 
11572: sub devalidate_remote_domconfs {
11573:     my ($dom,$cachekeys) = @_;
11574:     return unless (ref($cachekeys) eq 'HASH');
11575:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11576:     my %thismachine;
11577:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
11578:     my @posscached = ('domainconfig','domdefaults');
11579:     if (keys(%servers) > 1) {
11580:         foreach my $server (keys(%servers)) {
11581:             next if ($thismachine{$server});
11582:             my @cached;
11583:             foreach my $name (@posscached) {
11584:                 if ($cachekeys->{$name}) {
11585:                     push(@cached,&escape($name).':'.&escape($dom));
11586:                 }
11587:             }
11588:             if (@cached) {
11589:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
11590:             }
11591:         }
11592:     }
11593:     return;
11594: }
11595: 
11596: 1;

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