File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.46: download - view: text, annotated - select for diffs
Fri May 9 15:23:28 2014 UTC (10 years, 1 month ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  - Backport 1.242.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.46 2014/05/09 15:23:28 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 in each course by a CC',
  416:                               col2 => 'Value',},
  417:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  418:                               col2 => 'Value',},],
  419:                   print => \&print_coursedefaults,
  420:                   modify => \&modify_coursedefaults,
  421:                  },
  422:         'selfenrollment' => 
  423:                  {text   => 'Self-enrollment in Course/Community',
  424:                   help   => 'Domain_Configuration_Selfenrollment',
  425:                   header => [{col1 => 'Configuration Rights',
  426:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  427:                              {col1 => 'Defaults',
  428:                               col2 => 'Value'},
  429:                              {col1 => 'Self-enrollment validation (optional)',
  430:                               col2 => 'Value'},],
  431:                   print => \&print_selfenrollment,
  432:                   modify => \&modify_selfenrollment,
  433:                  },
  434:         'usersessions' =>
  435:                  {text  => 'User session hosting/offloading',
  436:                   help  => 'Domain_Configuration_User_Sessions',
  437:                   header => [{col1 => 'Domain server',
  438:                               col2 => 'Servers to offload sessions to when busy'},
  439:                              {col1 => 'Hosting of users from other domains',
  440:                               col2 => 'Rules'},
  441:                              {col1 => "Hosting domain's own users elsewhere",
  442:                               col2 => 'Rules'}],
  443:                   print => \&print_usersessions,
  444:                   modify => \&modify_usersessions,
  445:                  },
  446:          'loadbalancing' =>
  447:                  {text  => 'Dedicated Load Balancer(s)',
  448:                   help  => 'Domain_Configuration_Load_Balancing',
  449:                   header => [{col1 => 'Balancers',
  450:                               col2 => 'Default destinations',
  451:                               col3 => 'User affiliation',
  452:                               col4 => 'Overrides'},
  453:                             ],
  454:                   print => \&print_loadbalancing,
  455:                   modify => \&modify_loadbalancing,
  456:                  },
  457:     );
  458:     if (keys(%servers) > 1) {
  459:         $prefs{'login'}  = { text   => 'Log-in page options',
  460:                              help   => 'Domain_Configuration_Login_Page',
  461:                             header => [{col1 => 'Log-in Service',
  462:                                         col2 => 'Server Setting',},
  463:                                        {col1 => 'Log-in Page Items',
  464:                                         col2 => ''},
  465:                                        {col1 => 'Log-in Help',
  466:                                         col2 => 'Value'}],
  467:                             print => \&print_login,
  468:                             modify => \&modify_login,
  469:                            };
  470:     }
  471: 
  472:     my @roles = ('student','coordinator','author','admin');
  473:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  474:     &Apache::lonhtmlcommon::add_breadcrumb
  475:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  476:       text=>"Settings to display/modify"});
  477:     my $confname = $dom.'-domainconfig';
  478: 
  479:     if ($phase eq 'process') {
  480:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  481:                                                               \%prefs,\%domconfig,$confname,\@roles);
  482:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  483:             $r->rflush();
  484:             &devalidate_remote_domconfs($dom,$result);
  485:         }
  486:     } elsif ($phase eq 'display') {
  487:         my $js = &recaptcha_js().
  488:                  &toggle_display_js();
  489:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  490:             my ($othertitle,$usertypes,$types) =
  491:                 &Apache::loncommon::sorted_inst_types($dom);
  492:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  493:                                           $domconfig{'loadbalancing'}).
  494:                    &new_spares_js().
  495:                    &common_domprefs_js().
  496:                    &Apache::loncommon::javascript_array_indexof();
  497:         }
  498:         if (grep(/^requestcourses$/,@actions)) {
  499:             my $javascript_validations;
  500:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  501:             $js .= <<END;
  502: <script type="text/javascript">
  503: $javascript_validations
  504: </script>
  505: $coursebrowserjs
  506: END
  507:         }
  508:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  509:     } else {
  510: # check if domconfig user exists for the domain.
  511:         my $servadm = $r->dir_config('lonAdmEMail');
  512:         my ($configuserok,$author_ok,$switchserver) =
  513:             &config_check($dom,$confname,$servadm);
  514:         unless ($configuserok eq 'ok') {
  515:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  516:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  517:                           $confname).
  518:                       '<br />'
  519:             );
  520:             if ($switchserver) {
  521:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  522:                           '<br />'.
  523:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  524:                           '<br />'.
  525:                           &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).
  526:                           '<br />'.
  527:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  528:                 );
  529:             } else {
  530:                 $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.').
  531:                           '<br />'.
  532:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  533:                 );
  534:             }
  535:             $r->print(&Apache::loncommon::end_page());
  536:             return OK;
  537:         }
  538:         if (keys(%domconfig) == 0) {
  539:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  540:             my @ids=&Apache::lonnet::current_machine_ids();
  541:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  542:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  543:                 my @loginimages = ('img','logo','domlogo','login');
  544:                 my $custom_img_count = 0;
  545:                 foreach my $img (@loginimages) {
  546:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  547:                         $custom_img_count ++;
  548:                     }
  549:                 }
  550:                 foreach my $role (@roles) {
  551:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  552:                         $custom_img_count ++;
  553:                     }
  554:                 }
  555:                 if ($custom_img_count > 0) {
  556:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  557:                     my $switch_server = &check_switchserver($dom,$confname);
  558:                     $r->print(
  559:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  560:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  561:     &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 />'.
  562:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  563:                     if ($switch_server) {
  564:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  565:                     }
  566:                     $r->print(&Apache::loncommon::end_page());
  567:                     return OK;
  568:                 }
  569:             }
  570:         }
  571:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  572:     }
  573:     return OK;
  574: }
  575: 
  576: sub process_changes {
  577:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  578:     my %domconfig;
  579:     if (ref($values) eq 'HASH') {
  580:         %domconfig = %{$values};
  581:     }
  582:     my $output;
  583:     if ($action eq 'login') {
  584:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  585:     } elsif ($action eq 'rolecolors') {
  586:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  587:                                      $lastactref,%domconfig);
  588:     } elsif ($action eq 'quotas') {
  589:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  590:     } elsif ($action eq 'autoenroll') {
  591:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  592:     } elsif ($action eq 'autoupdate') {
  593:         $output = &modify_autoupdate($dom,%domconfig);
  594:     } elsif ($action eq 'autocreate') {
  595:         $output = &modify_autocreate($dom,%domconfig);
  596:     } elsif ($action eq 'directorysrch') {
  597:         $output = &modify_directorysrch($dom,%domconfig);
  598:     } elsif ($action eq 'usercreation') {
  599:         $output = &modify_usercreation($dom,%domconfig);
  600:     } elsif ($action eq 'selfcreation') {
  601:         $output = &modify_selfcreation($dom,%domconfig);
  602:     } elsif ($action eq 'usermodification') {
  603:         $output = &modify_usermodification($dom,%domconfig);
  604:     } elsif ($action eq 'contacts') {
  605:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  606:     } elsif ($action eq 'defaults') {
  607:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  608:     } elsif ($action eq 'scantron') {
  609:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  610:     } elsif ($action eq 'coursecategories') {
  611:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  612:     } elsif ($action eq 'serverstatuses') {
  613:         $output = &modify_serverstatuses($dom,%domconfig);
  614:     } elsif ($action eq 'requestcourses') {
  615:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  616:     } elsif ($action eq 'requestauthor') {
  617:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  618:     } elsif ($action eq 'coursedefaults') {
  619:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  620:     } elsif ($action eq 'selfenrollment') {
  621:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  622:     } elsif ($action eq 'usersessions') {
  623:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  624:     } elsif ($action eq 'loadbalancing') {
  625:         $output = &modify_loadbalancing($dom,%domconfig);
  626:     }
  627:     return $output;
  628: }
  629: 
  630: sub print_config_box {
  631:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  632:     my $rowtotal = 0;
  633:     my $output;
  634:     if ($action eq 'coursecategories') {
  635:         $output = &coursecategories_javascript($settings);
  636:     } elsif ($action eq 'defaults') {
  637:         $output = &defaults_javascript($settings); 
  638:     }
  639:     $output .=
  640:          '<table class="LC_nested_outer">
  641:           <tr>
  642:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  643:            &mt($item->{text}).'&nbsp;'.
  644:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  645:           '</tr>';
  646:     $rowtotal ++;
  647:     my $numheaders = 1;
  648:     if (ref($item->{'header'}) eq 'ARRAY') {
  649:         $numheaders = scalar(@{$item->{'header'}});
  650:     }
  651:     if ($numheaders > 1) {
  652:         my $colspan = '';
  653:         my $rightcolspan = '';
  654:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  655:             (($action eq 'login') && ($numheaders < 3))) {
  656:             $colspan = ' colspan="2"';
  657:         }
  658:         if ($action eq 'usersessions') {
  659:             $rightcolspan = ' colspan="3"'; 
  660:         }
  661:         $output .= '
  662:           <tr>
  663:            <td>
  664:             <table class="LC_nested">
  665:              <tr class="LC_info_row">
  666:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  667:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  668:              </tr>';
  669:         $rowtotal ++;
  670:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  671:             ($action eq 'usermodification') || ($action eq 'defaults') ||
  672:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
  673:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  674:         } elsif ($action eq 'coursecategories') {
  675:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  676:         } elsif ($action eq 'login') {
  677:             if ($numheaders == 3) {
  678:                 $colspan = ' colspan="2"';
  679:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  680:             } else {
  681:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  682:             }
  683:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  684:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  685:         } elsif ($action eq 'rolecolors') {
  686:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  687:         }
  688:         $output .= '
  689:            </table>
  690:           </td>
  691:          </tr>
  692:          <tr>
  693:            <td>
  694:             <table class="LC_nested">
  695:              <tr class="LC_info_row">
  696:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  697:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  698:              </tr>';
  699:             $rowtotal ++;
  700:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  701:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  702:             ($action eq 'usersessions') || ($action eq 'coursecategories')) {
  703:             if ($action eq 'coursecategories') {
  704:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  705:                 $colspan = ' colspan="2"';
  706:             } else {
  707:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  708:             }
  709:             $output .= '
  710:            </table>
  711:           </td>
  712:          </tr>
  713:          <tr>
  714:            <td>
  715:             <table class="LC_nested">
  716:              <tr class="LC_info_row">
  717:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  718:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  719:              </tr>'."\n";
  720:             if ($action eq 'coursecategories') {
  721:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  722:             } else {
  723:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  724:             }
  725:             $rowtotal ++;
  726:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  727:                   ($action eq 'defaults')) {
  728:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  729:         } elsif ($action eq 'login') {
  730:             if ($numheaders == 3) {
  731:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  732:            </table>
  733:           </td>
  734:          </tr>
  735:          <tr>
  736:            <td>
  737:             <table class="LC_nested">
  738:              <tr class="LC_info_row">
  739:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  740:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  741:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  742:                 $rowtotal ++;
  743:             } else {
  744:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  745:             }
  746:         } elsif ($action eq 'requestcourses') {
  747:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal).
  748:                        &print_studentcode($settings,\$rowtotal).'
  749:            </table>
  750:           </td>
  751:          </tr>
  752:          <tr>
  753:            <td>
  754:             <table class="LC_nested">
  755:              <tr class="LC_info_row">
  756:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  757:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  758:                        &textbookcourses_javascript($settings).
  759:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  760:             </table>
  761:            </td>
  762:           </tr>
  763:          <tr>
  764:            <td>
  765:             <table class="LC_nested">
  766:              <tr class="LC_info_row">
  767:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  768:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  769:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  770:             </table>
  771:            </td>
  772:           </tr>
  773:           <tr>
  774:            <td>
  775:             <table class="LC_nested">
  776:              <tr class="LC_info_row">
  777:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  778:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  779:              </tr>'.
  780:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  781:         } elsif ($action eq 'requestauthor') {
  782:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  783:         } elsif ($action eq 'rolecolors') {
  784:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  785:            </table>
  786:           </td>
  787:          </tr>
  788:          <tr>
  789:            <td>
  790:             <table class="LC_nested">
  791:              <tr class="LC_info_row">
  792:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  793:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  794:               <td class="LC_right_item" valign="top">'.
  795:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  796:              </tr>'.
  797:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  798:            </table>
  799:           </td>
  800:          </tr>
  801:          <tr>
  802:            <td>
  803:             <table class="LC_nested">
  804:              <tr class="LC_info_row">
  805:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  806:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  807:              </tr>'.
  808:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  809:             $rowtotal += 2;
  810:         }
  811:     } else {
  812:         $output .= '
  813:           <tr>
  814:            <td>
  815:             <table class="LC_nested">
  816:              <tr class="LC_info_row">';
  817:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  818:             $output .= '  
  819:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  820:         } elsif ($action eq 'serverstatuses') {
  821:             $output .= '
  822:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  823:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  824: 
  825:         } else {
  826:             $output .= '
  827:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  828:         }
  829:         if (defined($item->{'header'}->[0]->{'col3'})) {
  830:             $output .= '<td class="LC_left_item" valign="top">'.
  831:                        &mt($item->{'header'}->[0]->{'col2'});
  832:             if ($action eq 'serverstatuses') {
  833:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  834:             } 
  835:         } else {
  836:             $output .= '<td class="LC_right_item" valign="top">'.
  837:                        &mt($item->{'header'}->[0]->{'col2'});
  838:         }
  839:         $output .= '</td>';
  840:         if ($item->{'header'}->[0]->{'col3'}) {
  841:             if (defined($item->{'header'}->[0]->{'col4'})) {
  842:                 $output .= '<td class="LC_left_item" valign="top">'.
  843:                             &mt($item->{'header'}->[0]->{'col3'});
  844:             } else {
  845:                 $output .= '<td class="LC_right_item" valign="top">'.
  846:                            &mt($item->{'header'}->[0]->{'col3'});
  847:             }
  848:             if ($action eq 'serverstatuses') {
  849:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  850:             }
  851:             $output .= '</td>';
  852:         }
  853:         if ($item->{'header'}->[0]->{'col4'}) {
  854:             $output .= '<td class="LC_right_item" valign="top">'.
  855:                        &mt($item->{'header'}->[0]->{'col4'});
  856:         }
  857:         $output .= '</tr>';
  858:         $rowtotal ++;
  859:         if ($action eq 'quotas') {
  860:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  861:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  862:                  ($action eq 'contacts') || ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  863:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  864:         } elsif ($action eq 'scantron') {
  865:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  866:         } elsif ($action eq 'helpsettings') {
  867:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  868:         }
  869:     }
  870:     $output .= '
  871:    </table>
  872:   </td>
  873:  </tr>
  874: </table><br />';
  875:     return ($output,$rowtotal);
  876: }
  877: 
  878: sub print_login {
  879:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  880:     my ($css_class,$datatable);
  881:     my %choices = &login_choices();
  882: 
  883:     if ($caller eq 'service') {
  884:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  885:         my $choice = $choices{'disallowlogin'};
  886:         $css_class = ' class="LC_odd_row"';
  887:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  888:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  889:                       '<th>'.$choices{'server'}.'</th>'.
  890:                       '<th>'.$choices{'serverpath'}.'</th>'.
  891:                       '<th>'.$choices{'custompath'}.'</th>'.
  892:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  893:         my %disallowed;
  894:         if (ref($settings) eq 'HASH') {
  895:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  896:                %disallowed = %{$settings->{'loginvia'}};
  897:             }
  898:         }
  899:         foreach my $lonhost (sort(keys(%servers))) {
  900:             my $direct = 'selected="selected"';
  901:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  902:                 if ($disallowed{$lonhost}{'server'} ne '') {
  903:                     $direct = '';
  904:                 }
  905:             }
  906:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  907:                           '<td><select name="'.$lonhost.'_server">'.
  908:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  909:                           '</option>';
  910:             foreach my $hostid (sort(keys(%servers))) {
  911:                 next if ($servers{$hostid} eq $servers{$lonhost});
  912:                 my $selected = '';
  913:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  914:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  915:                         $selected = 'selected="selected"';
  916:                     }
  917:                 }
  918:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  919:                               $servers{$hostid}.'</option>';
  920:             }
  921:             $datatable .= '</select></td>'.
  922:                           '<td><select name="'.$lonhost.'_serverpath">';
  923:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  924:                 my $pathname = $path;
  925:                 if ($path eq 'custom') {
  926:                     $pathname = &mt('Custom Path').' ->';
  927:                 }
  928:                 my $selected = '';
  929:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  930:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  931:                         $selected = 'selected="selected"';
  932:                     }
  933:                 } elsif ($path eq '') {
  934:                     $selected = 'selected="selected"';
  935:                 }
  936:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  937:             }
  938:             $datatable .= '</select></td>';
  939:             my ($custom,$exempt);
  940:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  941:                 $custom = $disallowed{$lonhost}{'custompath'};
  942:                 $exempt = $disallowed{$lonhost}{'exempt'};
  943:             }
  944:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  945:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  946:                           '</tr>';
  947:         }
  948:         $datatable .= '</table></td></tr>';
  949:         return $datatable;
  950:     } elsif ($caller eq 'page') {
  951:         my %defaultchecked = ( 
  952:                                'coursecatalog' => 'on',
  953:                                'helpdesk'      => 'on',
  954:                                'adminmail'     => 'off',
  955:                                'newuser'       => 'off',
  956:                              );
  957:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  958:         my (%checkedon,%checkedoff);
  959:         foreach my $item (@toggles) {
  960:             if ($defaultchecked{$item} eq 'on') { 
  961:                 $checkedon{$item} = ' checked="checked" ';
  962:                 $checkedoff{$item} = ' ';
  963:             } elsif ($defaultchecked{$item} eq 'off') {
  964:                 $checkedoff{$item} = ' checked="checked" ';
  965:                 $checkedon{$item} = ' ';
  966:             }
  967:         }
  968:         my @images = ('img','logo','domlogo','login');
  969:         my @logintext = ('textcol','bgcol');
  970:         my @bgs = ('pgbg','mainbg','sidebg');
  971:         my @links = ('link','alink','vlink');
  972:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  973:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  974:         my (%is_custom,%designs);
  975:         my %defaults = (
  976:                        font => $defaultdesign{'login.font'},
  977:                        );
  978:         foreach my $item (@images) {
  979:             $defaults{$item} = $defaultdesign{'login.'.$item};
  980:             $defaults{'showlogo'}{$item} = 1;
  981:         }
  982:         foreach my $item (@bgs) {
  983:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  984:         }
  985:         foreach my $item (@logintext) {
  986:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  987:         }
  988:         foreach my $item (@links) {
  989:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  990:         }
  991:         if (ref($settings) eq 'HASH') {
  992:             foreach my $item (@toggles) {
  993:                 if ($settings->{$item} eq '1') {
  994:                     $checkedon{$item} =  ' checked="checked" ';
  995:                     $checkedoff{$item} = ' ';
  996:                 } elsif ($settings->{$item} eq '0') {
  997:                     $checkedoff{$item} =  ' checked="checked" ';
  998:                     $checkedon{$item} = ' ';
  999:                 }
 1000:             }
 1001:             foreach my $item (@images) {
 1002:                 if (defined($settings->{$item})) {
 1003:                     $designs{$item} = $settings->{$item};
 1004:                     $is_custom{$item} = 1;
 1005:                 }
 1006:                 if (defined($settings->{'showlogo'}{$item})) {
 1007:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1008:                 }
 1009:             }
 1010:             foreach my $item (@logintext) {
 1011:                 if ($settings->{$item} ne '') {
 1012:                     $designs{'logintext'}{$item} = $settings->{$item};
 1013:                     $is_custom{$item} = 1;
 1014:                 }
 1015:             }
 1016:             if ($settings->{'font'} ne '') {
 1017:                 $designs{'font'} = $settings->{'font'};
 1018:                 $is_custom{'font'} = 1;
 1019:             }
 1020:             foreach my $item (@bgs) {
 1021:                 if ($settings->{$item} ne '') {
 1022:                     $designs{'bgs'}{$item} = $settings->{$item};
 1023:                     $is_custom{$item} = 1;
 1024:                 }
 1025:             }
 1026:             foreach my $item (@links) {
 1027:                 if ($settings->{$item} ne '') {
 1028:                     $designs{'links'}{$item} = $settings->{$item};
 1029:                     $is_custom{$item} = 1;
 1030:                 }
 1031:             }
 1032:         } else {
 1033:             if ($designhash{$dom.'.login.font'} ne '') {
 1034:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1035:                 $is_custom{'font'} = 1;
 1036:             }
 1037:             foreach my $item (@images) {
 1038:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1039:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1040:                     $is_custom{$item} = 1;
 1041:                 }
 1042:             }
 1043:             foreach my $item (@bgs) {
 1044:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1045:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1046:                     $is_custom{$item} = 1;
 1047:                 }
 1048:             }
 1049:             foreach my $item (@links) {
 1050:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1051:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1052:                     $is_custom{$item} = 1;
 1053:                 }
 1054:             }
 1055:         }
 1056:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1057:                                                       logo => 'Institution Logo',
 1058:                                                       domlogo => 'Domain Logo',
 1059:                                                       login => 'Login box');
 1060:         my $itemcount = 1;
 1061:         foreach my $item (@toggles) {
 1062:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1063:             $datatable .=  
 1064:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1065:                 '</td><td>'.
 1066:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1067:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1068:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1069:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1070:                 '</tr>';
 1071:             $itemcount ++;
 1072:         }
 1073:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1074:         $datatable .= '</tr></table></td></tr>';
 1075:     } elsif ($caller eq 'help') {
 1076:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1077:         my $switchserver = &check_switchserver($dom,$confname);
 1078:         my $itemcount = 1;
 1079:         $defaulturl = '/adm/loginproblems.html';
 1080:         $defaulttype = 'default';
 1081:         %lt = &Apache::lonlocal::texthash (
 1082:                      del     => 'Delete?',
 1083:                      rep     => 'Replace:',
 1084:                      upl     => 'Upload:',
 1085:                      default => 'Default',
 1086:                      custom  => 'Custom',
 1087:                                              );
 1088:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1089:         my @currlangs;
 1090:         if (ref($settings) eq 'HASH') {
 1091:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1092:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1093:                     next if ($settings->{'helpurl'}{$key} eq '');
 1094:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1095:                     $type{$key} = 'custom';
 1096:                     unless ($key eq 'nolang') {
 1097:                         push(@currlangs,$key);
 1098:                     }
 1099:                 }
 1100:             } elsif ($settings->{'helpurl'} ne '') {
 1101:                 $type{'nolang'} = 'custom';
 1102:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1103:             }
 1104:         }
 1105:         foreach my $lang ('nolang',sort(@currlangs)) {
 1106:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1107:             $datatable .= '<tr'.$css_class.'>';
 1108:             if ($url{$lang} eq '') {
 1109:                 $url{$lang} = $defaulturl;
 1110:             }
 1111:             if ($type{$lang} eq '') {
 1112:                 $type{$lang} = $defaulttype;
 1113:             }
 1114:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1115:             if ($lang eq 'nolang') {
 1116:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1117:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1118:             } else {
 1119:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1120:                                   $langchoices{$lang},
 1121:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1122:             }
 1123:             $datatable .= '</span></td>'."\n".
 1124:                           '<td class="LC_left_item">';
 1125:             if ($type{$lang} eq 'custom') {
 1126:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1127:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1128:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1129:             } else {
 1130:                 $datatable .= $lt{'upl'};
 1131:             }
 1132:             $datatable .='<br />';
 1133:             if ($switchserver) {
 1134:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1135:             } else {
 1136:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1137:             }
 1138:             $datatable .= '</td></tr>';
 1139:             $itemcount ++;
 1140:         }
 1141:         my @addlangs;
 1142:         foreach my $lang (sort(keys(%langchoices))) {
 1143:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1144:             push(@addlangs,$lang);
 1145:         }
 1146:         if (@addlangs > 0) {
 1147:             my %toadd;
 1148:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1149:             $toadd{''} = &mt('Select');
 1150:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1151:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1152:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1153:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1154:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1155:             if ($switchserver) {
 1156:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1157:             } else {
 1158:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1159:             }
 1160:             $datatable .= '</td></tr>';
 1161:             $itemcount ++;
 1162:         }
 1163:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1164:     }
 1165:     return $datatable;
 1166: }
 1167: 
 1168: sub login_choices {
 1169:     my %choices =
 1170:         &Apache::lonlocal::texthash (
 1171:             coursecatalog => 'Display Course/Community Catalog link?',
 1172:             adminmail     => "Display Administrator's E-mail Address?",
 1173:             helpdesk      => 'Display "Contact Helpdesk" link',
 1174:             disallowlogin => "Login page requests redirected",
 1175:             hostid        => "Server",
 1176:             server        => "Redirect to:",
 1177:             serverpath    => "Path",
 1178:             custompath    => "Custom", 
 1179:             exempt        => "Exempt IP(s)",
 1180:             directlogin   => "No redirect",
 1181:             newuser       => "Link to create a user account",
 1182:             img           => "Header",
 1183:             logo          => "Main Logo",
 1184:             domlogo       => "Domain Logo",
 1185:             login         => "Log-in Header", 
 1186:             textcol       => "Text color",
 1187:             bgcol         => "Box color",
 1188:             bgs           => "Background colors",
 1189:             links         => "Link colors",
 1190:             font          => "Font color",
 1191:             pgbg          => "Header",
 1192:             mainbg        => "Page",
 1193:             sidebg        => "Login box",
 1194:             link          => "Link",
 1195:             alink         => "Active link",
 1196:             vlink         => "Visited link",
 1197:         );
 1198:     return %choices;
 1199: }
 1200: 
 1201: sub print_rolecolors {
 1202:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1203:     my %choices = &color_font_choices();
 1204:     my @bgs = ('pgbg','tabbg','sidebg');
 1205:     my @links = ('link','alink','vlink');
 1206:     my @images = ('img');
 1207:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1208:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1209:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1210:     my (%is_custom,%designs);
 1211:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1212:     if (ref($settings) eq 'HASH') {
 1213:         if (ref($settings->{$role}) eq 'HASH') {
 1214:             if ($settings->{$role}->{'img'} ne '') {
 1215:                 $designs{'img'} = $settings->{$role}->{'img'};
 1216:                 $is_custom{'img'} = 1;
 1217:             }
 1218:             if ($settings->{$role}->{'font'} ne '') {
 1219:                 $designs{'font'} = $settings->{$role}->{'font'};
 1220:                 $is_custom{'font'} = 1;
 1221:             }
 1222:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1223:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1224:                 $is_custom{'fontmenu'} = 1;
 1225:             }
 1226:             foreach my $item (@bgs) {
 1227:                 if ($settings->{$role}->{$item} ne '') {
 1228:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1229:                     $is_custom{$item} = 1;
 1230:                 }
 1231:             }
 1232:             foreach my $item (@links) {
 1233:                 if ($settings->{$role}->{$item} ne '') {
 1234:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1235:                     $is_custom{$item} = 1;
 1236:                 }
 1237:             }
 1238:         }
 1239:     } else {
 1240:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1241:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1242:             $is_custom{'img'} = 1;
 1243:         }
 1244:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1245:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1246:             $is_custom{'fontmenu'} = 1; 
 1247:         }
 1248:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1249:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1250:             $is_custom{'font'} = 1;
 1251:         }
 1252:         foreach my $item (@bgs) {
 1253:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1254:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1255:                 $is_custom{$item} = 1;
 1256:             
 1257:             }
 1258:         }
 1259:         foreach my $item (@links) {
 1260:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1261:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1262:                 $is_custom{$item} = 1;
 1263:             }
 1264:         }
 1265:     }
 1266:     my $itemcount = 1;
 1267:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1268:     $datatable .= '</tr></table></td></tr>';
 1269:     return $datatable;
 1270: }
 1271: 
 1272: sub role_defaults {
 1273:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1274:     my %defaults;
 1275:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1276:         return %defaults;
 1277:     }
 1278:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1279:     if ($role eq 'login') {
 1280:         %defaults = (
 1281:                        font => $defaultdesign{$role.'.font'},
 1282:                     );
 1283:         if (ref($logintext) eq 'ARRAY') {
 1284:             foreach my $item (@{$logintext}) {
 1285:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1286:             }
 1287:         }
 1288:         foreach my $item (@{$images}) {
 1289:             $defaults{'showlogo'}{$item} = 1;
 1290:         }
 1291:     } else {
 1292:         %defaults = (
 1293:                        img => $defaultdesign{$role.'.img'},
 1294:                        font => $defaultdesign{$role.'.font'},
 1295:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1296:                     );
 1297:     }
 1298:     foreach my $item (@{$bgs}) {
 1299:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1300:     }
 1301:     foreach my $item (@{$links}) {
 1302:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1303:     }
 1304:     foreach my $item (@{$images}) {
 1305:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1306:     }
 1307:     return %defaults;
 1308: }
 1309: 
 1310: sub display_color_options {
 1311:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1312:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1313:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1314:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1315:     my $datatable = '<tr'.$css_class.'>'.
 1316:         '<td>'.$choices->{'font'}.'</td>';
 1317:     if (!$is_custom->{'font'}) {
 1318:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1319:     } else {
 1320:         $datatable .= '<td>&nbsp;</td>';
 1321:     }
 1322:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1323: 
 1324:     $datatable .= '<td><span class="LC_nobreak">'.
 1325:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1326:                   ' value="'.$current_color.'" />&nbsp;'.
 1327:                   '&nbsp;</td></tr>';
 1328:     unless ($role eq 'login') { 
 1329:         $datatable .= '<tr'.$css_class.'>'.
 1330:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1331:         if (!$is_custom->{'fontmenu'}) {
 1332:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1333:         } else {
 1334:             $datatable .= '<td>&nbsp;</td>';
 1335:         }
 1336: 	$current_color = $designs->{'fontmenu'} ?
 1337: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1338:         $datatable .= '<td><span class="LC_nobreak">'.
 1339:                       '<input class="colorchooser" type="text" size="10" name="'
 1340: 		      .$role.'_fontmenu"'.
 1341:                       ' value="'.$current_color.'" />&nbsp;'.
 1342:                       '&nbsp;</td></tr>';
 1343:     }
 1344:     my $switchserver = &check_switchserver($dom,$confname);
 1345:     foreach my $img (@{$images}) {
 1346: 	$itemcount ++;
 1347:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1348:         $datatable .= '<tr'.$css_class.'>'.
 1349:                       '<td>'.$choices->{$img};
 1350:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1351:         if ($role eq 'login') {
 1352:             if ($img eq 'login') {
 1353:                 $login_hdr_pick =
 1354:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1355:                 $logincolors =
 1356:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1357:                                        $designs,$defaults);
 1358:             } elsif ($img ne 'domlogo') {
 1359:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1360:             }
 1361:         }
 1362:         $datatable .= '</td>';
 1363:         if ($designs->{$img} ne '') {
 1364:             $imgfile = $designs->{$img};
 1365: 	    $img_import = ($imgfile =~ m{^/adm/});
 1366:         } else {
 1367:             $imgfile = $defaults->{$img};
 1368:         }
 1369:         if ($imgfile) {
 1370:             my ($showfile,$fullsize);
 1371:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1372:                 my $urldir = $1;
 1373:                 my $filename = $2;
 1374:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1375:                 if (@info) {
 1376:                     my $thumbfile = 'tn-'.$filename;
 1377:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1378:                     if (@thumb) {
 1379:                         $showfile = $urldir.'/'.$thumbfile;
 1380:                     } else {
 1381:                         $showfile = $imgfile;
 1382:                     }
 1383:                 } else {
 1384:                     $showfile = '';
 1385:                 }
 1386:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1387:                 $showfile = $imgfile;
 1388:                 my $imgdir = $1;
 1389:                 my $filename = $2;
 1390:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1391:                     $showfile = "/$imgdir/tn-".$filename;
 1392:                 } else {
 1393:                     my $input = $londocroot.$imgfile;
 1394:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1395:                     if (!-e $output) {
 1396:                         my ($width,$height) = &thumb_dimensions();
 1397:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1398:                         if ($fullwidth ne '' && $fullheight ne '') {
 1399:                             if ($fullwidth > $width && $fullheight > $height) { 
 1400:                                 my $size = $width.'x'.$height;
 1401:                                 system("convert -sample $size $input $output");
 1402:                                 $showfile = "/$imgdir/tn-".$filename;
 1403:                             }
 1404:                         }
 1405:                     }
 1406:                 }
 1407:             }
 1408:             if ($showfile) {
 1409:                 if ($showfile =~ m{^/(adm|res)/}) {
 1410:                     if ($showfile =~ m{^/res/}) {
 1411:                         my $local_showfile =
 1412:                             &Apache::lonnet::filelocation('',$showfile);
 1413:                         &Apache::lonnet::repcopy($local_showfile);
 1414:                     }
 1415:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1416:                 }
 1417:                 if ($imgfile) {
 1418:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1419:                         if ($imgfile =~ m{^/res/}) {
 1420:                             my $local_imgfile =
 1421:                                 &Apache::lonnet::filelocation('',$imgfile);
 1422:                             &Apache::lonnet::repcopy($local_imgfile);
 1423:                         }
 1424:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1425:                     } else {
 1426:                         $fullsize = $imgfile;
 1427:                     }
 1428:                 }
 1429:                 $datatable .= '<td>';
 1430:                 if ($img eq 'login') {
 1431:                     $datatable .= $login_hdr_pick;
 1432:                 } 
 1433:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1434:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1435:             } else {
 1436:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1437:                               &mt('Upload:').'<br />';
 1438:             }
 1439:         } else {
 1440:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1441:                           &mt('Upload:').'<br />';
 1442:         }
 1443:         if ($switchserver) {
 1444:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1445:         } else {
 1446:             if ($img ne 'login') { # suppress file selection for Log-in header
 1447:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1448:             }
 1449:         }
 1450:         $datatable .= '</td></tr>';
 1451:     }
 1452:     $itemcount ++;
 1453:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1454:     $datatable .= '<tr'.$css_class.'>'.
 1455:                   '<td>'.$choices->{'bgs'}.'</td>';
 1456:     my $bgs_def;
 1457:     foreach my $item (@{$bgs}) {
 1458:         if (!$is_custom->{$item}) {
 1459:             $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>';
 1460:         }
 1461:     }
 1462:     if ($bgs_def) {
 1463:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1464:     } else {
 1465:         $datatable .= '<td>&nbsp;</td>';
 1466:     }
 1467:     $datatable .= '<td class="LC_right_item">'.
 1468:                   '<table border="0"><tr>';
 1469: 
 1470:     foreach my $item (@{$bgs}) {
 1471:         $datatable .= '<td align="center">'.$choices->{$item};
 1472: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1473:         if ($designs->{'bgs'}{$item}) {
 1474:             $datatable .= '&nbsp;';
 1475:         }
 1476:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1477:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1478:     }
 1479:     $datatable .= '</tr></table></td></tr>';
 1480:     $itemcount ++;
 1481:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1482:     $datatable .= '<tr'.$css_class.'>'.
 1483:                   '<td>'.$choices->{'links'}.'</td>';
 1484:     my $links_def;
 1485:     foreach my $item (@{$links}) {
 1486:         if (!$is_custom->{$item}) {
 1487:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1488:         }
 1489:     }
 1490:     if ($links_def) {
 1491:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1492:     } else {
 1493:         $datatable .= '<td>&nbsp;</td>';
 1494:     }
 1495:     $datatable .= '<td class="LC_right_item">'.
 1496:                   '<table border="0"><tr>';
 1497:     foreach my $item (@{$links}) {
 1498: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1499:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1500:         if ($designs->{'links'}{$item}) {
 1501:             $datatable.='&nbsp;';
 1502:         }
 1503:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1504:                       '" /></td>';
 1505:     }
 1506:     $$rowtotal += $itemcount;
 1507:     return $datatable;
 1508: }
 1509: 
 1510: sub logo_display_options {
 1511:     my ($img,$defaults,$designs) = @_;
 1512:     my $checkedon;
 1513:     if (ref($defaults) eq 'HASH') {
 1514:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1515:             if ($defaults->{'showlogo'}{$img}) {
 1516:                 $checkedon = 'checked="checked" ';     
 1517:             }
 1518:         } 
 1519:     }
 1520:     if (ref($designs) eq 'HASH') {
 1521:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1522:             if (defined($designs->{'showlogo'}{$img})) {
 1523:                 if ($designs->{'showlogo'}{$img} == 0) {
 1524:                     $checkedon = '';
 1525:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1526:                     $checkedon = 'checked="checked" ';
 1527:                 }
 1528:             }
 1529:         }
 1530:     }
 1531:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1532:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1533:            &mt('show').'</label>'."\n";
 1534: }
 1535: 
 1536: sub login_header_options  {
 1537:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1538:     my $output = '';
 1539:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1540:         $output .= &mt('Text default(s):').'<br />';
 1541:         if (!$is_custom->{'textcol'}) {
 1542:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1543:                        '&nbsp;&nbsp;&nbsp;';
 1544:         }
 1545:         if (!$is_custom->{'bgcol'}) {
 1546:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1547:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1548:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1549:         }
 1550:         $output .= '<br />';
 1551:     }
 1552:     $output .='<br />';
 1553:     return $output;
 1554: }
 1555: 
 1556: sub login_text_colors {
 1557:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1558:     my $color_menu = '<table border="0"><tr>';
 1559:     foreach my $item (@{$logintext}) {
 1560:         $color_menu .= '<td align="center">'.$choices->{$item};
 1561:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1562:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1563:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1564:     }
 1565:     $color_menu .= '</tr></table><br />';
 1566:     return $color_menu;
 1567: }
 1568: 
 1569: sub image_changes {
 1570:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1571:     my $output;
 1572:     if ($img eq 'login') {
 1573:             # suppress image for Log-in header
 1574:     } elsif (!$is_custom) {
 1575:         if ($img ne 'domlogo') {
 1576:             $output .= &mt('Default image:').'<br />';
 1577:         } else {
 1578:             $output .= &mt('Default in use:').'<br />';
 1579:         }
 1580:     }
 1581:     if ($img eq 'login') { # suppress image for Log-in header
 1582:         $output .= '<td>'.$logincolors;
 1583:     } else {
 1584:         if ($img_import) {
 1585:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1586:         }
 1587:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1588:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1589:         if ($is_custom) {
 1590:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1591:                        '<input type="checkbox" name="'.
 1592:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1593:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1594:         } else {
 1595:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1596:         }
 1597:     }
 1598:     return $output;
 1599: }
 1600: 
 1601: sub print_quotas {
 1602:     my ($dom,$settings,$rowtotal,$action) = @_;
 1603:     my $context;
 1604:     if ($action eq 'quotas') {
 1605:         $context = 'tools';
 1606:     } else {
 1607:         $context = $action;
 1608:     }
 1609:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1610:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1611:     my $typecount = 0;
 1612:     my ($css_class,%titles);
 1613:     if ($context eq 'requestcourses') {
 1614:         @usertools = ('official','unofficial','community','textbook');
 1615:         @options =('norequest','approval','validate','autolimit');
 1616:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1617:         %titles = &courserequest_titles();
 1618:     } elsif ($context eq 'requestauthor') {
 1619:         @usertools = ('author');
 1620:         @options = ('norequest','approval','automatic');
 1621:         %titles = &authorrequest_titles();
 1622:     } else {
 1623:         @usertools = ('aboutme','blog','webdav','portfolio');
 1624:         %titles = &tool_titles();
 1625:     }
 1626:     if (ref($types) eq 'ARRAY') {
 1627:         foreach my $type (@{$types}) {
 1628:             my ($currdefquota,$currauthorquota);
 1629:             unless (($context eq 'requestcourses') ||
 1630:                     ($context eq 'requestauthor')) {
 1631:                 if (ref($settings) eq 'HASH') {
 1632:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1633:                         $currdefquota = $settings->{defaultquota}->{$type};
 1634:                     } else {
 1635:                         $currdefquota = $settings->{$type};
 1636:                     }
 1637:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1638:                         $currauthorquota = $settings->{authorquota}->{$type};
 1639:                     }
 1640:                 }
 1641:             }
 1642:             if (defined($usertypes->{$type})) {
 1643:                 $typecount ++;
 1644:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1645:                 $datatable .= '<tr'.$css_class.'>'.
 1646:                               '<td>'.$usertypes->{$type}.'</td>'.
 1647:                               '<td class="LC_left_item">';
 1648:                 if ($context eq 'requestcourses') {
 1649:                     $datatable .= '<table><tr>';
 1650:                 }
 1651:                 my %cell;  
 1652:                 foreach my $item (@usertools) {
 1653:                     if ($context eq 'requestcourses') {
 1654:                         my ($curroption,$currlimit);
 1655:                         if (ref($settings) eq 'HASH') {
 1656:                             if (ref($settings->{$item}) eq 'HASH') {
 1657:                                 $curroption = $settings->{$item}->{$type};
 1658:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1659:                                     $currlimit = $1; 
 1660:                                 }
 1661:                             }
 1662:                         }
 1663:                         if (!$curroption) {
 1664:                             $curroption = 'norequest';
 1665:                         }
 1666:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1667:                         foreach my $option (@options) {
 1668:                             my $val = $option;
 1669:                             if ($option eq 'norequest') {
 1670:                                 $val = 0;  
 1671:                             }
 1672:                             if ($option eq 'validate') {
 1673:                                 my $canvalidate = 0;
 1674:                                 if (ref($validations{$item}) eq 'HASH') { 
 1675:                                     if ($validations{$item}{$type}) {
 1676:                                         $canvalidate = 1;
 1677:                                     }
 1678:                                 }
 1679:                                 next if (!$canvalidate);
 1680:                             }
 1681:                             my $checked = '';
 1682:                             if ($option eq $curroption) {
 1683:                                 $checked = ' checked="checked"';
 1684:                             } elsif ($option eq 'autolimit') {
 1685:                                 if ($curroption =~ /^autolimit/) {
 1686:                                     $checked = ' checked="checked"';
 1687:                                 }                       
 1688:                             } 
 1689:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1690:                                   '<input type="radio" name="crsreq_'.$item.
 1691:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1692:                                   $titles{$option}.'</label>';
 1693:                             if ($option eq 'autolimit') {
 1694:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1695:                                                 $item.'_limit_'.$type.'" size="1" '.
 1696:                                                 'value="'.$currlimit.'" />';
 1697:                             }
 1698:                             $cell{$item} .= '</span> ';
 1699:                             if ($option eq 'autolimit') {
 1700:                                 $cell{$item} .= $titles{'unlimited'};
 1701:                             }
 1702:                         }
 1703:                     } elsif ($context eq 'requestauthor') {
 1704:                         my $curroption;
 1705:                         if (ref($settings) eq 'HASH') {
 1706:                             $curroption = $settings->{$type};
 1707:                         }
 1708:                         if (!$curroption) {
 1709:                             $curroption = 'norequest';
 1710:                         }
 1711:                         foreach my $option (@options) {
 1712:                             my $val = $option;
 1713:                             if ($option eq 'norequest') {
 1714:                                 $val = 0;
 1715:                             }
 1716:                             my $checked = '';
 1717:                             if ($option eq $curroption) {
 1718:                                 $checked = ' checked="checked"';
 1719:                             }
 1720:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1721:                                   '<input type="radio" name="authorreq_'.$type.
 1722:                                   '" value="'.$val.'"'.$checked.' />'.
 1723:                                   $titles{$option}.'</label></span>&nbsp; ';
 1724:                         }
 1725:                     } else {
 1726:                         my $checked = 'checked="checked" ';
 1727:                         if (ref($settings) eq 'HASH') {
 1728:                             if (ref($settings->{$item}) eq 'HASH') {
 1729:                                 if ($settings->{$item}->{$type} == 0) {
 1730:                                     $checked = '';
 1731:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1732:                                     $checked =  'checked="checked" ';
 1733:                                 }
 1734:                             }
 1735:                         }
 1736:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1737:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1738:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1739:                                       '</label></span>&nbsp; ';
 1740:                     }
 1741:                 }
 1742:                 if ($context eq 'requestcourses') {
 1743:                     $datatable .= '</tr><tr>';
 1744:                     foreach my $item (@usertools) {
 1745:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1746:                     }
 1747:                     $datatable .= '</tr></table>';
 1748:                 }
 1749:                 $datatable .= '</td>';
 1750:                 unless (($context eq 'requestcourses') ||
 1751:                         ($context eq 'requestauthor')) {
 1752:                     $datatable .= 
 1753:                               '<td class="LC_right_item">'.
 1754:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1755:                               '<input type="text" name="quota_'.$type.
 1756:                               '" value="'.$currdefquota.
 1757:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1758:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1759:                               '<input type="text" name="authorquota_'.$type.
 1760:                               '" value="'.$currauthorquota.
 1761:                               '" size="5" /></span></td>';
 1762:                 }
 1763:                 $datatable .= '</tr>';
 1764:             }
 1765:         }
 1766:     }
 1767:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1768:         $defaultquota = '20';
 1769:         $authorquota = '500';
 1770:         if (ref($settings) eq 'HASH') {
 1771:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1772:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1773:             } elsif (defined($settings->{'default'})) {
 1774:                 $defaultquota = $settings->{'default'};
 1775:             }
 1776:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1777:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1778:             }
 1779:         }
 1780:     }
 1781:     $typecount ++;
 1782:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1783:     $datatable .= '<tr'.$css_class.'>'.
 1784:                   '<td>'.$othertitle.'</td>'.
 1785:                   '<td class="LC_left_item">';
 1786:     if ($context eq 'requestcourses') {
 1787:         $datatable .= '<table><tr>';
 1788:     }
 1789:     my %defcell;
 1790:     foreach my $item (@usertools) {
 1791:         if ($context eq 'requestcourses') {
 1792:             my ($curroption,$currlimit);
 1793:             if (ref($settings) eq 'HASH') {
 1794:                 if (ref($settings->{$item}) eq 'HASH') {
 1795:                     $curroption = $settings->{$item}->{'default'};
 1796:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1797:                         $currlimit = $1;
 1798:                     }
 1799:                 }
 1800:             }
 1801:             if (!$curroption) {
 1802:                 $curroption = 'norequest';
 1803:             }
 1804:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1805:             foreach my $option (@options) {
 1806:                 my $val = $option;
 1807:                 if ($option eq 'norequest') {
 1808:                     $val = 0;
 1809:                 }
 1810:                 if ($option eq 'validate') {
 1811:                     my $canvalidate = 0;
 1812:                     if (ref($validations{$item}) eq 'HASH') {
 1813:                         if ($validations{$item}{'default'}) {
 1814:                             $canvalidate = 1;
 1815:                         }
 1816:                     }
 1817:                     next if (!$canvalidate);
 1818:                 }
 1819:                 my $checked = '';
 1820:                 if ($option eq $curroption) {
 1821:                     $checked = ' checked="checked"';
 1822:                 } elsif ($option eq 'autolimit') {
 1823:                     if ($curroption =~ /^autolimit/) {
 1824:                         $checked = ' checked="checked"';
 1825:                     }
 1826:                 }
 1827:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1828:                                   '<input type="radio" name="crsreq_'.$item.
 1829:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1830:                                   $titles{$option}.'</label>';
 1831:                 if ($option eq 'autolimit') {
 1832:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1833:                                        $item.'_limit_default" size="1" '.
 1834:                                        'value="'.$currlimit.'" />';
 1835:                 }
 1836:                 $defcell{$item} .= '</span> ';
 1837:                 if ($option eq 'autolimit') {
 1838:                     $defcell{$item} .= $titles{'unlimited'};
 1839:                 }
 1840:             }
 1841:         } elsif ($context eq 'requestauthor') {
 1842:             my $curroption;
 1843:             if (ref($settings) eq 'HASH') {
 1844:                 $curroption = $settings->{'default'};
 1845:             }
 1846:             if (!$curroption) {
 1847:                 $curroption = 'norequest';
 1848:             }
 1849:             foreach my $option (@options) {
 1850:                 my $val = $option;
 1851:                 if ($option eq 'norequest') {
 1852:                     $val = 0;
 1853:                 }
 1854:                 my $checked = '';
 1855:                 if ($option eq $curroption) {
 1856:                     $checked = ' checked="checked"';
 1857:                 }
 1858:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1859:                               '<input type="radio" name="authorreq_default"'.
 1860:                               ' value="'.$val.'"'.$checked.' />'.
 1861:                               $titles{$option}.'</label></span>&nbsp; ';
 1862:             }
 1863:         } else {
 1864:             my $checked = 'checked="checked" ';
 1865:             if (ref($settings) eq 'HASH') {
 1866:                 if (ref($settings->{$item}) eq 'HASH') {
 1867:                     if ($settings->{$item}->{'default'} == 0) {
 1868:                         $checked = '';
 1869:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1870:                         $checked = 'checked="checked" ';
 1871:                     }
 1872:                 }
 1873:             }
 1874:             $datatable .= '<span class="LC_nobreak"><label>'.
 1875:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1876:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1877:                           '</label></span>&nbsp; ';
 1878:         }
 1879:     }
 1880:     if ($context eq 'requestcourses') {
 1881:         $datatable .= '</tr><tr>';
 1882:         foreach my $item (@usertools) {
 1883:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1884:         }
 1885:         $datatable .= '</tr></table>';
 1886:     }
 1887:     $datatable .= '</td>';
 1888:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1889:         $datatable .= '<td class="LC_right_item">'.
 1890:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1891:                       '<input type="text" name="defaultquota" value="'.
 1892:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1893:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1894:                       '<input type="text" name="authorquota" value="'.
 1895:                       $authorquota.'" size="5" /></span></td>';
 1896:     }
 1897:     $datatable .= '</tr>';
 1898:     $typecount ++;
 1899:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1900:     $datatable .= '<tr'.$css_class.'>'.
 1901:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1902:     if ($context eq 'requestcourses') {
 1903:         $datatable .= &mt('(overrides affiliation, if set)').
 1904:                       '</td>'.
 1905:                       '<td class="LC_left_item">'.
 1906:                       '<table><tr>';
 1907:     } else {
 1908:         $datatable .= &mt('(overrides affiliation, if checked)').
 1909:                       '</td>'.
 1910:                       '<td class="LC_left_item" colspan="2">'.
 1911:                       '<br />';
 1912:     }
 1913:     my %advcell;
 1914:     foreach my $item (@usertools) {
 1915:         if ($context eq 'requestcourses') {
 1916:             my ($curroption,$currlimit);
 1917:             if (ref($settings) eq 'HASH') {
 1918:                 if (ref($settings->{$item}) eq 'HASH') {
 1919:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1920:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1921:                         $currlimit = $1;
 1922:                     }
 1923:                 }
 1924:             }
 1925:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1926:             my $checked = '';
 1927:             if ($curroption eq '') {
 1928:                 $checked = ' checked="checked"';
 1929:             }
 1930:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1931:                                '<input type="radio" name="crsreq_'.$item.
 1932:                                '__LC_adv" value=""'.$checked.' />'.
 1933:                                &mt('No override set').'</label></span>&nbsp; ';
 1934:             foreach my $option (@options) {
 1935:                 my $val = $option;
 1936:                 if ($option eq 'norequest') {
 1937:                     $val = 0;
 1938:                 }
 1939:                 if ($option eq 'validate') {
 1940:                     my $canvalidate = 0;
 1941:                     if (ref($validations{$item}) eq 'HASH') {
 1942:                         if ($validations{$item}{'_LC_adv'}) {
 1943:                             $canvalidate = 1;
 1944:                         }
 1945:                     }
 1946:                     next if (!$canvalidate);
 1947:                 }
 1948:                 my $checked = '';
 1949:                 if ($val eq $curroption) {
 1950:                     $checked = ' checked="checked"';
 1951:                 } elsif ($option eq 'autolimit') {
 1952:                     if ($curroption =~ /^autolimit/) {
 1953:                         $checked = ' checked="checked"';
 1954:                     }
 1955:                 }
 1956:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1957:                                   '<input type="radio" name="crsreq_'.$item.
 1958:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1959:                                   $titles{$option}.'</label>';
 1960:                 if ($option eq 'autolimit') {
 1961:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1962:                                        $item.'_limit__LC_adv" size="1" '.
 1963:                                        'value="'.$currlimit.'" />';
 1964:                 }
 1965:                 $advcell{$item} .= '</span> ';
 1966:                 if ($option eq 'autolimit') {
 1967:                     $advcell{$item} .= $titles{'unlimited'};
 1968:                 }
 1969:             }
 1970:         } elsif ($context eq 'requestauthor') {
 1971:             my $curroption;
 1972:             if (ref($settings) eq 'HASH') {
 1973:                 $curroption = $settings->{'_LC_adv'};
 1974:             }
 1975:             my $checked = '';
 1976:             if ($curroption eq '') {
 1977:                 $checked = ' checked="checked"';
 1978:             }
 1979:             $datatable .= '<span class="LC_nobreak"><label>'.
 1980:                           '<input type="radio" name="authorreq__LC_adv"'.
 1981:                           ' value=""'.$checked.' />'.
 1982:                           &mt('No override set').'</label></span>&nbsp; ';
 1983:             foreach my $option (@options) {
 1984:                 my $val = $option;
 1985:                 if ($option eq 'norequest') {
 1986:                     $val = 0;
 1987:                 }
 1988:                 my $checked = '';
 1989:                 if ($val eq $curroption) {
 1990:                     $checked = ' checked="checked"';
 1991:                 }
 1992:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1993:                               '<input type="radio" name="authorreq__LC_adv"'.
 1994:                               ' value="'.$val.'"'.$checked.' />'.
 1995:                               $titles{$option}.'</label></span>&nbsp; ';
 1996:             }
 1997:         } else {
 1998:             my $checked = 'checked="checked" ';
 1999:             if (ref($settings) eq 'HASH') {
 2000:                 if (ref($settings->{$item}) eq 'HASH') {
 2001:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2002:                         $checked = '';
 2003:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2004:                         $checked = 'checked="checked" ';
 2005:                     }
 2006:                 }
 2007:             }
 2008:             $datatable .= '<span class="LC_nobreak"><label>'.
 2009:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2010:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2011:                           '</label></span>&nbsp; ';
 2012:         }
 2013:     }
 2014:     if ($context eq 'requestcourses') {
 2015:         $datatable .= '</tr><tr>';
 2016:         foreach my $item (@usertools) {
 2017:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2018:         }
 2019:         $datatable .= '</tr></table>';
 2020:     }
 2021:     $datatable .= '</td></tr>';
 2022:     $$rowtotal += $typecount;
 2023:     return $datatable;
 2024: }
 2025: 
 2026: sub print_requestmail {
 2027:     my ($dom,$action,$settings,$rowtotal) = @_;
 2028:     my ($now,$datatable,%currapp);
 2029:     $now = time;
 2030:     if (ref($settings) eq 'HASH') {
 2031:         if (ref($settings->{'notify'}) eq 'HASH') {
 2032:             if ($settings->{'notify'}{'approval'} ne '') {
 2033:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2034:             }
 2035:         }
 2036:     }
 2037:     my $numinrow = 2;
 2038:     my $css_class;
 2039:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2040:     my $text;
 2041:     if ($action eq 'requestcourses') {
 2042:         $text = &mt('Receive notification of course requests requiring approval');
 2043:     } elsif ($action eq 'requestauthor') {
 2044:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2045:     } else {
 2046:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2047:     }
 2048:     $datatable = '<tr'.$css_class.'>'.
 2049:                  ' <td>'.$text.'</td>'.
 2050:                  ' <td class="LC_left_item">';
 2051:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2052:                                                  $action.'notifyapproval',%currapp);
 2053:     if ($numdc > 0) {
 2054:         $datatable .= $table;
 2055:     } else {
 2056:         $datatable .= &mt('There are no active Domain Coordinators');
 2057:     }
 2058:     $datatable .='</td></tr>';
 2059:     $$rowtotal += $rows;
 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->{'uniquecode'}) eq 'HASH') {
 2069:         foreach my $type (@crstypes) {
 2070:             $current{$type} = $settings->{'uniquecode'}{$type};
 2071:         }
 2072:     }
 2073:     $output .= '<tr>'.
 2074:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2075:                '<td class="LC_left_item">';
 2076:     foreach my $type (@crstypes) {
 2077:         my $check = ' ';
 2078:         if ($current{$type}) {
 2079:             $check = ' checked="checked" ';
 2080:         }
 2081:         $output .= '<span class="LC_nobreak"><label>'.
 2082:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2083:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2084:     }
 2085:     $output .= '</td></tr>';
 2086:     $$rowtotal ++;
 2087:     return $output;
 2088: }
 2089: 
 2090: sub print_textbookcourses {
 2091:     my ($dom,$type,$settings,$rowtotal) = @_;
 2092:     my $rownum = 0;
 2093:     my $css_class;
 2094:     my $itemcount = 1;
 2095:     my $maxnum = 0;
 2096:     my $bookshash;
 2097:     if (ref($settings) eq 'HASH') {
 2098:         $bookshash = $settings->{$type};
 2099:     }
 2100:     my %ordered;
 2101:     if (ref($bookshash) eq 'HASH') {
 2102:         foreach my $item (keys(%{$bookshash})) {
 2103:             if (ref($bookshash->{$item}) eq 'HASH') {
 2104:                 my $num = $bookshash->{$item}{'order'};
 2105:                 $ordered{$num} = $item;
 2106:             }
 2107:         }
 2108:     }
 2109:     my $confname = $dom.'-domainconfig';
 2110:     my $switchserver = &check_switchserver($dom,$confname);
 2111:     my $maxnum = scalar(keys(%ordered));
 2112:     my $datatable;
 2113:     if (keys(%ordered)) {
 2114:         my @items = sort { $a <=> $b } keys(%ordered);
 2115:         for (my $i=0; $i<@items; $i++) {
 2116:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2117:             my $key = $ordered{$items[$i]};
 2118:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2119:             my $coursetitle = $coursehash{'description'};
 2120:             my ($subject,$title,$author,$image,$imgsrc,$cdom,$cnum);
 2121:             if (ref($bookshash->{$key}) eq 'HASH') {
 2122:                 $subject = $bookshash->{$key}->{'subject'};
 2123:                 $title = $bookshash->{$key}->{'title'};
 2124:                 if ($type eq 'textbooks') {
 2125:                     $author = $bookshash->{$key}->{'author'};
 2126:                     $image = $bookshash->{$key}->{'image'};
 2127:                     if ($image ne '') {
 2128:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2129:                         my $imagethumb = "$path/tn-".$imagefile;
 2130:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2131:                     }
 2132:                 }
 2133:             }
 2134:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2135:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2136:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2137:             for (my $k=0; $k<=$maxnum; $k++) {
 2138:                 my $vpos = $k+1;
 2139:                 my $selstr;
 2140:                 if ($k == $i) {
 2141:                     $selstr = ' selected="selected" ';
 2142:                 }
 2143:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2144:             }
 2145:             $datatable .= '</select>'.('&nbsp;'x2).
 2146:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2147:                 &mt('Delete?').'</label></span></td>'.
 2148:                 '<td colspan="2">'.
 2149:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2150:                 ('&nbsp;'x2).
 2151:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2152:             if ($type eq 'textbooks') {
 2153:                 $datatable .= ('&nbsp;'x2).
 2154:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2155:                               ('&nbsp;'x2).
 2156:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2157:                 if ($image) {
 2158:                     $datatable .= '<span class="LC_nobreak">'.
 2159:                                   $imgsrc.
 2160:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2161:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2162:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2163:                 }
 2164:                 if ($switchserver) {
 2165:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2166:                 } else {
 2167:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2168:                 }
 2169:             }
 2170:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2171:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2172:                           $coursetitle.'</span></td></tr>'."\n";
 2173:             $itemcount ++;
 2174:         }
 2175:     }
 2176:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2177:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2178:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2179:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2180:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2181:     for (my $k=0; $k<$maxnum+1; $k++) {
 2182:         my $vpos = $k+1;
 2183:         my $selstr;
 2184:         if ($k == $maxnum) {
 2185:             $selstr = ' selected="selected" ';
 2186:         }
 2187:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2188:     }
 2189:     $datatable .= '</select>&nbsp;'."\n".
 2190:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2191:                   '<td colspan="2">'.
 2192:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2193:                   ('&nbsp;'x2).
 2194:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2195:                   ('&nbsp;'x2);
 2196:     if ($type eq 'textbooks') {
 2197:         $datatable .= '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2198:                       ('&nbsp;'x2).
 2199:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2200:         if ($switchserver) {
 2201:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2202:         } else {
 2203:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2204:         }
 2205:     }
 2206:     $datatable .= '</span>'."\n".
 2207:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2208:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2209:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2210:                   &Apache::loncommon::selectcourse_link
 2211:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2212:                   '</span></td>'."\n".
 2213:                   '</tr>'."\n";
 2214:     $itemcount ++;
 2215:     return $datatable;
 2216: }
 2217: 
 2218: sub textbookcourses_javascript {
 2219:     my ($settings) = @_;
 2220:     return unless(ref($settings) eq 'HASH');
 2221:     my (%ordered,%total,%jstext);
 2222:     foreach my $type ('textbooks','templates') {
 2223:         $total{$type} = 0;
 2224:         if (ref($settings->{$type}) eq 'HASH') {
 2225:             foreach my $item (keys(%{$settings->{$type}})) {
 2226:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2227:                     my $num = $settings->{$type}->{$item}{'order'};
 2228:                     $ordered{$type}{$num} = $item;
 2229:                 }
 2230:             }
 2231:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2232:         }
 2233:         my @jsarray = ();
 2234:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2235:             push(@jsarray,$ordered{$type}{$item});
 2236:         }
 2237:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2238:     }
 2239:     return <<"ENDSCRIPT";
 2240: <script type="text/javascript">
 2241: // <![CDATA[
 2242: function reorderBooks(form,item,caller) {
 2243:     var changedVal;
 2244: $jstext{'textbooks'};
 2245: $jstext{'templates'};
 2246:     var newpos;
 2247:     var maxh;
 2248:     if (caller == 'textbooks') {  
 2249:         newpos = 'textbooks_addbook_pos';
 2250:         maxh = 1 + $total{'textbooks'};
 2251:     } else {
 2252:         newpos = 'templates_addbook_pos';
 2253:         maxh = 1 + $total{'templates'};
 2254:     }
 2255:     var current = new Array;
 2256:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2257:     if (item == newpos) {
 2258:         changedVal = newitemVal;
 2259:     } else {
 2260:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2261:         current[newitemVal] = newpos;
 2262:     }
 2263:     if (caller == 'textbooks') {
 2264:         for (var i=0; i<textbooks.length; i++) {
 2265:             var elementName = 'textbooks_'+textbooks[i];
 2266:             if (elementName != item) {
 2267:                 if (form.elements[elementName]) {
 2268:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2269:                     current[currVal] = elementName;
 2270:                 }
 2271:             }
 2272:         }
 2273:     }
 2274:     if (caller == 'templates') {
 2275:         for (var i=0; i<templates.length; i++) {
 2276:             var elementName = 'templates_'+templates[i];
 2277:             if (elementName != item) {
 2278:                 if (form.elements[elementName]) {
 2279:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2280:                     current[currVal] = elementName;
 2281:                 }
 2282:             }
 2283:         }
 2284:     }
 2285:     var oldVal;
 2286:     for (var j=0; j<maxh; j++) {
 2287:         if (current[j] == undefined) {
 2288:             oldVal = j;
 2289:         }
 2290:     }
 2291:     if (oldVal < changedVal) {
 2292:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2293:            var elementName = current[k];
 2294:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2295:         }
 2296:     } else {
 2297:         for (var k=changedVal; k<oldVal; k++) {
 2298:             var elementName = current[k];
 2299:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2300:         }
 2301:     }
 2302:     return;
 2303: }
 2304: 
 2305: // ]]>
 2306: </script>
 2307: 
 2308: ENDSCRIPT
 2309: }
 2310: 
 2311: sub print_autoenroll {
 2312:     my ($dom,$settings,$rowtotal) = @_;
 2313:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2314:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2315:     if (ref($settings) eq 'HASH') {
 2316:         if (exists($settings->{'run'})) {
 2317:             if ($settings->{'run'} eq '0') {
 2318:                 $runoff = ' checked="checked" ';
 2319:                 $runon = ' ';
 2320:             } else {
 2321:                 $runon = ' checked="checked" ';
 2322:                 $runoff = ' ';
 2323:             }
 2324:         } else {
 2325:             if ($autorun) {
 2326:                 $runon = ' checked="checked" ';
 2327:                 $runoff = ' ';
 2328:             } else {
 2329:                 $runoff = ' checked="checked" ';
 2330:                 $runon = ' ';
 2331:             }
 2332:         }
 2333:         if (exists($settings->{'co-owners'})) {
 2334:             if ($settings->{'co-owners'} eq '0') {
 2335:                 $coownersoff = ' checked="checked" ';
 2336:                 $coownerson = ' ';
 2337:             } else {
 2338:                 $coownerson = ' checked="checked" ';
 2339:                 $coownersoff = ' ';
 2340:             }
 2341:         } else {
 2342:             $coownersoff = ' checked="checked" ';
 2343:             $coownerson = ' ';
 2344:         }
 2345:         if (exists($settings->{'sender_domain'})) {
 2346:             $defdom = $settings->{'sender_domain'};
 2347:         }
 2348:     } else {
 2349:         if ($autorun) {
 2350:             $runon = ' checked="checked" ';
 2351:             $runoff = ' ';
 2352:         } else {
 2353:             $runoff = ' checked="checked" ';
 2354:             $runon = ' ';
 2355:         }
 2356:     }
 2357:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2358:     my $notif_sender;
 2359:     if (ref($settings) eq 'HASH') {
 2360:         $notif_sender = $settings->{'sender_uname'};
 2361:     }
 2362:     my $datatable='<tr class="LC_odd_row">'.
 2363:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2364:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2365:                   '<input type="radio" name="autoenroll_run"'.
 2366:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2367:                   '<label><input type="radio" name="autoenroll_run"'.
 2368:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2369:                   '</tr><tr>'.
 2370:                   '<td>'.&mt('Notification messages - sender').
 2371:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2372:                   &mt('username').':&nbsp;'.
 2373:                   '<input type="text" name="sender_uname" value="'.
 2374:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2375:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2376:                   '<tr class="LC_odd_row">'.
 2377:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2378:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2379:                   '<input type="radio" name="autoassign_coowners"'.
 2380:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2381:                   '<label><input type="radio" name="autoassign_coowners"'.
 2382:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2383:                   '</tr>';
 2384:     $$rowtotal += 3;
 2385:     return $datatable;
 2386: }
 2387: 
 2388: sub print_autoupdate {
 2389:     my ($position,$dom,$settings,$rowtotal) = @_;
 2390:     my $datatable;
 2391:     if ($position eq 'top') {
 2392:         my $updateon = ' ';
 2393:         my $updateoff = ' checked="checked" ';
 2394:         my $classlistson = ' ';
 2395:         my $classlistsoff = ' checked="checked" ';
 2396:         if (ref($settings) eq 'HASH') {
 2397:             if ($settings->{'run'} eq '1') {
 2398:                 $updateon = $updateoff;
 2399:                 $updateoff = ' ';
 2400:             }
 2401:             if ($settings->{'classlists'} eq '1') {
 2402:                 $classlistson = $classlistsoff;
 2403:                 $classlistsoff = ' ';
 2404:             }
 2405:         }
 2406:         my %title = (
 2407:                    run => 'Auto-update active?',
 2408:                    classlists => 'Update information in classlists?',
 2409:                     );
 2410:         $datatable = '<tr class="LC_odd_row">'. 
 2411:                   '<td>'.&mt($title{'run'}).'</td>'.
 2412:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2413:                   '<input type="radio" name="autoupdate_run"'.
 2414:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2415:                   '<label><input type="radio" name="autoupdate_run"'.
 2416:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2417:                   '</tr><tr>'.
 2418:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2419:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2420:                   '<label><input type="radio" name="classlists"'.
 2421:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2422:                   '<label><input type="radio" name="classlists"'.
 2423:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2424:                   '</tr>';
 2425:         $$rowtotal += 2;
 2426:     } elsif ($position eq 'middle') {
 2427:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2428:         my $numinrow = 3;
 2429:         my $locknamesettings;
 2430:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2431:                                      $dom,$numinrow,$othertitle,
 2432:                                     'lockablenames');
 2433:         $$rowtotal ++;
 2434:     } else {
 2435:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2436:         my @fields = ('lastname','firstname','middlename','generation',
 2437:                       'permanentemail','id');
 2438:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2439:         my $numrows = 0;
 2440:         if (ref($types) eq 'ARRAY') {
 2441:             if (@{$types} > 0) {
 2442:                 $datatable = 
 2443:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2444:                                          \@fields,$types,\$numrows);
 2445:                     $$rowtotal += @{$types}; 
 2446:             }
 2447:         }
 2448:         $datatable .= 
 2449:             &usertype_update_row($settings,{'default' => $othertitle},
 2450:                                  \%fieldtitles,\@fields,['default'],
 2451:                                  \$numrows);
 2452:         $$rowtotal ++;     
 2453:     }
 2454:     return $datatable;
 2455: }
 2456: 
 2457: sub print_autocreate {
 2458:     my ($dom,$settings,$rowtotal) = @_;
 2459:     my (%createon,%createoff,%currhash);
 2460:     my @types = ('xml','req');
 2461:     if (ref($settings) eq 'HASH') {
 2462:         foreach my $item (@types) {
 2463:             $createoff{$item} = ' checked="checked" ';
 2464:             $createon{$item} = ' ';
 2465:             if (exists($settings->{$item})) {
 2466:                 if ($settings->{$item}) {
 2467:                     $createon{$item} = ' checked="checked" ';
 2468:                     $createoff{$item} = ' ';
 2469:                 }
 2470:             }
 2471:         }
 2472:         if ($settings->{'xmldc'} ne '') {
 2473:             $currhash{$settings->{'xmldc'}} = 1;
 2474:         }
 2475:     } else {
 2476:         foreach my $item (@types) {
 2477:             $createoff{$item} = ' checked="checked" ';
 2478:             $createon{$item} = ' ';
 2479:         }
 2480:     }
 2481:     $$rowtotal += 2;
 2482:     my $numinrow = 2;
 2483:     my $datatable='<tr class="LC_odd_row">'.
 2484:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2485:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2486:                   '<input type="radio" name="autocreate_xml"'.
 2487:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2488:                   '<label><input type="radio" name="autocreate_xml"'.
 2489:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2490:                   '</td></tr><tr>'.
 2491:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2492:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2493:                   '<input type="radio" name="autocreate_req"'.
 2494:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2495:                   '<label><input type="radio" name="autocreate_req"'.
 2496:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2497:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2498:                                                    'autocreate_xmldc',%currhash);
 2499:     if ($numdc > 1) {
 2500:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2501:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2502:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2503:     } else {
 2504:         $datatable .= $dctable.'</td></tr>';
 2505:     }
 2506:     $$rowtotal += $rows;
 2507:     return $datatable;
 2508: }
 2509: 
 2510: sub print_directorysrch {
 2511:     my ($dom,$settings,$rowtotal) = @_;
 2512:     my $srchon = ' ';
 2513:     my $srchoff = ' checked="checked" ';
 2514:     my ($exacton,$containson,$beginson);
 2515:     my $localon = ' ';
 2516:     my $localoff = ' checked="checked" ';
 2517:     if (ref($settings) eq 'HASH') {
 2518:         if ($settings->{'available'} eq '1') {
 2519:             $srchon = $srchoff;
 2520:             $srchoff = ' ';
 2521:         }
 2522:         if ($settings->{'localonly'} eq '1') {
 2523:             $localon = $localoff;
 2524:             $localoff = ' ';
 2525:         }
 2526:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2527:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2528:                 if ($type eq 'exact') {
 2529:                     $exacton = ' checked="checked" ';
 2530:                 } elsif ($type eq 'contains') {
 2531:                     $containson = ' checked="checked" ';
 2532:                 } elsif ($type eq 'begins') {
 2533:                     $beginson = ' checked="checked" ';
 2534:                 }
 2535:             }
 2536:         } else {
 2537:             if ($settings->{'searchtypes'} eq 'exact') {
 2538:                 $exacton = ' checked="checked" ';
 2539:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2540:                 $containson = ' checked="checked" ';
 2541:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2542:                 $exacton = ' checked="checked" ';
 2543:                 $containson = ' checked="checked" ';
 2544:             }
 2545:         }
 2546:     }
 2547:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2548:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2549: 
 2550:     my $numinrow = 4;
 2551:     my $cansrchrow = 0;
 2552:     my $datatable='<tr class="LC_odd_row">'.
 2553:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2554:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2555:                   '<input type="radio" name="dirsrch_available"'.
 2556:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2557:                   '<label><input type="radio" name="dirsrch_available"'.
 2558:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2559:                   '</tr><tr>'.
 2560:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2561:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2562:                   '<input type="radio" name="dirsrch_localonly"'.
 2563:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2564:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2565:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2566:                   '</tr>';
 2567:     $$rowtotal += 2;
 2568:     if (ref($usertypes) eq 'HASH') {
 2569:         if (keys(%{$usertypes}) > 0) {
 2570:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2571:                                          $numinrow,$othertitle,'cansearch');
 2572:             $cansrchrow = 1;
 2573:         }
 2574:     }
 2575:     if ($cansrchrow) {
 2576:         $$rowtotal ++;
 2577:         $datatable .= '<tr>';
 2578:     } else {
 2579:         $datatable .= '<tr class="LC_odd_row">';
 2580:     }
 2581:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2582:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2583:     foreach my $title (@{$titleorder}) {
 2584:         if (defined($searchtitles->{$title})) {
 2585:             my $check = ' ';
 2586:             if (ref($settings) eq 'HASH') {
 2587:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2588:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2589:                         $check = ' checked="checked" ';
 2590:                     }
 2591:                 }
 2592:             }
 2593:             $datatable .= '<td class="LC_left_item">'.
 2594:                           '<span class="LC_nobreak"><label>'.
 2595:                           '<input type="checkbox" name="searchby" '.
 2596:                           'value="'.$title.'"'.$check.'/>'.
 2597:                           $searchtitles->{$title}.'</label></span></td>';
 2598:         }
 2599:     }
 2600:     $datatable .= '</tr></table></td></tr>';
 2601:     $$rowtotal ++;
 2602:     if ($cansrchrow) {
 2603:         $datatable .= '<tr class="LC_odd_row">';
 2604:     } else {
 2605:         $datatable .= '<tr>';
 2606:     }
 2607:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2608:                   '<td class="LC_left_item" colspan="2">'.
 2609:                   '<span class="LC_nobreak"><label>'.
 2610:                   '<input type="checkbox" name="searchtypes" '.
 2611:                   $exacton.' value="exact" />'.&mt('Exact match').
 2612:                   '</label>&nbsp;'.
 2613:                   '<label><input type="checkbox" name="searchtypes" '.
 2614:                   $beginson.' value="begins" />'.&mt('Begins with').
 2615:                   '</label>&nbsp;'.
 2616:                   '<label><input type="checkbox" name="searchtypes" '.
 2617:                   $containson.' value="contains" />'.&mt('Contains').
 2618:                   '</label></span></td></tr>';
 2619:     $$rowtotal ++;
 2620:     return $datatable;
 2621: }
 2622: 
 2623: sub print_contacts {
 2624:     my ($dom,$settings,$rowtotal) = @_;
 2625:     my $datatable;
 2626:     my @contacts = ('adminemail','supportemail');
 2627:     my (%checked,%to,%otheremails,%bccemails);
 2628:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2629:                     'requestsmail','updatesmail','idconflictsmail');
 2630:     foreach my $type (@mailings) {
 2631:         $otheremails{$type} = '';
 2632:     }
 2633:     $bccemails{'helpdeskmail'} = '';
 2634:     if (ref($settings) eq 'HASH') {
 2635:         foreach my $item (@contacts) {
 2636:             if (exists($settings->{$item})) {
 2637:                 $to{$item} = $settings->{$item};
 2638:             }
 2639:         }
 2640:         foreach my $type (@mailings) {
 2641:             if (exists($settings->{$type})) {
 2642:                 if (ref($settings->{$type}) eq 'HASH') {
 2643:                     foreach my $item (@contacts) {
 2644:                         if ($settings->{$type}{$item}) {
 2645:                             $checked{$type}{$item} = ' checked="checked" ';
 2646:                         }
 2647:                     }
 2648:                     $otheremails{$type} = $settings->{$type}{'others'};
 2649:                     if ($type eq 'helpdeskmail') {
 2650:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2651:                     }
 2652:                 }
 2653:             } elsif ($type eq 'lonstatusmail') {
 2654:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2655:             }
 2656:         }
 2657:     } else {
 2658:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2659:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2660:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2661:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2662:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2663:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2664:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2665:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2666:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2667:     }
 2668:     my ($titles,$short_titles) = &contact_titles();
 2669:     my $rownum = 0;
 2670:     my $css_class;
 2671:     foreach my $item (@contacts) {
 2672:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2673:         $datatable .= '<tr'.$css_class.'>'. 
 2674:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2675:                   '</span></td><td class="LC_right_item">'.
 2676:                   '<input type="text" name="'.$item.'" value="'.
 2677:                   $to{$item}.'" /></td></tr>';
 2678:         $rownum ++;
 2679:     }
 2680:     foreach my $type (@mailings) {
 2681:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2682:         $datatable .= '<tr'.$css_class.'>'.
 2683:                       '<td><span class="LC_nobreak">'.
 2684:                       $titles->{$type}.': </span></td>'.
 2685:                       '<td class="LC_left_item">'.
 2686:                       '<span class="LC_nobreak">';
 2687:         foreach my $item (@contacts) {
 2688:             $datatable .= '<label>'.
 2689:                           '<input type="checkbox" name="'.$type.'"'.
 2690:                           $checked{$type}{$item}.
 2691:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2692:                           '</label>&nbsp;';
 2693:         }
 2694:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2695:                       '<input type="text" name="'.$type.'_others" '.
 2696:                       'value="'.$otheremails{$type}.'"  />';
 2697:         if ($type eq 'helpdeskmail') {
 2698:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2699:                           '<input type="text" name="'.$type.'_bcc" '.
 2700:                           'value="'.$bccemails{$type}.'"  />';
 2701:         }
 2702:         $datatable .= '</td></tr>'."\n";
 2703:         $rownum ++;
 2704:     }
 2705:     my %choices;
 2706:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2707:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2708:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2709:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2710:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2711:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2712:     my @toggles = ('reporterrors','reportupdates');
 2713:     my %defaultchecked = ('reporterrors'  => 'on',
 2714:                           'reportupdates' => 'on');
 2715:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2716:                                                \%choices,$rownum);
 2717:     $datatable .= $reports;
 2718:     $$rowtotal += $rownum;
 2719:     return $datatable;
 2720: }
 2721: 
 2722: sub print_helpsettings {
 2723:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2724:     my ($datatable,$itemcount);
 2725:     $itemcount = 1;
 2726:     my (%choices,%defaultchecked,@toggles);
 2727:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2728:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2729:                                  &mt('LON-CAPA bug tracker'),600,500));
 2730:     %defaultchecked = ('submitbugs' => 'on');
 2731:     @toggles = ('submitbugs',);
 2732: 
 2733:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2734:                                                  \%choices,$itemcount);
 2735:     return $datatable;
 2736: }
 2737: 
 2738: sub radiobutton_prefs {
 2739:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2740:         $additional) = @_;
 2741:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2742:                    (ref($choices) eq 'HASH'));
 2743: 
 2744:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2745: 
 2746:     foreach my $item (@{$toggles}) {
 2747:         if ($defaultchecked->{$item} eq 'on') {
 2748:             $checkedon{$item} = ' checked="checked" ';
 2749:             $checkedoff{$item} = ' ';
 2750:         } elsif ($defaultchecked->{$item} eq 'off') {
 2751:             $checkedoff{$item} = ' checked="checked" ';
 2752:             $checkedon{$item} = ' ';
 2753:         }
 2754:     }
 2755:     if (ref($settings) eq 'HASH') {
 2756:         foreach my $item (@{$toggles}) {
 2757:             if ($settings->{$item} eq '1') {
 2758:                 $checkedon{$item} =  ' checked="checked" ';
 2759:                 $checkedoff{$item} = ' ';
 2760:             } elsif ($settings->{$item} eq '0') {
 2761:                 $checkedoff{$item} =  ' checked="checked" ';
 2762:                 $checkedon{$item} = ' ';
 2763:             }
 2764:         }
 2765:     }
 2766:     if ($onclick) {
 2767:         $onclick = ' onclick="'.$onclick.'"';
 2768:     }
 2769:     foreach my $item (@{$toggles}) {
 2770:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2771:         $datatable .=
 2772:             '<tr'.$css_class.'><td valign="top">'.
 2773:             '<span class="LC_nobreak">'.$choices->{$item}.
 2774:             '</span></td>'.
 2775:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2776:             '<label><input type="radio" name="'.
 2777:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2778:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2779:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2780:             '</span>'.$additional.
 2781:             '</td>'.
 2782:             '</tr>';
 2783:         $itemcount ++;
 2784:     }
 2785:     return ($datatable,$itemcount);
 2786: }
 2787: 
 2788: sub print_coursedefaults {
 2789:     my ($position,$dom,$settings,$rowtotal) = @_;
 2790:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2791:     my $itemcount = 1;
 2792:     my %choices =  &Apache::lonlocal::texthash (
 2793:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2794:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2795:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2796:         coursecredits        => 'Credits can be specified for courses',
 2797:     );
 2798:     my %staticdefaults = (
 2799:                            anonsurvey_threshold => 10,
 2800:                            uploadquota          => 500,
 2801:                          );
 2802:     if ($position eq 'top') {
 2803:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2804:         @toggles = ('canuse_pdfforms');
 2805:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2806:                                                  \%choices,$itemcount);
 2807:     } else {
 2808:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2809:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2810:             %curruploadquota);
 2811:         my $currusecredits = 0;
 2812:         my @types = ('official','unofficial','community','textbook');
 2813:         if (ref($settings) eq 'HASH') {
 2814:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2815:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2816:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2817:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2818:                 }
 2819:             }
 2820:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2821:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2822:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2823:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2824:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2825:                     ($def_textbook_credits ne '')) {
 2826:                     $currusecredits = 1;
 2827:                 }
 2828:             }
 2829:         }
 2830:         if (!$currdefresponder) {
 2831:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2832:         } elsif ($currdefresponder < 1) {
 2833:             $currdefresponder = 1;
 2834:         }
 2835:         foreach my $type (@types) {
 2836:             if ($curruploadquota{$type} eq '') {
 2837:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2838:             }
 2839:         }
 2840:         $datatable .=
 2841:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2842:                 $choices{'anonsurvey_threshold'}.
 2843:                 '</span></td>'.
 2844:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2845:                 '<input type="text" name="anonsurvey_threshold"'.
 2846:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2847:                 '</td></tr>'."\n";
 2848:         $itemcount ++;
 2849:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2850:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2851:                       $choices{'uploadquota'}.
 2852:                       '</span></td>'.
 2853:                       '<td align="right" class="LC_right_item">'.
 2854:                       '<table><tr>';
 2855:         foreach my $type (@types) {
 2856:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2857:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2858:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2859:         }
 2860:         $datatable .= '</tr></table></td></tr>'."\n";
 2861:         $itemcount ++;
 2862:         my $onclick = "toggleDisplay(this.form,'credits');";
 2863:         my $display = 'none';
 2864:         if ($currusecredits) {
 2865:             $display = 'block';
 2866:         }
 2867:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2868:                          '<span class="LC_nobreak">'.
 2869:                          &mt('Default credits for official courses [_1]',
 2870:                          '<input type="text" name="official_credits" value="'.
 2871:                          $def_official_credits.'" size="3" />').
 2872:                          '</span><br />'.
 2873:                          '<span class="LC_nobreak">'.
 2874:                          &mt('Default credits for unofficial courses [_1]',
 2875:                          '<input type="text" name="unofficial_credits" value="'.
 2876:                          $def_unofficial_credits.'" size="3" />').
 2877:                          '</span><br />'.
 2878:                          '<span class="LC_nobreak">'.
 2879:                          &mt('Default credits for textbook courses [_1]',
 2880:                          '<input type="text" name="textbook_credits" value="'.
 2881:                          $def_textbook_credits.'" size="3" />').
 2882:                          '</span></div>'."\n";
 2883:         %defaultchecked = ('coursecredits' => 'off');
 2884:         @toggles = ('coursecredits');
 2885:         my $current = {
 2886:                         'coursecredits' => $currusecredits,
 2887:                       };
 2888:         (my $table,$itemcount) =
 2889:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2890:                                \%choices,$itemcount,$onclick,$additional);
 2891:         $datatable .= $table;
 2892:         $itemcount ++;
 2893:     }
 2894:     $$rowtotal += $itemcount;
 2895:     return $datatable;
 2896: }
 2897: 
 2898: sub print_selfenrollment {
 2899:     my ($position,$dom,$settings,$rowtotal) = @_;
 2900:     my ($css_class,$datatable);
 2901:     my $itemcount = 1;
 2902:     my @types = ('official','unofficial','community','textbook');
 2903:     if (($position eq 'top') || ($position eq 'middle')) {
 2904:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 2905:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 2906:         my @rows;
 2907:         my $key;
 2908:         if ($position eq 'top') {
 2909:             $key = 'admin'; 
 2910:             if (ref($rowsref) eq 'ARRAY') {
 2911:                 @rows = @{$rowsref};
 2912:             }
 2913:         } elsif ($position eq 'middle') {
 2914:             $key = 'default';
 2915:             @rows = ('types','registered','approval','limit');
 2916:         }
 2917:         foreach my $row (@rows) {
 2918:             if (defined($titlesref->{$row})) {
 2919:                 $itemcount ++;
 2920:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2921:                 $datatable .= '<tr'.$css_class.'>'.
 2922:                               '<td>'.$titlesref->{$row}.'</td>'.
 2923:                               '<td class="LC_left_item">'.
 2924:                               '<table><tr>';
 2925:                 my (%current,%currentcap);
 2926:                 if (ref($settings) eq 'HASH') {
 2927:                     if (ref($settings->{$key}) eq 'HASH') {
 2928:                         foreach my $type (@types) {
 2929:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2930:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 2931:                             }
 2932:                             if (($row eq 'limit') && ($key eq 'default')) {
 2933:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2934:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 2935:                                 }
 2936:                             }
 2937:                         }
 2938:                     }
 2939:                 }
 2940:                 my %roles = (
 2941:                              '0' => &Apache::lonnet::plaintext('dc'),
 2942:                             ); 
 2943:             
 2944:                 foreach my $type (@types) {
 2945:                     unless (($row eq 'registered') && ($key eq 'default')) {
 2946:                         $datatable .= '<th>'.&mt($type).'</th>';
 2947:                     }
 2948:                 }
 2949:                 unless (($row eq 'registered') && ($key eq 'default')) {
 2950:                     $datatable .= '</tr><tr>';
 2951:                 }
 2952:                 foreach my $type (@types) {
 2953:                     if ($type eq 'community') {
 2954:                         $roles{'1'} = &mt('Community personnel');
 2955:                     } else {
 2956:                         $roles{'1'} = &mt('Course personnel');
 2957:                     }
 2958:                     $datatable .= '<td style="vertical-align: top">';
 2959:                     if ($position eq 'top') {
 2960:                         my %checked;
 2961:                         if ($current{$type} eq '0') {
 2962:                             $checked{'0'} = ' checked="checked"';
 2963:                         } else {
 2964:                             $checked{'1'} = ' checked="checked"';
 2965:                         }
 2966:                         foreach my $role ('1','0') {
 2967:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2968:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 2969:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 2970:                                           $roles{$role}.'</label></span> ';
 2971:                         }
 2972:                     } else {
 2973:                         if ($row eq 'types') {
 2974:                             my %checked;
 2975:                             if ($current{$type} =~ /^(all|dom)$/) {
 2976:                                 $checked{$1} = ' checked="checked"';
 2977:                             } else {
 2978:                                 $checked{''} = ' checked="checked"';
 2979:                             }
 2980:                             foreach my $val ('','dom','all') {
 2981:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2982:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2983:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2984:                             }
 2985:                         } elsif ($row eq 'registered') {
 2986:                             my %checked;
 2987:                             if ($current{$type} eq '1') {
 2988:                                 $checked{'1'} = ' checked="checked"';
 2989:                             } else {
 2990:                                 $checked{'0'} = ' checked="checked"';
 2991:                             }
 2992:                             foreach my $val ('0','1') {
 2993:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2994:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2995:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2996:                             }
 2997:                         } elsif ($row eq 'approval') {
 2998:                             my %checked;
 2999:                             if ($current{$type} =~ /^([12])$/) {
 3000:                                 $checked{$1} = ' checked="checked"';
 3001:                             } else {
 3002:                                 $checked{'0'} = ' checked="checked"';
 3003:                             }
 3004:                             for my $val (0..2) {
 3005:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3006:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3007:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3008:                             }
 3009:                         } elsif ($row eq 'limit') {
 3010:                             my %checked;
 3011:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3012:                                 $checked{$1} = ' checked="checked"';
 3013:                             } else {
 3014:                                 $checked{'none'} = ' checked="checked"';
 3015:                             }
 3016:                             my $cap;
 3017:                             if ($currentcap{$type} =~ /^\d+$/) {
 3018:                                 $cap = $currentcap{$type};
 3019:                             }
 3020:                             foreach my $val ('none','allstudents','selfenrolled') {
 3021:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3022:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3023:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3024:                             }
 3025:                             $datatable .= '<br />'.
 3026:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3027:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3028:                                           '</span>'; 
 3029:                         }
 3030:                     }
 3031:                     $datatable .= '</td>';
 3032:                 }
 3033:                 $datatable .= '</tr>';
 3034:             }
 3035:             $datatable .= '</table></td></tr>';
 3036:         }
 3037:     } elsif ($position eq 'bottom') {
 3038:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3039:     }
 3040:     $$rowtotal += $itemcount;
 3041:     return $datatable;
 3042: }
 3043: 
 3044: sub print_validation_rows {
 3045:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3046:     my ($itemsref,$namesref,$fieldsref);
 3047:     if ($caller eq 'selfenroll') { 
 3048:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3049:     } elsif ($caller eq 'requestcourses') {
 3050:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3051:     }
 3052:     my %currvalidation;
 3053:     if (ref($settings) eq 'HASH') {
 3054:         if (ref($settings->{'validation'}) eq 'HASH') {
 3055:             %currvalidation = %{$settings->{'validation'}};
 3056:         }
 3057:     }
 3058:     my $datatable;
 3059:     my $itemcount = 0;
 3060:     foreach my $item (@{$itemsref}) {
 3061:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3062:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3063:                       $namesref->{$item}.
 3064:                       '</span></td>'.
 3065:                       '<td class="LC_left_item">';
 3066:         if (($item eq 'url') || ($item eq 'button')) {
 3067:             $datatable .= '<span class="LC_nobreak">'.
 3068:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3069:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3070:         } elsif ($item eq 'fields') {
 3071:             my @currfields;
 3072:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3073:                 @currfields = @{$currvalidation{$item}};
 3074:             }
 3075:             foreach my $field (@{$fieldsref}) {
 3076:                 my $check = '';
 3077:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3078:                     $check = ' checked="checked"';
 3079:                 }
 3080:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3081:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3082:                               ' value="'.$field.'"'.$check.' />'.$field.
 3083:                               '</label></span> ';
 3084:             }
 3085:         } elsif ($item eq 'markup') {
 3086:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3087:                            $currvalidation{$item}.
 3088:                               '</textarea>';
 3089:         }
 3090:         $datatable .= '</td></tr>'."\n";
 3091:         if (ref($rowtotal)) {
 3092:             $itemcount ++;
 3093:         }
 3094:     }
 3095:     if ($caller eq 'requestcourses') {
 3096:         my %currhash;
 3097:         if (ref($settings->{'validation'}) eq 'HASH') {
 3098:             if ($settings->{'validation'}{'dc'} ne '') {
 3099:                 $currhash{$settings->{'validation'}{'dc'}} = 1;
 3100:             }
 3101:         }
 3102:         my $numinrow = 2;
 3103:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3104:                                                        'validationdc',%currhash);
 3105:         if ($numdc > 1) {
 3106:             $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 3107:                           &mt('Course creation processed as: (choose Dom. Coord.)').
 3108:                           '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3109:         } else {
 3110:             $datatable .= $dctable.'</td></tr>';
 3111:         }
 3112:         $itemcount ++;
 3113:     }
 3114:     if (ref($rowtotal)) {
 3115:         $$rowtotal += $itemcount;
 3116:     }
 3117:     return $datatable;
 3118: }
 3119: 
 3120: sub print_usersessions {
 3121:     my ($position,$dom,$settings,$rowtotal) = @_;
 3122:     my ($css_class,$datatable,%checked,%choices);
 3123:     my (%by_ip,%by_location,@intdoms);
 3124:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3125: 
 3126:     my @alldoms = &Apache::lonnet::all_domains();
 3127:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3128:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3129:     my %altids = &id_for_thisdom(%servers);
 3130:     my $itemcount = 1;
 3131:     if ($position eq 'top') {
 3132:         if (keys(%serverhomes) > 1) {
 3133:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3134:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 3135:         } else {
 3136:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3137:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3138:         }
 3139:     } else {
 3140:         if (keys(%by_location) == 0) {
 3141:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3142:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3143:         } else {
 3144:             my %lt = &usersession_titles();
 3145:             my $numinrow = 5;
 3146:             my $prefix;
 3147:             my @types;
 3148:             if ($position eq 'bottom') {
 3149:                 $prefix = 'remote';
 3150:                 @types = ('version','excludedomain','includedomain');
 3151:             } else {
 3152:                 $prefix = 'hosted';
 3153:                 @types = ('excludedomain','includedomain');
 3154:             }
 3155:             my (%current,%checkedon,%checkedoff);
 3156:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3157:             my @locations = sort(keys(%by_location));
 3158:             foreach my $type (@types) {
 3159:                 $checkedon{$type} = '';
 3160:                 $checkedoff{$type} = ' checked="checked"';
 3161:             }
 3162:             if (ref($settings) eq 'HASH') {
 3163:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3164:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3165:                         $current{$key} = $settings->{$prefix}{$key};
 3166:                         if ($key eq 'version') {
 3167:                             if ($current{$key} ne '') {
 3168:                                 $checkedon{$key} = ' checked="checked"';
 3169:                                 $checkedoff{$key} = '';
 3170:                             }
 3171:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3172:                             $checkedon{$key} = ' checked="checked"';
 3173:                             $checkedoff{$key} = '';
 3174:                         }
 3175:                     }
 3176:                 }
 3177:             }
 3178:             foreach my $type (@types) {
 3179:                 next if ($type ne 'version' && !@locations);
 3180:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3181:                 $datatable .= '<tr'.$css_class.'>
 3182:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3183:                                <span class="LC_nobreak">&nbsp;
 3184:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3185:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3186:                 if ($type eq 'version') {
 3187:                     my $selector = '<select name="'.$prefix.'_version">';
 3188:                     foreach my $version (@lcversions) {
 3189:                         my $selected = '';
 3190:                         if ($current{'version'} eq $version) {
 3191:                             $selected = ' selected="selected"';
 3192:                         }
 3193:                         $selector .= ' <option value="'.$version.'"'.
 3194:                                      $selected.'>'.$version.'</option>';
 3195:                     }
 3196:                     $selector .= '</select> ';
 3197:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3198:                 } else {
 3199:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3200:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3201:                                  ' />'.('&nbsp;'x2).
 3202:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3203:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3204:                                  "\n".
 3205:                                  '</div><div><table>';
 3206:                     my $rem;
 3207:                     for (my $i=0; $i<@locations; $i++) {
 3208:                         my ($showloc,$value,$checkedtype);
 3209:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3210:                             my $ip = $by_location{$locations[$i]}->[0];
 3211:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3212:                                  $value = join(':',@{$by_ip{$ip}});
 3213:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3214:                                 if (ref($current{$type}) eq 'ARRAY') {
 3215:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3216:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3217:                                             $checkedtype = ' checked="checked"';
 3218:                                             last;
 3219:                                         }
 3220:                                     }
 3221:                                 }
 3222:                             }
 3223:                         }
 3224:                         $rem = $i%($numinrow);
 3225:                         if ($rem == 0) {
 3226:                             if ($i > 0) {
 3227:                                 $datatable .= '</tr>';
 3228:                             }
 3229:                             $datatable .= '<tr>';
 3230:                         }
 3231:                         $datatable .= '<td class="LC_left_item">'.
 3232:                                       '<span class="LC_nobreak"><label>'.
 3233:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3234:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3235:                                       '</label></span></td>';
 3236:                     }
 3237:                     $rem = @locations%($numinrow);
 3238:                     my $colsleft = $numinrow - $rem;
 3239:                     if ($colsleft > 1 ) {
 3240:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3241:                                       '&nbsp;</td>';
 3242:                     } elsif ($colsleft == 1) {
 3243:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3244:                     }
 3245:                     $datatable .= '</tr></table>';
 3246:                 }
 3247:                 $datatable .= '</td></tr>';
 3248:                 $itemcount ++;
 3249:             }
 3250:         }
 3251:     }
 3252:     $$rowtotal += $itemcount;
 3253:     return $datatable;
 3254: }
 3255: 
 3256: sub build_location_hashes {
 3257:     my ($intdoms,$by_ip,$by_location) = @_;
 3258:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3259:                   (ref($by_location) eq 'HASH')); 
 3260:     my %iphost = &Apache::lonnet::get_iphost();
 3261:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3262:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3263:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3264:         foreach my $id (@{$iphost{$primary_ip}}) {
 3265:             my $intdom = &Apache::lonnet::internet_dom($id);
 3266:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3267:                 push(@{$intdoms},$intdom);
 3268:             }
 3269:         }
 3270:     }
 3271:     foreach my $ip (keys(%iphost)) {
 3272:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3273:             foreach my $id (@{$iphost{$ip}}) {
 3274:                 my $location = &Apache::lonnet::internet_dom($id);
 3275:                 if ($location) {
 3276:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3277:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3278:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3279:                             push(@{$by_ip->{$ip}},$location);
 3280:                         }
 3281:                     } else {
 3282:                         $by_ip->{$ip} = [$location];
 3283:                     }
 3284:                 }
 3285:             }
 3286:         }
 3287:     }
 3288:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3289:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3290:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3291:             my $first = $by_ip->{$ip}->[0];
 3292:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3293:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3294:                     push(@{$by_location->{$first}},$ip);
 3295:                 }
 3296:             } else {
 3297:                 $by_location->{$first} = [$ip];
 3298:             }
 3299:         }
 3300:     }
 3301:     return;
 3302: }
 3303: 
 3304: sub current_offloads_to {
 3305:     my ($dom,$settings,$servers) = @_;
 3306:     my (%spareid,%otherdomconfigs);
 3307:     if (ref($servers) eq 'HASH') {
 3308:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3309:             my $gotspares;
 3310:             if (ref($settings) eq 'HASH') {
 3311:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3312:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3313:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3314:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3315:                         $gotspares = 1;
 3316:                     }
 3317:                 }
 3318:             }
 3319:             unless ($gotspares) {
 3320:                 my $gotspares;
 3321:                 my $serverhomeID =
 3322:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3323:                 my $serverhomedom =
 3324:                     &Apache::lonnet::host_domain($serverhomeID);
 3325:                 if ($serverhomedom ne $dom) {
 3326:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3327:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3328:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3329:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3330:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3331:                                 $gotspares = 1;
 3332:                             }
 3333:                         }
 3334:                     } else {
 3335:                         $otherdomconfigs{$serverhomedom} =
 3336:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3337:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3338:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3339:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3340:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) 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:                             }
 3347:                         }
 3348:                     }
 3349:                 }
 3350:             }
 3351:             unless ($gotspares) {
 3352:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3353:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3354:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3355:                } else {
 3356:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3357:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3358:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3359:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3360:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3361:                     } else {
 3362:                         my %what = (
 3363:                              spareid => 1,
 3364:                         );
 3365:                         my ($result,$returnhash) = 
 3366:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3367:                         if ($result eq 'ok') { 
 3368:                             if (ref($returnhash) eq 'HASH') {
 3369:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3370:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3371:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3372:                                 }
 3373:                             }
 3374:                         }
 3375:                     }
 3376:                 }
 3377:             }
 3378:         }
 3379:     }
 3380:     return %spareid;
 3381: }
 3382: 
 3383: sub spares_row {
 3384:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3385:     my $css_class;
 3386:     my $numinrow = 4;
 3387:     my $itemcount = 1;
 3388:     my $datatable;
 3389:     my %typetitles = &sparestype_titles();
 3390:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3391:         foreach my $server (sort(keys(%{$servers}))) {
 3392:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3393:             my ($othercontrol,$serverdom);
 3394:             if ($serverhome ne $server) {
 3395:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3396:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3397:             } else {
 3398:                 $serverdom = &Apache::lonnet::host_domain($server);
 3399:                 if ($serverdom ne $dom) {
 3400:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3401:                 }
 3402:             }
 3403:             next unless (ref($spareid->{$server}) eq 'HASH');
 3404:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3405:             $datatable .= '<tr'.$css_class.'>
 3406:                            <td rowspan="2">
 3407:                             <span class="LC_nobreak">'.
 3408:                           &mt('[_1] when busy, offloads to:'
 3409:                               ,'<b>'.$server.'</b>').
 3410:                           "\n";
 3411:             my (%current,%canselect);
 3412:             my @choices = 
 3413:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3414:             foreach my $type ('primary','default') {
 3415:                 if (ref($spareid->{$server}) eq 'HASH') {
 3416:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3417:                         my @spares = @{$spareid->{$server}{$type}};
 3418:                         if (@spares > 0) {
 3419:                             if ($othercontrol) {
 3420:                                 $current{$type} = join(', ',@spares);
 3421:                             } else {
 3422:                                 $current{$type} .= '<table>';
 3423:                                 my $numspares = scalar(@spares);
 3424:                                 for (my $i=0;  $i<@spares; $i++) {
 3425:                                     my $rem = $i%($numinrow);
 3426:                                     if ($rem == 0) {
 3427:                                         if ($i > 0) {
 3428:                                             $current{$type} .= '</tr>';
 3429:                                         }
 3430:                                         $current{$type} .= '<tr>';
 3431:                                     }
 3432:                                     $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;'.
 3433:                                                        $spareid->{$server}{$type}[$i].
 3434:                                                        '</label></td>'."\n";
 3435:                                 }
 3436:                                 my $rem = @spares%($numinrow);
 3437:                                 my $colsleft = $numinrow - $rem;
 3438:                                 if ($colsleft > 1 ) {
 3439:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3440:                                                        '" class="LC_left_item">'.
 3441:                                                        '&nbsp;</td>';
 3442:                                 } elsif ($colsleft == 1) {
 3443:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3444:                                 }
 3445:                                 $current{$type} .= '</tr></table>';
 3446:                             }
 3447:                         }
 3448:                     }
 3449:                     if ($current{$type} eq '') {
 3450:                         $current{$type} = &mt('None specified');
 3451:                     }
 3452:                     if ($othercontrol) {
 3453:                         if ($type eq 'primary') {
 3454:                             $canselect{$type} = $othercontrol;
 3455:                         }
 3456:                     } else {
 3457:                         $canselect{$type} = 
 3458:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3459:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3460:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3461:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3462:                         if (@choices > 0) {
 3463:                             foreach my $lonhost (@choices) {
 3464:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3465:                             }
 3466:                         }
 3467:                         $canselect{$type} .= '</select>'."\n";
 3468:                     }
 3469:                 } else {
 3470:                     $current{$type} = &mt('Could not be determined');
 3471:                     if ($type eq 'primary') {
 3472:                         $canselect{$type} =  $othercontrol;
 3473:                     }
 3474:                 }
 3475:                 if ($type eq 'default') {
 3476:                     $datatable .= '<tr'.$css_class.'>';
 3477:                 }
 3478:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3479:                               '<td>'.$current{$type}.'</td>'."\n".
 3480:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3481:             }
 3482:             $itemcount ++;
 3483:         }
 3484:     }
 3485:     $$rowtotal += $itemcount;
 3486:     return $datatable;
 3487: }
 3488: 
 3489: sub possible_newspares {
 3490:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3491:     my $serverhostname = &Apache::lonnet::hostname($server);
 3492:     my %excluded;
 3493:     if ($serverhostname ne '') {
 3494:         %excluded = (
 3495:                        $serverhostname => 1,
 3496:                     );
 3497:     }
 3498:     if (ref($currspares) eq 'HASH') {
 3499:         foreach my $type (keys(%{$currspares})) {
 3500:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3501:                 if (@{$currspares->{$type}} > 0) {
 3502:                     foreach my $curr (@{$currspares->{$type}}) {
 3503:                         my $hostname = &Apache::lonnet::hostname($curr);
 3504:                         $excluded{$hostname} = 1;
 3505:                     }
 3506:                 }
 3507:             }
 3508:         }
 3509:     }
 3510:     my @choices;
 3511:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3512:         if (keys(%{$serverhomes}) > 1) {
 3513:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3514:                 unless ($excluded{$name}) {
 3515:                     if (exists($altids->{$serverhomes->{$name}})) {
 3516:                         push(@choices,$altids->{$serverhomes->{$name}});
 3517:                     } else {
 3518:                         push(@choices,$serverhomes->{$name});
 3519:                     }
 3520:                 }
 3521:             }
 3522:         }
 3523:     }
 3524:     return sort(@choices);
 3525: }
 3526: 
 3527: sub print_loadbalancing {
 3528:     my ($dom,$settings,$rowtotal) = @_;
 3529:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3530:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3531:     my $numinrow = 1;
 3532:     my $datatable;
 3533:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3534:     my (%currbalancer,%currtargets,%currrules,%existing);
 3535:     if (ref($settings) eq 'HASH') {
 3536:         %existing = %{$settings};
 3537:     }
 3538:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3539:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3540:                                   \%currtargets,\%currrules);
 3541:     } else {
 3542:         return;
 3543:     }
 3544:     my ($othertitle,$usertypes,$types) =
 3545:         &Apache::loncommon::sorted_inst_types($dom);
 3546:     my $rownum = 8;
 3547:     if (ref($types) eq 'ARRAY') {
 3548:         $rownum += scalar(@{$types});
 3549:     }
 3550:     my @css_class = ('LC_odd_row','LC_even_row');
 3551:     my $balnum = 0;
 3552:     my $islast;
 3553:     my (@toshow,$disabledtext);
 3554:     if (keys(%currbalancer) > 0) {
 3555:         @toshow = sort(keys(%currbalancer));
 3556:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3557:             push(@toshow,'');
 3558:         }
 3559:     } else {
 3560:         @toshow = ('');
 3561:         $disabledtext = &mt('No existing load balancer');
 3562:     }
 3563:     foreach my $lonhost (@toshow) {
 3564:         if ($balnum == scalar(@toshow)-1) {
 3565:             $islast = 1;
 3566:         } else {
 3567:             $islast = 0;
 3568:         }
 3569:         my $cssidx = $balnum%2;
 3570:         my $targets_div_style = 'display: none';
 3571:         my $disabled_div_style = 'display: block';
 3572:         my $homedom_div_style = 'display: none';
 3573:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3574:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3575:                       '<p>';
 3576:         if ($lonhost eq '') {
 3577:             $datatable .= '<span class="LC_nobreak">';
 3578:             if (keys(%currbalancer) > 0) {
 3579:                 $datatable .= &mt('Add balancer:');
 3580:             } else {
 3581:                 $datatable .= &mt('Enable balancer:');
 3582:             }
 3583:             $datatable .= '&nbsp;'.
 3584:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3585:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3586:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3587:                           '<option value="" selected="selected">'.&mt('None').
 3588:                           '</option>'."\n";
 3589:             foreach my $server (sort(keys(%servers))) {
 3590:                 next if ($currbalancer{$server});
 3591:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3592:             }
 3593:             $datatable .=
 3594:                 '</select>'."\n".
 3595:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3596:         } else {
 3597:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3598:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3599:                            &mt('Stop balancing').'</label>'.
 3600:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3601:             $targets_div_style = 'display: block';
 3602:             $disabled_div_style = 'display: none';
 3603:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3604:                 $homedom_div_style = 'display: block';
 3605:             }
 3606:         }
 3607:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3608:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3609:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3610:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3611:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3612:         my @sparestypes = ('primary','default');
 3613:         my %typetitles = &sparestype_titles();
 3614:         foreach my $sparetype (@sparestypes) {
 3615:             my $targettable;
 3616:             for (my $i=0; $i<$numspares; $i++) {
 3617:                 my $checked;
 3618:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3619:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3620:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3621:                             $checked = ' checked="checked"';
 3622:                         }
 3623:                     }
 3624:                 }
 3625:                 my ($chkboxval,$disabled);
 3626:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3627:                     $chkboxval = $spares[$i];
 3628:                 }
 3629:                 if (exists($currbalancer{$spares[$i]})) {
 3630:                     $disabled = ' disabled="disabled"';
 3631:                 }
 3632:                 $targettable .=
 3633:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3634:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3635:                     '</span></label></td>';
 3636:                 my $rem = $i%($numinrow);
 3637:                 if ($rem == 0) {
 3638:                     if (($i > 0) && ($i < $numspares-1)) {
 3639:                         $targettable .= '</tr>';
 3640:                     }
 3641:                     if ($i < $numspares-1) {
 3642:                         $targettable .= '<tr>';
 3643:                     }
 3644:                 }
 3645:             }
 3646:             if ($targettable ne '') {
 3647:                 my $rem = $numspares%($numinrow);
 3648:                 my $colsleft = $numinrow - $rem;
 3649:                 if ($colsleft > 1 ) {
 3650:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3651:                                     '&nbsp;</td>';
 3652:                 } elsif ($colsleft == 1) {
 3653:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3654:                 }
 3655:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3656:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3657:             }
 3658:         }
 3659:         $datatable .= '</div></td></tr>'.
 3660:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3661:                                            $othertitle,$usertypes,$types,\%servers,
 3662:                                            \%currbalancer,$lonhost,
 3663:                                            $targets_div_style,$homedom_div_style,
 3664:                                            $css_class[$cssidx],$balnum,$islast);
 3665:         $$rowtotal += $rownum;
 3666:         $balnum ++;
 3667:     }
 3668:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3669:     return $datatable;
 3670: }
 3671: 
 3672: sub get_loadbalancers_config {
 3673:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3674:     return unless ((ref($servers) eq 'HASH') &&
 3675:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3676:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3677:     if (keys(%{$existing}) > 0) {
 3678:         my $oldlonhost;
 3679:         foreach my $key (sort(keys(%{$existing}))) {
 3680:             if ($key eq 'lonhost') {
 3681:                 $oldlonhost = $existing->{'lonhost'};
 3682:                 $currbalancer->{$oldlonhost} = 1;
 3683:             } elsif ($key eq 'targets') {
 3684:                 if ($oldlonhost) {
 3685:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3686:                 }
 3687:             } elsif ($key eq 'rules') {
 3688:                 if ($oldlonhost) {
 3689:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3690:                 }
 3691:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3692:                 $currbalancer->{$key} = 1;
 3693:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3694:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3695:             }
 3696:         }
 3697:     } else {
 3698:         my ($balancerref,$targetsref) =
 3699:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3700:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3701:             foreach my $server (sort(keys(%{$balancerref}))) {
 3702:                 $currbalancer->{$server} = 1;
 3703:                 $currtargets->{$server} = $targetsref->{$server};
 3704:             }
 3705:         }
 3706:     }
 3707:     return;
 3708: }
 3709: 
 3710: sub loadbalancing_rules {
 3711:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3712:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3713:         $css_class,$balnum,$islast) = @_;
 3714:     my $output;
 3715:     my $num = 0;
 3716:     my ($alltypes,$othertypes,$titles) =
 3717:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3718:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3719:         foreach my $type (@{$alltypes}) {
 3720:             $num ++;
 3721:             my $current;
 3722:             if (ref($currrules) eq 'HASH') {
 3723:                 $current = $currrules->{$type};
 3724:             }
 3725:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3726:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3727:                     $current = '';
 3728:                 }
 3729:             }
 3730:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3731:                                              $servers,$currbalancer,$lonhost,$dom,
 3732:                                              $targets_div_style,$homedom_div_style,
 3733:                                              $css_class,$balnum,$num,$islast);
 3734:         }
 3735:     }
 3736:     return $output;
 3737: }
 3738: 
 3739: sub loadbalancing_titles {
 3740:     my ($dom,$intdom,$usertypes,$types) = @_;
 3741:     my %othertypes = (
 3742:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3743:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3744:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3745:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3746:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3747:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3748:                      );
 3749:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3750:     if (ref($types) eq 'ARRAY') {
 3751:         unshift(@alltypes,@{$types},'default');
 3752:     }
 3753:     my %titles;
 3754:     foreach my $type (@alltypes) {
 3755:         if ($type =~ /^_LC_/) {
 3756:             $titles{$type} = $othertypes{$type};
 3757:         } elsif ($type eq 'default') {
 3758:             $titles{$type} = &mt('All users from [_1]',$dom);
 3759:             if (ref($types) eq 'ARRAY') {
 3760:                 if (@{$types} > 0) {
 3761:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3762:                 }
 3763:             }
 3764:         } elsif (ref($usertypes) eq 'HASH') {
 3765:             $titles{$type} = $usertypes->{$type};
 3766:         }
 3767:     }
 3768:     return (\@alltypes,\%othertypes,\%titles);
 3769: }
 3770: 
 3771: sub loadbalance_rule_row {
 3772:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3773:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3774:     my @rulenames;
 3775:     my %ruletitles = &offloadtype_text();
 3776:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3777:         @rulenames = ('balancer','offloadedto');
 3778:     } else {
 3779:         @rulenames = ('default','homeserver');
 3780:         if ($type eq '_LC_external') {
 3781:             push(@rulenames,'externalbalancer');
 3782:         } else {
 3783:             push(@rulenames,'specific');
 3784:         }
 3785:         push(@rulenames,'none');
 3786:     }
 3787:     my $style = $targets_div_style;
 3788:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3789:         $style = $homedom_div_style;
 3790:     }
 3791:     my $space;
 3792:     if ($islast && $num == 1) {
 3793:         $space = '<div display="inline-block">&nbsp;</div>';
 3794:     }
 3795:     my $output =
 3796:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3797:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3798:         '<td valaign="top">'.$space.
 3799:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3800:     for (my $i=0; $i<@rulenames; $i++) {
 3801:         my $rule = $rulenames[$i];
 3802:         my ($checked,$extra);
 3803:         if ($rulenames[$i] eq 'default') {
 3804:             $rule = '';
 3805:         }
 3806:         if ($rulenames[$i] eq 'specific') {
 3807:             if (ref($servers) eq 'HASH') {
 3808:                 my $default;
 3809:                 if (($current ne '') && (exists($servers->{$current}))) {
 3810:                     $checked = ' checked="checked"';
 3811:                 }
 3812:                 unless ($checked) {
 3813:                     $default = ' selected="selected"';
 3814:                 }
 3815:                 $extra =
 3816:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3817:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3818:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3819:                     '<option value=""'.$default.'></option>'."\n";
 3820:                 foreach my $server (sort(keys(%{$servers}))) {
 3821:                     if (ref($currbalancer) eq 'HASH') {
 3822:                         next if (exists($currbalancer->{$server}));
 3823:                     }
 3824:                     my $selected;
 3825:                     if ($server eq $current) {
 3826:                         $selected = ' selected="selected"';
 3827:                     }
 3828:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3829:                 }
 3830:                 $extra .= '</select>';
 3831:             }
 3832:         } elsif ($rule eq $current) {
 3833:             $checked = ' checked="checked"';
 3834:         }
 3835:         $output .= '<span class="LC_nobreak"><label>'.
 3836:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3837:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3838:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3839:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3840:                    '</label>'.$extra.'</span><br />'."\n";
 3841:     }
 3842:     $output .= '</div></td></tr>'."\n";
 3843:     return $output;
 3844: }
 3845: 
 3846: sub offloadtype_text {
 3847:     my %ruletitles = &Apache::lonlocal::texthash (
 3848:            'default'          => 'Offloads to default destinations',
 3849:            'homeserver'       => "Offloads to user's home server",
 3850:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3851:            'specific'         => 'Offloads to specific server',
 3852:            'none'             => 'No offload',
 3853:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3854:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3855:     );
 3856:     return %ruletitles;
 3857: }
 3858: 
 3859: sub sparestype_titles {
 3860:     my %typestitles = &Apache::lonlocal::texthash (
 3861:                           'primary' => 'primary',
 3862:                           'default' => 'default',
 3863:                       );
 3864:     return %typestitles;
 3865: }
 3866: 
 3867: sub contact_titles {
 3868:     my %titles = &Apache::lonlocal::texthash (
 3869:                    'supportemail' => 'Support E-mail address',
 3870:                    'adminemail'   => 'Default Server Admin E-mail address',
 3871:                    'errormail'    => 'Error reports to be e-mailed to',
 3872:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3873:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3874:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3875:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3876:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3877:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3878:                  );
 3879:     my %short_titles = &Apache::lonlocal::texthash (
 3880:                            adminemail   => 'Admin E-mail address',
 3881:                            supportemail => 'Support E-mail',
 3882:                        );   
 3883:     return (\%titles,\%short_titles);
 3884: }
 3885: 
 3886: sub tool_titles {
 3887:     my %titles = &Apache::lonlocal::texthash (
 3888:                      aboutme    => 'Personal web page',
 3889:                      blog       => 'Blog',
 3890:                      webdav     => 'WebDAV',
 3891:                      portfolio  => 'Portfolio',
 3892:                      official   => 'Official courses (with institutional codes)',
 3893:                      unofficial => 'Unofficial courses',
 3894:                      community  => 'Communities',
 3895:                      textbook   => 'Textbook courses',
 3896:                  );
 3897:     return %titles;
 3898: }
 3899: 
 3900: sub courserequest_titles {
 3901:     my %titles = &Apache::lonlocal::texthash (
 3902:                                    official   => 'Official',
 3903:                                    unofficial => 'Unofficial',
 3904:                                    community  => 'Communities',
 3905:                                    textbook   => 'Textbook',
 3906:                                    norequest  => 'Not allowed',
 3907:                                    approval   => 'Approval by Dom. Coord.',
 3908:                                    validate   => 'With validation',
 3909:                                    autolimit  => 'Numerical limit',
 3910:                                    unlimited  => '(blank for unlimited)',
 3911:                  );
 3912:     return %titles;
 3913: }
 3914: 
 3915: sub authorrequest_titles {
 3916:     my %titles = &Apache::lonlocal::texthash (
 3917:                                    norequest  => 'Not allowed',
 3918:                                    approval   => 'Approval by Dom. Coord.',
 3919:                                    automatic  => 'Automatic approval',
 3920:                  );
 3921:     return %titles;
 3922: }
 3923: 
 3924: sub courserequest_conditions {
 3925:     my %conditions = &Apache::lonlocal::texthash (
 3926:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3927:        validate   => '(Processing of request subject to institutional validation).',
 3928:                  );
 3929:     return %conditions;
 3930: }
 3931: 
 3932: 
 3933: sub print_usercreation {
 3934:     my ($position,$dom,$settings,$rowtotal) = @_;
 3935:     my $numinrow = 4;
 3936:     my $datatable;
 3937:     if ($position eq 'top') {
 3938:         $$rowtotal ++;
 3939:         my $rowcount = 0;
 3940:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3941:         if (ref($rules) eq 'HASH') {
 3942:             if (keys(%{$rules}) > 0) {
 3943:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3944:                                                 $ruleorder,$numinrow,$rowcount);
 3945:                 $$rowtotal ++;
 3946:                 $rowcount ++;
 3947:             }
 3948:         }
 3949:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3950:         if (ref($idrules) eq 'HASH') {
 3951:             if (keys(%{$idrules}) > 0) {
 3952:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3953:                                                 $idruleorder,$numinrow,$rowcount);
 3954:                 $$rowtotal ++;
 3955:                 $rowcount ++;
 3956:             }
 3957:         }
 3958:         if ($rowcount == 0) {
 3959:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3960:             $$rowtotal ++;
 3961:             $rowcount ++;
 3962:         }
 3963:     } elsif ($position eq 'middle') {
 3964:         my @creators = ('author','course','requestcrs');
 3965:         my ($rules,$ruleorder) =
 3966:             &Apache::lonnet::inst_userrules($dom,'username');
 3967:         my %lt = &usercreation_types();
 3968:         my %checked;
 3969:         if (ref($settings) eq 'HASH') {
 3970:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3971:                 foreach my $item (@creators) {
 3972:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3973:                 }
 3974:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3975:                 foreach my $item (@creators) {
 3976:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3977:                         $checked{$item} = 'none';
 3978:                     }
 3979:                 }
 3980:             }
 3981:         }
 3982:         my $rownum = 0;
 3983:         foreach my $item (@creators) {
 3984:             $rownum ++;
 3985:             if ($checked{$item} eq '') {
 3986:                 $checked{$item} = 'any';
 3987:             }
 3988:             my $css_class;
 3989:             if ($rownum%2) {
 3990:                 $css_class = '';
 3991:             } else {
 3992:                 $css_class = ' class="LC_odd_row" ';
 3993:             }
 3994:             $datatable .= '<tr'.$css_class.'>'.
 3995:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3996:                          '</span></td><td align="right">';
 3997:             my @options = ('any');
 3998:             if (ref($rules) eq 'HASH') {
 3999:                 if (keys(%{$rules}) > 0) {
 4000:                     push(@options,('official','unofficial'));
 4001:                 }
 4002:             }
 4003:             push(@options,'none');
 4004:             foreach my $option (@options) {
 4005:                 my $type = 'radio';
 4006:                 my $check = ' ';
 4007:                 if ($checked{$item} eq $option) {
 4008:                     $check = ' checked="checked" ';
 4009:                 } 
 4010:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4011:                               '<input type="'.$type.'" name="can_createuser_'.
 4012:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 4013:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 4014:             }
 4015:             $datatable .= '</td></tr>';
 4016:         }
 4017:     } else {
 4018:         my @contexts = ('author','course','domain');
 4019:         my @authtypes = ('int','krb4','krb5','loc');
 4020:         my %checked;
 4021:         if (ref($settings) eq 'HASH') {
 4022:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 4023:                 foreach my $item (@contexts) {
 4024:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 4025:                         foreach my $auth (@authtypes) {
 4026:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 4027:                                 $checked{$item}{$auth} = ' checked="checked" ';
 4028:                             }
 4029:                         }
 4030:                     }
 4031:                 }
 4032:             }
 4033:         } else {
 4034:             foreach my $item (@contexts) {
 4035:                 foreach my $auth (@authtypes) {
 4036:                     $checked{$item}{$auth} = ' checked="checked" ';
 4037:                 }
 4038:             }
 4039:         }
 4040:         my %title = &context_names();
 4041:         my %authname = &authtype_names();
 4042:         my $rownum = 0;
 4043:         my $css_class; 
 4044:         foreach my $item (@contexts) {
 4045:             if ($rownum%2) {
 4046:                 $css_class = '';
 4047:             } else {
 4048:                 $css_class = ' class="LC_odd_row" ';
 4049:             }
 4050:             $datatable .=   '<tr'.$css_class.'>'.
 4051:                             '<td>'.$title{$item}.
 4052:                             '</td><td class="LC_left_item">'.
 4053:                             '<span class="LC_nobreak">';
 4054:             foreach my $auth (@authtypes) {
 4055:                 $datatable .= '<label>'. 
 4056:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4057:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4058:                               $authname{$auth}.'</label>&nbsp;';
 4059:             }
 4060:             $datatable .= '</span></td></tr>';
 4061:             $rownum ++;
 4062:         }
 4063:         $$rowtotal += $rownum;
 4064:     }
 4065:     return $datatable;
 4066: }
 4067: 
 4068: sub print_selfcreation {
 4069:     my ($position,$dom,$settings,$rowtotal) = @_;
 4070:     my (@selfcreate,$createsettings,$processing,$datatable);
 4071:     if (ref($settings) eq 'HASH') {
 4072:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4073:             $createsettings = $settings->{'cancreate'};
 4074:             if (ref($createsettings) eq 'HASH') {
 4075:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4076:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4077:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4078:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4079:                         @selfcreate = ('email','login','sso');
 4080:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4081:                         @selfcreate = ($createsettings->{'selfcreate'});
 4082:                     }
 4083:                 }
 4084:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4085:                     $processing = $createsettings->{'selfcreateprocessing'};
 4086:                 }
 4087:             }
 4088:         }
 4089:     }
 4090:     my %radiohash;
 4091:     my $numinrow = 4;
 4092:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4093:     if ($position eq 'top') {
 4094:         my %choices = &Apache::lonlocal::texthash (
 4095:                                                       cancreate_login      => 'Institutional Login',
 4096:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4097:                                                   );
 4098:         my @toggles = sort(keys(%choices));
 4099:         my %defaultchecked = (
 4100:                                'cancreate_login' => 'off',
 4101:                                'cancreate_sso'   => 'off',
 4102:                              );
 4103:         my ($onclick,$itemcount);
 4104:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4105:                                                      \%choices,$itemcount,$onclick);
 4106:         $$rowtotal += $itemcount;
 4107:         
 4108:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4109: 
 4110:         if (ref($usertypes) eq 'HASH') {
 4111:             if (keys(%{$usertypes}) > 0) {
 4112:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4113:                                              $dom,$numinrow,$othertitle,
 4114:                                              'statustocreate',$$rowtotal);
 4115:                 $$rowtotal ++;
 4116:             }
 4117:         }
 4118:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 4119:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4120:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 4121:         my $rem;
 4122:         my $numperrow = 2;
 4123:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4124:         $datatable .= '<tr'.$css_class.'>'.
 4125:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 4126:                      '<td class="LC_left_item">'."\n".
 4127:                      '<table><tr><td>'."\n";
 4128:         for (my $i=0; $i<@fields; $i++) {
 4129:             $rem = $i%($numperrow);
 4130:             if ($rem == 0) {
 4131:                 if ($i > 0) {
 4132:                     $datatable .= '</tr>';
 4133:                 }
 4134:                 $datatable .= '<tr>';
 4135:             }
 4136:             my $currval;
 4137:             if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 4138:                 $currval = $createsettings->{'shibenv'}{$fields[$i]};
 4139:             }
 4140:             $datatable .= '<td class="LC_left_item">'.
 4141:                           '<span class="LC_nobreak">'.
 4142:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 4143:                           'value="'.$currval.'" size="10" />&nbsp;'.
 4144:                           $fieldtitles{$fields[$i]}.'</span></td>';
 4145:         }
 4146:         my $colsleft = $numperrow - $rem;
 4147:         if ($colsleft > 1 ) {
 4148:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4149:                          '&nbsp;</td>';
 4150:         } elsif ($colsleft == 1) {
 4151:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4152:         }
 4153:         $datatable .= '</tr></table></td></tr>';
 4154:         $$rowtotal ++;
 4155:     } elsif ($position eq 'middle') {
 4156:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4157:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4158:         $usertypes->{'default'} = $othertitle;
 4159:         if (ref($types) eq 'ARRAY') {
 4160:             push(@{$types},'default');
 4161:             $usertypes->{'default'} = $othertitle;
 4162:             foreach my $status (@{$types}) {
 4163:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4164:                                                        $numinrow,$$rowtotal,$usertypes);
 4165:                 $$rowtotal ++;
 4166:             }
 4167:         }
 4168:     } else {
 4169:         my %choices = &Apache::lonlocal::texthash (
 4170:                                                       cancreate_email => 'E-mail address as username',
 4171:                                                   );
 4172:         my @toggles = sort(keys(%choices));
 4173:         my %defaultchecked = (
 4174:                                'cancreate_email' => 'off',
 4175:                              );
 4176:         my $itemcount = 0;
 4177:         my $display = 'none';
 4178:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 4179:             $display = 'block';
 4180:         }
 4181:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 4182:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 4183:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4184:         my $usertypes = {};
 4185:         my $order = [];
 4186:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 4187:             $usertypes = $domdefaults{'inststatustypes'};
 4188:             $order = $domdefaults{'inststatusguest'};
 4189:         }
 4190:         if (ref($order) eq 'ARRAY') {
 4191:             push(@{$order},'default');
 4192:             if (@{$order} > 1) {
 4193:                 $usertypes->{'default'} = &mt('Other users');
 4194:                 $additional .= '<table><tr>';
 4195:                 foreach my $status (@{$order}) {
 4196:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 4197:                 }
 4198:                 $additional .= '</tr><tr>';
 4199:                 foreach my $status (@{$order}) {
 4200:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 4201:                 }
 4202:                 $additional .= '</tr></table>';
 4203:             } else {
 4204:                 $usertypes->{'default'} = &mt('All users');
 4205:                 $additional .= &email_as_username($rowtotal,$processing);
 4206:             }
 4207:         }
 4208:         $additional .= '</div>'."\n";
 4209: 
 4210:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4211:                                                      \%choices,$$rowtotal,$onclick,$additional);
 4212:         $$rowtotal ++;
 4213:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4214:         $$rowtotal ++;
 4215:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4216:         $numinrow = 1;
 4217:         if (ref($order) eq 'ARRAY') {
 4218:             foreach my $status (@{$order}) {
 4219:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4220:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4221:                 $$rowtotal ++;
 4222:             }
 4223:         }
 4224:         my ($emailrules,$emailruleorder) =
 4225:             &Apache::lonnet::inst_userrules($dom,'email');
 4226:         if (ref($emailrules) eq 'HASH') {
 4227:             if (keys(%{$emailrules}) > 0) {
 4228:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4229:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4230:                 $$rowtotal ++;
 4231:             }
 4232:         }
 4233:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4234:     }
 4235:     return $datatable;
 4236: }
 4237: 
 4238: sub email_as_username {
 4239:     my ($rowtotal,$processing,$type) = @_;
 4240:     my %choices =
 4241:         &Apache::lonlocal::texthash (
 4242:                                       automatic => 'Automatic approval',
 4243:                                       approval  => 'Queued for approval',
 4244:                                     );
 4245:     my $output;
 4246:     foreach my $option ('automatic','approval') {
 4247:         my $checked;
 4248:         if (ref($processing) eq 'HASH') {
 4249:             if ($type eq '') {   
 4250:                 if (!exists($processing->{'default'})) {
 4251:                     if ($option eq 'automatic') {
 4252:                         $checked = ' checked="checked"';
 4253:                     }
 4254:                 } else {
 4255:                     if ($processing->{'default'} eq $option) {
 4256:                         $checked = ' checked="checked"';
 4257:                     }
 4258:                 }
 4259:             } else {
 4260:                 if (!exists($processing->{$type})) {
 4261:                     if ($option eq 'automatic') {
 4262:                         $checked = ' checked="checked"';
 4263:                     }
 4264:                 } else {
 4265:                     if ($processing->{$type} eq $option) {
 4266:                         $checked = ' checked="checked"';
 4267:                     }
 4268:                 }
 4269:             }
 4270:         } elsif ($option eq 'automatic') {
 4271:             $checked = ' checked="checked"'; 
 4272:         }
 4273:         my $name = 'cancreate_emailprocess';
 4274:         if (($type ne '') && ($type ne 'default')) {
 4275:             $name .= '_'.$type;
 4276:         }
 4277:         $output .= '<span class="LC_nobreak"><label>'.
 4278:                    '<input type="radio" name="'.$name.'"'.
 4279:                    $checked.' value="'.$option.'" />'.
 4280:                    $choices{$option}.'</label></span>';
 4281:         if ($type eq '') {
 4282:             $output .= '&nbsp;';
 4283:         } else {
 4284:             $output .= '<br />';
 4285:         }
 4286:     }
 4287:     $$rowtotal ++;
 4288:     return $output;
 4289: }
 4290: 
 4291: sub captcha_choice {
 4292:     my ($context,$settings,$itemcount) = @_;
 4293:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4294:     my %lt = &captcha_phrases();
 4295:     $keyentry = 'hidden';
 4296:     if ($context eq 'cancreate') {
 4297:         $rowname = &mt('CAPTCHA validation');
 4298:     } elsif ($context eq 'login') {
 4299:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4300:     }
 4301:     if (ref($settings) eq 'HASH') {
 4302:         if ($settings->{'captcha'}) {
 4303:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4304:         } else {
 4305:             $checked{'original'} = ' checked="checked"';
 4306:         }
 4307:         if ($settings->{'captcha'} eq 'recaptcha') {
 4308:             $pubtext = $lt{'pub'};
 4309:             $privtext = $lt{'priv'};
 4310:             $keyentry = 'text';
 4311:         }
 4312:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4313:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4314:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4315:         }
 4316:     } else {
 4317:         $checked{'original'} = ' checked="checked"';
 4318:     }
 4319:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4320:     my $output = '<tr'.$css_class.'>'.
 4321:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4322:                  '<table><tr><td>'."\n";
 4323:     foreach my $option ('original','recaptcha','notused') {
 4324:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4325:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4326:                    $lt{$option}.'</label></span>';
 4327:         unless ($option eq 'notused') {
 4328:             $output .= ('&nbsp;'x2)."\n";
 4329:         }
 4330:     }
 4331: #
 4332: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4333: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4334: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4335: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4336: #
 4337:     $output .= '</td></tr>'."\n".
 4338:                '<tr><td>'."\n".
 4339:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4340:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4341:                $currpub.'" size="40" /></span><br />'."\n".
 4342:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4343:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4344:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4345:                '</td></tr>';
 4346:     return $output;
 4347: }
 4348: 
 4349: sub user_formats_row {
 4350:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4351:     my $output;
 4352:     my %text = (
 4353:                    'username' => 'new usernames',
 4354:                    'id'       => 'IDs',
 4355:                    'email'    => 'self-created accounts (e-mail)',
 4356:                );
 4357:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4358:     $output = '<tr '.$css_class.'>'.
 4359:               '<td><span class="LC_nobreak">';
 4360:     if ($type eq 'email') {
 4361:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4362:     } else {
 4363:         $output .= &mt("Format rules to check for $text{$type}: ");
 4364:     }
 4365:     $output .= '</span></td>'.
 4366:                '<td class="LC_left_item" colspan="2"><table>';
 4367:     my $rem;
 4368:     if (ref($ruleorder) eq 'ARRAY') {
 4369:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4370:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4371:                 my $rem = $i%($numinrow);
 4372:                 if ($rem == 0) {
 4373:                     if ($i > 0) {
 4374:                         $output .= '</tr>';
 4375:                     }
 4376:                     $output .= '<tr>';
 4377:                 }
 4378:                 my $check = ' ';
 4379:                 if (ref($settings) eq 'HASH') {
 4380:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4381:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4382:                             $check = ' checked="checked" ';
 4383:                         }
 4384:                     }
 4385:                 }
 4386:                 $output .= '<td class="LC_left_item">'.
 4387:                            '<span class="LC_nobreak"><label>'.
 4388:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4389:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4390:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4391:             }
 4392:         }
 4393:         $rem = @{$ruleorder}%($numinrow);
 4394:     }
 4395:     my $colsleft = $numinrow - $rem;
 4396:     if ($colsleft > 1 ) {
 4397:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4398:                    '&nbsp;</td>';
 4399:     } elsif ($colsleft == 1) {
 4400:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4401:     }
 4402:     $output .= '</tr></table></td></tr>';
 4403:     return $output;
 4404: }
 4405: 
 4406: sub usercreation_types {
 4407:     my %lt = &Apache::lonlocal::texthash (
 4408:                     author     => 'When adding a co-author',
 4409:                     course     => 'When adding a user to a course',
 4410:                     requestcrs => 'When requesting a course',
 4411:                     any        => 'Any',
 4412:                     official   => 'Institutional only ',
 4413:                     unofficial => 'Non-institutional only',
 4414:                     none       => 'None',
 4415:     );
 4416:     return %lt;
 4417: }
 4418: 
 4419: sub selfcreation_types {
 4420:     my %lt = &Apache::lonlocal::texthash (
 4421:                     selfcreate => 'User creates own account',
 4422:                     any        => 'Any',
 4423:                     official   => 'Institutional only ',
 4424:                     unofficial => 'Non-institutional only',
 4425:                     email      => 'E-mail address',
 4426:                     login      => 'Institutional Login',
 4427:                     sso        => 'SSO',
 4428:              );
 4429: }
 4430: 
 4431: sub authtype_names {
 4432:     my %lt = &Apache::lonlocal::texthash(
 4433:                       int    => 'Internal',
 4434:                       krb4   => 'Kerberos 4',
 4435:                       krb5   => 'Kerberos 5',
 4436:                       loc    => 'Local',
 4437:                   );
 4438:     return %lt;
 4439: }
 4440: 
 4441: sub context_names {
 4442:     my %context_title = &Apache::lonlocal::texthash(
 4443:        author => 'Creating users when an Author',
 4444:        course => 'Creating users when in a course',
 4445:        domain => 'Creating users when a Domain Coordinator',
 4446:     );
 4447:     return %context_title;
 4448: }
 4449: 
 4450: sub print_usermodification {
 4451:     my ($position,$dom,$settings,$rowtotal) = @_;
 4452:     my $numinrow = 4;
 4453:     my ($context,$datatable,$rowcount);
 4454:     if ($position eq 'top') {
 4455:         $rowcount = 0;
 4456:         $context = 'author'; 
 4457:         foreach my $role ('ca','aa') {
 4458:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4459:                                                    $numinrow,$rowcount);
 4460:             $$rowtotal ++;
 4461:             $rowcount ++;
 4462:         }
 4463:     } elsif ($position eq 'bottom') {
 4464:         $context = 'course';
 4465:         $rowcount = 0;
 4466:         foreach my $role ('st','ep','ta','in','cr') {
 4467:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4468:                                                    $numinrow,$rowcount);
 4469:             $$rowtotal ++;
 4470:             $rowcount ++;
 4471:         }
 4472:     }
 4473:     return $datatable;
 4474: }
 4475: 
 4476: sub print_defaults {
 4477:     my ($position,$dom,$settings,$rowtotal) = @_;
 4478:     my $rownum = 0;
 4479:     my ($datatable,$css_class);
 4480:     if ($position eq 'top') {
 4481:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4482:                      'datelocale_def','portal_def');
 4483:         my %defaults;
 4484:         if (ref($settings) eq 'HASH') {
 4485:             %defaults = %{$settings};
 4486:         } else {
 4487:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4488:             foreach my $item (@items) {
 4489:                 $defaults{$item} = $domdefaults{$item};
 4490:             }
 4491:         }
 4492:         my $titles = &defaults_titles($dom);
 4493:         foreach my $item (@items) {
 4494:             if ($rownum%2) {
 4495:                 $css_class = '';
 4496:             } else {
 4497:                 $css_class = ' class="LC_odd_row" ';
 4498:             }
 4499:             $datatable .= '<tr'.$css_class.'>'.
 4500:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4501:                           '</span></td><td class="LC_right_item" colspan="3">';
 4502:             if ($item eq 'auth_def') {
 4503:                 my @authtypes = ('internal','krb4','krb5','localauth');
 4504:                 my %shortauth = (
 4505:                                  internal => 'int',
 4506:                                  krb4 => 'krb4',
 4507:                                  krb5 => 'krb5',
 4508:                                  localauth  => 'loc'
 4509:                                 );
 4510:                 my %authnames = &authtype_names();
 4511:                 foreach my $auth (@authtypes) {
 4512:                     my $checked = ' ';
 4513:                     if ($defaults{$item} eq $auth) {
 4514:                         $checked = ' checked="checked" ';
 4515:                     }
 4516:                     $datatable .= '<label><input type="radio" name="'.$item.
 4517:                                   '" value="'.$auth.'"'.$checked.'/>'.
 4518:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4519:                 }
 4520:             } elsif ($item eq 'timezone_def') {
 4521:                 my $includeempty = 1;
 4522:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4523:             } elsif ($item eq 'datelocale_def') {
 4524:                 my $includeempty = 1;
 4525:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4526:             } elsif ($item eq 'lang_def') {
 4527:                 my %langchoices = &get_languages_hash();
 4528:                 $langchoices{''} = 'No language preference';
 4529:                 %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4530:                 $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4531:                                                               \%langchoices);
 4532:             } else {
 4533:                 my $size;
 4534:                 if ($item eq 'portal_def') {
 4535:                     $size = ' size="25"';
 4536:                 }
 4537:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4538:                               $defaults{$item}.'"'.$size.' />';
 4539:             }
 4540:             $datatable .= '</td></tr>';
 4541:             $rownum ++;
 4542:         }
 4543:     } else {
 4544:         my (%defaults);
 4545:         if (ref($settings) eq 'HASH') {
 4546:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 4547:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 4548:                 my $maxnum = @{$settings->{'inststatusorder'}};
 4549:                 for (my $i=0; $i<$maxnum; $i++) {
 4550:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 4551:                     my $item = $settings->{'inststatusorder'}->[$i];
 4552:                     my $title = $settings->{'inststatustypes'}->{$item};
 4553:                     my $guestok;
 4554:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 4555:                         $guestok = 1;
 4556:                     }
 4557:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 4558:                     $datatable .= '<tr'.$css_class.'>'.
 4559:                                   '<td><span class="LC_nobreak">'.
 4560:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 4561:                     for (my $k=0; $k<=$maxnum; $k++) {
 4562:                         my $vpos = $k+1;
 4563:                         my $selstr;
 4564:                         if ($k == $i) {
 4565:                             $selstr = ' selected="selected" ';
 4566:                         }
 4567:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4568:                     }
 4569:                     my ($checkedon,$checkedoff);
 4570:                     $checkedoff = ' checked="checked"';
 4571:                     if ($guestok) {
 4572:                         $checkedon = $checkedoff;
 4573:                         $checkedoff = ''; 
 4574:                     }
 4575:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 4576:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 4577:                                   &mt('delete').'</span></td>'.
 4578:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 4579:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 4580:                                   '</span></td>'.
 4581:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4582:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 4583:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 4584:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 4585:                                   &mt('No').'</label></span></td></tr>';
 4586:                 }
 4587:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 4588:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 4589:                 $datatable .= '<tr '.$css_class.'>'.
 4590:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 4591:                 for (my $k=0; $k<=$maxnum; $k++) {
 4592:                     my $vpos = $k+1;
 4593:                     my $selstr;
 4594:                     if ($k == $maxnum) {
 4595:                         $selstr = ' selected="selected" ';
 4596:                     }
 4597:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4598:                 }
 4599:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 4600:                               '<input type="text" size="10" name="addinststatus" value="" /></span>'.
 4601:                               '&nbsp;'.&mt('(new)').
 4602:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 4603:                               &mt('Name displayed:').
 4604:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 4605:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 4606:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 4607:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 4608:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 4609:                               &mt('No').'</label></span></td></tr>';
 4610:                               '</tr>'."\n";
 4611:                 $rownum ++;
 4612:             }
 4613:         }
 4614:     }
 4615:     $$rowtotal += $rownum;
 4616:     return $datatable;
 4617: }
 4618: 
 4619: sub get_languages_hash {
 4620:     my %langchoices;
 4621:     foreach my $id (&Apache::loncommon::languageids()) {
 4622:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4623:         if ($code ne '') {
 4624:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4625:         }
 4626:     }
 4627:     return %langchoices;
 4628: }
 4629: 
 4630: sub defaults_titles {
 4631:     my ($dom) = @_;
 4632:     my %titles = &Apache::lonlocal::texthash (
 4633:                    'auth_def'      => 'Default authentication type',
 4634:                    'auth_arg_def'  => 'Default authentication argument',
 4635:                    'lang_def'      => 'Default language',
 4636:                    'timezone_def'  => 'Default timezone',
 4637:                    'datelocale_def' => 'Default locale for dates',
 4638:                    'portal_def'     => 'Portal/Default URL',
 4639:                  );
 4640:     if ($dom) {
 4641:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4642:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4643:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4644:         $protocol = 'http' if ($protocol ne 'https');
 4645:         if ($uint_dom) {
 4646:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4647:                                          $uint_dom);
 4648:         }
 4649:     }
 4650:     return (\%titles);
 4651: }
 4652: 
 4653: sub print_scantronformat {
 4654:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4655:     my $itemcount = 1;
 4656:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4657:         %confhash);
 4658:     my $switchserver = &check_switchserver($dom,$confname);
 4659:     my %lt = &Apache::lonlocal::texthash (
 4660:                 default => 'Default bubblesheet format file error',
 4661:                 custom  => 'Custom bubblesheet format file error',
 4662:              );
 4663:     my %scantronfiles = (
 4664:         default => 'default.tab',
 4665:         custom => 'custom.tab',
 4666:     );
 4667:     foreach my $key (keys(%scantronfiles)) {
 4668:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4669:                               .$scantronfiles{$key};
 4670:     }
 4671:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4672:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4673:         if (!$switchserver) {
 4674:             my $servadm = $r->dir_config('lonAdmEMail');
 4675:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4676:             if ($configuserok eq 'ok') {
 4677:                 if ($author_ok eq 'ok') {
 4678:                     my %legacyfile = (
 4679:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4680:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4681:                     );
 4682:                     my %md5chk;
 4683:                     foreach my $type (keys(%legacyfile)) {
 4684:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4685:                         chomp($md5chk{$type});
 4686:                     }
 4687:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4688:                         foreach my $type (keys(%legacyfile)) {
 4689:                             ($scantronurls{$type},my $error) = 
 4690:                                 &legacy_scantronformat($r,$dom,$confname,
 4691:                                                  $type,$legacyfile{$type},
 4692:                                                  $scantronurls{$type},
 4693:                                                  $scantronfiles{$type});
 4694:                             if ($error ne '') {
 4695:                                 $error{$type} = $error;
 4696:                             }
 4697:                         }
 4698:                         if (keys(%error) == 0) {
 4699:                             $is_custom = 1;
 4700:                             $confhash{'scantron'}{'scantronformat'} = 
 4701:                                 $scantronurls{'custom'};
 4702:                             my $putresult = 
 4703:                                 &Apache::lonnet::put_dom('configuration',
 4704:                                                          \%confhash,$dom);
 4705:                             if ($putresult ne 'ok') {
 4706:                                 $error{'custom'} = 
 4707:                                     '<span class="LC_error">'.
 4708:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4709:                             }
 4710:                         }
 4711:                     } else {
 4712:                         ($scantronurls{'default'},my $error) =
 4713:                             &legacy_scantronformat($r,$dom,$confname,
 4714:                                           'default',$legacyfile{'default'},
 4715:                                           $scantronurls{'default'},
 4716:                                           $scantronfiles{'default'});
 4717:                         if ($error eq '') {
 4718:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4719:                             my $putresult =
 4720:                                 &Apache::lonnet::put_dom('configuration',
 4721:                                                          \%confhash,$dom);
 4722:                             if ($putresult ne 'ok') {
 4723:                                 $error{'default'} =
 4724:                                     '<span class="LC_error">'.
 4725:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4726:                             }
 4727:                         } else {
 4728:                             $error{'default'} = $error;
 4729:                         }
 4730:                     }
 4731:                 }
 4732:             }
 4733:         } else {
 4734:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4735:         }
 4736:     }
 4737:     if (ref($settings) eq 'HASH') {
 4738:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4739:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4740:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4741:                 $scantronurl = '';
 4742:             } else {
 4743:                 $scantronurl = $settings->{'scantronformat'};
 4744:             }
 4745:             $is_custom = 1;
 4746:         } else {
 4747:             $scantronurl = $scantronurls{'default'};
 4748:         }
 4749:     } else {
 4750:         if ($is_custom) {
 4751:             $scantronurl = $scantronurls{'custom'};
 4752:         } else {
 4753:             $scantronurl = $scantronurls{'default'};
 4754:         }
 4755:     }
 4756:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4757:     $datatable .= '<tr'.$css_class.'>';
 4758:     if (!$is_custom) {
 4759:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4760:                       '<span class="LC_nobreak">';
 4761:         if ($scantronurl) {
 4762:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4763:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4764:         } else {
 4765:             $datatable = &mt('File unavailable for display');
 4766:         }
 4767:         $datatable .= '</span></td>';
 4768:         if (keys(%error) == 0) { 
 4769:             $datatable .= '<td valign="bottom">';
 4770:             if (!$switchserver) {
 4771:                 $datatable .= &mt('Upload:').'<br />';
 4772:             }
 4773:         } else {
 4774:             my $errorstr;
 4775:             foreach my $key (sort(keys(%error))) {
 4776:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4777:             }
 4778:             $datatable .= '<td>'.$errorstr;
 4779:         }
 4780:     } else {
 4781:         if (keys(%error) > 0) {
 4782:             my $errorstr;
 4783:             foreach my $key (sort(keys(%error))) {
 4784:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4785:             } 
 4786:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4787:         } elsif ($scantronurl) {
 4788:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4789:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4790:             $datatable .= '<td><span class="LC_nobreak">'.
 4791:                           $link.
 4792:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4793:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4794:                           '<td><span class="LC_nobreak">&nbsp;'.
 4795:                           &mt('Replace:').'</span><br />';
 4796:         }
 4797:     }
 4798:     if (keys(%error) == 0) {
 4799:         if ($switchserver) {
 4800:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4801:         } else {
 4802:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4803:                          '<input type="file" name="scantronformat" /></span>';
 4804:         }
 4805:     }
 4806:     $datatable .= '</td></tr>';
 4807:     $$rowtotal ++;
 4808:     return $datatable;
 4809: }
 4810: 
 4811: sub legacy_scantronformat {
 4812:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4813:     my ($url,$error);
 4814:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4815:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4816:         (my $result,$url) =
 4817:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4818:                          '','',$newfile);
 4819:         if ($result ne 'ok') {
 4820:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4821:         }
 4822:     }
 4823:     return ($url,$error);
 4824: }
 4825: 
 4826: sub print_coursecategories {
 4827:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4828:     my $datatable;
 4829:     if ($position eq 'top') {
 4830:         my (%checked);
 4831:         my @catitems = ('unauth','auth');
 4832:         my @cattypes = ('std','domonly','codesrch','none');
 4833:         $checked{'unauth'} = 'std';
 4834:         $checked{'auth'} = 'std';
 4835:         if (ref($settings) eq 'HASH') {
 4836:             foreach my $type (@cattypes) {
 4837:                 if ($type eq $settings->{'unauth'}) {
 4838:                     $checked{'unauth'} = $type;
 4839:                 }
 4840:                 if ($type eq $settings->{'auth'}) {
 4841:                     $checked{'auth'} = $type;
 4842:                 }
 4843:             }
 4844:         }
 4845:         my %lt = &Apache::lonlocal::texthash (
 4846:                                                unauth   => 'Catalog type for unauthenticated users',
 4847:                                                auth     => 'Catalog type for authenticated users',
 4848:                                                none     => 'No catalog',
 4849:                                                std      => 'Standard catalog',
 4850:                                                domonly  => 'Domain-only catalog',
 4851:                                                codesrch => "Code search form",
 4852:                                              );
 4853:        my $itemcount = 0;
 4854:        foreach my $item (@catitems) {
 4855:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 4856:            $datatable .= '<tr '.$css_class.'>'.
 4857:                          '<td>'.$lt{$item}.'</td>'.
 4858:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 4859:            foreach my $type (@cattypes) {
 4860:                my $ischecked;
 4861:                if ($checked{$item} eq $type) {
 4862:                    $ischecked=' checked="checked"';
 4863:                }
 4864:                $datatable .= '<label>'.
 4865:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 4866:                              ' />'.$lt{$type}.'</label>&nbsp;';
 4867:            }
 4868:            $datatable .= '</td></tr>';
 4869:            $itemcount ++;
 4870:         }
 4871:         $$rowtotal += $itemcount;
 4872:     } elsif ($position eq 'middle') {
 4873:         my $toggle_cats_crs = ' ';
 4874:         my $toggle_cats_dom = ' checked="checked" ';
 4875:         my $can_cat_crs = ' ';
 4876:         my $can_cat_dom = ' checked="checked" ';
 4877:         my $toggle_catscomm_comm = ' ';
 4878:         my $toggle_catscomm_dom = ' checked="checked" ';
 4879:         my $can_catcomm_comm = ' ';
 4880:         my $can_catcomm_dom = ' checked="checked" ';
 4881: 
 4882:         if (ref($settings) eq 'HASH') {
 4883:             if ($settings->{'togglecats'} eq 'crs') {
 4884:                 $toggle_cats_crs = $toggle_cats_dom;
 4885:                 $toggle_cats_dom = ' ';
 4886:             }
 4887:             if ($settings->{'categorize'} eq 'crs') {
 4888:                 $can_cat_crs = $can_cat_dom;
 4889:                 $can_cat_dom = ' ';
 4890:             }
 4891:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4892:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4893:                 $toggle_catscomm_dom = ' ';
 4894:             }
 4895:             if ($settings->{'categorizecomm'} eq 'comm') {
 4896:                 $can_catcomm_comm = $can_catcomm_dom;
 4897:                 $can_catcomm_dom = ' ';
 4898:             }
 4899:         }
 4900:         my %title = &Apache::lonlocal::texthash (
 4901:                      togglecats     => 'Show/Hide a course in catalog',
 4902:                      togglecatscomm => 'Show/Hide a community in catalog',
 4903:                      categorize     => 'Assign a category to a course',
 4904:                      categorizecomm => 'Assign a category to a community',
 4905:                     );
 4906:         my %level = &Apache::lonlocal::texthash (
 4907:                      dom  => 'Set in Domain',
 4908:                      crs  => 'Set in Course',
 4909:                      comm => 'Set in Community',
 4910:                     );
 4911:         $datatable = '<tr class="LC_odd_row">'.
 4912:                   '<td>'.$title{'togglecats'}.'</td>'.
 4913:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4914:                   '<input type="radio" name="togglecats"'.
 4915:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4916:                   '<label><input type="radio" name="togglecats"'.
 4917:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4918:                   '</tr><tr>'.
 4919:                   '<td>'.$title{'categorize'}.'</td>'.
 4920:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4921:                   '<label><input type="radio" name="categorize"'.
 4922:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4923:                   '<label><input type="radio" name="categorize"'.
 4924:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4925:                   '</tr><tr class="LC_odd_row">'.
 4926:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4927:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4928:                   '<input type="radio" name="togglecatscomm"'.
 4929:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4930:                   '<label><input type="radio" name="togglecatscomm"'.
 4931:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4932:                   '</tr><tr>'.
 4933:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4934:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4935:                   '<label><input type="radio" name="categorizecomm"'.
 4936:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4937:                   '<label><input type="radio" name="categorizecomm"'.
 4938:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4939:                   '</tr>';
 4940:         $$rowtotal += 4;
 4941:     } else {
 4942:         my $css_class;
 4943:         my $itemcount = 1;
 4944:         my $cathash; 
 4945:         if (ref($settings) eq 'HASH') {
 4946:             $cathash = $settings->{'cats'};
 4947:         }
 4948:         if (ref($cathash) eq 'HASH') {
 4949:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4950:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4951:                                                    \%allitems,\%idx,\@jsarray);
 4952:             my $maxdepth = scalar(@cats);
 4953:             my $colattrib = '';
 4954:             if ($maxdepth > 2) {
 4955:                 $colattrib = ' colspan="2" ';
 4956:             }
 4957:             my @path;
 4958:             if (@cats > 0) {
 4959:                 if (ref($cats[0]) eq 'ARRAY') {
 4960:                     my $numtop = @{$cats[0]};
 4961:                     my $maxnum = $numtop;
 4962:                     my %default_names = (
 4963:                           instcode    => &mt('Official courses'),
 4964:                           communities => &mt('Communities'),
 4965:                     );
 4966: 
 4967:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4968:                         ($cathash->{'instcode::0'} eq '') ||
 4969:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4970:                         ($cathash->{'communities::0'} eq '')) {
 4971:                         $maxnum ++;
 4972:                     }
 4973:                     my $lastidx;
 4974:                     for (my $i=0; $i<$numtop; $i++) {
 4975:                         my $parent = $cats[0][$i];
 4976:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4977:                         my $item = &escape($parent).'::0';
 4978:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4979:                         $lastidx = $idx{$item};
 4980:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4981:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4982:                         for (my $k=0; $k<=$maxnum; $k++) {
 4983:                             my $vpos = $k+1;
 4984:                             my $selstr;
 4985:                             if ($k == $i) {
 4986:                                 $selstr = ' selected="selected" ';
 4987:                             }
 4988:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4989:                         }
 4990:                         $datatable .= '</select></span></td><td>';
 4991:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4992:                             $datatable .=  '<span class="LC_nobreak">'
 4993:                                            .$default_names{$parent}.'</span>';
 4994:                             if ($parent eq 'instcode') {
 4995:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4996:                                               .&mt('with institutional codes')
 4997:                                               .')</span></td><td'.$colattrib.'>';
 4998:                             } else {
 4999:                                 $datatable .= '<table><tr><td>';
 5000:                             }
 5001:                             $datatable .= '<span class="LC_nobreak">'
 5002:                                           .'<label><input type="radio" name="'
 5003:                                           .$parent.'" value="1" checked="checked" />'
 5004:                                           .&mt('Display').'</label>';
 5005:                             if ($parent eq 'instcode') {
 5006:                                 $datatable .= '&nbsp;';
 5007:                             } else {
 5008:                                 $datatable .= '</span></td></tr><tr><td>'
 5009:                                               .'<span class="LC_nobreak">';
 5010:                             }
 5011:                             $datatable .= '<label><input type="radio" name="'
 5012:                                           .$parent.'" value="0" />'
 5013:                                           .&mt('Do not display').'</label></span>';
 5014:                             if ($parent eq 'communities') {
 5015:                                 $datatable .= '</td></tr></table>';
 5016:                             }
 5017:                             $datatable .= '</td>';
 5018:                         } else {
 5019:                             $datatable .= $parent
 5020:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 5021:                                           .'<input type="checkbox" name="deletecategory" '
 5022:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 5023:                         }
 5024:                         my $depth = 1;
 5025:                         push(@path,$parent);
 5026:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 5027:                         pop(@path);
 5028:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 5029:                         $itemcount ++;
 5030:                     }
 5031:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5032:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 5033:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 5034:                     for (my $k=0; $k<=$maxnum; $k++) {
 5035:                         my $vpos = $k+1;
 5036:                         my $selstr;
 5037:                         if ($k == $numtop) {
 5038:                             $selstr = ' selected="selected" ';
 5039:                         }
 5040:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5041:                     }
 5042:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 5043:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 5044:                                   .'</tr>'."\n";
 5045:                     $itemcount ++;
 5046:                     foreach my $default ('instcode','communities') {
 5047:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 5048:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5049:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 5050:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 5051:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5052:                             for (my $k=0; $k<=$maxnum; $k++) {
 5053:                                 my $vpos = $k+1;
 5054:                                 my $selstr;
 5055:                                 if ($k == $maxnum) {
 5056:                                     $selstr = ' selected="selected" ';
 5057:                                 }
 5058:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5059:                             }
 5060:                             $datatable .= '</select></span></td>'.
 5061:                                           '<td><span class="LC_nobreak">'.
 5062:                                           $default_names{$default}.'</span>';
 5063:                             if ($default eq 'instcode') {
 5064:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5065:                                               .&mt('with institutional codes').')</span>';
 5066:                             }
 5067:                             $datatable .= '</td>'
 5068:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5069:                                           .&mt('Display').'</label>&nbsp;'
 5070:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5071:                                           .&mt('Do not display').'</label></span></td></tr>';
 5072:                         }
 5073:                     }
 5074:                 }
 5075:             } else {
 5076:                 $datatable .= &initialize_categories($itemcount);
 5077:             }
 5078:         } else {
 5079:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 5080:                           .&initialize_categories($itemcount);
 5081:         }
 5082:         $$rowtotal += $itemcount;
 5083:     }
 5084:     return $datatable;
 5085: }
 5086: 
 5087: sub print_serverstatuses {
 5088:     my ($dom,$settings,$rowtotal) = @_;
 5089:     my $datatable;
 5090:     my @pages = &serverstatus_pages();
 5091:     my (%namedaccess,%machineaccess);
 5092:     foreach my $type (@pages) {
 5093:         $namedaccess{$type} = '';
 5094:         $machineaccess{$type}= '';
 5095:     }
 5096:     if (ref($settings) eq 'HASH') {
 5097:         foreach my $type (@pages) {
 5098:             if (exists($settings->{$type})) {
 5099:                 if (ref($settings->{$type}) eq 'HASH') {
 5100:                     foreach my $key (keys(%{$settings->{$type}})) {
 5101:                         if ($key eq 'namedusers') {
 5102:                             $namedaccess{$type} = $settings->{$type}->{$key};
 5103:                         } elsif ($key eq 'machines') {
 5104:                             $machineaccess{$type} = $settings->{$type}->{$key};
 5105:                         }
 5106:                     }
 5107:                 }
 5108:             }
 5109:         }
 5110:     }
 5111:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 5112:     my $rownum = 0;
 5113:     my $css_class;
 5114:     foreach my $type (@pages) {
 5115:         $rownum ++;
 5116:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 5117:         $datatable .= '<tr'.$css_class.'>'.
 5118:                       '<td><span class="LC_nobreak">'.
 5119:                       $titles->{$type}.'</span></td>'.
 5120:                       '<td class="LC_left_item">'.
 5121:                       '<input type="text" name="'.$type.'_namedusers" '.
 5122:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 5123:                       '<td class="LC_right_item">'.
 5124:                       '<span class="LC_nobreak">'.
 5125:                       '<input type="text" name="'.$type.'_machines" '.
 5126:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 5127:                       '</td></tr>'."\n";
 5128:     }
 5129:     $$rowtotal += $rownum;
 5130:     return $datatable;
 5131: }
 5132: 
 5133: sub serverstatus_pages {
 5134:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 5135:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 5136:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 5137:             'uniquecodes','diskusage');
 5138: }
 5139: 
 5140: sub defaults_javascript {
 5141:     my ($settings) = @_;
 5142:     my ($output,$jstext); 
 5143:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5144:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 5145:         if ($maxnum eq '') {
 5146:             $maxnum = 0;
 5147:         }
 5148:         $maxnum ++;
 5149:         $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 5150:         return <<"ENDSCRIPT";
 5151: <script type="text/javascript">
 5152: // <![CDATA[
 5153: function reorderTypes(form,caller) {
 5154:     var changedVal;
 5155: $jstext 
 5156:     var newpos = 'addinststatus_pos';
 5157:     var current = new Array;
 5158:     var maxh = $maxnum;
 5159:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5160:     var oldVal;
 5161:     if (caller == newpos) {
 5162:         changedVal = newitemVal;
 5163:     } else {
 5164:         var curritem = 'inststatus_pos_'+caller;
 5165:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 5166:         current[newitemVal] = newpos;
 5167:     }
 5168:     for (var i=0; i<inststatuses.length; i++) {
 5169:         if (inststatuses[i] != caller) {
 5170:             var elementName = 'inststatus_pos_'+inststatuses[i];
 5171:             if (form.elements[elementName]) {
 5172:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5173:                 current[currVal] = elementName;
 5174:             }
 5175:         }
 5176:     }
 5177:     for (var j=0; j<maxh; j++) {
 5178:         if (current[j] == undefined) {
 5179:             oldVal = j;
 5180:         }
 5181:     }
 5182:     if (oldVal < changedVal) {
 5183:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5184:            var elementName = current[k];
 5185:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5186:         }
 5187:     } else {
 5188:         for (var k=changedVal; k<oldVal; k++) {
 5189:             var elementName = current[k];
 5190:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5191:         }
 5192:     }
 5193:     return;
 5194: }
 5195: 
 5196: // ]]>
 5197: </script>
 5198: 
 5199: ENDSCRIPT
 5200:     }
 5201: }
 5202: 
 5203: sub coursecategories_javascript {
 5204:     my ($settings) = @_;
 5205:     my ($output,$jstext,$cathash);
 5206:     if (ref($settings) eq 'HASH') {
 5207:         $cathash = $settings->{'cats'};
 5208:     }
 5209:     if (ref($cathash) eq 'HASH') {
 5210:         my (@cats,@jsarray,%idx);
 5211:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 5212:         if (@jsarray > 0) {
 5213:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 5214:             for (my $i=0; $i<@jsarray; $i++) {
 5215:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 5216:                     my $catstr = join('","',@{$jsarray[$i]});
 5217:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 5218:                 }
 5219:             }
 5220:         }
 5221:     } else {
 5222:         $jstext  = '    var categories = Array(1);'."\n".
 5223:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 5224:     }
 5225:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 5226:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 5227:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category.'); 
 5228:     $output = <<"ENDSCRIPT";
 5229: <script type="text/javascript">
 5230: // <![CDATA[
 5231: function reorderCats(form,parent,item,idx) {
 5232:     var changedVal;
 5233: $jstext
 5234:     var newpos = 'addcategory_pos';
 5235:     if (parent == '') {
 5236:         var has_instcode = 0;
 5237:         var maxtop = categories[idx].length;
 5238:         for (var j=0; j<maxtop; j++) {
 5239:             if (categories[idx][j] == 'instcode::0') {
 5240:                 has_instcode == 1;
 5241:             }
 5242:         }
 5243:         if (has_instcode == 0) {
 5244:             categories[idx][maxtop] = 'instcode_pos';
 5245:         }
 5246:     } else {
 5247:         newpos += '_'+parent;
 5248:     }
 5249:     var maxh = 1 + categories[idx].length;
 5250:     var current = new Array;
 5251:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5252:     if (item == newpos) {
 5253:         changedVal = newitemVal;
 5254:     } else {
 5255:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5256:         current[newitemVal] = newpos;
 5257:     }
 5258:     for (var i=0; i<categories[idx].length; i++) {
 5259:         var elementName = categories[idx][i];
 5260:         if (elementName != item) {
 5261:             if (form.elements[elementName]) {
 5262:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5263:                 current[currVal] = elementName;
 5264:             }
 5265:         }
 5266:     }
 5267:     var oldVal;
 5268:     for (var j=0; j<maxh; j++) {
 5269:         if (current[j] == undefined) {
 5270:             oldVal = j;
 5271:         }
 5272:     }
 5273:     if (oldVal < changedVal) {
 5274:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5275:            var elementName = current[k];
 5276:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5277:         }
 5278:     } else {
 5279:         for (var k=changedVal; k<oldVal; k++) {
 5280:             var elementName = current[k];
 5281:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5282:         }
 5283:     }
 5284:     return;
 5285: }
 5286: 
 5287: function categoryCheck(form) {
 5288:     if (form.elements['addcategory_name'].value == 'instcode') {
 5289:         alert('$instcode_reserved\\n$choose_again');
 5290:         return false;
 5291:     }
 5292:     if (form.elements['addcategory_name'].value == 'communities') {
 5293:         alert('$communities_reserved\\n$choose_again');
 5294:         return false;
 5295:     }
 5296:     return true;
 5297: }
 5298: 
 5299: // ]]>
 5300: </script>
 5301: 
 5302: ENDSCRIPT
 5303:     return $output;
 5304: }
 5305: 
 5306: sub initialize_categories {
 5307:     my ($itemcount) = @_;
 5308:     my ($datatable,$css_class,$chgstr);
 5309:     my %default_names = (
 5310:                       instcode    => 'Official courses (with institutional codes)',
 5311:                       communities => 'Communities',
 5312:                         );
 5313:     my $select0 = ' selected="selected"';
 5314:     my $select1 = '';
 5315:     foreach my $default ('instcode','communities') {
 5316:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5317:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 5318:         if ($default eq 'communities') {
 5319:             $select1 = $select0;
 5320:             $select0 = '';
 5321:         }
 5322:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5323:                      .'<select name="'.$default.'_pos">'
 5324:                      .'<option value="0"'.$select0.'>1</option>'
 5325:                      .'<option value="1"'.$select1.'>2</option>'
 5326:                      .'<option value="2">3</option></select>&nbsp;'
 5327:                      .$default_names{$default}
 5328:                      .'</span></td><td><span class="LC_nobreak">'
 5329:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5330:                      .&mt('Display').'</label>&nbsp;<label>'
 5331:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5332:                  .'</label></span></td></tr>';
 5333:         $itemcount ++;
 5334:     }
 5335:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5336:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5337:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5338:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5339:                   .'<option value="0">1</option>'
 5340:                   .'<option value="1">2</option>'
 5341:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5342:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5343:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5344:     return $datatable;
 5345: }
 5346: 
 5347: sub build_category_rows {
 5348:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5349:     my ($text,$name,$item,$chgstr);
 5350:     if (ref($cats) eq 'ARRAY') {
 5351:         my $maxdepth = scalar(@{$cats});
 5352:         if (ref($cats->[$depth]) eq 'HASH') {
 5353:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5354:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5355:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5356:                 $text .= '<td><table class="LC_data_table">';
 5357:                 my ($idxnum,$parent_name,$parent_item);
 5358:                 my $higher = $depth - 1;
 5359:                 if ($higher == 0) {
 5360:                     $parent_name = &escape($parent).'::'.$higher;
 5361:                 } else {
 5362:                     if (ref($path) eq 'ARRAY') {
 5363:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5364:                     }
 5365:                 }
 5366:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5367:                 for (my $j=0; $j<=$numchildren; $j++) {
 5368:                     if ($j < $numchildren) {
 5369:                         $name = $cats->[$depth]{$parent}[$j];
 5370:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5371:                         $idxnum = $idx->{$item};
 5372:                     } else {
 5373:                         $name = $parent_name;
 5374:                         $item = $parent_item;
 5375:                     }
 5376:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5377:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5378:                     for (my $i=0; $i<=$numchildren; $i++) {
 5379:                         my $vpos = $i+1;
 5380:                         my $selstr;
 5381:                         if ($j == $i) {
 5382:                             $selstr = ' selected="selected" ';
 5383:                         }
 5384:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5385:                     }
 5386:                     $text .= '</select>&nbsp;';
 5387:                     if ($j < $numchildren) {
 5388:                         my $deeper = $depth+1;
 5389:                         $text .= $name.'&nbsp;'
 5390:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5391:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5392:                         if(ref($path) eq 'ARRAY') {
 5393:                             push(@{$path},$name);
 5394:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5395:                             pop(@{$path});
 5396:                         }
 5397:                     } else {
 5398:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5399:                         if ($j == $numchildren) {
 5400:                             $text .= $name;
 5401:                         } else {
 5402:                             $text .= $item;
 5403:                         }
 5404:                         $text .= '" value="" />';
 5405:                     }
 5406:                     $text .= '</td></tr>';
 5407:                 }
 5408:                 $text .= '</table></td>';
 5409:             } else {
 5410:                 my $higher = $depth-1;
 5411:                 if ($higher == 0) {
 5412:                     $name = &escape($parent).'::'.$higher;
 5413:                 } else {
 5414:                     if (ref($path) eq 'ARRAY') {
 5415:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5416:                     }
 5417:                 }
 5418:                 my $colspan;
 5419:                 if ($parent ne 'instcode') {
 5420:                     $colspan = $maxdepth - $depth - 1;
 5421:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5422:                 }
 5423:             }
 5424:         }
 5425:     }
 5426:     return $text;
 5427: }
 5428: 
 5429: sub modifiable_userdata_row {
 5430:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5431:     my ($role,$rolename,$statustype);
 5432:     $role = $item;
 5433:     if ($context eq 'cancreate') {
 5434:         if ($item =~ /^emailusername_(.+)$/) {
 5435:             $statustype = $1;
 5436:             $role = 'emailusername';
 5437:             if (ref($usertypes) eq 'HASH') {
 5438:                 if ($usertypes->{$statustype}) {
 5439:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5440:                 } else {
 5441:                     $rolename = &mt('Data provided by user');
 5442:                 }
 5443:             }
 5444:         }
 5445:     } elsif ($context eq 'selfcreate') {
 5446:         if (ref($usertypes) eq 'HASH') {
 5447:             $rolename = $usertypes->{$role};
 5448:         } else {
 5449:             $rolename = $role;
 5450:         }
 5451:     } else {
 5452:         if ($role eq 'cr') {
 5453:             $rolename = &mt('Custom role');
 5454:         } else {
 5455:             $rolename = &Apache::lonnet::plaintext($role);
 5456:         }
 5457:     }
 5458:     my (@fields,%fieldtitles);
 5459:     if (ref($fieldsref) eq 'ARRAY') {
 5460:         @fields = @{$fieldsref};
 5461:     } else {
 5462:         @fields = ('lastname','firstname','middlename','generation',
 5463:                    'permanentemail','id');
 5464:     }
 5465:     if ((ref($titlesref) eq 'HASH')) {
 5466:         %fieldtitles = %{$titlesref};
 5467:     } else {
 5468:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5469:     }
 5470:     my $output;
 5471:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5472:     $output = '<tr '.$css_class.'>'.
 5473:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5474:               '<td class="LC_left_item" colspan="2"><table>';
 5475:     my $rem;
 5476:     my %checks;
 5477:     if (ref($settings) eq 'HASH') {
 5478:         if (ref($settings->{$context}) eq 'HASH') {
 5479:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5480:                 my $hashref = $settings->{$context}->{$role};
 5481:                 if ($role eq 'emailusername') {
 5482:                     if ($statustype) {
 5483:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5484:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5485:                             if (ref($hashref) eq 'HASH') { 
 5486:                                 foreach my $field (@fields) {
 5487:                                     if ($hashref->{$field}) {
 5488:                                         $checks{$field} = $hashref->{$field};
 5489:                                     }
 5490:                                 }
 5491:                             }
 5492:                         }
 5493:                     }
 5494:                 } else {
 5495:                     if (ref($hashref) eq 'HASH') {
 5496:                         foreach my $field (@fields) {
 5497:                             if ($hashref->{$field}) {
 5498:                                 $checks{$field} = ' checked="checked" ';
 5499:                             }
 5500:                         }
 5501:                     }
 5502:                 }
 5503:             }
 5504:         }
 5505:     }
 5506:      
 5507:     for (my $i=0; $i<@fields; $i++) {
 5508:         my $rem = $i%($numinrow);
 5509:         if ($rem == 0) {
 5510:             if ($i > 0) {
 5511:                 $output .= '</tr>';
 5512:             }
 5513:             $output .= '<tr>';
 5514:         }
 5515:         my $check = ' ';
 5516:         unless ($role eq 'emailusername') {
 5517:             if (exists($checks{$fields[$i]})) {
 5518:                 $check = $checks{$fields[$i]}
 5519:             } else {
 5520:                 if ($role eq 'st') {
 5521:                     if (ref($settings) ne 'HASH') {
 5522:                         $check = ' checked="checked" '; 
 5523:                     }
 5524:                 }
 5525:             }
 5526:         }
 5527:         $output .= '<td class="LC_left_item">'.
 5528:                    '<span class="LC_nobreak">';
 5529:         if ($role eq 'emailusername') {
 5530:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5531:                 $checks{$fields[$i]} = 'omit';
 5532:             }
 5533:             foreach my $option ('required','optional','omit') {
 5534:                 my $checked='';
 5535:                 if ($checks{$fields[$i]} eq $option) {
 5536:                     $checked='checked="checked" ';
 5537:                 }
 5538:                 $output .= '<label>'.
 5539:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5540:                            &mt($option).'</label>'.('&nbsp;' x2);
 5541:             }
 5542:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5543:         } else {
 5544:             $output .= '<label>'.
 5545:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5546:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5547:                        '</label>';
 5548:         }
 5549:         $output .= '</span></td>';
 5550:         $rem = @fields%($numinrow);
 5551:     }
 5552:     my $colsleft = $numinrow - $rem;
 5553:     if ($colsleft > 1 ) {
 5554:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5555:                    '&nbsp;</td>';
 5556:     } elsif ($colsleft == 1) {
 5557:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5558:     }
 5559:     $output .= '</tr></table></td></tr>';
 5560:     return $output;
 5561: }
 5562: 
 5563: sub insttypes_row {
 5564:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5565:     my %lt = &Apache::lonlocal::texthash (
 5566:                       cansearch => 'Users allowed to search',
 5567:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5568:                       lockablenames => 'User preference to lock name',
 5569:              );
 5570:     my $showdom;
 5571:     if ($context eq 'cansearch') {
 5572:         $showdom = ' ('.$dom.')';
 5573:     }
 5574:     my $class = 'LC_left_item';
 5575:     if ($context eq 'statustocreate') {
 5576:         $class = 'LC_right_item';
 5577:     }
 5578:     my $css_class = ' class="LC_odd_row"';
 5579:     if ($rownum ne '') { 
 5580:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5581:     }
 5582:     my $output = '<tr'.$css_class.'>'.
 5583:                  '<td>'.$lt{$context}.$showdom.
 5584:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5585:     my $rem;
 5586:     if (ref($types) eq 'ARRAY') {
 5587:         for (my $i=0; $i<@{$types}; $i++) {
 5588:             if (defined($usertypes->{$types->[$i]})) {
 5589:                 my $rem = $i%($numinrow);
 5590:                 if ($rem == 0) {
 5591:                     if ($i > 0) {
 5592:                         $output .= '</tr>';
 5593:                     }
 5594:                     $output .= '<tr>';
 5595:                 }
 5596:                 my $check = ' ';
 5597:                 if (ref($settings) eq 'HASH') {
 5598:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5599:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5600:                             $check = ' checked="checked" ';
 5601:                         }
 5602:                     } elsif ($context eq 'statustocreate') {
 5603:                         $check = ' checked="checked" ';
 5604:                     }
 5605:                 }
 5606:                 $output .= '<td class="LC_left_item">'.
 5607:                            '<span class="LC_nobreak"><label>'.
 5608:                            '<input type="checkbox" name="'.$context.'" '.
 5609:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5610:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5611:             }
 5612:         }
 5613:         $rem = @{$types}%($numinrow);
 5614:     }
 5615:     my $colsleft = $numinrow - $rem;
 5616:     if (($rem == 0) && (@{$types} > 0)) {
 5617:         $output .= '<tr>';
 5618:     }
 5619:     if ($colsleft > 1) {
 5620:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5621:     } else {
 5622:         $output .= '<td class="LC_left_item">';
 5623:     }
 5624:     my $defcheck = ' ';
 5625:     if (ref($settings) eq 'HASH') {  
 5626:         if (ref($settings->{$context}) eq 'ARRAY') {
 5627:             if (grep(/^default$/,@{$settings->{$context}})) {
 5628:                 $defcheck = ' checked="checked" ';
 5629:             }
 5630:         } elsif ($context eq 'statustocreate') {
 5631:             $defcheck = ' checked="checked" ';
 5632:         }
 5633:     }
 5634:     $output .= '<span class="LC_nobreak"><label>'.
 5635:                '<input type="checkbox" name="'.$context.'" '.
 5636:                'value="default"'.$defcheck.'/>'.
 5637:                $othertitle.'</label></span></td>'.
 5638:                '</tr></table></td></tr>';
 5639:     return $output;
 5640: }
 5641: 
 5642: sub sorted_searchtitles {
 5643:     my %searchtitles = &Apache::lonlocal::texthash(
 5644:                          'uname' => 'username',
 5645:                          'lastname' => 'last name',
 5646:                          'lastfirst' => 'last name, first name',
 5647:                      );
 5648:     my @titleorder = ('uname','lastname','lastfirst');
 5649:     return (\%searchtitles,\@titleorder);
 5650: }
 5651: 
 5652: sub sorted_searchtypes {
 5653:     my %srchtypes_desc = (
 5654:                            exact    => 'is exact match',
 5655:                            contains => 'contains ..',
 5656:                            begins   => 'begins with ..',
 5657:                          );
 5658:     my @srchtypeorder = ('exact','begins','contains');
 5659:     return (\%srchtypes_desc,\@srchtypeorder);
 5660: }
 5661: 
 5662: sub usertype_update_row {
 5663:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5664:     my $datatable;
 5665:     my $numinrow = 4;
 5666:     foreach my $type (@{$types}) {
 5667:         if (defined($usertypes->{$type})) {
 5668:             $$rownums ++;
 5669:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5670:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5671:                           '</td><td class="LC_left_item"><table>';
 5672:             for (my $i=0; $i<@{$fields}; $i++) {
 5673:                 my $rem = $i%($numinrow);
 5674:                 if ($rem == 0) {
 5675:                     if ($i > 0) {
 5676:                         $datatable .= '</tr>';
 5677:                     }
 5678:                     $datatable .= '<tr>';
 5679:                 }
 5680:                 my $check = ' ';
 5681:                 if (ref($settings) eq 'HASH') {
 5682:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5683:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5684:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5685:                                 $check = ' checked="checked" ';
 5686:                             }
 5687:                         }
 5688:                     }
 5689:                 }
 5690: 
 5691:                 if ($i == @{$fields}-1) {
 5692:                     my $colsleft = $numinrow - $rem;
 5693:                     if ($colsleft > 1) {
 5694:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5695:                     } else {
 5696:                         $datatable .= '<td>';
 5697:                     }
 5698:                 } else {
 5699:                     $datatable .= '<td>';
 5700:                 }
 5701:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5702:                               '<input type="checkbox" name="updateable_'.$type.
 5703:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5704:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5705:             }
 5706:             $datatable .= '</tr></table></td></tr>';
 5707:         }
 5708:     }
 5709:     return $datatable;
 5710: }
 5711: 
 5712: sub modify_login {
 5713:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5714:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5715:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5716:     %title = ( coursecatalog => 'Display course catalog',
 5717:                adminmail => 'Display administrator E-mail address',
 5718:                helpdesk  => 'Display "Contact Helpdesk" link',
 5719:                newuser => 'Link for visitors to create a user account',
 5720:                loginheader => 'Log-in box header');
 5721:     @offon = ('off','on');
 5722:     if (ref($domconfig{login}) eq 'HASH') {
 5723:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5724:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5725:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5726:             }
 5727:         }
 5728:     }
 5729:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5730:                                            \%domconfig,\%loginhash);
 5731:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5732:     foreach my $item (@toggles) {
 5733:         $loginhash{login}{$item} = $env{'form.'.$item};
 5734:     }
 5735:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5736:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5737:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5738:                                          \%loginhash);
 5739:     }
 5740: 
 5741:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5742:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5743:     if (keys(%servers) > 1) {
 5744:         foreach my $lonhost (keys(%servers)) {
 5745:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5746:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5747:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5748:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5749:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5750:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5751:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5752:                         $changes{'loginvia'}{$lonhost} = 1;
 5753:                     } else {
 5754:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5755:                         $changes{'loginvia'}{$lonhost} = 1;
 5756:                     }
 5757:                 } else {
 5758:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5759:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5760:                         $changes{'loginvia'}{$lonhost} = 1;
 5761:                     }
 5762:                 }
 5763:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5764:                     foreach my $item (@loginvia_attribs) {
 5765:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5766:                     }
 5767:                 } else {
 5768:                     foreach my $item (@loginvia_attribs) {
 5769:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5770:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5771:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5772:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5773:                                 $new = '/';
 5774:                             }
 5775:                         }
 5776:                         if (($item eq 'custompath') && 
 5777:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5778:                             $new = '';
 5779:                         }
 5780:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5781:                             $changes{'loginvia'}{$lonhost} = 1;
 5782:                         }
 5783:                         if ($item eq 'exempt') {
 5784:                             $new =~ s/^\s+//;
 5785:                             $new =~ s/\s+$//;
 5786:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5787:                             my @okips;
 5788:                             foreach my $ip (@poss_ips) {
 5789:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5790:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5791:                                         push(@okips,$ip); 
 5792:                                     }
 5793:                                 }
 5794:                             }
 5795:                             if (@okips > 0) {
 5796:                                 $new = join(',',@okips); 
 5797:                             } else {
 5798:                                 $new = ''; 
 5799:                             }
 5800:                         }
 5801:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5802:                     }
 5803:                 }
 5804:             } else {
 5805:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5806:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5807:                     $changes{'loginvia'}{$lonhost} = 1;
 5808:                     foreach my $item (@loginvia_attribs) {
 5809:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5810:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5811:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5812:                                 $new = '/';
 5813:                             }
 5814:                         }
 5815:                         if (($item eq 'custompath') && 
 5816:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5817:                             $new = '';
 5818:                         }
 5819:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5820:                     }
 5821:                 }
 5822:             }
 5823:         }
 5824:     }
 5825: 
 5826:     my $servadm = $r->dir_config('lonAdmEMail');
 5827:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5828:     if (ref($domconfig{'login'}) eq 'HASH') {
 5829:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5830:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5831:                 if ($lang eq 'nolang') {
 5832:                     push(@currlangs,$lang);
 5833:                 } elsif (defined($langchoices{$lang})) {
 5834:                     push(@currlangs,$lang);
 5835:                 } else {
 5836:                     next;
 5837:                 }
 5838:             }
 5839:         }
 5840:     }
 5841:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5842:     if (@currlangs > 0) {
 5843:         foreach my $lang (@currlangs) {
 5844:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5845:                 $changes{'helpurl'}{$lang} = 1;
 5846:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5847:                 $changes{'helpurl'}{$lang} = 1;
 5848:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5849:                 push(@newlangs,$lang);
 5850:             } else {
 5851:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5852:             }
 5853:         }
 5854:     }
 5855:     unless (grep(/^nolang$/,@currlangs)) {
 5856:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5857:             $changes{'helpurl'}{'nolang'} = 1;
 5858:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5859:             push(@newlangs,'nolang');
 5860:         }
 5861:     }
 5862:     if ($env{'form.loginhelpurl_add_lang'}) {
 5863:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5864:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5865:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5866:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5867:         }
 5868:     }
 5869:     if ((@newlangs > 0) || ($addedfile)) {
 5870:         my $error;
 5871:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5872:         if ($configuserok eq 'ok') {
 5873:             if ($switchserver) {
 5874:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5875:             } elsif ($author_ok eq 'ok') {
 5876:                 my @allnew = @newlangs;
 5877:                 if ($addedfile ne '') {
 5878:                     push(@allnew,$addedfile);
 5879:                 }
 5880:                 foreach my $lang (@allnew) {
 5881:                     my $formelem = 'loginhelpurl_'.$lang;
 5882:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5883:                         $formelem = 'loginhelpurl_add_file';
 5884:                     }
 5885:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5886:                                                                "help/$lang",'','',$newfile{$lang});
 5887:                     if ($result eq 'ok') {
 5888:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5889:                         $changes{'helpurl'}{$lang} = 1;
 5890:                     } else {
 5891:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5892:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5893:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5894:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5895: 
 5896:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5897:                         }
 5898:                     }
 5899:                 }
 5900:             } else {
 5901:                 $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);
 5902:             }
 5903:         } else {
 5904:             $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);
 5905:         }
 5906:         if ($error) {
 5907:             &Apache::lonnet::logthis($error);
 5908:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5909:         }
 5910:     }
 5911:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5912: 
 5913:     my $defaulthelpfile = '/adm/loginproblems.html';
 5914:     my $defaulttext = &mt('Default in use');
 5915: 
 5916:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5917:                                              $dom);
 5918:     if ($putresult eq 'ok') {
 5919:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5920:         my %defaultchecked = (
 5921:                     'coursecatalog' => 'on',
 5922:                     'helpdesk'      => 'on',
 5923:                     'adminmail'     => 'off',
 5924:                     'newuser'       => 'off',
 5925:         );
 5926:         if (ref($domconfig{'login'}) eq 'HASH') {
 5927:             foreach my $item (@toggles) {
 5928:                 if ($defaultchecked{$item} eq 'on') { 
 5929:                     if (($domconfig{'login'}{$item} eq '0') &&
 5930:                         ($env{'form.'.$item} eq '1')) {
 5931:                         $changes{$item} = 1;
 5932:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5933:                               $domconfig{'login'}{$item} eq '1') &&
 5934:                              ($env{'form.'.$item} eq '0')) {
 5935:                         $changes{$item} = 1;
 5936:                     }
 5937:                 } elsif ($defaultchecked{$item} eq 'off') {
 5938:                     if (($domconfig{'login'}{$item} eq '1') &&
 5939:                         ($env{'form.'.$item} eq '0')) {
 5940:                         $changes{$item} = 1;
 5941:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5942:                               $domconfig{'login'}{$item} eq '0') &&
 5943:                              ($env{'form.'.$item} eq '1')) {
 5944:                         $changes{$item} = 1;
 5945:                     }
 5946:                 }
 5947:             }
 5948:         }
 5949:         if (keys(%changes) > 0 || $colchgtext) {
 5950:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5951:             if (ref($lastactref) eq 'HASH') {
 5952:                 $lastactref->{'domainconfig'} = 1;
 5953:             }
 5954:             $resulttext = &mt('Changes made:').'<ul>';
 5955:             foreach my $item (sort(keys(%changes))) {
 5956:                 if ($item eq 'loginvia') {
 5957:                     if (ref($changes{$item}) eq 'HASH') {
 5958:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5959:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5960:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5961:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5962:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5963:                                     $protocol = 'http' if ($protocol ne 'https');
 5964:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5965: 
 5966:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5967:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5968:                                     } else {
 5969:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5970:                                     }
 5971:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5972:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5973:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5974:                                     }
 5975:                                     $resulttext .= '</li>';
 5976:                                 } else {
 5977:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5978:                                 }
 5979:                             } else {
 5980:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5981:                             }
 5982:                         }
 5983:                         $resulttext .= '</ul></li>';
 5984:                     }
 5985:                 } elsif ($item eq 'helpurl') {
 5986:                     if (ref($changes{$item}) eq 'HASH') {
 5987:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5988:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5989:                                 my ($chg,$link);
 5990:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5991:                                 if ($lang eq 'nolang') {
 5992:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5993:                                 } else {
 5994:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5995:                                 }
 5996:                                 $resulttext .= '<li>'.$chg.'</li>';
 5997:                             } else {
 5998:                                 my $chg;
 5999:                                 if ($lang eq 'nolang') {
 6000:                                     $chg = &mt('custom log-in help file for no preferred language');
 6001:                                 } else {
 6002:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 6003:                                 }
 6004:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 6005:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 6006:                                                       '?inhibitmenu=yes',$chg,600,500).
 6007:                                                '</li>';
 6008:                             }
 6009:                         }
 6010:                     }
 6011:                 } elsif ($item eq 'captcha') {
 6012:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6013:                         my $chgtxt;
 6014:                         if ($loginhash{'login'}{$item} eq 'notused') {
 6015:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 6016:                         } else {
 6017:                             my %captchas = &captcha_phrases();
 6018:                             if ($captchas{$loginhash{'login'}{$item}}) {
 6019:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 6020:                             } else {
 6021:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 6022:                             }
 6023:                         }
 6024:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6025:                     }
 6026:                 } elsif ($item eq 'recaptchakeys') {
 6027:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6028:                         my ($privkey,$pubkey);
 6029:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 6030:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 6031:                             $privkey = $loginhash{'login'}{$item}{'private'};
 6032:                         }
 6033:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 6034:                         if (!$pubkey) {
 6035:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 6036:                         } else {
 6037:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6038:                         }
 6039:                         if (!$privkey) {
 6040:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 6041:                         } else {
 6042:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6043:                         }
 6044:                         $chgtxt .= '</ul>';
 6045:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6046:                     }
 6047:                 } else {
 6048:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 6049:                 }
 6050:             }
 6051:             $resulttext .= $colchgtext.'</ul>';
 6052:         } else {
 6053:             $resulttext = &mt('No changes made to log-in page settings');
 6054:         }
 6055:     } else {
 6056:         $resulttext = '<span class="LC_error">'.
 6057: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6058:     }
 6059:     if ($errors) {
 6060:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 6061:                        $errors.'</ul>';
 6062:     }
 6063:     return $resulttext;
 6064: }
 6065: 
 6066: sub color_font_choices {
 6067:     my %choices =
 6068:         &Apache::lonlocal::texthash (
 6069:             img => "Header",
 6070:             bgs => "Background colors",
 6071:             links => "Link colors",
 6072:             images => "Images",
 6073:             font => "Font color",
 6074:             fontmenu => "Font menu",
 6075:             pgbg => "Page",
 6076:             tabbg => "Header",
 6077:             sidebg => "Border",
 6078:             link => "Link",
 6079:             alink => "Active link",
 6080:             vlink => "Visited link",
 6081:         );
 6082:     return %choices;
 6083: }
 6084: 
 6085: sub modify_rolecolors {
 6086:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 6087:     my ($resulttext,%rolehash);
 6088:     $rolehash{'rolecolors'} = {};
 6089:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 6090:         if ($domconfig{'rolecolors'} eq '') {
 6091:             $domconfig{'rolecolors'} = {};
 6092:         }
 6093:     }
 6094:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 6095:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 6096:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 6097:                                              $dom);
 6098:     if ($putresult eq 'ok') {
 6099:         if (keys(%changes) > 0) {
 6100:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6101:             if (ref($lastactref) eq 'HASH') {
 6102:                 $lastactref->{'domainconfig'} = 1;
 6103:             }
 6104:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 6105:                                              $rolehash{'rolecolors'});
 6106:         } else {
 6107:             $resulttext = &mt('No changes made to default color schemes');
 6108:         }
 6109:     } else {
 6110:         $resulttext = '<span class="LC_error">'.
 6111: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6112:     }
 6113:     if ($errors) {
 6114:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6115:                        $errors.'</ul>';
 6116:     }
 6117:     return $resulttext;
 6118: }
 6119: 
 6120: sub modify_colors {
 6121:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 6122:     my (%changes,%choices);
 6123:     my @bgs;
 6124:     my @links = ('link','alink','vlink');
 6125:     my @logintext;
 6126:     my @images;
 6127:     my $servadm = $r->dir_config('lonAdmEMail');
 6128:     my $errors;
 6129:     my %defaults;
 6130:     foreach my $role (@{$roles}) {
 6131:         if ($role eq 'login') {
 6132:             %choices = &login_choices();
 6133:             @logintext = ('textcol','bgcol');
 6134:         } else {
 6135:             %choices = &color_font_choices();
 6136:         }
 6137:         if ($role eq 'login') {
 6138:             @images = ('img','logo','domlogo','login');
 6139:             @bgs = ('pgbg','mainbg','sidebg');
 6140:         } else {
 6141:             @images = ('img');
 6142:             @bgs = ('pgbg','tabbg','sidebg');
 6143:         }
 6144:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 6145:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 6146:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 6147:         }
 6148:         if ($role eq 'login') {
 6149:             foreach my $item (@logintext) {
 6150:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6151:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6152:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6153:                 }
 6154:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 6155:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6156:                 }
 6157:             }
 6158:         } else {
 6159:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 6160:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 6161:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 6162:             }
 6163:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 6164:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 6165:             }
 6166:         }
 6167:         foreach my $item (@bgs) {
 6168:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6169:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6170:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6171:             }
 6172:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 6173:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6174:             }
 6175:         }
 6176:         foreach my $item (@links) {
 6177:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6178:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6179:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6180:             }
 6181:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 6182:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6183:             }
 6184:         }
 6185:         my ($configuserok,$author_ok,$switchserver) = 
 6186:             &config_check($dom,$confname,$servadm);
 6187:         my ($width,$height) = &thumb_dimensions();
 6188:         if (ref($domconfig->{$role}) ne 'HASH') {
 6189:             $domconfig->{$role} = {};
 6190:         }
 6191:         foreach my $img (@images) {
 6192:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 6193:                 if (defined($env{'form.login_showlogo_'.$img})) {
 6194:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 6195:                 } else { 
 6196:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 6197:                 }
 6198:             } 
 6199: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 6200: 		 && !defined($domconfig->{$role}{$img})
 6201: 		 && !$env{'form.'.$role.'_del_'.$img}
 6202: 		 && $env{'form.'.$role.'_import_'.$img}) {
 6203: 		# import the old configured image from the .tab setting
 6204: 		# if they haven't provided a new one 
 6205: 		$domconfig->{$role}{$img} = 
 6206: 		    $env{'form.'.$role.'_import_'.$img};
 6207: 	    }
 6208:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 6209:                 my $error;
 6210:                 if ($configuserok eq 'ok') {
 6211:                     if ($switchserver) {
 6212:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 6213:                     } else {
 6214:                         if ($author_ok eq 'ok') {
 6215:                             my ($result,$logourl) = 
 6216:                                 &publishlogo($r,'upload',$role.'_'.$img,
 6217:                                            $dom,$confname,$img,$width,$height);
 6218:                             if ($result eq 'ok') {
 6219:                                 $confhash->{$role}{$img} = $logourl;
 6220:                                 $changes{$role}{'images'}{$img} = 1;
 6221:                             } else {
 6222:                                 $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);
 6223:                             }
 6224:                         } else {
 6225:                             $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);
 6226:                         }
 6227:                     }
 6228:                 } else {
 6229:                     $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);
 6230:                 }
 6231:                 if ($error) {
 6232:                     &Apache::lonnet::logthis($error);
 6233:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6234:                 }
 6235:             } elsif ($domconfig->{$role}{$img} ne '') {
 6236:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 6237:                     my $error;
 6238:                     if ($configuserok eq 'ok') {
 6239: # is confname an author?
 6240:                         if ($switchserver eq '') {
 6241:                             if ($author_ok eq 'ok') {
 6242:                                 my ($result,$logourl) = 
 6243:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 6244:                                             $dom,$confname,$img,$width,$height);
 6245:                                 if ($result eq 'ok') {
 6246:                                     $confhash->{$role}{$img} = $logourl;
 6247: 				    $changes{$role}{'images'}{$img} = 1;
 6248:                                 }
 6249:                             }
 6250:                         }
 6251:                     }
 6252:                 }
 6253:             }
 6254:         }
 6255:         if (ref($domconfig) eq 'HASH') {
 6256:             if (ref($domconfig->{$role}) eq 'HASH') {
 6257:                 foreach my $img (@images) {
 6258:                     if ($domconfig->{$role}{$img} ne '') {
 6259:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6260:                             $confhash->{$role}{$img} = '';
 6261:                             $changes{$role}{'images'}{$img} = 1;
 6262:                         } else {
 6263:                             if ($confhash->{$role}{$img} eq '') {
 6264:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 6265:                             }
 6266:                         }
 6267:                     } else {
 6268:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6269:                             $confhash->{$role}{$img} = '';
 6270:                             $changes{$role}{'images'}{$img} = 1;
 6271:                         } 
 6272:                     }
 6273:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 6274:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 6275:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 6276:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 6277:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 6278:                             }
 6279:                         } else {
 6280:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6281:                                 $changes{$role}{'showlogo'}{$img} = 1;
 6282:                             }
 6283:                         }
 6284:                     }
 6285:                 }
 6286:                 if ($domconfig->{$role}{'font'} ne '') {
 6287:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 6288:                         $changes{$role}{'font'} = 1;
 6289:                     }
 6290:                 } else {
 6291:                     if ($confhash->{$role}{'font'}) {
 6292:                         $changes{$role}{'font'} = 1;
 6293:                     }
 6294:                 }
 6295:                 if ($role ne 'login') {
 6296:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 6297:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 6298:                             $changes{$role}{'fontmenu'} = 1;
 6299:                         }
 6300:                     } else {
 6301:                         if ($confhash->{$role}{'fontmenu'}) {
 6302:                             $changes{$role}{'fontmenu'} = 1;
 6303:                         }
 6304:                     }
 6305:                 }
 6306:                 foreach my $item (@bgs) {
 6307:                     if ($domconfig->{$role}{$item} ne '') {
 6308:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6309:                             $changes{$role}{'bgs'}{$item} = 1;
 6310:                         } 
 6311:                     } else {
 6312:                         if ($confhash->{$role}{$item}) {
 6313:                             $changes{$role}{'bgs'}{$item} = 1;
 6314:                         }
 6315:                     }
 6316:                 }
 6317:                 foreach my $item (@links) {
 6318:                     if ($domconfig->{$role}{$item} ne '') {
 6319:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6320:                             $changes{$role}{'links'}{$item} = 1;
 6321:                         }
 6322:                     } else {
 6323:                         if ($confhash->{$role}{$item}) {
 6324:                             $changes{$role}{'links'}{$item} = 1;
 6325:                         }
 6326:                     }
 6327:                 }
 6328:                 foreach my $item (@logintext) {
 6329:                     if ($domconfig->{$role}{$item} ne '') {
 6330:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6331:                             $changes{$role}{'logintext'}{$item} = 1;
 6332:                         }
 6333:                     } else {
 6334:                         if ($confhash->{$role}{$item}) {
 6335:                             $changes{$role}{'logintext'}{$item} = 1;
 6336:                         }
 6337:                     }
 6338:                 }
 6339:             } else {
 6340:                 &default_change_checker($role,\@images,\@links,\@bgs,
 6341:                                         \@logintext,$confhash,\%changes); 
 6342:             }
 6343:         } else {
 6344:             &default_change_checker($role,\@images,\@links,\@bgs,
 6345:                                     \@logintext,$confhash,\%changes); 
 6346:         }
 6347:     }
 6348:     return ($errors,%changes);
 6349: }
 6350: 
 6351: sub config_check {
 6352:     my ($dom,$confname,$servadm) = @_;
 6353:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6354:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6355:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6356:                                                    $confname,$servadm);
 6357:     if ($configuserok eq 'ok') {
 6358:         $switchserver = &check_switchserver($dom,$confname);
 6359:         if ($switchserver eq '') {
 6360:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6361:         }
 6362:     }
 6363:     return ($configuserok,$author_ok,$switchserver);
 6364: }
 6365: 
 6366: sub default_change_checker {
 6367:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6368:     foreach my $item (@{$links}) {
 6369:         if ($confhash->{$role}{$item}) {
 6370:             $changes->{$role}{'links'}{$item} = 1;
 6371:         }
 6372:     }
 6373:     foreach my $item (@{$bgs}) {
 6374:         if ($confhash->{$role}{$item}) {
 6375:             $changes->{$role}{'bgs'}{$item} = 1;
 6376:         }
 6377:     }
 6378:     foreach my $item (@{$logintext}) {
 6379:         if ($confhash->{$role}{$item}) {
 6380:             $changes->{$role}{'logintext'}{$item} = 1;
 6381:         }
 6382:     }
 6383:     foreach my $img (@{$images}) {
 6384:         if ($env{'form.'.$role.'_del_'.$img}) {
 6385:             $confhash->{$role}{$img} = '';
 6386:             $changes->{$role}{'images'}{$img} = 1;
 6387:         }
 6388:         if ($role eq 'login') {
 6389:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6390:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6391:             }
 6392:         }
 6393:     }
 6394:     if ($confhash->{$role}{'font'}) {
 6395:         $changes->{$role}{'font'} = 1;
 6396:     }
 6397: }
 6398: 
 6399: sub display_colorchgs {
 6400:     my ($dom,$changes,$roles,$confhash) = @_;
 6401:     my (%choices,$resulttext);
 6402:     if (!grep(/^login$/,@{$roles})) {
 6403:         $resulttext = &mt('Changes made:').'<br />';
 6404:     }
 6405:     foreach my $role (@{$roles}) {
 6406:         if ($role eq 'login') {
 6407:             %choices = &login_choices();
 6408:         } else {
 6409:             %choices = &color_font_choices();
 6410:         }
 6411:         if (ref($changes->{$role}) eq 'HASH') {
 6412:             if ($role ne 'login') {
 6413:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6414:             }
 6415:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6416:                 if ($role ne 'login') {
 6417:                     $resulttext .= '<ul>';
 6418:                 }
 6419:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6420:                     if ($role ne 'login') {
 6421:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6422:                     }
 6423:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6424:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6425:                             if ($confhash->{$role}{$key}{$item}) {
 6426:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6427:                             } else {
 6428:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6429:                             }
 6430:                         } elsif ($confhash->{$role}{$item} eq '') {
 6431:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6432:                         } else {
 6433:                             my $newitem = $confhash->{$role}{$item};
 6434:                             if ($key eq 'images') {
 6435:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6436:                             }
 6437:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6438:                         }
 6439:                     }
 6440:                     if ($role ne 'login') {
 6441:                         $resulttext .= '</ul></li>';
 6442:                     }
 6443:                 } else {
 6444:                     if ($confhash->{$role}{$key} eq '') {
 6445:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6446:                     } else {
 6447:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6448:                     }
 6449:                 }
 6450:                 if ($role ne 'login') {
 6451:                     $resulttext .= '</ul>';
 6452:                 }
 6453:             }
 6454:         }
 6455:     }
 6456:     return $resulttext;
 6457: }
 6458: 
 6459: sub thumb_dimensions {
 6460:     return ('200','50');
 6461: }
 6462: 
 6463: sub check_dimensions {
 6464:     my ($inputfile) = @_;
 6465:     my ($fullwidth,$fullheight);
 6466:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6467:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6468:             my $imageinfo = <PIPE>;
 6469:             if (!close(PIPE)) {
 6470:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6471:             }
 6472:             chomp($imageinfo);
 6473:             my ($fullsize) = 
 6474:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6475:             if ($fullsize) {
 6476:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6477:             }
 6478:         }
 6479:     }
 6480:     return ($fullwidth,$fullheight);
 6481: }
 6482: 
 6483: sub check_configuser {
 6484:     my ($uhome,$dom,$confname,$servadm) = @_;
 6485:     my ($configuserok,%currroles);
 6486:     if ($uhome eq 'no_host') {
 6487:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6488:         my $configpass = &LONCAPA::Enrollment::create_password();
 6489:         $configuserok = 
 6490:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6491:                              $configpass,'','','','','',undef,$servadm);
 6492:     } else {
 6493:         $configuserok = 'ok';
 6494:         %currroles = 
 6495:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6496:     }
 6497:     return ($configuserok,%currroles);
 6498: }
 6499: 
 6500: sub check_authorstatus {
 6501:     my ($dom,$confname,%currroles) = @_;
 6502:     my $author_ok;
 6503:     if (!$currroles{':'.$dom.':au'}) {
 6504:         my $start = time;
 6505:         my $end = 0;
 6506:         $author_ok = 
 6507:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6508:                                         'au',$end,$start,'','','domconfig');
 6509:     } else {
 6510:         $author_ok = 'ok';
 6511:     }
 6512:     return $author_ok;
 6513: }
 6514: 
 6515: sub publishlogo {
 6516:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6517:     my ($output,$fname,$logourl);
 6518:     if ($action eq 'upload') {
 6519:         $fname=$env{'form.'.$formname.'.filename'};
 6520:         chop($env{'form.'.$formname});
 6521:     } else {
 6522:         ($fname) = ($formname =~ /([^\/]+)$/);
 6523:     }
 6524:     if ($savefileas ne '') {
 6525:         $fname = $savefileas;
 6526:     }
 6527:     $fname=&Apache::lonnet::clean_filename($fname);
 6528: # See if there is anything left
 6529:     unless ($fname) { return ('error: no uploaded file'); }
 6530:     $fname="$subdir/$fname";
 6531:     my $docroot=$r->dir_config('lonDocRoot');
 6532:     my $filepath="$docroot/priv";
 6533:     my $relpath = "$dom/$confname";
 6534:     my ($fnamepath,$file,$fetchthumb);
 6535:     $file=$fname;
 6536:     if ($fname=~m|/|) {
 6537:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6538:     }
 6539:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6540:     my $count;
 6541:     for ($count=5;$count<=$#parts;$count++) {
 6542:         $filepath.="/$parts[$count]";
 6543:         if ((-e $filepath)!=1) {
 6544:             mkdir($filepath,02770);
 6545:         }
 6546:     }
 6547:     # Check for bad extension and disallow upload
 6548:     if ($file=~/\.(\w+)$/ &&
 6549:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6550:         $output = 
 6551:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6552:     } elsif ($file=~/\.(\w+)$/ &&
 6553:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6554:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6555:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6556:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6557:     } elsif (-d "$filepath/$file") {
 6558:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6559:     } else {
 6560:         my $source = $filepath.'/'.$file;
 6561:         my $logfile;
 6562:         if (!open($logfile,">>$source".'.log')) {
 6563:             return (&mt('No write permission to Authoring Space'));
 6564:         }
 6565:         print $logfile
 6566: "\n================= Publish ".localtime()." ================\n".
 6567: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6568: # Save the file
 6569:         if (!open(FH,'>'.$source)) {
 6570:             &Apache::lonnet::logthis('Failed to create '.$source);
 6571:             return (&mt('Failed to create file'));
 6572:         }
 6573:         if ($action eq 'upload') {
 6574:             if (!print FH ($env{'form.'.$formname})) {
 6575:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6576:                 return (&mt('Failed to write file'));
 6577:             }
 6578:         } else {
 6579:             my $original = &Apache::lonnet::filelocation('',$formname);
 6580:             if(!copy($original,$source)) {
 6581:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6582:                 return (&mt('Failed to write file'));
 6583:             }
 6584:         }
 6585:         close(FH);
 6586:         chmod(0660, $source); # Permissions to rw-rw---.
 6587: 
 6588:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6589:         my $copyfile=$targetdir.'/'.$file;
 6590: 
 6591:         my @parts=split(/\//,$targetdir);
 6592:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6593:         for (my $count=5;$count<=$#parts;$count++) {
 6594:             $path.="/$parts[$count]";
 6595:             if (!-e $path) {
 6596:                 print $logfile "\nCreating directory ".$path;
 6597:                 mkdir($path,02770);
 6598:             }
 6599:         }
 6600:         my $versionresult;
 6601:         if (-e $copyfile) {
 6602:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6603:         } else {
 6604:             $versionresult = 'ok';
 6605:         }
 6606:         if ($versionresult eq 'ok') {
 6607:             if (copy($source,$copyfile)) {
 6608:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6609:                 $output = 'ok';
 6610:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6611:                 push(@{$modified_urls},[$copyfile,$source]);
 6612:                 my $metaoutput = 
 6613:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6614:                 unless ($registered_cleanup) {
 6615:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6616:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6617:                     $registered_cleanup=1;
 6618:                 }
 6619:             } else {
 6620:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6621:                 $output = &mt('Failed to copy file to RES space').", $!";
 6622:             }
 6623:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6624:                 my $inputfile = $filepath.'/'.$file;
 6625:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6626:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6627:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6628:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6629:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6630:                         system("convert -sample $thumbsize $inputfile $outfile");
 6631:                         chmod(0660, $filepath.'/tn-'.$file);
 6632:                         if (-e $outfile) {
 6633:                             my $copyfile=$targetdir.'/tn-'.$file;
 6634:                             if (copy($outfile,$copyfile)) {
 6635:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6636:                                 my $thumb_metaoutput = 
 6637:                                     &write_metadata($dom,$confname,$formname,
 6638:                                                     $targetdir,'tn-'.$file,$logfile);
 6639:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6640:                                 unless ($registered_cleanup) {
 6641:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6642:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6643:                                     $registered_cleanup=1;
 6644:                                 }
 6645:                             } else {
 6646:                                 print $logfile "\nUnable to write ".$copyfile.
 6647:                                                ':'.$!."\n";
 6648:                             }
 6649:                         }
 6650:                     }
 6651:                 }
 6652:             }
 6653:         } else {
 6654:             $output = $versionresult;
 6655:         }
 6656:     }
 6657:     return ($output,$logourl);
 6658: }
 6659: 
 6660: sub logo_versioning {
 6661:     my ($targetdir,$file,$logfile) = @_;
 6662:     my $target = $targetdir.'/'.$file;
 6663:     my ($maxversion,$fn,$extn,$output);
 6664:     $maxversion = 0;
 6665:     if ($file =~ /^(.+)\.(\w+)$/) {
 6666:         $fn=$1;
 6667:         $extn=$2;
 6668:     }
 6669:     opendir(DIR,$targetdir);
 6670:     while (my $filename=readdir(DIR)) {
 6671:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6672:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6673:         }
 6674:     }
 6675:     $maxversion++;
 6676:     print $logfile "\nCreating old version ".$maxversion."\n";
 6677:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6678:     if (copy($target,$copyfile)) {
 6679:         print $logfile "Copied old target to ".$copyfile."\n";
 6680:         $copyfile=$copyfile.'.meta';
 6681:         if (copy($target.'.meta',$copyfile)) {
 6682:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6683:             $output = 'ok';
 6684:         } else {
 6685:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6686:             $output = &mt('Failed to copy old meta').", $!, ";
 6687:         }
 6688:     } else {
 6689:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6690:         $output = &mt('Failed to copy old target').", $!, ";
 6691:     }
 6692:     return $output;
 6693: }
 6694: 
 6695: sub write_metadata {
 6696:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6697:     my (%metadatafields,%metadatakeys,$output);
 6698:     $metadatafields{'title'}=$formname;
 6699:     $metadatafields{'creationdate'}=time;
 6700:     $metadatafields{'lastrevisiondate'}=time;
 6701:     $metadatafields{'copyright'}='public';
 6702:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6703:                                          $env{'user.domain'};
 6704:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6705:     $metadatafields{'domain'}=$dom;
 6706:     {
 6707:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6708:         my $mfh;
 6709:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6710:             foreach (sort(keys(%metadatafields))) {
 6711:                 unless ($_=~/\./) {
 6712:                     my $unikey=$_;
 6713:                     $unikey=~/^([A-Za-z]+)/;
 6714:                     my $tag=$1;
 6715:                     $tag=~tr/A-Z/a-z/;
 6716:                     print $mfh "\n\<$tag";
 6717:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6718:                         my $value=$metadatafields{$unikey.'.'.$_};
 6719:                         $value=~s/\"/\'\'/g;
 6720:                         print $mfh ' '.$_.'="'.$value.'"';
 6721:                     }
 6722:                     print $mfh '>'.
 6723:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6724:                             .'</'.$tag.'>';
 6725:                 }
 6726:             }
 6727:             $output = 'ok';
 6728:             print $logfile "\nWrote metadata";
 6729:             close($mfh);
 6730:         } else {
 6731:             print $logfile "\nFailed to open metadata file";
 6732:             $output = &mt('Could not write metadata');
 6733:         }
 6734:     }
 6735:     return $output;
 6736: }
 6737: 
 6738: sub notifysubscribed {
 6739:     foreach my $targetsource (@{$modified_urls}){
 6740:         next unless (ref($targetsource) eq 'ARRAY');
 6741:         my ($target,$source)=@{$targetsource};
 6742:         if ($source ne '') {
 6743:             if (open(my $logfh,'>>'.$source.'.log')) {
 6744:                 print $logfh "\nCleanup phase: Notifications\n";
 6745:                 my @subscribed=&subscribed_hosts($target);
 6746:                 foreach my $subhost (@subscribed) {
 6747:                     print $logfh "\nNotifying host ".$subhost.':';
 6748:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6749:                     print $logfh $reply;
 6750:                 }
 6751:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6752:                 foreach my $subhost (@subscribedmeta) {
 6753:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6754:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6755:                                                         $subhost);
 6756:                     print $logfh $reply;
 6757:                 }
 6758:                 print $logfh "\n============ Done ============\n";
 6759:                 close($logfh);
 6760:             }
 6761:         }
 6762:     }
 6763:     return OK;
 6764: }
 6765: 
 6766: sub subscribed_hosts {
 6767:     my ($target) = @_;
 6768:     my @subscribed;
 6769:     if (open(my $fh,"<$target.subscription")) {
 6770:         while (my $subline=<$fh>) {
 6771:             if ($subline =~ /^($match_lonid):/) {
 6772:                 my $host = $1;
 6773:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6774:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6775:                         push(@subscribed,$host);
 6776:                     }
 6777:                 }
 6778:             }
 6779:         }
 6780:     }
 6781:     return @subscribed;
 6782: }
 6783: 
 6784: sub check_switchserver {
 6785:     my ($dom,$confname) = @_;
 6786:     my ($allowed,$switchserver);
 6787:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6788:     if ($home eq 'no_host') {
 6789:         $home = &Apache::lonnet::domain($dom,'primary');
 6790:     }
 6791:     my @ids=&Apache::lonnet::current_machine_ids();
 6792:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6793:     if (!$allowed) {
 6794: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6795:     }
 6796:     return $switchserver;
 6797: }
 6798: 
 6799: sub modify_quotas {
 6800:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6801:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6802:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6803:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 6804:         $validationfieldsref);
 6805:     if ($action eq 'quotas') {
 6806:         $context = 'tools'; 
 6807:     } else {
 6808:         $context = $action;
 6809:     }
 6810:     if ($context eq 'requestcourses') {
 6811:         @usertools = ('official','unofficial','community','textbook');
 6812:         @options =('norequest','approval','validate','autolimit');
 6813:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6814:         %titles = &courserequest_titles();
 6815:         $toolregexp = join('|',@usertools);
 6816:         %conditions = &courserequest_conditions();
 6817:         $confname = $dom.'-domainconfig';
 6818:         my $servadm = $r->dir_config('lonAdmEMail');
 6819:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6820:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 6821:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6822:     } elsif ($context eq 'requestauthor') {
 6823:         @usertools = ('author');
 6824:         %titles = &authorrequest_titles();
 6825:     } else {
 6826:         @usertools = ('aboutme','blog','webdav','portfolio');
 6827:         %titles = &tool_titles();
 6828:     }
 6829:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6830:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6831:     foreach my $key (keys(%env)) {
 6832:         if ($context eq 'requestcourses') {
 6833:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6834:                 my $item = $1;
 6835:                 my $type = $2;
 6836:                 if ($type =~ /^limit_(.+)/) {
 6837:                     $limithash{$item}{$1} = $env{$key};
 6838:                 } else {
 6839:                     $confhash{$item}{$type} = $env{$key};
 6840:                 }
 6841:             }
 6842:         } elsif ($context eq 'requestauthor') {
 6843:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6844:                 $confhash{$1} = $env{$key};
 6845:             }
 6846:         } else {
 6847:             if ($key =~ /^form\.quota_(.+)$/) {
 6848:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6849:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6850:                 $confhash{'authorquota'}{$1} = $env{$key};
 6851:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6852:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6853:             }
 6854:         }
 6855:     }
 6856:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6857:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6858:         @approvalnotify = sort(@approvalnotify);
 6859:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6860:         my @crstypes = ('official','unofficial','community','textbook');
 6861:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6862:         foreach my $type (@hasuniquecode) {
 6863:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6864:                 $confhash{'uniquecode'}{$type} = 1;
 6865:             }
 6866:         }
 6867:         my (%newbook,%allpos);
 6868:         if ($context eq 'requestcourses') {
 6869:             foreach my $type ('textbooks','templates') {
 6870:                 @{$allpos{$type}} = (); 
 6871:                 my $invalid;
 6872:                 if ($type eq 'textbooks') {
 6873:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 6874:                 } else {
 6875:                     $invalid = &mt('Invalid LON-CAPA course for template');
 6876:                 }
 6877:                 if ($env{'form.'.$type.'_addbook'}) {
 6878:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 6879:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 6880:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 6881:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 6882:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 6883:                         } else {
 6884:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 6885:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 6886:                             $position =~ s/\D+//g;
 6887:                             if ($position ne '') {
 6888:                                 $allpos{$type}[$position] = $newbook{$type};
 6889:                             }
 6890:                         }
 6891:                     } else {
 6892:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 6893:                     }
 6894:                 }
 6895:             } 
 6896:         }
 6897:         if (ref($domconfig{$action}) eq 'HASH') {
 6898:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6899:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6900:                     $changes{'notify'}{'approval'} = 1;
 6901:                 }
 6902:             } else {
 6903:                 if ($confhash{'notify'}{'approval'}) {
 6904:                     $changes{'notify'}{'approval'} = 1;
 6905:                 }
 6906:             }
 6907:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6908:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6909:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6910:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6911:                             $changes{'uniquecode'} = 1;
 6912:                         }
 6913:                     }
 6914:                     unless ($changes{'uniquecode'}) {
 6915:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6916:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6917:                                 $changes{'uniquecode'} = 1;
 6918:                             }
 6919:                         }
 6920:                     }
 6921:                } else {
 6922:                    $changes{'uniquecode'} = 1;
 6923:                }
 6924:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6925:                 $changes{'uniquecode'} = 1;
 6926:             }
 6927:             if ($context eq 'requestcourses') {
 6928:                 foreach my $type ('textbooks','templates') {
 6929:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 6930:                         my %deletions;
 6931:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 6932:                         if (@todelete) {
 6933:                             map { $deletions{$_} = 1; } @todelete;
 6934:                         }
 6935:                         my %imgdeletions;
 6936:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 6937:                         if (@todeleteimages) {
 6938:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 6939:                         }
 6940:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 6941:                         for (my $i=0; $i<=$maxnum; $i++) {
 6942:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 6943:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 6944:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 6945:                                 if ($deletions{$key}) {
 6946:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 6947:                                         #FIXME need to obsolete item in RES space
 6948:                                     }
 6949:                                     next;
 6950:                                 } else {
 6951:                                     my $newpos = $env{'form.'.$itemid};
 6952:                                     $newpos =~ s/\D+//g;
 6953:                                     foreach my $item ('subject','title','author') {
 6954:                                         next if (($item eq 'author') && ($type eq 'templates'));
 6955:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 6956:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 6957:                                             $changes{$type}{$key} = 1;
 6958:                                         }
 6959:                                     }
 6960:                                     $allpos{$type}[$newpos] = $key;
 6961:                                 }
 6962:                                 if ($imgdeletions{$key}) {
 6963:                                     $changes{$type}{$key} = 1;
 6964:                                     #FIXME need to obsolete item in RES space
 6965:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 6966:                                     my ($cdom,$cnum) = split(/_/,$key);
 6967:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 6968:                                                                                   $cdom,$cnum,$type,$configuserok,
 6969:                                                                                   $switchserver,$author_ok);
 6970:                                     if ($imgurl) {
 6971:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 6972:                                         $changes{$type}{$key} = 1; 
 6973:                                     }
 6974:                                     if ($error) {
 6975:                                         &Apache::lonnet::logthis($error);
 6976:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6977:                                     } 
 6978:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 6979:                                     $confhash{$type}{$key}{'image'} = 
 6980:                                         $domconfig{$action}{$type}{$key}{'image'};
 6981:                                 }
 6982:                             }
 6983:                         }
 6984:                     }
 6985:                 }
 6986:             }
 6987:         } else {
 6988:             if ($confhash{'notify'}{'approval'}) {
 6989:                 $changes{'notify'}{'approval'} = 1;
 6990:             }
 6991:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 6992:                 $changes{'uniquecode'} = 1;
 6993:             }
 6994:         }
 6995:         if ($context eq 'requestcourses') {
 6996:             foreach my $type ('textbooks','templates') {
 6997:                 if ($newbook{$type}) {
 6998:                     $changes{$type}{$newbook{$type}} = 1;
 6999:                     foreach my $item ('subject','title','author') {
 7000:                         next if (($item eq 'author') && ($type eq 'template'));
 7001:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 7002:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 7003:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 7004:                         }
 7005:                     }
 7006:                     if ($type eq 'textbooks') {
 7007:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 7008:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 7009:                             my ($imageurl,$error) =
 7010:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 7011:                                                         $configuserok,$switchserver,$author_ok);
 7012:                             if ($imageurl) {
 7013:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 7014:                             }
 7015:                             if ($error) {
 7016:                                 &Apache::lonnet::logthis($error);
 7017:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7018:                             }
 7019:                         }
 7020:                     }
 7021:                 }
 7022:                 if (@{$allpos{$type}} > 0) {
 7023:                     my $idx = 0;
 7024:                     foreach my $item (@{$allpos{$type}}) {
 7025:                         if ($item ne '') {
 7026:                             $confhash{$type}{$item}{'order'} = $idx;
 7027:                             if (ref($domconfig{$action}) eq 'HASH') {
 7028:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7029:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 7030:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 7031:                                             $changes{$type}{$item} = 1;
 7032:                                         }
 7033:                                     }
 7034:                                 }
 7035:                             }
 7036:                             $idx ++;
 7037:                         }
 7038:                     }
 7039:                 }
 7040:             }
 7041:             if (ref($validationitemsref) eq 'ARRAY') {
 7042:                 foreach my $item (@{$validationitemsref}) {
 7043:                     if ($item eq 'fields') {
 7044:                         my @changed;
 7045:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 7046:                         if (@{$confhash{'validation'}{$item}} > 0) {
 7047:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 7048:                         }
 7049:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7050:                             if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 7051:                                 @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 7052:                                                                               $domconfig{'requestcourses'}{'validation'}{$item});
 7053:                             } else {
 7054:                                 @changed = @{$confhash{'validation'}{$item}};
 7055:                             }
 7056:                         } else {
 7057:                             @changed = @{$confhash{'validation'}{$item}};
 7058:                         }
 7059:                         if (@changed) {
 7060:                             if ($confhash{'validation'}{$item}) {
 7061:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 7062:                             } else {
 7063:                                 $changes{'validation'}{$item} = &mt('None');
 7064:                             }
 7065:                         }
 7066:                     } else {
 7067:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 7068:                         if ($item eq 'markup') {
 7069:                             if ($env{'form.requestcourses_validation_'.$item}) {
 7070:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 7071:                             }
 7072:                         }
 7073:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7074:                             if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 7075:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7076:                             }
 7077:                         } else {
 7078:                             if ($confhash{'validation'}{$item} ne '') {
 7079:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7080:                             }
 7081:                         }
 7082:                     }
 7083:                 }
 7084:             }
 7085:             if ($env{'form.validationdc'}) {
 7086:                 my $newval = $env{'form.validationdc'};
 7087:                 my %domcoords = &get_active_dcs($dom);
 7088:                 if (exists($domcoords{$newval})) {
 7089:                     $confhash{'validation'}{'dc'} = $newval;
 7090:                 }
 7091:             }
 7092:             if (ref($confhash{'validation'}) eq 'HASH') {
 7093:                 if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7094:                     if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7095:                         unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7096:                             if ($confhash{'validation'}{'dc'} eq '') {
 7097:                                 $changes{'validation'}{'dc'} = &mt('None');
 7098:                             } else {
 7099:                                 $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7100:                             }
 7101:                         }
 7102:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 7103:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7104:                     }
 7105:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 7106:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7107:                 }
 7108:             } elsif (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7109:                 if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7110:                     $changes{'validation'}{'dc'} = &mt('None');
 7111:                 }
 7112:             }
 7113:         }
 7114:     } else {
 7115:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 7116:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 7117:     }
 7118:     foreach my $item (@usertools) {
 7119:         foreach my $type (@{$types},'default','_LC_adv') {
 7120:             my $unset; 
 7121:             if ($context eq 'requestcourses') {
 7122:                 $unset = '0';
 7123:                 if ($type eq '_LC_adv') {
 7124:                     $unset = '';
 7125:                 }
 7126:                 if ($confhash{$item}{$type} eq 'autolimit') {
 7127:                     $confhash{$item}{$type} .= '=';
 7128:                     unless ($limithash{$item}{$type} =~ /\D/) {
 7129:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 7130:                     }
 7131:                 }
 7132:             } elsif ($context eq 'requestauthor') {
 7133:                 $unset = '0';
 7134:                 if ($type eq '_LC_adv') {
 7135:                     $unset = '';
 7136:                 }
 7137:             } else {
 7138:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 7139:                     $confhash{$item}{$type} = 1;
 7140:                 } else {
 7141:                     $confhash{$item}{$type} = 0;
 7142:                 }
 7143:             }
 7144:             if (ref($domconfig{$action}) eq 'HASH') {
 7145:                 if ($action eq 'requestauthor') {
 7146:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 7147:                         $changes{$type} = 1;
 7148:                     }
 7149:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 7150:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 7151:                         $changes{$item}{$type} = 1;
 7152:                     }
 7153:                 } else {
 7154:                     if ($context eq 'requestcourses') {
 7155:                         if ($confhash{$item}{$type} ne $unset) {
 7156:                             $changes{$item}{$type} = 1;
 7157:                         }
 7158:                     } else {
 7159:                         if (!$confhash{$item}{$type}) {
 7160:                             $changes{$item}{$type} = 1;
 7161:                         }
 7162:                     }
 7163:                 }
 7164:             } else {
 7165:                 if ($context eq 'requestcourses') {
 7166:                     if ($confhash{$item}{$type} ne $unset) {
 7167:                         $changes{$item}{$type} = 1;
 7168:                     }
 7169:                 } elsif ($context eq 'requestauthor') {
 7170:                     if ($confhash{$type} ne $unset) {
 7171:                         $changes{$type} = 1;
 7172:                     }
 7173:                 } else {
 7174:                     if (!$confhash{$item}{$type}) {
 7175:                         $changes{$item}{$type} = 1;
 7176:                     }
 7177:                 }
 7178:             }
 7179:         }
 7180:     }
 7181:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7182:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 7183:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7184:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 7185:                     if (exists($confhash{'defaultquota'}{$key})) {
 7186:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 7187:                             $changes{'defaultquota'}{$key} = 1;
 7188:                         }
 7189:                     } else {
 7190:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 7191:                     }
 7192:                 }
 7193:             } else {
 7194:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 7195:                     if (exists($confhash{'defaultquota'}{$key})) {
 7196:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 7197:                             $changes{'defaultquota'}{$key} = 1;
 7198:                         }
 7199:                     } else {
 7200:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 7201:                     }
 7202:                 }
 7203:             }
 7204:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7205:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 7206:                     if (exists($confhash{'authorquota'}{$key})) {
 7207:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 7208:                             $changes{'authorquota'}{$key} = 1;
 7209:                         }
 7210:                     } else {
 7211:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 7212:                     }
 7213:                 }
 7214:             }
 7215:         }
 7216:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 7217:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 7218:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7219:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7220:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 7221:                             $changes{'defaultquota'}{$key} = 1;
 7222:                         }
 7223:                     } else {
 7224:                         if (!exists($domconfig{'quotas'}{$key})) {
 7225:                             $changes{'defaultquota'}{$key} = 1;
 7226:                         }
 7227:                     }
 7228:                 } else {
 7229:                     $changes{'defaultquota'}{$key} = 1;
 7230:                 }
 7231:             }
 7232:         }
 7233:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 7234:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 7235:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7236:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7237:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 7238:                             $changes{'authorquota'}{$key} = 1;
 7239:                         }
 7240:                     } else {
 7241:                         $changes{'authorquota'}{$key} = 1;
 7242:                     }
 7243:                 } else {
 7244:                     $changes{'authorquota'}{$key} = 1;
 7245:                 }
 7246:             }
 7247:         }
 7248:     }
 7249: 
 7250:     if ($context eq 'requestauthor') {
 7251:         $domdefaults{'requestauthor'} = \%confhash;
 7252:     } else {
 7253:         foreach my $key (keys(%confhash)) {
 7254:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 7255:                 $domdefaults{$key} = $confhash{$key};
 7256:             }
 7257:         }
 7258:     }
 7259: 
 7260:     my %quotahash = (
 7261:                       $action => { %confhash }
 7262:                     );
 7263:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 7264:                                              $dom);
 7265:     if ($putresult eq 'ok') {
 7266:         if (keys(%changes) > 0) {
 7267:             my $cachetime = 24*60*60;
 7268:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7269:             if (ref($lastactref) eq 'HASH') {
 7270:                 $lastactref->{'domdefaults'} = 1;
 7271:             }
 7272:             $resulttext = &mt('Changes made:').'<ul>';
 7273:             unless (($context eq 'requestcourses') ||
 7274:                     ($context eq 'requestauthor')) {
 7275:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 7276:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 7277:                     foreach my $type (@{$types},'default') {
 7278:                         if (defined($changes{'defaultquota'}{$type})) {
 7279:                             my $typetitle = $usertypes->{$type};
 7280:                             if ($type eq 'default') {
 7281:                                 $typetitle = $othertitle;
 7282:                             }
 7283:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 7284:                         }
 7285:                     }
 7286:                     $resulttext .= '</ul></li>';
 7287:                 }
 7288:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 7289:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 7290:                     foreach my $type (@{$types},'default') {
 7291:                         if (defined($changes{'authorquota'}{$type})) {
 7292:                             my $typetitle = $usertypes->{$type};
 7293:                             if ($type eq 'default') {
 7294:                                 $typetitle = $othertitle;
 7295:                             }
 7296:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 7297:                         }
 7298:                     }
 7299:                     $resulttext .= '</ul></li>';
 7300:                 }
 7301:             }
 7302:             my %newenv;
 7303:             foreach my $item (@usertools) {
 7304:                 my (%haschgs,%inconf);
 7305:                 if ($context eq 'requestauthor') {
 7306:                     %haschgs = %changes;
 7307:                     %inconf = %confhash;
 7308:                 } else {
 7309:                     if (ref($changes{$item}) eq 'HASH') {
 7310:                         %haschgs = %{$changes{$item}};
 7311:                     }
 7312:                     if (ref($confhash{$item}) eq 'HASH') {
 7313:                         %inconf = %{$confhash{$item}};
 7314:                     }
 7315:                 }
 7316:                 if (keys(%haschgs) > 0) {
 7317:                     my $newacc = 
 7318:                         &Apache::lonnet::usertools_access($env{'user.name'},
 7319:                                                           $env{'user.domain'},
 7320:                                                           $item,'reload',$context);
 7321:                     if (($context eq 'requestcourses') ||
 7322:                         ($context eq 'requestauthor')) {
 7323:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 7324:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 7325:                         }
 7326:                     } else {
 7327:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 7328:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 7329:                         }
 7330:                     }
 7331:                     unless ($context eq 'requestauthor') {
 7332:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 7333:                     }
 7334:                     foreach my $type (@{$types},'default','_LC_adv') {
 7335:                         if ($haschgs{$type}) {
 7336:                             my $typetitle = $usertypes->{$type};
 7337:                             if ($type eq 'default') {
 7338:                                 $typetitle = $othertitle;
 7339:                             } elsif ($type eq '_LC_adv') {
 7340:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 7341:                             }
 7342:                             if ($inconf{$type}) {
 7343:                                 if ($context eq 'requestcourses') {
 7344:                                     my $cond;
 7345:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 7346:                                         if ($1 eq '') {
 7347:                                             $cond = &mt('(Automatic processing of any request).');
 7348:                                         } else {
 7349:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 7350:                                         }
 7351:                                     } else { 
 7352:                                         $cond = $conditions{$inconf{$type}};
 7353:                                     }
 7354:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 7355:                                 } elsif ($context eq 'requestauthor') {
 7356:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 7357:                                                              $titles{$inconf{$type}},$typetitle);
 7358: 
 7359:                                 } else {
 7360:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7361:                                 }
 7362:                             } else {
 7363:                                 if ($type eq '_LC_adv') {
 7364:                                     if ($inconf{$type} eq '0') {
 7365:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7366:                                     } else { 
 7367:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7368:                                     }
 7369:                                 } else {
 7370:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7371:                                 }
 7372:                             }
 7373:                         }
 7374:                     }
 7375:                     unless ($context eq 'requestauthor') {
 7376:                         $resulttext .= '</ul></li>';
 7377:                     }
 7378:                 }
 7379:             }
 7380:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7381:                 if (ref($changes{'notify'}) eq 'HASH') {
 7382:                     if ($changes{'notify'}{'approval'}) {
 7383:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7384:                             if ($confhash{'notify'}{'approval'}) {
 7385:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7386:                             } else {
 7387:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7388:                             }
 7389:                         }
 7390:                     }
 7391:                 }
 7392:             }
 7393:             if ($action eq 'requestcourses') {
 7394:                 my @offon = ('off','on');
 7395:                 if ($changes{'uniquecode'}) {
 7396:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7397:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7398:                         $resulttext .= '<li>'.
 7399:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7400:                                        '</li>';
 7401:                     } else {
 7402:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7403:                                        '</li>';
 7404:                     }
 7405:                 }
 7406:                 foreach my $type ('textbooks','templates') {
 7407:                     if (ref($changes{$type}) eq 'HASH') {
 7408:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 7409:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 7410:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 7411:                             my $coursetitle = $coursehash{'description'};
 7412:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 7413:                             $resulttext .= '<li>';
 7414:                             foreach my $item ('subject','title','author') {
 7415:                                 next if (($item eq 'author') && ($type eq 'templates'));
 7416:                                 my $name = $item.':';
 7417:                                 $name =~ s/^(\w)/\U$1/;
 7418:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 7419:                             }
 7420:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7421:                             if ($type eq 'textbooks') {
 7422:                                 if ($confhash{$type}{$key}{'image'}) {
 7423:                                     $resulttext .= ' '.&mt('Image: [_1]',
 7424:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 7425:                                                    ' alt="Textbook cover" />').'<br />';
 7426:                                 }
 7427:                             }
 7428:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7429:                         }
 7430:                         $resulttext .= '</ul></li>';
 7431:                     }
 7432:                 }
 7433:                 if (ref($changes{'validation'}) eq 'HASH') {
 7434:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7435:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7436:                         foreach my $item (@{$validationitemsref}) {
 7437:                             if (exists($changes{'validation'}{$item})) {
 7438:                                 if ($item eq 'markup') {
 7439:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7440:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7441:                                 } else {
 7442:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7443:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7444:                                 }
 7445:                             }
 7446:                         }
 7447:                         if (exists($changes{'validation'}{'dc'})) {
 7448:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7449:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7450:                         }
 7451:                     }
 7452:                 }
 7453:             }
 7454:             $resulttext .= '</ul>';
 7455:             if (keys(%newenv)) {
 7456:                 &Apache::lonnet::appenv(\%newenv);
 7457:             }
 7458:         } else {
 7459:             if ($context eq 'requestcourses') {
 7460:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7461:             } elsif ($context eq 'requestauthor') {
 7462:                 $resulttext = &mt('No changes made to rights to request author space.');
 7463:             } else {
 7464:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7465:             }
 7466:         }
 7467:     } else {
 7468:         $resulttext = '<span class="LC_error">'.
 7469: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7470:     }
 7471:     if ($errors) {
 7472:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7473:                        '<ul>'.$errors.'</ul></p>';
 7474:     }
 7475:     return $resulttext;
 7476: }
 7477: 
 7478: sub process_textbook_image {
 7479:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 7480:     my $filename = $env{'form.'.$caller.'.filename'};
 7481:     my ($error,$url);
 7482:     my ($width,$height) = (50,50);
 7483:     if ($configuserok eq 'ok') {
 7484:         if ($switchserver) {
 7485:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7486:                          $switchserver);
 7487:         } elsif ($author_ok eq 'ok') {
 7488:             my ($result,$imageurl) =
 7489:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7490:                              "$type/$dom/$cnum/cover",$width,$height);
 7491:             if ($result eq 'ok') {
 7492:                 $url = $imageurl;
 7493:             } else {
 7494:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7495:             }
 7496:         } else {
 7497:             $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);
 7498:         }
 7499:     } else {
 7500:         $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);
 7501:     }
 7502:     return ($url,$error);
 7503: }
 7504: 
 7505: sub modify_autoenroll {
 7506:     my ($dom,$lastactref,%domconfig) = @_;
 7507:     my ($resulttext,%changes);
 7508:     my %currautoenroll;
 7509:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7510:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7511:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7512:         }
 7513:     }
 7514:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7515:     my %title = ( run => 'Auto-enrollment active',
 7516:                   sender => 'Sender for notification messages',
 7517:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7518:     my @offon = ('off','on');
 7519:     my $sender_uname = $env{'form.sender_uname'};
 7520:     my $sender_domain = $env{'form.sender_domain'};
 7521:     if ($sender_domain eq '') {
 7522:         $sender_uname = '';
 7523:     } elsif ($sender_uname eq '') {
 7524:         $sender_domain = '';
 7525:     }
 7526:     my $coowners = $env{'form.autoassign_coowners'};
 7527:     my %autoenrollhash =  (
 7528:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7529:                                        'sender_uname' => $sender_uname,
 7530:                                        'sender_domain' => $sender_domain,
 7531:                                        'co-owners' => $coowners,
 7532:                                 }
 7533:                      );
 7534:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7535:                                              $dom);
 7536:     if ($putresult eq 'ok') {
 7537:         if (exists($currautoenroll{'run'})) {
 7538:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7539:                  $changes{'run'} = 1;
 7540:              }
 7541:         } elsif ($autorun) {
 7542:             if ($env{'form.autoenroll_run'} ne '1') {
 7543:                  $changes{'run'} = 1;
 7544:             }
 7545:         }
 7546:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7547:             $changes{'sender'} = 1;
 7548:         }
 7549:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7550:             $changes{'sender'} = 1;
 7551:         }
 7552:         if ($currautoenroll{'co-owners'} ne '') {
 7553:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7554:                 $changes{'coowners'} = 1;
 7555:             }
 7556:         } elsif ($coowners) {
 7557:             $changes{'coowners'} = 1;
 7558:         }      
 7559:         if (keys(%changes) > 0) {
 7560:             $resulttext = &mt('Changes made:').'<ul>';
 7561:             if ($changes{'run'}) {
 7562:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7563:             }
 7564:             if ($changes{'sender'}) {
 7565:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7566:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7567:                 } else {
 7568:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7569:                 }
 7570:             }
 7571:             if ($changes{'coowners'}) {
 7572:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7573:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7574:                 if (ref($lastactref) eq 'HASH') {
 7575:                     $lastactref->{'domainconfig'} = 1;
 7576:                 }
 7577:             }
 7578:             $resulttext .= '</ul>';
 7579:         } else {
 7580:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7581:         }
 7582:     } else {
 7583:         $resulttext = '<span class="LC_error">'.
 7584: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7585:     }
 7586:     return $resulttext;
 7587: }
 7588: 
 7589: sub modify_autoupdate {
 7590:     my ($dom,%domconfig) = @_;
 7591:     my ($resulttext,%currautoupdate,%fields,%changes);
 7592:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7593:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7594:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7595:         }
 7596:     }
 7597:     my @offon = ('off','on');
 7598:     my %title = &Apache::lonlocal::texthash (
 7599:                    run => 'Auto-update:',
 7600:                    classlists => 'Updates to user information in classlists?'
 7601:                 );
 7602:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7603:     my %fieldtitles = &Apache::lonlocal::texthash (
 7604:                         id => 'Student/Employee ID',
 7605:                         permanentemail => 'E-mail address',
 7606:                         lastname => 'Last Name',
 7607:                         firstname => 'First Name',
 7608:                         middlename => 'Middle Name',
 7609:                         generation => 'Generation',
 7610:                       );
 7611:     $othertitle = &mt('All users');
 7612:     if (keys(%{$usertypes}) >  0) {
 7613:         $othertitle = &mt('Other users');
 7614:     }
 7615:     foreach my $key (keys(%env)) {
 7616:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7617:             my ($usertype,$item) = ($1,$2);
 7618:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7619:                 if ($usertype eq 'default') {   
 7620:                     push(@{$fields{$1}},$2);
 7621:                 } elsif (ref($types) eq 'ARRAY') {
 7622:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7623:                         push(@{$fields{$1}},$2);
 7624:                     }
 7625:                 }
 7626:             }
 7627:         }
 7628:     }
 7629:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 7630:     @lockablenames = sort(@lockablenames);
 7631:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 7632:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7633:         if (@changed) {
 7634:             $changes{'lockablenames'} = 1;
 7635:         }
 7636:     } else {
 7637:         if (@lockablenames) {
 7638:             $changes{'lockablenames'} = 1;
 7639:         }
 7640:     }
 7641:     my %updatehash = (
 7642:                       autoupdate => { run => $env{'form.autoupdate_run'},
 7643:                                       classlists => $env{'form.classlists'},
 7644:                                       fields => {%fields},
 7645:                                       lockablenames => \@lockablenames,
 7646:                                     }
 7647:                      );
 7648:     foreach my $key (keys(%currautoupdate)) {
 7649:         if (($key eq 'run') || ($key eq 'classlists')) {
 7650:             if (exists($updatehash{autoupdate}{$key})) {
 7651:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 7652:                     $changes{$key} = 1;
 7653:                 }
 7654:             }
 7655:         } elsif ($key eq 'fields') {
 7656:             if (ref($currautoupdate{$key}) eq 'HASH') {
 7657:                 foreach my $item (@{$types},'default') {
 7658:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 7659:                         my $change = 0;
 7660:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 7661:                             if (!exists($fields{$item})) {
 7662:                                 $change = 1;
 7663:                                 last;
 7664:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 7665:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 7666:                                     $change = 1;
 7667:                                     last;
 7668:                                 }
 7669:                             }
 7670:                         }
 7671:                         if ($change) {
 7672:                             push(@{$changes{$key}},$item);
 7673:                         }
 7674:                     } 
 7675:                 }
 7676:             }
 7677:         } elsif ($key eq 'lockablenames') {
 7678:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 7679:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7680:                 if (@changed) {
 7681:                     $changes{'lockablenames'} = 1;
 7682:                 }
 7683:             } else {
 7684:                 if (@lockablenames) {
 7685:                     $changes{'lockablenames'} = 1;
 7686:                 }
 7687:             }
 7688:         }
 7689:     }
 7690:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 7691:         if (@lockablenames) {
 7692:             $changes{'lockablenames'} = 1;
 7693:         }
 7694:     }
 7695:     foreach my $item (@{$types},'default') {
 7696:         if (defined($fields{$item})) {
 7697:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 7698:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 7699:                     my $change = 0;
 7700:                     if (ref($fields{$item}) eq 'ARRAY') {
 7701:                         foreach my $type (@{$fields{$item}}) {
 7702:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 7703:                                 $change = 1;
 7704:                                 last;
 7705:                             }
 7706:                         }
 7707:                     }
 7708:                     if ($change) {
 7709:                         push(@{$changes{'fields'}},$item);
 7710:                     }
 7711:                 } else {
 7712:                     push(@{$changes{'fields'}},$item);
 7713:                 }
 7714:             } else {
 7715:                 push(@{$changes{'fields'}},$item);
 7716:             }
 7717:         }
 7718:     }
 7719:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7720:                                              $dom);
 7721:     if ($putresult eq 'ok') {
 7722:         if (keys(%changes) > 0) {
 7723:             $resulttext = &mt('Changes made:').'<ul>';
 7724:             foreach my $key (sort(keys(%changes))) {
 7725:                 if ($key eq 'lockablenames') {
 7726:                     $resulttext .= '<li>';
 7727:                     if (@lockablenames) {
 7728:                         $usertypes->{'default'} = $othertitle;
 7729:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7730:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7731:                     } else {
 7732:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7733:                     }
 7734:                     $resulttext .= '</li>';
 7735:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7736:                     foreach my $item (@{$changes{$key}}) {
 7737:                         my @newvalues;
 7738:                         foreach my $type (@{$fields{$item}}) {
 7739:                             push(@newvalues,$fieldtitles{$type});
 7740:                         }
 7741:                         my $newvaluestr;
 7742:                         if (@newvalues > 0) {
 7743:                             $newvaluestr = join(', ',@newvalues);
 7744:                         } else {
 7745:                             $newvaluestr = &mt('none');
 7746:                         }
 7747:                         if ($item eq 'default') {
 7748:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7749:                         } else {
 7750:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7751:                         }
 7752:                     }
 7753:                 } else {
 7754:                     my $newvalue;
 7755:                     if ($key eq 'run') {
 7756:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7757:                     } else {
 7758:                         $newvalue = $offon[$env{'form.'.$key}];
 7759:                     }
 7760:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7761:                 }
 7762:             }
 7763:             $resulttext .= '</ul>';
 7764:         } else {
 7765:             $resulttext = &mt('No changes made to autoupdates');
 7766:         }
 7767:     } else {
 7768:         $resulttext = '<span class="LC_error">'.
 7769: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7770:     }
 7771:     return $resulttext;
 7772: }
 7773: 
 7774: sub modify_autocreate {
 7775:     my ($dom,%domconfig) = @_;
 7776:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7777:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7778:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7779:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7780:         }
 7781:     }
 7782:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7783:                  req => 'Auto-creation of validated requests for official courses',
 7784:                  xmldc => 'Identity of course creator of courses from XML files',
 7785:                );
 7786:     my @types = ('xml','req');
 7787:     foreach my $item (@types) {
 7788:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7789:         $newvals{$item} =~ s/\D//g;
 7790:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7791:     }
 7792:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7793:     my %domcoords = &get_active_dcs($dom);
 7794:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7795:         $newvals{'xmldc'} = '';
 7796:     } 
 7797:     %autocreatehash =  (
 7798:                         autocreate => { xml => $newvals{'xml'},
 7799:                                         req => $newvals{'req'},
 7800:                                       }
 7801:                        );
 7802:     if ($newvals{'xmldc'} ne '') {
 7803:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7804:     }
 7805:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7806:                                              $dom);
 7807:     if ($putresult eq 'ok') {
 7808:         my @items = @types;
 7809:         if ($newvals{'xml'}) {
 7810:             push(@items,'xmldc');
 7811:         }
 7812:         foreach my $item (@items) {
 7813:             if (exists($currautocreate{$item})) {
 7814:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7815:                     $changes{$item} = 1;
 7816:                 }
 7817:             } elsif ($newvals{$item}) {
 7818:                 $changes{$item} = 1;
 7819:             }
 7820:         }
 7821:         if (keys(%changes) > 0) {
 7822:             my @offon = ('off','on'); 
 7823:             $resulttext = &mt('Changes made:').'<ul>';
 7824:             foreach my $item (@types) {
 7825:                 if ($changes{$item}) {
 7826:                     my $newtxt = $offon[$newvals{$item}];
 7827:                     $resulttext .= '<li>'.
 7828:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7829:                                        '<b>','</b>').
 7830:                                    '</li>';
 7831:                 }
 7832:             }
 7833:             if ($changes{'xmldc'}) {
 7834:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7835:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7836:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7837:             }
 7838:             $resulttext .= '</ul>';
 7839:         } else {
 7840:             $resulttext = &mt('No changes made to auto-creation settings');
 7841:         }
 7842:     } else {
 7843:         $resulttext = '<span class="LC_error">'.
 7844:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7845:     }
 7846:     return $resulttext;
 7847: }
 7848: 
 7849: sub modify_directorysrch {
 7850:     my ($dom,%domconfig) = @_;
 7851:     my ($resulttext,%changes);
 7852:     my %currdirsrch;
 7853:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7854:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7855:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7856:         }
 7857:     }
 7858:     my %title = ( available => 'Directory search available',
 7859:                   localonly => 'Other domains can search',
 7860:                   searchby => 'Search types',
 7861:                   searchtypes => 'Search latitude');
 7862:     my @offon = ('off','on');
 7863:     my @otherdoms = ('Yes','No');
 7864: 
 7865:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7866:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7867:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7868: 
 7869:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7870:     if (keys(%{$usertypes}) == 0) {
 7871:         @cansearch = ('default');
 7872:     } else {
 7873:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7874:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7875:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7876:                     push(@{$changes{'cansearch'}},$type);
 7877:                 }
 7878:             }
 7879:             foreach my $type (@cansearch) {
 7880:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7881:                     push(@{$changes{'cansearch'}},$type);
 7882:                 }
 7883:             }
 7884:         } else {
 7885:             push(@{$changes{'cansearch'}},@cansearch);
 7886:         }
 7887:     }
 7888: 
 7889:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7890:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7891:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7892:                 push(@{$changes{'searchby'}},$by);
 7893:             }
 7894:         }
 7895:         foreach my $by (@searchby) {
 7896:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7897:                 push(@{$changes{'searchby'}},$by);
 7898:             }
 7899:         }
 7900:     } else {
 7901:         push(@{$changes{'searchby'}},@searchby);
 7902:     }
 7903: 
 7904:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7905:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7906:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7907:                 push(@{$changes{'searchtypes'}},$type);
 7908:             }
 7909:         }
 7910:         foreach my $type (@searchtypes) {
 7911:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7912:                 push(@{$changes{'searchtypes'}},$type);
 7913:             }
 7914:         }
 7915:     } else {
 7916:         if (exists($currdirsrch{'searchtypes'})) {
 7917:             foreach my $type (@searchtypes) {  
 7918:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7919:                     push(@{$changes{'searchtypes'}},$type);
 7920:                 }
 7921:             }
 7922:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7923:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7924:             }   
 7925:         } else {
 7926:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7927:         }
 7928:     }
 7929: 
 7930:     my %dirsrch_hash =  (
 7931:             directorysrch => { available => $env{'form.dirsrch_available'},
 7932:                                cansearch => \@cansearch,
 7933:                                localonly => $env{'form.dirsrch_localonly'},
 7934:                                searchby => \@searchby,
 7935:                                searchtypes => \@searchtypes,
 7936:                              }
 7937:             );
 7938:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7939:                                              $dom);
 7940:     if ($putresult eq 'ok') {
 7941:         if (exists($currdirsrch{'available'})) {
 7942:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7943:                  $changes{'available'} = 1;
 7944:              }
 7945:         } else {
 7946:             if ($env{'form.dirsrch_available'} eq '1') {
 7947:                 $changes{'available'} = 1;
 7948:             }
 7949:         }
 7950:         if (exists($currdirsrch{'localonly'})) {
 7951:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7952:                  $changes{'localonly'} = 1;
 7953:              }
 7954:         } else {
 7955:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7956:                 $changes{'localonly'} = 1;
 7957:             }
 7958:         }
 7959:         if (keys(%changes) > 0) {
 7960:             $resulttext = &mt('Changes made:').'<ul>';
 7961:             if ($changes{'available'}) {
 7962:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7963:             }
 7964:             if ($changes{'localonly'}) {
 7965:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7966:             }
 7967: 
 7968:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7969:                 my $chgtext;
 7970:                 if (ref($usertypes) eq 'HASH') {
 7971:                     if (keys(%{$usertypes}) > 0) {
 7972:                         foreach my $type (@{$types}) {
 7973:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7974:                                 $chgtext .= $usertypes->{$type}.'; ';
 7975:                             }
 7976:                         }
 7977:                         if (grep(/^default$/,@cansearch)) {
 7978:                             $chgtext .= $othertitle;
 7979:                         } else {
 7980:                             $chgtext =~ s/\; $//;
 7981:                         }
 7982:                         $resulttext .=
 7983:                             '<li>'.
 7984:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 7985:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 7986:                             '</li>';
 7987:                     }
 7988:                 }
 7989:             }
 7990:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 7991:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 7992:                 my $chgtext;
 7993:                 foreach my $type (@{$titleorder}) {
 7994:                     if (grep(/^\Q$type\E$/,@searchby)) {
 7995:                         if (defined($searchtitles->{$type})) {
 7996:                             $chgtext .= $searchtitles->{$type}.'; ';
 7997:                         }
 7998:                     }
 7999:                 }
 8000:                 $chgtext =~ s/\; $//;
 8001:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 8002:             }
 8003:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 8004:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 8005:                 my $chgtext;
 8006:                 foreach my $type (@{$srchtypeorder}) {
 8007:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 8008:                         if (defined($srchtypes_desc->{$type})) {
 8009:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 8010:                         }
 8011:                     }
 8012:                 }
 8013:                 $chgtext =~ s/\; $//;
 8014:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 8015:             }
 8016:             $resulttext .= '</ul>';
 8017:         } else {
 8018:             $resulttext = &mt('No changes made to institution directory search settings');
 8019:         }
 8020:     } else {
 8021:         $resulttext = '<span class="LC_error">'.
 8022:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 8023:     }
 8024:     return $resulttext;
 8025: }
 8026: 
 8027: sub modify_contacts {
 8028:     my ($dom,$lastactref,%domconfig) = @_;
 8029:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 8030:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 8031:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 8032:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 8033:         }
 8034:     }
 8035:     my (%others,%to,%bcc);
 8036:     my @contacts = ('supportemail','adminemail');
 8037:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 8038:                     'requestsmail','updatesmail','idconflictsmail');
 8039:     my @toggles = ('reporterrors','reportupdates');
 8040:     foreach my $type (@mailings) {
 8041:         @{$newsetting{$type}} = 
 8042:             &Apache::loncommon::get_env_multiple('form.'.$type);
 8043:         foreach my $item (@contacts) {
 8044:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 8045:                 $contacts_hash{contacts}{$type}{$item} = 1;
 8046:             } else {
 8047:                 $contacts_hash{contacts}{$type}{$item} = 0;
 8048:             }
 8049:         }  
 8050:         $others{$type} = $env{'form.'.$type.'_others'};
 8051:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 8052:         if ($type eq 'helpdeskmail') {
 8053:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 8054:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 8055:         }
 8056:     }
 8057:     foreach my $item (@contacts) {
 8058:         $to{$item} = $env{'form.'.$item};
 8059:         $contacts_hash{'contacts'}{$item} = $to{$item};
 8060:     }
 8061:     foreach my $item (@toggles) {
 8062:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 8063:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 8064:         }
 8065:     }
 8066:     if (keys(%currsetting) > 0) {
 8067:         foreach my $item (@contacts) {
 8068:             if ($to{$item} ne $currsetting{$item}) {
 8069:                 $changes{$item} = 1;
 8070:             }
 8071:         }
 8072:         foreach my $type (@mailings) {
 8073:             foreach my $item (@contacts) {
 8074:                 if (ref($currsetting{$type}) eq 'HASH') {
 8075:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 8076:                         push(@{$changes{$type}},$item);
 8077:                     }
 8078:                 } else {
 8079:                     push(@{$changes{$type}},@{$newsetting{$type}});
 8080:                 }
 8081:             }
 8082:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 8083:                 push(@{$changes{$type}},'others');
 8084:             }
 8085:             if ($type eq 'helpdeskmail') {   
 8086:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 8087:                     push(@{$changes{$type}},'bcc'); 
 8088:                 }
 8089:             }
 8090:         }
 8091:     } else {
 8092:         my %default;
 8093:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 8094:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 8095:         $default{'errormail'} = 'adminemail';
 8096:         $default{'packagesmail'} = 'adminemail';
 8097:         $default{'helpdeskmail'} = 'supportemail';
 8098:         $default{'lonstatusmail'} = 'adminemail';
 8099:         $default{'requestsmail'} = 'adminemail';
 8100:         $default{'updatesmail'} = 'adminemail';
 8101:         foreach my $item (@contacts) {
 8102:            if ($to{$item} ne $default{$item}) {
 8103:               $changes{$item} = 1;
 8104:            }
 8105:         }
 8106:         foreach my $type (@mailings) {
 8107:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 8108:                
 8109:                 push(@{$changes{$type}},@{$newsetting{$type}});
 8110:             }
 8111:             if ($others{$type} ne '') {
 8112:                 push(@{$changes{$type}},'others');
 8113:             }
 8114:             if ($type eq 'helpdeskmail') {
 8115:                 if ($bcc{$type} ne '') {
 8116:                     push(@{$changes{$type}},'bcc');
 8117:                 }
 8118:             }
 8119:         }
 8120:     }
 8121:     foreach my $item (@toggles) {
 8122:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 8123:             $changes{$item} = 1;
 8124:         } elsif ((!$env{'form.'.$item}) &&
 8125:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 8126:             $changes{$item} = 1;
 8127:         }
 8128:     }
 8129:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 8130:                                              $dom);
 8131:     if ($putresult eq 'ok') {
 8132:         if (keys(%changes) > 0) {
 8133:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8134:             if (ref($lastactref) eq 'HASH') {
 8135:                 $lastactref->{'domainconfig'} = 1;
 8136:             }
 8137:             my ($titles,$short_titles)  = &contact_titles();
 8138:             $resulttext = &mt('Changes made:').'<ul>';
 8139:             foreach my $item (@contacts) {
 8140:                 if ($changes{$item}) {
 8141:                     $resulttext .= '<li>'.$titles->{$item}.
 8142:                                     &mt(' set to: ').
 8143:                                     '<span class="LC_cusr_emph">'.
 8144:                                     $to{$item}.'</span></li>';
 8145:                 }
 8146:             }
 8147:             foreach my $type (@mailings) {
 8148:                 if (ref($changes{$type}) eq 'ARRAY') {
 8149:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 8150:                     my @text;
 8151:                     foreach my $item (@{$newsetting{$type}}) {
 8152:                         push(@text,$short_titles->{$item});
 8153:                     }
 8154:                     if ($others{$type} ne '') {
 8155:                         push(@text,$others{$type});
 8156:                     }
 8157:                     $resulttext .= '<span class="LC_cusr_emph">'.
 8158:                                    join(', ',@text).'</span>';
 8159:                     if ($type eq 'helpdeskmail') {
 8160:                         if ($bcc{$type} ne '') {
 8161:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 8162:                         }
 8163:                     }
 8164:                     $resulttext .= '</li>';
 8165:                 }
 8166:             }
 8167:             my @offon = ('off','on');
 8168:             if ($changes{'reporterrors'}) {
 8169:                 $resulttext .= '<li>'.
 8170:                                &mt('E-mail error reports to [_1] set to "'.
 8171:                                    $offon[$env{'form.reporterrors'}].'".',
 8172:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8173:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 8174:                                '</li>';
 8175:             }
 8176:             if ($changes{'reportupdates'}) {
 8177:                 $resulttext .= '<li>'.
 8178:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 8179:                                     $offon[$env{'form.reportupdates'}].'".',
 8180:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8181:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 8182:                                 '</li>';
 8183:             }
 8184:             $resulttext .= '</ul>';
 8185:         } else {
 8186:             $resulttext = &mt('No changes made to contact information');
 8187:         }
 8188:     } else {
 8189:         $resulttext = '<span class="LC_error">'.
 8190:             &mt('An error occurred: [_1].',$putresult).'</span>';
 8191:     }
 8192:     return $resulttext;
 8193: }
 8194: 
 8195: sub modify_usercreation {
 8196:     my ($dom,%domconfig) = @_;
 8197:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 8198:     my $warningmsg;
 8199:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8200:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8201:             if ($key eq 'cancreate') {
 8202:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8203:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8204:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8205:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 8206:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8207:                         } else {
 8208:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8209:                         }
 8210:                     }
 8211:                 }
 8212:             } elsif ($key eq 'email_rule') {
 8213:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8214:             } else {
 8215:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8216:             }
 8217:         }
 8218:     }
 8219:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 8220:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 8221:     my @contexts = ('author','course','requestcrs');
 8222:     foreach my $item(@contexts) {
 8223:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 8224:     }
 8225:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8226:         foreach my $item (@contexts) {
 8227:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 8228:                 push(@{$changes{'cancreate'}},$item);
 8229:             }
 8230:         }
 8231:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 8232:         foreach my $item (@contexts) {
 8233:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 8234:                 if ($cancreate{$item} ne 'any') {
 8235:                     push(@{$changes{'cancreate'}},$item);
 8236:                 }
 8237:             } else {
 8238:                 if ($cancreate{$item} ne 'none') {
 8239:                     push(@{$changes{'cancreate'}},$item);
 8240:                 }
 8241:             }
 8242:         }
 8243:     } else {
 8244:         foreach my $item (@contexts)  {
 8245:             push(@{$changes{'cancreate'}},$item);
 8246:         }
 8247:     }
 8248: 
 8249:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 8250:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 8251:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 8252:                 push(@{$changes{'username_rule'}},$type);
 8253:             }
 8254:         }
 8255:         foreach my $type (@username_rule) {
 8256:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 8257:                 push(@{$changes{'username_rule'}},$type);
 8258:             }
 8259:         }
 8260:     } else {
 8261:         push(@{$changes{'username_rule'}},@username_rule);
 8262:     }
 8263: 
 8264:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 8265:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 8266:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 8267:                 push(@{$changes{'id_rule'}},$type);
 8268:             }
 8269:         }
 8270:         foreach my $type (@id_rule) {
 8271:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 8272:                 push(@{$changes{'id_rule'}},$type);
 8273:             }
 8274:         }
 8275:     } else {
 8276:         push(@{$changes{'id_rule'}},@id_rule);
 8277:     }
 8278: 
 8279:     my @authen_contexts = ('author','course','domain');
 8280:     my @authtypes = ('int','krb4','krb5','loc');
 8281:     my %authhash;
 8282:     foreach my $item (@authen_contexts) {
 8283:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 8284:         foreach my $auth (@authtypes) {
 8285:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 8286:                 $authhash{$item}{$auth} = 1;
 8287:             } else {
 8288:                 $authhash{$item}{$auth} = 0;
 8289:             }
 8290:         }
 8291:     }
 8292:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 8293:         foreach my $item (@authen_contexts) {
 8294:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 8295:                 foreach my $auth (@authtypes) {
 8296:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 8297:                         push(@{$changes{'authtypes'}},$item);
 8298:                         last;
 8299:                     }
 8300:                 }
 8301:             }
 8302:         }
 8303:     } else {
 8304:         foreach my $item (@authen_contexts) {
 8305:             push(@{$changes{'authtypes'}},$item);
 8306:         }
 8307:     }
 8308: 
 8309:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 8310:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 8311:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 8312:     $save_usercreate{'id_rule'} = \@id_rule;
 8313:     $save_usercreate{'username_rule'} = \@username_rule,
 8314:     $save_usercreate{'authtypes'} = \%authhash;
 8315: 
 8316:     my %usercreation_hash =  (
 8317:         usercreation     => \%save_usercreate,
 8318:     );
 8319: 
 8320:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 8321:                                              $dom);
 8322: 
 8323:     if ($putresult eq 'ok') {
 8324:         if (keys(%changes) > 0) {
 8325:             $resulttext = &mt('Changes made:').'<ul>';
 8326:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8327:                 my %lt = &usercreation_types();
 8328:                 foreach my $type (@{$changes{'cancreate'}}) {
 8329:                     my $chgtext = $lt{$type}.', ';
 8330:                     if ($cancreate{$type} eq 'none') {
 8331:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 8332:                     } elsif ($cancreate{$type} eq 'any') {
 8333:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 8334:                     } elsif ($cancreate{$type} eq 'official') {
 8335:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 8336:                     } elsif ($cancreate{$type} eq 'unofficial') {
 8337:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 8338:                     }
 8339:                     $resulttext .= '<li>'.$chgtext.'</li>';
 8340:                 }
 8341:             }
 8342:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 8343:                 my ($rules,$ruleorder) = 
 8344:                     &Apache::lonnet::inst_userrules($dom,'username');
 8345:                 my $chgtext = '<ul>';
 8346:                 foreach my $type (@username_rule) {
 8347:                     if (ref($rules->{$type}) eq 'HASH') {
 8348:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 8349:                     }
 8350:                 }
 8351:                 $chgtext .= '</ul>';
 8352:                 if (@username_rule > 0) {
 8353:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 8354:                 } else {
 8355:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 8356:                 }
 8357:             }
 8358:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 8359:                 my ($idrules,$idruleorder) = 
 8360:                     &Apache::lonnet::inst_userrules($dom,'id');
 8361:                 my $chgtext = '<ul>';
 8362:                 foreach my $type (@id_rule) {
 8363:                     if (ref($idrules->{$type}) eq 'HASH') {
 8364:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8365:                     }
 8366:                 }
 8367:                 $chgtext .= '</ul>';
 8368:                 if (@id_rule > 0) {
 8369:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8370:                 } else {
 8371:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8372:                 }
 8373:             }
 8374:             my %authname = &authtype_names();
 8375:             my %context_title = &context_names();
 8376:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8377:                 my $chgtext = '<ul>';
 8378:                 foreach my $type (@{$changes{'authtypes'}}) {
 8379:                     my @allowed;
 8380:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8381:                     foreach my $auth (@authtypes) {
 8382:                         if ($authhash{$type}{$auth}) {
 8383:                             push(@allowed,$authname{$auth});
 8384:                         }
 8385:                     }
 8386:                     if (@allowed > 0) {
 8387:                         $chgtext .= join(', ',@allowed).'</li>';
 8388:                     } else {
 8389:                         $chgtext .= &mt('none').'</li>';
 8390:                     }
 8391:                 }
 8392:                 $chgtext .= '</ul>';
 8393:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8394:                 $resulttext .= '</li>';
 8395:             }
 8396:             $resulttext .= '</ul>';
 8397:         } else {
 8398:             $resulttext = &mt('No changes made to user creation settings');
 8399:         }
 8400:     } else {
 8401:         $resulttext = '<span class="LC_error">'.
 8402:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8403:     }
 8404:     if ($warningmsg ne '') {
 8405:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8406:     }
 8407:     return $resulttext;
 8408: }
 8409: 
 8410: sub modify_selfcreation {
 8411:     my ($dom,%domconfig) = @_;
 8412:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8413:     my (%save_usercreate,%save_usermodify);
 8414:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8415:     if (ref($types) eq 'ARRAY') {
 8416:         $usertypes->{'default'} = $othertitle;
 8417:         push(@{$types},'default');
 8418:     }
 8419: #
 8420: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8421: #
 8422:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8423:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8424:             if ($key eq 'cancreate') {
 8425:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8426:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8427:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8428:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8429:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 8430:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 8431:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8432:                         } else {
 8433:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8434:                         }
 8435:                     }
 8436:                 }
 8437:             } elsif ($key eq 'email_rule') {
 8438:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8439:             } else {
 8440:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8441:             }
 8442:         }
 8443:     }
 8444: #
 8445: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8446: #
 8447:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8448:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8449:             if ($key eq 'selfcreate') {
 8450:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8451:             } else {
 8452:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8453:             }
 8454:         }
 8455:     }
 8456: 
 8457:     my @contexts = ('selfcreate');
 8458:     @{$cancreate{'selfcreate'}} = ();
 8459:     %{$cancreate{'emailusername'}} = ();
 8460:     @{$cancreate{'statustocreate'}} = ();
 8461:     %{$cancreate{'selfcreateprocessing'}} = ();
 8462:     %{$cancreate{'shibenv'}} = ();
 8463:     my %selfcreatetypes = (
 8464:                              sso   => 'users authenticated by institutional single sign on',
 8465:                              login => 'users authenticated by institutional log-in',
 8466:                              email => 'users who provide a valid e-mail address for use as username',
 8467:                           );
 8468: #
 8469: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8470: # is permitted.
 8471: #
 8472: 
 8473:     my @statuses;
 8474:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 8475:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 8476:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 8477:         }
 8478:     }
 8479:     push(@statuses,'default');
 8480: 
 8481:     foreach my $item ('login','sso','email') {
 8482:         if ($item eq 'email') {
 8483:             if ($env{'form.cancreate_email'}) {
 8484:                 push(@{$cancreate{'selfcreate'}},'email');
 8485:                 push(@contexts,'selfcreateprocessing');
 8486:                 foreach my $type (@statuses) {
 8487:                     if ($type eq 'default') {
 8488:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 8489:                     } else { 
 8490:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 8491:                     }
 8492:                 }
 8493:             }
 8494:         } else {
 8495:             if ($env{'form.cancreate_'.$item}) {
 8496:                 push(@{$cancreate{'selfcreate'}},$item);
 8497:             }
 8498:         }
 8499:     }
 8500:     my (@email_rule,%userinfo,%savecaptcha);
 8501:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8502: #
 8503: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8504: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8505: #
 8506: 
 8507:     if ($env{'form.cancreate_email'} eq 'email') {
 8508:         push(@contexts,'emailusername');
 8509:         if (ref($types) eq 'ARRAY') {
 8510:             foreach my $type (@{$types}) {
 8511:                 if (ref($infofields) eq 'ARRAY') {
 8512:                     foreach my $field (@{$infofields}) {
 8513:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8514:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8515:                         }
 8516:                     }
 8517:                 }
 8518:             }
 8519:         }
 8520: #
 8521: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8522: # queued requests for self-creation of account using e-mail address as username
 8523: #
 8524: 
 8525:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8526:         @approvalnotify = sort(@approvalnotify);
 8527:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8528:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8529:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8530:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8531:                     push(@{$changes{'cancreate'}},'notify');
 8532:                 }
 8533:             } else {
 8534:                 if ($cancreate{'notify'}{'approval'}) {
 8535:                     push(@{$changes{'cancreate'}},'notify');
 8536:                 }
 8537:             }
 8538:         } elsif ($cancreate{'notify'}{'approval'}) {
 8539:             push(@{$changes{'cancreate'}},'notify');
 8540:         }
 8541: 
 8542: #
 8543: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8544: #
 8545:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8546:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8547:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8548:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8549:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8550:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8551:                         push(@{$changes{'email_rule'}},$type);
 8552:                     }
 8553:                 }
 8554:             }
 8555:             if (@email_rule > 0) {
 8556:                 foreach my $type (@email_rule) {
 8557:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8558:                         push(@{$changes{'email_rule'}},$type);
 8559:                     }
 8560:                 }
 8561:             }
 8562:         } elsif (@email_rule > 0) {
 8563:             push(@{$changes{'email_rule'}},@email_rule);
 8564:         }
 8565:     }
 8566: #  
 8567: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 8568: # institutional log-in.
 8569: #
 8570:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8571:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8572:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8573:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8574:             $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.').' '.
 8575:                           &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.');
 8576:         }
 8577:     }
 8578:     my @fields = ('lastname','firstname','middlename','generation',
 8579:                   'permanentemail','id');
 8580:     my @shibfields = (@fields,'inststatus');
 8581:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8582: #
 8583: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8584: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8585: # may self-create accounts 
 8586: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8587: # which the user may supply, if institutional data is unavailable.
 8588: #
 8589:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8590:         if (ref($types) eq 'ARRAY') {
 8591:             if (@{$types} > 1) {
 8592:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8593:                 push(@contexts,'statustocreate');
 8594:             } else {
 8595:                 undef($cancreate{'statustocreate'});
 8596:             } 
 8597:             foreach my $type (@{$types}) {
 8598:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8599:                 foreach my $field (@fields) {
 8600:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8601:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8602:                     } else {
 8603:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8604:                     }
 8605:                 }
 8606:             }
 8607:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8608:                 foreach my $type (@{$types}) {
 8609:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8610:                         foreach my $field (@fields) {
 8611:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8612:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8613:                                 push(@{$changes{'selfcreate'}},$type);
 8614:                                 last;
 8615:                             }
 8616:                         }
 8617:                     }
 8618:                 }
 8619:             } else {
 8620:                 foreach my $type (@{$types}) {
 8621:                     push(@{$changes{'selfcreate'}},$type);
 8622:                 }
 8623:             }
 8624:         }
 8625:         foreach my $field (@shibfields) {
 8626:             if ($env{'form.shibenv_'.$field} ne '') {
 8627:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 8628:             }
 8629:         }
 8630:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8631:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 8632:                 foreach my $field (@shibfields) {
 8633:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 8634:                         push(@{$changes{'cancreate'}},'shibenv');
 8635:                     }
 8636:                 }
 8637:             } else {
 8638:                 foreach my $field (@shibfields) {
 8639:                     if ($env{'form.shibenv_'.$field}) {
 8640:                         push(@{$changes{'cancreate'}},'shibenv');
 8641:                         last;
 8642:                     }
 8643:                 }
 8644:             }
 8645:         }
 8646:     }
 8647:     foreach my $item (@contexts) {
 8648:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 8649:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 8650:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 8651:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 8652:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8653:                             push(@{$changes{'cancreate'}},$item);
 8654:                         }
 8655:                     }
 8656:                 }
 8657:             }
 8658:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8659:                 foreach my $type (@{$cancreate{$item}}) {
 8660:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 8661:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8662:                             push(@{$changes{'cancreate'}},$item);
 8663:                         }
 8664:                     }
 8665:                 }
 8666:             }
 8667:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 8668:             if (ref($cancreate{$item}) eq 'HASH') {
 8669:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 8670:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 8671:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 8672:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 8673:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8674:                                     push(@{$changes{'cancreate'}},$item);
 8675:                                 }
 8676:                             }
 8677:                         }
 8678:                     } elsif ($item eq 'selfcreateprocessing') {
 8679:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 8680:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8681:                                 push(@{$changes{'cancreate'}},$item);
 8682:                             }
 8683:                         }
 8684:                     } else {
 8685:                         if (!$cancreate{$item}{$curr}) {
 8686:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8687:                                 push(@{$changes{'cancreate'}},$item);
 8688:                             }
 8689:                         }
 8690:                     }
 8691:                 }
 8692:                 foreach my $field (keys(%{$cancreate{$item}})) {
 8693:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 8694:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 8695:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 8696:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 8697:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8698:                                         push(@{$changes{'cancreate'}},$item);
 8699:                                     }
 8700:                                 }
 8701:                             } else {
 8702:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8703:                                     push(@{$changes{'cancreate'}},$item);
 8704:                                 }
 8705:                             }
 8706:                         }
 8707:                     } elsif ($item eq 'selfcreateprocessing') {
 8708:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 8709:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8710:                                 push(@{$changes{'cancreate'}},$item);
 8711:                             }
 8712:                         }
 8713:                     } else {
 8714:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 8715:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8716:                                 push(@{$changes{'cancreate'}},$item);
 8717:                             }
 8718:                         }
 8719:                     }
 8720:                 }
 8721:             }
 8722:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 8723:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8724:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 8725:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8726:                         push(@{$changes{'cancreate'}},$item);
 8727:                     }
 8728:                 }
 8729:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 8730:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 8731:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8732:                         push(@{$changes{'cancreate'}},$item);
 8733:                     }
 8734:                 }
 8735:             }
 8736:         } elsif ($item eq 'emailusername') {
 8737:             if (ref($cancreate{$item}) eq 'HASH') {
 8738:                 foreach my $type (keys(%{$cancreate{$item}})) {
 8739:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 8740:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 8741:                             if ($cancreate{$item}{$type}{$field}) {
 8742:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8743:                                     push(@{$changes{'cancreate'}},$item);
 8744:                                 }
 8745:                                 last;
 8746:                             }
 8747:                         }
 8748:                     }
 8749:                 }
 8750:             }
 8751:         }
 8752:     }
 8753: #
 8754: # Populate %save_usercreate hash with updates to self-creation configuration.
 8755: #
 8756:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 8757:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 8758:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 8759:     if (ref($cancreate{'notify'}) eq 'HASH') {
 8760:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 8761:     }
 8762:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
 8763:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
 8764:     }
 8765:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8766:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 8767:     }
 8768:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
 8769:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
 8770:     }
 8771:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 8772:     $save_usercreate{'emailrule'} = \@email_rule;
 8773: 
 8774:     my %userconfig_hash = (
 8775:             usercreation     => \%save_usercreate,
 8776:             usermodification => \%save_usermodify,
 8777:     );
 8778:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8779:                                              $dom);
 8780: #
 8781: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8782: #
 8783:     if ($putresult eq 'ok') {
 8784:         if (keys(%changes) > 0) {
 8785:             $resulttext = &mt('Changes made:').'<ul>';
 8786:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8787:                 my %lt = &selfcreation_types();
 8788:                 foreach my $type (@{$changes{'cancreate'}}) {
 8789:                     my $chgtext;
 8790:                     if ($type eq 'selfcreate') {
 8791:                         if (@{$cancreate{$type}} == 0) {
 8792:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 8793:                         } else {
 8794:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 8795:                                         '<ul>';
 8796:                             foreach my $case (@{$cancreate{$type}}) {
 8797:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 8798:                             }
 8799:                             $chgtext .= '</ul>';
 8800:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 8801:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 8802:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8803:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 8804:                                             $chgtext .= '<br />'.
 8805:                                                         '<span class="LC_warning">'.
 8806:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8807:                                                         '</span>';
 8808:                                         }
 8809:                                     }
 8810:                                 }
 8811:                             }
 8812:                         }
 8813:                     } elsif ($type eq 'shibenv') {
 8814:                         if (keys(%{$cancreate{$type}}) == 0) {
 8815:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
 8816:                         } else {
 8817:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
 8818:                                         '<ul>';
 8819:                             foreach my $field (@shibfields) {
 8820:                                 next if ($cancreate{$type}{$field} eq '');
 8821:                                 if ($field eq 'inststatus') {
 8822:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
 8823:                                 } else {
 8824:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
 8825:                                 }
 8826:                             }
 8827:                             $chgtext .= '</ul>';
 8828:                         }  
 8829:                     } elsif ($type eq 'statustocreate') {
 8830:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 8831:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 8832:                             if (@{$cancreate{'selfcreate'}} > 0) {
 8833:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 8834:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 8835:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 8836:                                         $chgtext .= '<br />'.
 8837:                                                     '<span class="LC_warning">'.
 8838:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8839:                                                     '</span>';
 8840:                                     }
 8841:                                 } elsif (ref($usertypes) eq 'HASH') {
 8842:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8843:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 8844:                                     } else {
 8845:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 8846:                                     }
 8847:                                     $chgtext .= '<ul>';
 8848:                                     foreach my $case (@{$cancreate{$type}}) {
 8849:                                         if ($case eq 'default') {
 8850:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 8851:                                         } else {
 8852:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 8853:                                         }
 8854:                                     }
 8855:                                     $chgtext .= '</ul>';
 8856:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8857:                                         $chgtext .= '<br /><span class="LC_warning">'.
 8858:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 8859:                                                     '</span>';
 8860:                                     }
 8861:                                 }
 8862:                             } else {
 8863:                                 if (@{$cancreate{$type}} == 0) {
 8864:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8865:                                 } else {
 8866:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8867:                                 }
 8868:                             }
 8869:                         }
 8870:                     } elsif ($type eq 'selfcreateprocessing') {
 8871:                         my %choices = &Apache::lonlocal::texthash (
 8872:                                                                     automatic => 'Automatic approval',
 8873:                                                                     approval  => 'Queued for approval',
 8874:                                                                   );
 8875:                         if (@statuses > 1) {
 8876:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
 8877:                                         '<ul>';
 8878:                            foreach my $type (@statuses) {
 8879:                                if ($type eq 'default') {
 8880:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8881:                                } else {
 8882:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8883:                                }
 8884:                            }
 8885:                            $chgtext .= '</ul>';
 8886:                         } else {
 8887:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
 8888:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
 8889:                         }
 8890:                     } elsif ($type eq 'captcha') {
 8891:                         if ($savecaptcha{$type} eq 'notused') {
 8892:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8893:                         } else {
 8894:                             my %captchas = &captcha_phrases();
 8895:                             if ($captchas{$savecaptcha{$type}}) {
 8896:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8897:                             } else {
 8898:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8899:                             }
 8900:                         }
 8901:                     } elsif ($type eq 'recaptchakeys') {
 8902:                         my ($privkey,$pubkey);
 8903:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8904:                             $pubkey = $savecaptcha{$type}{'public'};
 8905:                             $privkey = $savecaptcha{$type}{'private'};
 8906:                         }
 8907:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8908:                         if (!$pubkey) {
 8909:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8910:                         } else {
 8911:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8912:                         }
 8913:                         if (!$privkey) {
 8914:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8915:                         } else {
 8916:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8917:                         }
 8918:                         $chgtext .= '</ul>';
 8919:                     } elsif ($type eq 'emailusername') {
 8920:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8921:                             if (ref($types) eq 'ARRAY') {
 8922:                                 foreach my $type (@{$types}) {
 8923:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 8924:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 8925:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',$usertypes->{$type}).
 8926:                                                     '<ul>';
 8927:                                             foreach my $field (@{$infofields}) {
 8928:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 8929:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8930:                                                 }
 8931:                                             }
 8932:                                         }
 8933:                                         $chgtext .= '</ul>';
 8934:                                     } else {
 8935:                                         $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 />';
 8936:                                     }
 8937:                                 }
 8938:                             }
 8939:                         }
 8940:                     } elsif ($type eq 'notify') {
 8941:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8942:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8943:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8944:                                 if ($cancreate{'notify'}{'approval'}) {
 8945:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8946:                                 }
 8947:                             }
 8948:                         }
 8949:                     }
 8950:                     if ($chgtext) {
 8951:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8952:                     }
 8953:                 }
 8954:             }
 8955:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8956:                 my ($emailrules,$emailruleorder) =
 8957:                     &Apache::lonnet::inst_userrules($dom,'email');
 8958:                 my $chgtext = '<ul>';
 8959:                 foreach my $type (@email_rule) {
 8960:                     if (ref($emailrules->{$type}) eq 'HASH') {
 8961:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 8962:                     }
 8963:                 }
 8964:                 $chgtext .= '</ul>';
 8965:                 if (@email_rule > 0) {
 8966:                     $resulttext .= '<li>'.
 8967:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 8968:                                        $chgtext.
 8969:                                    '</li>';
 8970:                 } else {
 8971:                     $resulttext .= '<li>'.
 8972:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 8973:                                    '</li>';
 8974:                 }
 8975:             }
 8976:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 8977:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 8978:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8979:                 foreach my $type (@{$changes{'selfcreate'}}) {
 8980:                     my $typename = $type;
 8981:                     if (ref($usertypes) eq 'HASH') {
 8982:                         if ($usertypes->{$type} ne '') {
 8983:                             $typename = $usertypes->{$type};
 8984:                         }
 8985:                     }
 8986:                     my @modifiable;
 8987:                     $resulttext .= '<li>'.
 8988:                                     &mt('Self-creation of account by users with status: [_1]',
 8989:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 8990:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 8991:                     foreach my $field (@fields) {
 8992:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 8993:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 8994:                         }
 8995:                     }
 8996:                     if (@modifiable > 0) {
 8997:                         $resulttext .= join(', ',@modifiable);
 8998:                     } else {
 8999:                         $resulttext .= &mt('none');
 9000:                     }
 9001:                     $resulttext .= '</li>';
 9002:                 }
 9003:                 $resulttext .= '</ul></li>';
 9004:             }
 9005:             $resulttext .= '</ul>';
 9006:         } else {
 9007:             $resulttext = &mt('No changes made to self-creation settings');
 9008:         }
 9009:     } else {
 9010:         $resulttext = '<span class="LC_error">'.
 9011:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9012:     }
 9013:     if ($warningmsg ne '') {
 9014:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9015:     }
 9016:     return $resulttext;
 9017: }
 9018: 
 9019: sub process_captcha {
 9020:     my ($container,$changes,$newsettings,$current) = @_;
 9021:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 9022:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 9023:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 9024:         $newsettings->{'captcha'} = 'original';
 9025:     }
 9026:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 9027:         if ($container eq 'cancreate') {
 9028:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9029:                 push(@{$changes->{'cancreate'}},'captcha');
 9030:             } elsif (!defined($changes->{'cancreate'})) {
 9031:                 $changes->{'cancreate'} = ['captcha'];
 9032:             }
 9033:         } else {
 9034:             $changes->{'captcha'} = 1;
 9035:         }
 9036:     }
 9037:     my ($newpub,$newpriv,$currpub,$currpriv);
 9038:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 9039:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 9040:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 9041:         $newpub =~ s/\W//g;
 9042:         $newpriv =~ s/\W//g;
 9043:         $newsettings->{'recaptchakeys'} = {
 9044:                                              public  => $newpub,
 9045:                                              private => $newpriv,
 9046:                                           };
 9047:     }
 9048:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 9049:         $currpub = $current->{'recaptchakeys'}{'public'};
 9050:         $currpriv = $current->{'recaptchakeys'}{'private'};
 9051:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 9052:             $newsettings->{'recaptchakeys'} = {
 9053:                                                  public  => '',
 9054:                                                  private => '',
 9055:                                               }
 9056:         }
 9057:     }
 9058:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 9059:         if ($container eq 'cancreate') {
 9060:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9061:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 9062:             } elsif (!defined($changes->{'cancreate'})) {
 9063:                 $changes->{'cancreate'} = ['recaptchakeys'];
 9064:             }
 9065:         } else {
 9066:             $changes->{'recaptchakeys'} = 1;
 9067:         }
 9068:     }
 9069:     return;
 9070: }
 9071: 
 9072: sub modify_usermodification {
 9073:     my ($dom,%domconfig) = @_;
 9074:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 9075:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9076:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9077:             if ($key eq 'selfcreate') {
 9078:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 9079:             } else {  
 9080:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 9081:             }
 9082:         }
 9083:     }
 9084:     my @contexts = ('author','course');
 9085:     my %context_title = (
 9086:                            author => 'In author context',
 9087:                            course => 'In course context',
 9088:                         );
 9089:     my @fields = ('lastname','firstname','middlename','generation',
 9090:                   'permanentemail','id');
 9091:     my %roles = (
 9092:                   author => ['ca','aa'],
 9093:                   course => ['st','ep','ta','in','cr'],
 9094:                 );
 9095:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9096:     foreach my $context (@contexts) {
 9097:         foreach my $role (@{$roles{$context}}) {
 9098:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 9099:             foreach my $item (@fields) {
 9100:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 9101:                     $modifyhash{$context}{$role}{$item} = 1;
 9102:                 } else {
 9103:                     $modifyhash{$context}{$role}{$item} = 0;
 9104:                 }
 9105:             }
 9106:         }
 9107:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 9108:             foreach my $role (@{$roles{$context}}) {
 9109:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 9110:                     foreach my $field (@fields) {
 9111:                         if ($modifyhash{$context}{$role}{$field} ne 
 9112:                                 $curr_usermodification{$context}{$role}{$field}) {
 9113:                             push(@{$changes{$context}},$role);
 9114:                             last;
 9115:                         }
 9116:                     }
 9117:                 }
 9118:             }
 9119:         } else {
 9120:             foreach my $context (@contexts) {
 9121:                 foreach my $role (@{$roles{$context}}) {
 9122:                     push(@{$changes{$context}},$role);
 9123:                 }
 9124:             }
 9125:         }
 9126:     }
 9127:     my %usermodification_hash =  (
 9128:                                    usermodification => \%modifyhash,
 9129:                                  );
 9130:     my $putresult = &Apache::lonnet::put_dom('configuration',
 9131:                                              \%usermodification_hash,$dom);
 9132:     if ($putresult eq 'ok') {
 9133:         if (keys(%changes) > 0) {
 9134:             $resulttext = &mt('Changes made: ').'<ul>';
 9135:             foreach my $context (@contexts) {
 9136:                 if (ref($changes{$context}) eq 'ARRAY') {
 9137:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 9138:                     if (ref($changes{$context}) eq 'ARRAY') {
 9139:                         foreach my $role (@{$changes{$context}}) {
 9140:                             my $rolename;
 9141:                             if ($role eq 'cr') {
 9142:                                 $rolename = &mt('Custom');
 9143:                             } else {
 9144:                                 $rolename = &Apache::lonnet::plaintext($role);
 9145:                             }
 9146:                             my @modifiable;
 9147:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 9148:                             foreach my $field (@fields) {
 9149:                                 if ($modifyhash{$context}{$role}{$field}) {
 9150:                                     push(@modifiable,$fieldtitles{$field});
 9151:                                 }
 9152:                             }
 9153:                             if (@modifiable > 0) {
 9154:                                 $resulttext .= join(', ',@modifiable);
 9155:                             } else {
 9156:                                 $resulttext .= &mt('none'); 
 9157:                             }
 9158:                             $resulttext .= '</li>';
 9159:                         }
 9160:                         $resulttext .= '</ul></li>';
 9161:                     }
 9162:                 }
 9163:             }
 9164:             $resulttext .= '</ul>';
 9165:         } else {
 9166:             $resulttext = &mt('No changes made to user modification settings');
 9167:         }
 9168:     } else {
 9169:         $resulttext = '<span class="LC_error">'.
 9170:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9171:     }
 9172:     return $resulttext;
 9173: }
 9174: 
 9175: sub modify_defaults {
 9176:     my ($dom,$lastactref,%domconfig) = @_;
 9177:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 9178:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9179:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 9180:     my @authtypes = ('internal','krb4','krb5','localauth');
 9181:     foreach my $item (@items) {
 9182:         $newvalues{$item} = $env{'form.'.$item};
 9183:         if ($item eq 'auth_def') {
 9184:             if ($newvalues{$item} ne '') {
 9185:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 9186:                     push(@errors,$item);
 9187:                 }
 9188:             }
 9189:         } elsif ($item eq 'lang_def') {
 9190:             if ($newvalues{$item} ne '') {
 9191:                 if ($newvalues{$item} =~ /^(\w+)/) {
 9192:                     my $langcode = $1;
 9193:                     if ($langcode ne 'x_chef') {
 9194:                         if (code2language($langcode) eq '') {
 9195:                             push(@errors,$item);
 9196:                         }
 9197:                     }
 9198:                 } else {
 9199:                     push(@errors,$item);
 9200:                 }
 9201:             }
 9202:         } elsif ($item eq 'timezone_def') {
 9203:             if ($newvalues{$item} ne '') {
 9204:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 9205:                     push(@errors,$item);   
 9206:                 }
 9207:             }
 9208:         } elsif ($item eq 'datelocale_def') {
 9209:             if ($newvalues{$item} ne '') {
 9210:                 my @datelocale_ids = DateTime::Locale->ids();
 9211:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 9212:                     push(@errors,$item);
 9213:                 }
 9214:             }
 9215:         } elsif ($item eq 'portal_def') {
 9216:             if ($newvalues{$item} ne '') {
 9217:                 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])\/?$/) {
 9218:                     push(@errors,$item);
 9219:                 }
 9220:             }
 9221:         }
 9222:         if (grep(/^\Q$item\E$/,@errors)) {
 9223:             $newvalues{$item} = $domdefaults{$item};
 9224:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 9225:             $changes{$item} = 1;
 9226:         }
 9227:         $domdefaults{$item} = $newvalues{$item};
 9228:     }
 9229:     my %defaults_hash = (
 9230:                          defaults => \%newvalues,
 9231:                         );
 9232:     my $title = &defaults_titles();
 9233: 
 9234:     my $currinststatus;
 9235:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9236:         $currinststatus = $domconfig{'inststatus'};
 9237:     } else {
 9238:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9239:         $currinststatus = {
 9240:                              inststatustypes => $usertypes,
 9241:                              inststatusorder => $types,
 9242:                              inststatusguest => [],
 9243:                           };
 9244:     }
 9245:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
 9246:     my @allpos;
 9247:     my %guests;
 9248:     my %alltypes;
 9249:     my ($currtitles,$currguests,$currorder);
 9250:     if (ref($currinststatus) eq 'HASH') {
 9251:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
 9252:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
 9253:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
 9254:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
 9255:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
 9256:                     }
 9257:                 }
 9258:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
 9259:                     my $position = $env{'form.inststatus_pos_'.$type};
 9260:                     $position =~ s/\D+//g;
 9261:                     $allpos[$position] = $type;
 9262:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
 9263:                     $alltypes{$type} =~ s/`//g;
 9264:                     if ($env{'form.inststatus_guest_'.$type}) {
 9265:                         $guests{$type} = 1;
 9266:                     }
 9267:                 }
 9268:             }
 9269:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
 9270:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
 9271:             }
 9272:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
 9273:             $currtitles =~ s/,$//;
 9274:         }
 9275:     }
 9276:     if ($env{'form.addinststatus'}) {
 9277:         my $newtype = $env{'form.addinststatus'};
 9278:         $newtype =~ s/\W//g;
 9279:         unless (exists($alltypes{$newtype})) {
 9280:             if ($env{'form.addinststatus_guest'}) {
 9281:                 $guests{$newtype} = 1;
 9282:             }
 9283:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
 9284:             $alltypes{$newtype} =~ s/`//g; 
 9285:             my $position = $env{'form.addinststatus_pos'};
 9286:             $position =~ s/\D+//g;
 9287:             if ($position ne '') {
 9288:                 $allpos[$position] = $newtype;
 9289:             }
 9290:         }
 9291:     }
 9292:     my (@orderedstatus,@orderedguests);
 9293:     foreach my $type (@allpos) {
 9294:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
 9295:             push(@orderedstatus,$type);
 9296:             if ($guests{$type}) {
 9297:                 push(@orderedguests,$type);
 9298:             }
 9299:         }
 9300:     }
 9301:     foreach my $type (keys(%alltypes)) {
 9302:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
 9303:             delete($alltypes{$type});
 9304:         }
 9305:     }
 9306:     $defaults_hash{'inststatus'} = {
 9307:                                      inststatustypes => \%alltypes,
 9308:                                      inststatusorder => \@orderedstatus,
 9309:                                      inststatusguest => \@orderedguests,
 9310:                                    };
 9311:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
 9312:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 9313:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
 9314:         }
 9315:     }
 9316:     if ($currorder ne join(',',@orderedstatus)) {
 9317:         $changes{'inststatus'}{'inststatusorder'} = 1;
 9318:     }
 9319:     if ($currguests ne join(',',@orderedguests)) {
 9320:         $changes{'inststatus'}{'inststatusguest'} = 1;
 9321:     }
 9322:     my $newtitles;
 9323:     foreach my $item (@orderedstatus) {
 9324:         $newtitles .= $alltypes{$item}.',';
 9325:     }
 9326:     $newtitles =~ s/,$//;
 9327:     if ($currtitles ne $newtitles) {
 9328:         $changes{'inststatus'}{'inststatustypes'} = 1;
 9329:     }
 9330:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 9331:                                              $dom);
 9332:     if ($putresult eq 'ok') {
 9333:         if (keys(%changes) > 0) {
 9334:             $resulttext = &mt('Changes made:').'<ul>';
 9335:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 9336:             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";
 9337:             foreach my $item (sort(keys(%changes))) {
 9338:                 if ($item eq 'inststatus') {
 9339:                     if (ref($changes{'inststatus'}) eq 'HASH') {
 9340:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
 9341:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
 9342:                             foreach my $type (@orderedstatus) { 
 9343:                                 $resulttext .= $alltypes{$type}.', ';
 9344:                             }
 9345:                             $resulttext =~ s/, $//;
 9346:                             $resulttext .= '</li>';
 9347:                         }
 9348:                         if ($changes{'inststatus'}{'inststatusguest'}) {
 9349:                             $resulttext .= '<li>'; 
 9350:                             if (@orderedguests) {
 9351:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
 9352:                                 foreach my $type (@orderedguests) {
 9353:                                     $resulttext .= $alltypes{$type}.', ';
 9354:                                 }
 9355:                                 $resulttext =~ s/, $//;
 9356:                             } else {
 9357:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
 9358:                             }
 9359:                             $resulttext .= '</li>';
 9360:                         }
 9361:                     }
 9362:                 } else {
 9363:                     my $value = $env{'form.'.$item};
 9364:                     if ($value eq '') {
 9365:                         $value = &mt('none');
 9366:                     } elsif ($item eq 'auth_def') {
 9367:                         my %authnames = &authtype_names();
 9368:                         my %shortauth = (
 9369:                                           internal   => 'int',
 9370:                                           krb4       => 'krb4',
 9371:                                           krb5       => 'krb5',
 9372:                                           localauth  => 'loc',
 9373:                         );
 9374:                         $value = $authnames{$shortauth{$value}};
 9375:                     }
 9376:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 9377:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
 9378:                 }
 9379:             }
 9380:             $resulttext .= '</ul>';
 9381:             $mailmsgtext .= "\n";
 9382:             my $cachetime = 24*60*60;
 9383:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9384:             if (ref($lastactref) eq 'HASH') {
 9385:                 $lastactref->{'domdefaults'} = 1;
 9386:             }
 9387:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 9388:                 my $notify = 1;
 9389:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 9390:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 9391:                         $notify = 0;
 9392:                     }
 9393:                 }
 9394:                 if ($notify) {
 9395:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 9396:                                                "LON-CAPA Domain Settings Change - $dom",
 9397:                                                $mailmsgtext);
 9398:                 }
 9399:             }
 9400:         } else {
 9401:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 9402:         }
 9403:     } else {
 9404:         $resulttext = '<span class="LC_error">'.
 9405:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9406:     }
 9407:     if (@errors > 0) {
 9408:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 9409:         foreach my $item (@errors) {
 9410:             $resulttext .= ' "'.$title->{$item}.'",';
 9411:         }
 9412:         $resulttext =~ s/,$//;
 9413:     }
 9414:     return $resulttext;
 9415: }
 9416: 
 9417: sub modify_scantron {
 9418:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9419:     my ($resulttext,%confhash,%changes,$errors);
 9420:     my $custom = 'custom.tab';
 9421:     my $default = 'default.tab';
 9422:     my $servadm = $r->dir_config('lonAdmEMail');
 9423:     my ($configuserok,$author_ok,$switchserver) = 
 9424:         &config_check($dom,$confname,$servadm);
 9425:     if ($env{'form.scantronformat.filename'} ne '') {
 9426:         my $error;
 9427:         if ($configuserok eq 'ok') {
 9428:             if ($switchserver) {
 9429:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 9430:             } else {
 9431:                 if ($author_ok eq 'ok') {
 9432:                     my ($result,$scantronurl) =
 9433:                         &publishlogo($r,'upload','scantronformat',$dom,
 9434:                                      $confname,'scantron','','',$custom);
 9435:                     if ($result eq 'ok') {
 9436:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 9437:                         $changes{'scantronformat'} = 1;
 9438:                     } else {
 9439:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 9440:                     }
 9441:                 } else {
 9442:                     $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);
 9443:                 }
 9444:             }
 9445:         } else {
 9446:             $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);
 9447:         }
 9448:         if ($error) {
 9449:             &Apache::lonnet::logthis($error);
 9450:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9451:         }
 9452:     }
 9453:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 9454:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 9455:             if ($env{'form.scantronformat_del'}) {
 9456:                 $confhash{'scantron'}{'scantronformat'} = '';
 9457:                 $changes{'scantronformat'} = 1;
 9458:             }
 9459:         }
 9460:     }
 9461:     if (keys(%confhash) > 0) {
 9462:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 9463:                                                  $dom);
 9464:         if ($putresult eq 'ok') {
 9465:             if (keys(%changes) > 0) {
 9466:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 9467:                     $resulttext = &mt('Changes made:').'<ul>';
 9468:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 9469:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 9470:                     } else {
 9471:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 9472:                     }
 9473:                     $resulttext .= '</ul>';
 9474:                 } else {
 9475:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 9476:                 }
 9477:                 $resulttext .= '</ul>';
 9478:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9479:                 if (ref($lastactref) eq 'HASH') {
 9480:                     $lastactref->{'domainconfig'} = 1;
 9481:                 }
 9482:             } else {
 9483:                 $resulttext = &mt('No changes made to bubblesheet format file');
 9484:             }
 9485:         } else {
 9486:             $resulttext = '<span class="LC_error">'.
 9487:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 9488:         }
 9489:     } else {
 9490:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 9491:     }
 9492:     if ($errors) {
 9493:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9494:                        $errors.'</ul>';
 9495:     }
 9496:     return $resulttext;
 9497: }
 9498: 
 9499: sub modify_coursecategories {
 9500:     my ($dom,$lastactref,%domconfig) = @_;
 9501:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 9502:         $cathash);
 9503:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 9504:     my @catitems = ('unauth','auth');
 9505:     my @cattypes = ('std','domonly','codesrch','none');
 9506:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9507:         $cathash = $domconfig{'coursecategories'}{'cats'};
 9508:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 9509:             $changes{'togglecats'} = 1;
 9510:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 9511:         }
 9512:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 9513:             $changes{'categorize'} = 1;
 9514:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 9515:         }
 9516:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 9517:             $changes{'togglecatscomm'} = 1;
 9518:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 9519:         }
 9520:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 9521:             $changes{'categorizecomm'} = 1;
 9522:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 9523:         }
 9524:         foreach my $item (@catitems) {
 9525:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9526:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
 9527:                     $changes{$item} = 1;
 9528:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9529:                 }
 9530:             }
 9531:         }
 9532:     } else {
 9533:         $changes{'togglecats'} = 1;
 9534:         $changes{'categorize'} = 1;
 9535:         $changes{'togglecatscomm'} = 1;
 9536:         $changes{'categorizecomm'} = 1;
 9537:         $domconfig{'coursecategories'} = {
 9538:                                              togglecats => $env{'form.togglecats'},
 9539:                                              categorize => $env{'form.categorize'},
 9540:                                              togglecatscomm => $env{'form.togglecatscomm'},
 9541:                                              categorizecomm => $env{'form.categorizecomm'},
 9542:                                          };
 9543:         foreach my $item (@catitems) {
 9544:             if ($env{'form.coursecat_'.$item} ne 'std') {
 9545:                 $changes{$item} = 1;
 9546:             }
 9547:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9548:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9549:             }
 9550:         }
 9551:     }
 9552:     if (ref($cathash) eq 'HASH') {
 9553:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 9554:             push (@deletecategory,'instcode::0');
 9555:         }
 9556:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 9557:             push(@deletecategory,'communities::0');
 9558:         }
 9559:     }
 9560:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 9561:     if (ref($cathash) eq 'HASH') {
 9562:         if (@deletecategory > 0) {
 9563:             #FIXME Need to remove category from all courses using a deleted category 
 9564:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 9565:             foreach my $item (@deletecategory) {
 9566:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 9567:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 9568:                     $deletions{$item} = 1;
 9569:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 9570:                 }
 9571:             }
 9572:         }
 9573:         foreach my $item (keys(%{$cathash})) {
 9574:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9575:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 9576:                 $reorderings{$item} = 1;
 9577:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 9578:             }
 9579:             if ($env{'form.addcategory_name_'.$item} ne '') {
 9580:                 my $newcat = $env{'form.addcategory_name_'.$item};
 9581:                 my $newdepth = $depth+1;
 9582:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9583:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 9584:                 $adds{$newitem} = 1; 
 9585:             }
 9586:             if ($env{'form.subcat_'.$item} ne '') {
 9587:                 my $newcat = $env{'form.subcat_'.$item};
 9588:                 my $newdepth = $depth+1;
 9589:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9590:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 9591:                 $adds{$newitem} = 1;
 9592:             }
 9593:         }
 9594:     }
 9595:     if ($env{'form.instcode'} eq '1') {
 9596:         if (ref($cathash) eq 'HASH') {
 9597:             my $newitem = 'instcode::0';
 9598:             if ($cathash->{$newitem} eq '') {  
 9599:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9600:                 $adds{$newitem} = 1;
 9601:             }
 9602:         } else {
 9603:             my $newitem = 'instcode::0';
 9604:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9605:             $adds{$newitem} = 1;
 9606:         }
 9607:     }
 9608:     if ($env{'form.communities'} eq '1') {
 9609:         if (ref($cathash) eq 'HASH') {
 9610:             my $newitem = 'communities::0';
 9611:             if ($cathash->{$newitem} eq '') {
 9612:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9613:                 $adds{$newitem} = 1;
 9614:             }
 9615:         } else {
 9616:             my $newitem = 'communities::0';
 9617:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9618:             $adds{$newitem} = 1;
 9619:         }
 9620:     }
 9621:     if ($env{'form.addcategory_name'} ne '') {
 9622:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 9623:             ($env{'form.addcategory_name'} ne 'communities')) {
 9624:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 9625:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 9626:             $adds{$newitem} = 1;
 9627:         }
 9628:     }
 9629:     my $putresult;
 9630:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9631:         if (keys(%deletions) > 0) {
 9632:             foreach my $key (keys(%deletions)) {
 9633:                 if ($predelallitems{$key} ne '') {
 9634:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 9635:                 }
 9636:             }
 9637:         }
 9638:         my (@chkcats,@chktrails,%chkallitems);
 9639:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 9640:         if (ref($chkcats[0]) eq 'ARRAY') {
 9641:             my $depth = 0;
 9642:             my $chg = 0;
 9643:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 9644:                 my $name = $chkcats[0][$i];
 9645:                 my $item;
 9646:                 if ($name eq '') {
 9647:                     $chg ++;
 9648:                 } else {
 9649:                     $item = &escape($name).'::0';
 9650:                     if ($chg) {
 9651:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 9652:                     }
 9653:                     $depth ++; 
 9654:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 9655:                     $depth --;
 9656:                 }
 9657:             }
 9658:         }
 9659:     }
 9660:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9661:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 9662:         if ($putresult eq 'ok') {
 9663:             my %title = (
 9664:                          togglecats     => 'Show/Hide a course in catalog',
 9665:                          categorize     => 'Assign a category to a course',
 9666:                          togglecatscomm => 'Show/Hide a community in catalog',
 9667:                          categorizecomm => 'Assign a category to a community',
 9668:                         );
 9669:             my %level = (
 9670:                          dom  => 'set in Domain ("Modify Course/Community")',
 9671:                          crs  => 'set in Course ("Course Configuration")',
 9672:                          comm => 'set in Community ("Community Configuration")',
 9673:                          none     => 'No catalog',
 9674:                          std      => 'Standard catalog',
 9675:                          domonly  => 'Domain-only catalog',
 9676:                          codesrch => 'Code search form',
 9677:                         );
 9678:             $resulttext = &mt('Changes made:').'<ul>';
 9679:             if ($changes{'togglecats'}) {
 9680:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 9681:             }
 9682:             if ($changes{'categorize'}) {
 9683:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 9684:             }
 9685:             if ($changes{'togglecatscomm'}) {
 9686:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 9687:             }
 9688:             if ($changes{'categorizecomm'}) {
 9689:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 9690:             }
 9691:             if ($changes{'unauth'}) {
 9692:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
 9693:             }
 9694:             if ($changes{'auth'}) {
 9695:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
 9696:             }
 9697:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9698:                 my $cathash;
 9699:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9700:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 9701:                 } else {
 9702:                     $cathash = {};
 9703:                 } 
 9704:                 my (@cats,@trails,%allitems);
 9705:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 9706:                 if (keys(%deletions) > 0) {
 9707:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 9708:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 9709:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 9710:                     }
 9711:                     $resulttext .= '</ul></li>';
 9712:                 }
 9713:                 if (keys(%reorderings) > 0) {
 9714:                     my %sort_by_trail;
 9715:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 9716:                     foreach my $key (keys(%reorderings)) {
 9717:                         if ($allitems{$key} ne '') {
 9718:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9719:                         }
 9720:                     }
 9721:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9722:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9723:                     }
 9724:                     $resulttext .= '</ul></li>';
 9725:                 }
 9726:                 if (keys(%adds) > 0) {
 9727:                     my %sort_by_trail;
 9728:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 9729:                     foreach my $key (keys(%adds)) {
 9730:                         if ($allitems{$key} ne '') {
 9731:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9732:                         }
 9733:                     }
 9734:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9735:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9736:                     }
 9737:                     $resulttext .= '</ul></li>';
 9738:                 }
 9739:             }
 9740:             $resulttext .= '</ul>';
 9741:             if ($changes{'unauth'} || $changes{'auth'}) {
 9742:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9743:                 if (ref($lastactref) eq 'HASH') {
 9744:                     $lastactref->{'domainconfig'} = 1;
 9745:                 }
 9746:             }
 9747:         } else {
 9748:             $resulttext = '<span class="LC_error">'.
 9749:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9750:         }
 9751:     } else {
 9752:         $resulttext = &mt('No changes made to course and community categories');
 9753:     }
 9754:     return $resulttext;
 9755: }
 9756: 
 9757: sub modify_serverstatuses {
 9758:     my ($dom,%domconfig) = @_;
 9759:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 9760:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 9761:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 9762:     }
 9763:     my @pages = &serverstatus_pages();
 9764:     foreach my $type (@pages) {
 9765:         $newserverstatus{$type}{'namedusers'} = '';
 9766:         $newserverstatus{$type}{'machines'} = '';
 9767:         if (defined($env{'form.'.$type.'_namedusers'})) {
 9768:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 9769:             my @okusers;
 9770:             foreach my $user (@users) {
 9771:                 my ($uname,$udom) = split(/:/,$user);
 9772:                 if (($udom =~ /^$match_domain$/) &&   
 9773:                     (&Apache::lonnet::domain($udom)) &&
 9774:                     ($uname =~ /^$match_username$/)) {
 9775:                     if (!grep(/^\Q$user\E/,@okusers)) {
 9776:                         push(@okusers,$user);
 9777:                     }
 9778:                 }
 9779:             }
 9780:             if (@okusers > 0) {
 9781:                  @okusers = sort(@okusers);
 9782:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 9783:             }
 9784:         }
 9785:         if (defined($env{'form.'.$type.'_machines'})) {
 9786:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 9787:             my @okmachines;
 9788:             foreach my $ip (@machines) {
 9789:                 my @parts = split(/\./,$ip);
 9790:                 next if (@parts < 4);
 9791:                 my $badip = 0;
 9792:                 for (my $i=0; $i<4; $i++) {
 9793:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 9794:                         $badip = 1;
 9795:                         last;
 9796:                     }
 9797:                 }
 9798:                 if (!$badip) {
 9799:                     push(@okmachines,$ip);     
 9800:                 }
 9801:             }
 9802:             @okmachines = sort(@okmachines);
 9803:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 9804:         }
 9805:     }
 9806:     my %serverstatushash =  (
 9807:                                 serverstatuses => \%newserverstatus,
 9808:                             );
 9809:     foreach my $type (@pages) {
 9810:         foreach my $setting ('namedusers','machines') {
 9811:             my (@current,@new);
 9812:             if (ref($currserverstatus{$type}) eq 'HASH') {
 9813:                 if ($currserverstatus{$type}{$setting} ne '') { 
 9814:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 9815:                 }
 9816:             }
 9817:             if ($newserverstatus{$type}{$setting} ne '') {
 9818:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 9819:             }
 9820:             if (@current > 0) {
 9821:                 if (@new > 0) {
 9822:                     foreach my $item (@current) {
 9823:                         if (!grep(/^\Q$item\E$/,@new)) {
 9824:                             $changes{$type}{$setting} = 1;
 9825:                             last;
 9826:                         }
 9827:                     }
 9828:                     foreach my $item (@new) {
 9829:                         if (!grep(/^\Q$item\E$/,@current)) {
 9830:                             $changes{$type}{$setting} = 1;
 9831:                             last;
 9832:                         }
 9833:                     }
 9834:                 } else {
 9835:                     $changes{$type}{$setting} = 1;
 9836:                 }
 9837:             } elsif (@new > 0) {
 9838:                 $changes{$type}{$setting} = 1;
 9839:             }
 9840:         }
 9841:     }
 9842:     if (keys(%changes) > 0) {
 9843:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9844:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9845:                                                  \%serverstatushash,$dom);
 9846:         if ($putresult eq 'ok') {
 9847:             $resulttext .= &mt('Changes made:').'<ul>';
 9848:             foreach my $type (@pages) {
 9849:                 if (ref($changes{$type}) eq 'HASH') {
 9850:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 9851:                     if ($changes{$type}{'namedusers'}) {
 9852:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 9853:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 9854:                         } else {
 9855:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 9856:                         }
 9857:                     }
 9858:                     if ($changes{$type}{'machines'}) {
 9859:                         if ($newserverstatus{$type}{'machines'} eq '') {
 9860:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 9861:                         } else {
 9862:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 9863:                         }
 9864: 
 9865:                     }
 9866:                     $resulttext .= '</ul></li>';
 9867:                 }
 9868:             }
 9869:             $resulttext .= '</ul>';
 9870:         } else {
 9871:             $resulttext = '<span class="LC_error">'.
 9872:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 9873: 
 9874:         }
 9875:     } else {
 9876:         $resulttext = &mt('No changes made to access to server status pages');
 9877:     }
 9878:     return $resulttext;
 9879: }
 9880: 
 9881: sub modify_helpsettings {
 9882:     my ($r,$dom,$confname,%domconfig) = @_;
 9883:     my ($resulttext,$errors,%changes,%helphash);
 9884:     my %defaultchecked = ('submitbugs' => 'on');
 9885:     my @offon = ('off','on');
 9886:     my @toggles = ('submitbugs');
 9887:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 9888:         foreach my $item (@toggles) {
 9889:             if ($defaultchecked{$item} eq 'on') { 
 9890:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9891:                     if ($env{'form.'.$item} eq '0') {
 9892:                         $changes{$item} = 1;
 9893:                     }
 9894:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9895:                     $changes{$item} = 1;
 9896:                 }
 9897:             } elsif ($defaultchecked{$item} eq 'off') {
 9898:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9899:                     if ($env{'form.'.$item} eq '1') {
 9900:                         $changes{$item} = 1;
 9901:                     }
 9902:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9903:                     $changes{$item} = 1;
 9904:                 }
 9905:             }
 9906:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 9907:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 9908:             }
 9909:         }
 9910:     }
 9911:     my $putresult;
 9912:     if (keys(%changes) > 0) {
 9913:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 9914:         if ($putresult eq 'ok') {
 9915:             $resulttext = &mt('Changes made:').'<ul>';
 9916:             foreach my $item (sort(keys(%changes))) {
 9917:                 if ($item eq 'submitbugs') {
 9918:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 9919:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 9920:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 9921:                 }
 9922:             }
 9923:             $resulttext .= '</ul>';
 9924:         } else {
 9925:             $resulttext = &mt('No changes made to help settings');
 9926:             $errors .= '<li><span class="LC_error">'.
 9927:                        &mt('An error occurred storing the settings: [_1]',
 9928:                            $putresult).'</span></li>';
 9929:         }
 9930:     }
 9931:     if ($errors) {
 9932:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9933:                        $errors.'</ul>';
 9934:     }
 9935:     return $resulttext;
 9936: }
 9937: 
 9938: sub modify_coursedefaults {
 9939:     my ($dom,$lastactref,%domconfig) = @_;
 9940:     my ($resulttext,$errors,%changes,%defaultshash);
 9941:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 9942:     my @toggles = ('canuse_pdfforms');
 9943:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 9944:                    'uploadquota_community','uploadquota_textbook');
 9945:     my @types = ('official','unofficial','community','textbook');
 9946:     my %staticdefaults = (
 9947:                            anonsurvey_threshold => 10,
 9948:                            uploadquota          => 500,
 9949:                          );
 9950: 
 9951:     $defaultshash{'coursedefaults'} = {};
 9952: 
 9953:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 9954:         if ($domconfig{'coursedefaults'} eq '') {
 9955:             $domconfig{'coursedefaults'} = {};
 9956:         }
 9957:     }
 9958: 
 9959:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 9960:         foreach my $item (@toggles) {
 9961:             if ($defaultchecked{$item} eq 'on') {
 9962:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9963:                     ($env{'form.'.$item} eq '0')) {
 9964:                     $changes{$item} = 1;
 9965:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9966:                     $changes{$item} = 1;
 9967:                 }
 9968:             } elsif ($defaultchecked{$item} eq 'off') {
 9969:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9970:                     ($env{'form.'.$item} eq '1')) {
 9971:                     $changes{$item} = 1;
 9972:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9973:                     $changes{$item} = 1;
 9974:                 }
 9975:             }
 9976:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 9977:         }
 9978:         foreach my $item (@numbers) {
 9979:             my ($currdef,$newdef);
 9980:             $newdef = $env{'form.'.$item};
 9981:             if ($item eq 'anonsurvey_threshold') {
 9982:                 $currdef = $domconfig{'coursedefaults'}{$item};
 9983:                 $newdef =~ s/\D//g;
 9984:                 if ($newdef eq '' || $newdef < 1) {
 9985:                     $newdef = 1;
 9986:                 }
 9987:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 9988:             } else {
 9989:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 9990:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9991:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 9992:                 }
 9993:                 $newdef =~ s/[^\w.\-]//g;
 9994:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 9995:             }
 9996:             if ($currdef ne $newdef) {
 9997:                 my $staticdef;
 9998:                 if ($item eq 'anonsurvey_threshold') {
 9999:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
10000:                         $changes{$item} = 1;
10001:                     }
10002:                 } else {
10003:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
10004:                         $changes{'uploadquota'} = 1;
10005:                     }
10006:                 }
10007:             }
10008:         }
10009: 
10010:         my $officialcreds = $env{'form.official_credits'};
10011:         $officialcreds =~ s/[^\d.]+//g;
10012:         my $unofficialcreds = $env{'form.unofficial_credits'};
10013:         $unofficialcreds =~ s/[^\d.]+//g;
10014:         my $textbookcreds = $env{'form.textbook_credits'};
10015:         $textbookcreds =~ s/[^\d.]+//g;
10016:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
10017:                 ($env{'form.coursecredits'} eq '1')) {
10018:                 $changes{'coursecredits'} = 1;
10019:         } else {
10020:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
10021:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
10022:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
10023:                 $changes{'coursecredits'} = 1;
10024:             }
10025:         }
10026:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
10027:             official   => $officialcreds,
10028:             unofficial => $unofficialcreds,
10029:             textbook   => $textbookcreds,
10030:         }
10031:     }
10032:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10033:                                              $dom);
10034:     if ($putresult eq 'ok') {
10035:         if (keys(%changes) > 0) {
10036:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10037:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || 
10038:                 ($changes{'uploadquota'})) { 
10039:                 if ($changes{'canuse_pdfforms'}) {
10040:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
10041:                 }
10042:                 if ($changes{'coursecredits'}) {
10043:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10044:                         $domdefaults{'officialcredits'} =
10045:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
10046:                         $domdefaults{'unofficialcredits'} =
10047:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
10048:                         $domdefaults{'textbookcredits'} =
10049:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
10050:                     }
10051:                 }
10052:                 if ($changes{'uploadquota'}) {
10053:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10054:                         foreach my $type (@types) {
10055:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
10056:                         }
10057:                     }
10058:                 }
10059:                 my $cachetime = 24*60*60;
10060:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10061:                 if (ref($lastactref) eq 'HASH') {
10062:                     $lastactref->{'domdefaults'} = 1;
10063:                 }
10064:             }
10065:             $resulttext = &mt('Changes made:').'<ul>';
10066:             foreach my $item (sort(keys(%changes))) {
10067:                 if ($item eq 'canuse_pdfforms') {
10068:                     if ($env{'form.'.$item} eq '1') {
10069:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
10070:                     } else {
10071:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
10072:                     }
10073:                 } elsif ($item eq 'anonsurvey_threshold') {
10074:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
10075:                 } elsif ($item eq 'uploadquota') {
10076:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10077:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
10078:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
10079:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
10080:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
10081: 
10082:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
10083:                                        '</ul>'.
10084:                                        '</li>';
10085:                     } else {
10086:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
10087:                     }
10088:                 } elsif ($item eq 'coursecredits') {
10089:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10090:                         if (($domdefaults{'officialcredits'} eq '') &&
10091:                             ($domdefaults{'unofficialcredits'} eq '') &&
10092:                             ($domdefaults{'textbookcredits'} eq '')) {
10093:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10094:                         } else {
10095:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
10096:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
10097:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
10098:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
10099:                                            '</ul>'.
10100:                                            '</li>';
10101:                         }
10102:                     } else {
10103:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10104:                     }
10105:                 }
10106:             }
10107:             $resulttext .= '</ul>';
10108:         } else {
10109:             $resulttext = &mt('No changes made to course defaults');
10110:         }
10111:     } else {
10112:         $resulttext = '<span class="LC_error">'.
10113:             &mt('An error occurred: [_1]',$putresult).'</span>';
10114:     }
10115:     return $resulttext;
10116: }
10117: 
10118: sub modify_selfenrollment {
10119:     my ($dom,$lastactref,%domconfig) = @_;
10120:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
10121:     my @types = ('official','unofficial','community','textbook');
10122:     my %titles = &tool_titles();
10123:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
10124:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
10125:     $ordered{'default'} = ['types','registered','approval','limit'];
10126: 
10127:     my (%roles,%shown,%toplevel);
10128:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
10129: 
10130:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
10131:         if ($domconfig{'selfenrollment'} eq '') {
10132:             $domconfig{'selfenrollment'} = {};
10133:         }
10134:     }
10135:     %toplevel = (
10136:                   admin      => 'Configuration Rights',
10137:                   default    => 'Default settings',
10138:                   validation => 'Validation of self-enrollment requests',
10139:                 );
10140:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
10141: 
10142:     if (ref($ordered{'admin'}) eq 'ARRAY') {
10143:         foreach my $item (@{$ordered{'admin'}}) {
10144:             foreach my $type (@types) {
10145:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
10146:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
10147:                 } else {
10148:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
10149:                 }
10150:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
10151:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
10152:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
10153:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
10154:                             push(@{$changes{'admin'}{$type}},$item);
10155:                         }
10156:                     } else {
10157:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
10158:                             push(@{$changes{'admin'}{$type}},$item);
10159:                         }
10160:                     }
10161:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
10162:                     push(@{$changes{'admin'}{$type}},$item);
10163:                 }
10164:             }
10165:         }
10166:     }
10167: 
10168:     foreach my $item (@{$ordered{'default'}}) {
10169:         foreach my $type (@types) {
10170:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
10171:             if ($item eq 'types') {
10172:                 unless (($value eq 'all') || ($value eq 'dom')) {
10173:                     $value = '';
10174:                 }
10175:             } elsif ($item eq 'registered') {
10176:                 unless ($value eq '1') {
10177:                     $value = 0;
10178:                 }
10179:             } elsif ($item eq 'approval') {
10180:                 unless ($value =~ /^[012]$/) {
10181:                     $value = 0;
10182:                 }
10183:             } else {
10184:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10185:                     $value = 'none';
10186:                 }
10187:             }
10188:             $selfenrollhash{'default'}{$type}{$item} = $value;
10189:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
10190:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10191:                     if ($selfenrollhash{'default'}{$type}{$item} ne
10192:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
10193:                          push(@{$changes{'default'}{$type}},$item);
10194:                     }
10195:                 } else {
10196:                     push(@{$changes{'default'}{$type}},$item);
10197:                 }
10198:             } else {
10199:                 push(@{$changes{'default'}{$type}},$item);
10200:             }
10201:             if ($item eq 'limit') {
10202:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10203:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
10204:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
10205:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
10206:                     }
10207:                 } else {
10208:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
10209:                 }
10210:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10211:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
10212:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
10213:                          push(@{$changes{'default'}{$type}},'cap');
10214:                     }
10215:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
10216:                     push(@{$changes{'default'}{$type}},'cap');
10217:                 }
10218:             }
10219:         }
10220:     }
10221: 
10222:     foreach my $item (@{$itemsref}) {
10223:         if ($item eq 'fields') {
10224:             my @changed;
10225:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
10226:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
10227:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
10228:             }
10229:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10230:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
10231:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
10232:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
10233:                 } else {
10234:                     @changed = @{$selfenrollhash{'validation'}{$item}};
10235:                 }
10236:             } else {
10237:                 @changed = @{$selfenrollhash{'validation'}{$item}};
10238:             }
10239:             if (@changed) {
10240:                 if ($selfenrollhash{'validation'}{$item}) { 
10241:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
10242:                 } else {
10243:                     $changes{'validation'}{$item} = &mt('None');
10244:                 }
10245:             }
10246:         } else {
10247:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
10248:             if ($item eq 'markup') {
10249:                if ($env{'form.selfenroll_validation_'.$item}) {
10250:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10251:                }
10252:             }
10253:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10254:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
10255:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
10256:                 }
10257:             }
10258:         }
10259:     }
10260: 
10261:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
10262:                                              $dom);
10263:     if ($putresult eq 'ok') {
10264:         if (keys(%changes) > 0) {
10265:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10266:             $resulttext = &mt('Changes made:').'<ul>';
10267:             foreach my $key ('admin','default','validation') {
10268:                 if (ref($changes{$key}) eq 'HASH') {
10269:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
10270:                     if ($key eq 'validation') {
10271:                         foreach my $item (@{$itemsref}) {
10272:                             if (exists($changes{$key}{$item})) {
10273:                                 if ($item eq 'markup') {
10274:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10275:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
10276:                                 } else {  
10277:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10278:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
10279:                                 }
10280:                             }
10281:                         }
10282:                     } else {
10283:                         foreach my $type (@types) {
10284:                             if ($type eq 'community') {
10285:                                 $roles{'1'} = &mt('Community personnel');
10286:                             } else {
10287:                                 $roles{'1'} = &mt('Course personnel');
10288:                             }
10289:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
10290:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
10291:                                     if ($key eq 'admin') {
10292:                                         my @mgrdc = ();
10293:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10294:                                             foreach my $item (@{$ordered{'admin'}}) {
10295:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
10296:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
10297:                                                         push(@mgrdc,$item);
10298:                                                     }
10299:                                                 }
10300:                                             }
10301:                                             if (@mgrdc) {
10302:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
10303:                                             } else {
10304:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
10305:                                             }
10306:                                         }
10307:                                     } else {
10308:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10309:                                             foreach my $item (@{$ordered{$key}}) {
10310:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10311:                                                     $domdefaults{$type.'selfenroll'.$item} =
10312:                                                         $selfenrollhash{$key}{$type}{$item};
10313:                                                 }
10314:                                             }
10315:                                         }
10316:                                     }
10317:                                 }
10318:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
10319:                                 foreach my $item (@{$ordered{$key}}) {
10320:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10321:                                         $resulttext .= '<li>';
10322:                                         if ($key eq 'admin') {
10323:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
10324:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
10325:                                         } else {
10326:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
10327:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
10328:                                         }
10329:                                         $resulttext .= '</li>';
10330:                                     }
10331:                                 }
10332:                                 $resulttext .= '</ul></li>';
10333:                             }
10334:                         }
10335:                         $resulttext .= '</ul></li>'; 
10336:                     }
10337:                 }
10338:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
10339:                     my $cachetime = 24*60*60;
10340:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10341:                     if (ref($lastactref) eq 'HASH') {
10342:                         $lastactref->{'domdefaults'} = 1;
10343:                     }
10344:                 }
10345:             }
10346:             $resulttext .= '</ul>';
10347:         } else {
10348:             $resulttext = &mt('No changes made to self-enrollment settings');
10349:         }
10350:     } else {
10351:         $resulttext = '<span class="LC_error">'.
10352:             &mt('An error occurred: [_1]',$putresult).'</span>';
10353:     }
10354:     return $resulttext;
10355: }
10356: 
10357: sub modify_usersessions {
10358:     my ($dom,$lastactref,%domconfig) = @_;
10359:     my @hostingtypes = ('version','excludedomain','includedomain');
10360:     my @offloadtypes = ('primary','default');
10361:     my %types = (
10362:                   remote => \@hostingtypes,
10363:                   hosted => \@hostingtypes,
10364:                   spares => \@offloadtypes,
10365:                 );
10366:     my @prefixes = ('remote','hosted','spares');
10367:     my @lcversions = &Apache::lonnet::all_loncaparevs();
10368:     my (%by_ip,%by_location,@intdoms);
10369:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
10370:     my @locations = sort(keys(%by_location));
10371:     my (%defaultshash,%changes);
10372:     foreach my $prefix (@prefixes) {
10373:         $defaultshash{'usersessions'}{$prefix} = {};
10374:     }
10375:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10376:     my $resulttext;
10377:     my %iphost = &Apache::lonnet::get_iphost();
10378:     foreach my $prefix (@prefixes) {
10379:         next if ($prefix eq 'spares');
10380:         foreach my $type (@{$types{$prefix}}) {
10381:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
10382:             if ($type eq 'version') {
10383:                 my $value = $env{'form.'.$prefix.'_'.$type};
10384:                 my $okvalue;
10385:                 if ($value ne '') {
10386:                     if (grep(/^\Q$value\E$/,@lcversions)) {
10387:                         $okvalue = $value;
10388:                     }
10389:                 }
10390:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10391:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10392:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
10393:                             if ($inuse == 0) {
10394:                                 $changes{$prefix}{$type} = 1;
10395:                             } else {
10396:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
10397:                                     $changes{$prefix}{$type} = 1;
10398:                                 }
10399:                                 if ($okvalue ne '') {
10400:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10401:                                 } 
10402:                             }
10403:                         } else {
10404:                             if (($inuse == 1) && ($okvalue ne '')) {
10405:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10406:                                 $changes{$prefix}{$type} = 1;
10407:                             }
10408:                         }
10409:                     } else {
10410:                         if (($inuse == 1) && ($okvalue ne '')) {
10411:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10412:                             $changes{$prefix}{$type} = 1;
10413:                         }
10414:                     }
10415:                 } else {
10416:                     if (($inuse == 1) && ($okvalue ne '')) {
10417:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10418:                         $changes{$prefix}{$type} = 1;
10419:                     }
10420:                 }
10421:             } else {
10422:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
10423:                 my @okvals;
10424:                 foreach my $val (@vals) {
10425:                     if ($val =~ /:/) {
10426:                         my @items = split(/:/,$val);
10427:                         foreach my $item (@items) {
10428:                             if (ref($by_location{$item}) eq 'ARRAY') {
10429:                                 push(@okvals,$item);
10430:                             }
10431:                         }
10432:                     } else {
10433:                         if (ref($by_location{$val}) eq 'ARRAY') {
10434:                             push(@okvals,$val);
10435:                         }
10436:                     }
10437:                 }
10438:                 @okvals = sort(@okvals);
10439:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10440:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10441:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10442:                             if ($inuse == 0) {
10443:                                 $changes{$prefix}{$type} = 1; 
10444:                             } else {
10445:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10446:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
10447:                                 if (@changed > 0) {
10448:                                     $changes{$prefix}{$type} = 1;
10449:                                 }
10450:                             }
10451:                         } else {
10452:                             if ($inuse == 1) {
10453:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10454:                                 $changes{$prefix}{$type} = 1;
10455:                             }
10456:                         } 
10457:                     } else {
10458:                         if ($inuse == 1) {
10459:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10460:                             $changes{$prefix}{$type} = 1;
10461:                         }
10462:                     }
10463:                 } else {
10464:                     if ($inuse == 1) {
10465:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10466:                         $changes{$prefix}{$type} = 1;
10467:                     }
10468:                 }
10469:             }
10470:         }
10471:     }
10472: 
10473:     my @alldoms = &Apache::lonnet::all_domains();
10474:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10475:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
10476:     my $savespares;
10477: 
10478:     foreach my $lonhost (sort(keys(%servers))) {
10479:         my $serverhomeID =
10480:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
10481:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
10482:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
10483:         my %spareschg;
10484:         foreach my $type (@{$types{'spares'}}) {
10485:             my @okspares;
10486:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
10487:             foreach my $server (@checked) {
10488:                 if (&Apache::lonnet::hostname($server) ne '') {
10489:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
10490:                         unless (grep(/^\Q$server\E$/,@okspares)) {
10491:                             push(@okspares,$server);
10492:                         }
10493:                     }
10494:                 }
10495:             }
10496:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
10497:             my $newspare;
10498:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
10499:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
10500:                     $newspare = $new;
10501:                 }
10502:             }
10503:             my @spares;
10504:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
10505:                 @spares = sort(@okspares,$newspare);
10506:             } else {
10507:                 @spares = sort(@okspares);
10508:             }
10509:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
10510:             if (ref($spareid{$lonhost}) eq 'HASH') {
10511:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
10512:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
10513:                     if (@diffs > 0) {
10514:                         $spareschg{$type} = 1;
10515:                     }
10516:                 }
10517:             }
10518:         }
10519:         if (keys(%spareschg) > 0) {
10520:             $changes{'spares'}{$lonhost} = \%spareschg;
10521:         }
10522:     }
10523: 
10524:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
10525:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
10526:             if (ref($changes{'spares'}) eq 'HASH') {
10527:                 if (keys(%{$changes{'spares'}}) > 0) {
10528:                     $savespares = 1;
10529:                 }
10530:             }
10531:         } else {
10532:             $savespares = 1;
10533:         }
10534:     }
10535: 
10536:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
10537:     if ((keys(%changes) > 0) || ($savespares)) {
10538:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10539:                                                  $dom);
10540:         if ($putresult eq 'ok') {
10541:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10542:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
10543:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
10544:                 }
10545:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
10546:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
10547:                 }
10548:             }
10549:             my $cachetime = 24*60*60;
10550:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10551:             if (ref($lastactref) eq 'HASH') {
10552:                 $lastactref->{'domdefaults'} = 1;
10553:             }
10554:             if (keys(%changes) > 0) {
10555:                 my %lt = &usersession_titles();
10556:                 $resulttext = &mt('Changes made:').'<ul>';
10557:                 foreach my $prefix (@prefixes) {
10558:                     if (ref($changes{$prefix}) eq 'HASH') {
10559:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
10560:                         if ($prefix eq 'spares') {
10561:                             if (ref($changes{$prefix}) eq 'HASH') {
10562:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
10563:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
10564:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
10565:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
10566:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
10567:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
10568:                                         foreach my $type (@{$types{$prefix}}) {
10569:                                             if ($changes{$prefix}{$lonhost}{$type}) {
10570:                                                 my $offloadto = &mt('None');
10571:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
10572:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
10573:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
10574:                                                     }
10575:                                                 }
10576:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
10577:                                             }
10578:                                         }
10579:                                     }
10580:                                     $resulttext .= '</li>';
10581:                                 }
10582:                             }
10583:                         } else {
10584:                             foreach my $type (@{$types{$prefix}}) {
10585:                                 if (defined($changes{$prefix}{$type})) {
10586:                                     my $newvalue;
10587:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10588:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
10589:                                             if ($type eq 'version') {
10590:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
10591:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10592:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
10593:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
10594:                                                 }
10595:                                             }
10596:                                         }
10597:                                     }
10598:                                     if ($newvalue eq '') {
10599:                                         if ($type eq 'version') {
10600:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
10601:                                         } else {
10602:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
10603:                                         }
10604:                                     } else {
10605:                                         if ($type eq 'version') {
10606:                                             $newvalue .= ' '.&mt('(or later)'); 
10607:                                         }
10608:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
10609:                                     }
10610:                                 }
10611:                             }
10612:                         }
10613:                         $resulttext .= '</ul>';
10614:                     }
10615:                 }
10616:                 $resulttext .= '</ul>';
10617:             } else {
10618:                 $resulttext = $nochgmsg;
10619:             }
10620:         } else {
10621:             $resulttext = '<span class="LC_error">'.
10622:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10623:         }
10624:     } else {
10625:         $resulttext = $nochgmsg;
10626:     }
10627:     return $resulttext;
10628: }
10629: 
10630: sub modify_loadbalancing {
10631:     my ($dom,%domconfig) = @_;
10632:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
10633:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
10634:     my ($othertitle,$usertypes,$types) =
10635:         &Apache::loncommon::sorted_inst_types($dom);
10636:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10637:     my @sparestypes = ('primary','default');
10638:     my %typetitles = &sparestype_titles();
10639:     my $resulttext;
10640:     my (%currbalancer,%currtargets,%currrules,%existing);
10641:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10642:         %existing = %{$domconfig{'loadbalancing'}};
10643:     }
10644:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
10645:                               \%currtargets,\%currrules);
10646:     my ($saveloadbalancing,%defaultshash,%changes);
10647:     my ($alltypes,$othertypes,$titles) =
10648:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
10649:     my %ruletitles = &offloadtype_text();
10650:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
10651:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
10652:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
10653:         if ($balancer eq '') {
10654:             next;
10655:         }
10656:         if (!exists($servers{$balancer})) {
10657:             if (exists($currbalancer{$balancer})) {
10658:                 push(@{$changes{'delete'}},$balancer);
10659:             }
10660:             next;
10661:         }
10662:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
10663:             push(@{$changes{'delete'}},$balancer);
10664:             next;
10665:         }
10666:         if (!exists($currbalancer{$balancer})) {
10667:             push(@{$changes{'add'}},$balancer);
10668:         }
10669:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
10670:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
10671:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
10672:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10673:             $saveloadbalancing = 1;
10674:         }
10675:         foreach my $sparetype (@sparestypes) {
10676:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
10677:             my @offloadto;
10678:             foreach my $target (@targets) {
10679:                 if (($servers{$target}) && ($target ne $balancer)) {
10680:                     if ($sparetype eq 'default') {
10681:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
10682:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
10683:                         }
10684:                     }
10685:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
10686:                         push(@offloadto,$target);
10687:                     }
10688:                 }
10689:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
10690:             }
10691:         }
10692:         if (ref($currtargets{$balancer}) eq 'HASH') {
10693:             foreach my $sparetype (@sparestypes) {
10694:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
10695:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
10696:                     if (@targetdiffs > 0) {
10697:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10698:                     }
10699:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10700:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10701:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10702:                     }
10703:                 }
10704:             }
10705:         } else {
10706:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
10707:                 foreach my $sparetype (@sparestypes) {
10708:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10709:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10710:                             $changes{'curr'}{$balancer}{'targets'} = 1;
10711:                         }
10712:                     }
10713:                 }
10714:             }
10715:         }
10716:         my $ishomedom;
10717:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
10718:             $ishomedom = 1;
10719:         }
10720:         if (ref($alltypes) eq 'ARRAY') {
10721:             foreach my $type (@{$alltypes}) {
10722:                 my $rule;
10723:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
10724:                          (!$ishomedom)) {
10725:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
10726:                 }
10727:                 if ($rule eq 'specific') {
10728:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
10729:                 }
10730:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
10731:                 if (ref($currrules{$balancer}) eq 'HASH') {
10732:                     if ($rule ne $currrules{$balancer}{$type}) {
10733:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10734:                     }
10735:                 } elsif ($rule ne '') {
10736:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10737:                 }
10738:             }
10739:         }
10740:     }
10741:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
10742:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
10743:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
10744:             $defaultshash{'loadbalancing'} = {};
10745:         }
10746:         my $putresult = &Apache::lonnet::put_dom('configuration',
10747:                                                  \%defaultshash,$dom);
10748:         if ($putresult eq 'ok') {
10749:             if (keys(%changes) > 0) {
10750:                 if (ref($changes{'delete'}) eq 'ARRAY') {
10751:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
10752:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
10753:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10754:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10755:                     }
10756:                 }
10757:                 if (ref($changes{'add'}) eq 'ARRAY') {
10758:                     foreach my $balancer (sort(@{$changes{'add'}})) {
10759:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
10760:                     }
10761:                 }
10762:                 if (ref($changes{'curr'}) eq 'HASH') {
10763:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
10764:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
10765:                             if ($changes{'curr'}{$balancer}{'targets'}) {
10766:                                 my %offloadstr;
10767:                                 foreach my $sparetype (@sparestypes) {
10768:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10769:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10770:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10771:                                         }
10772:                                     }
10773:                                 }
10774:                                 if (keys(%offloadstr) == 0) {
10775:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
10776:                                 } else {
10777:                                     my $showoffload;
10778:                                     foreach my $sparetype (@sparestypes) {
10779:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
10780:                                         if (defined($offloadstr{$sparetype})) {
10781:                                             $showoffload .= $offloadstr{$sparetype};
10782:                                         } else {
10783:                                             $showoffload .= &mt('None');
10784:                                         }
10785:                                         $showoffload .= ('&nbsp;'x3);
10786:                                     }
10787:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
10788:                                 }
10789:                             }
10790:                         }
10791:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
10792:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
10793:                                 foreach my $type (@{$alltypes}) {
10794:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
10795:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
10796:                                         my $balancetext;
10797:                                         if ($rule eq '') {
10798:                                             $balancetext =  $ruletitles{'default'};
10799:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
10800:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
10801:                                             $balancetext =  $ruletitles{$rule};
10802:                                         } else {
10803:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
10804:                                         }
10805:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
10806:                                     }
10807:                                 }
10808:                             }
10809:                         }
10810:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10811:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10812:                     }
10813:                 }
10814:                 if ($resulttext ne '') {
10815:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
10816:                 } else {
10817:                     $resulttext = $nochgmsg;
10818:                 }
10819:             } else {
10820:                 $resulttext = $nochgmsg;
10821:             }
10822:         } else {
10823:             $resulttext = '<span class="LC_error">'.
10824:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10825:         }
10826:     } else {
10827:         $resulttext = $nochgmsg;
10828:     }
10829:     return $resulttext;
10830: }
10831: 
10832: sub recurse_check {
10833:     my ($chkcats,$categories,$depth,$name) = @_;
10834:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
10835:         my $chg = 0;
10836:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
10837:             my $category = $chkcats->[$depth]{$name}[$j];
10838:             my $item;
10839:             if ($category eq '') {
10840:                 $chg ++;
10841:             } else {
10842:                 my $deeper = $depth + 1;
10843:                 $item = &escape($category).':'.&escape($name).':'.$depth;
10844:                 if ($chg) {
10845:                     $categories->{$item} -= $chg;
10846:                 }
10847:                 &recurse_check($chkcats,$categories,$deeper,$category);
10848:                 $deeper --;
10849:             }
10850:         }
10851:     }
10852:     return;
10853: }
10854: 
10855: sub recurse_cat_deletes {
10856:     my ($item,$coursecategories,$deletions) = @_;
10857:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10858:     my $subdepth = $depth + 1;
10859:     if (ref($coursecategories) eq 'HASH') {
10860:         foreach my $subitem (keys(%{$coursecategories})) {
10861:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
10862:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
10863:                 delete($coursecategories->{$subitem});
10864:                 $deletions->{$subitem} = 1;
10865:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
10866:             }
10867:         }
10868:     }
10869:     return;
10870: }
10871: 
10872: sub get_active_dcs {
10873:     my ($dom) = @_;
10874:     my $now = time;
10875:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
10876:     my %domcoords;
10877:     my $numdcs = 0;
10878:     foreach my $server (keys(%dompersonnel)) {
10879:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
10880:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
10881:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
10882:         }
10883:     }
10884:     return %domcoords;
10885: }
10886: 
10887: sub active_dc_picker {
10888:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
10889:     my %domcoords = &get_active_dcs($dom);
10890:     my @domcoord = keys(%domcoords);
10891:     if (keys(%currhash)) {
10892:         foreach my $dc (keys(%currhash)) {
10893:             unless (exists($domcoords{$dc})) {
10894:                 push(@domcoord,$dc);
10895:             }
10896:         }
10897:     }
10898:     @domcoord = sort(@domcoord);
10899:     my $numdcs = scalar(@domcoord);
10900:     my $rows = 0;
10901:     my $table;
10902:     if ($numdcs > 1) {
10903:         $table = '<table>';
10904:         for (my $i=0; $i<@domcoord; $i++) {
10905:             my $rem = $i%($numinrow);
10906:             if ($rem == 0) {
10907:                 if ($i > 0) {
10908:                     $table .= '</tr>';
10909:                 }
10910:                 $table .= '<tr>';
10911:                 $rows ++;
10912:             }
10913:             my $check = '';
10914:             if ($inputtype eq 'radio') {
10915:                 if (keys(%currhash) == 0) {
10916:                     if (!$i) {
10917:                         $check = ' checked="checked"';
10918:                     }
10919:                 } elsif (exists($currhash{$domcoord[$i]})) {
10920:                     $check = ' checked="checked"';
10921:                 }
10922:             } else {
10923:                 if (exists($currhash{$domcoord[$i]})) {
10924:                     $check = ' checked="checked"';
10925:                 }
10926:             }
10927:             if ($i == @domcoord - 1) {
10928:                 my $colsleft = $numinrow - $rem;
10929:                 if ($colsleft > 1) {
10930:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
10931:                 } else {
10932:                     $table .= '<td class="LC_left_item">';
10933:                 }
10934:             } else {
10935:                 $table .= '<td class="LC_left_item">';
10936:             }
10937:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
10938:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10939:             $table .= '<span class="LC_nobreak"><label>'.
10940:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
10941:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
10942:             if ($user ne $dcname.':'.$dcdom) {
10943:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10944:             }
10945:             $table .= '</label></span></td>';
10946:         }
10947:         $table .= '</tr></table>';
10948:     } elsif ($numdcs == 1) {
10949:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
10950:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10951:         if ($inputtype eq 'radio') {
10952:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
10953:             if ($user ne $dcname.':'.$dcdom) {
10954:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10955:             }
10956:         } else {
10957:             my $check;
10958:             if (exists($currhash{$domcoord[0]})) {
10959:                 $check = ' checked="checked"';
10960:             }
10961:             $table .= '<span class="LC_nobreak"><label>'.
10962:                       '<input type="checkbox" name="'.$name.'" '.
10963:                       'value="'.$domcoord[0].'"'.$check.' />'.$user;
10964:             if ($user ne $dcname.':'.$dcdom) {
10965:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10966:             }
10967:             $table .= '</label></span>';
10968:             $rows ++;
10969:         }
10970:     }
10971:     return ($numdcs,$table,$rows);
10972: }
10973: 
10974: sub usersession_titles {
10975:     return &Apache::lonlocal::texthash(
10976:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
10977:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
10978:                spares => 'Servers offloaded to, when busy',
10979:                version => 'LON-CAPA version requirement',
10980:                excludedomain => 'Allow all, but exclude specific domains',
10981:                includedomain => 'Deny all, but include specific domains',
10982:                primary => 'Primary (checked first)',
10983:                default => 'Default',
10984:            );
10985: }
10986: 
10987: sub id_for_thisdom {
10988:     my (%servers) = @_;
10989:     my %altids;
10990:     foreach my $server (keys(%servers)) {
10991:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
10992:         if ($serverhome ne $server) {
10993:             $altids{$serverhome} = $server;
10994:         }
10995:     }
10996:     return %altids;
10997: }
10998: 
10999: sub count_servers {
11000:     my ($currbalancer,%servers) = @_;
11001:     my (@spares,$numspares);
11002:     foreach my $lonhost (sort(keys(%servers))) {
11003:         next if ($currbalancer eq $lonhost);
11004:         push(@spares,$lonhost);
11005:     }
11006:     if ($currbalancer) {
11007:         $numspares = scalar(@spares);
11008:     } else {
11009:         $numspares = scalar(@spares) - 1;
11010:     }
11011:     return ($numspares,@spares);
11012: }
11013: 
11014: sub lonbalance_targets_js {
11015:     my ($dom,$types,$servers,$settings) = @_;
11016:     my $select = &mt('Select');
11017:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
11018:     if (ref($servers) eq 'HASH') {
11019:         $alltargets = join("','",sort(keys(%{$servers})));
11020:         my @homedoms;
11021:         foreach my $server (sort(keys(%{$servers}))) {
11022:             if (&Apache::lonnet::host_domain($server) eq $dom) {
11023:                 push(@homedoms,'1');
11024:             } else {
11025:                 push(@homedoms,'0');
11026:             }
11027:         }
11028:         $allishome = join("','",@homedoms);
11029:     }
11030:     if (ref($types) eq 'ARRAY') {
11031:         if (@{$types} > 0) {
11032:             @alltypes = @{$types};
11033:         }
11034:     }
11035:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
11036:     $allinsttypes = join("','",@alltypes);
11037:     my (%currbalancer,%currtargets,%currrules,%existing);
11038:     if (ref($settings) eq 'HASH') {
11039:         %existing = %{$settings};
11040:     }
11041:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
11042:                               \%currtargets,\%currrules);
11043:     my $balancers = join("','",sort(keys(%currbalancer)));
11044:     return <<"END";
11045: 
11046: <script type="text/javascript">
11047: // <![CDATA[
11048: 
11049: currBalancers = new Array('$balancers');
11050: 
11051: function toggleTargets(balnum) {
11052:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11053:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
11054:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
11055:     var prevbalancer = prevhostitem.value;
11056:     var baltotal = document.getElementById('loadbalancing_total').value;
11057:     prevhostitem.value = balancer;
11058:     if (prevbalancer != '') {
11059:         var prevIdx = currBalancers.indexOf(prevbalancer);
11060:         if (prevIdx != -1) {
11061:             currBalancers.splice(prevIdx,1);
11062:         }
11063:     }
11064:     if (balancer == '') {
11065:         hideSpares(balnum);
11066:     } else {
11067:         var currIdx = currBalancers.indexOf(balancer);
11068:         if (currIdx == -1) {
11069:             currBalancers.push(balancer);
11070:         }
11071:         var homedoms = new Array('$allishome');
11072:         var ishomedom = homedoms[lonhostitem.selectedIndex];
11073:         showSpares(balancer,ishomedom,balnum);
11074:     }
11075:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
11076:     return;
11077: }
11078: 
11079: function showSpares(balancer,ishomedom,balnum) {
11080:     var alltargets = new Array('$alltargets');
11081:     var insttypes = new Array('$allinsttypes');
11082:     var offloadtypes = new Array('primary','default');
11083: 
11084:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
11085:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
11086:  
11087:     for (var i=0; i<offloadtypes.length; i++) {
11088:         var count = 0;
11089:         for (var j=0; j<alltargets.length; j++) {
11090:             if (alltargets[j] != balancer) {
11091:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
11092:                 item.value = alltargets[j];
11093:                 item.style.textAlign='left';
11094:                 item.style.textFace='normal';
11095:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
11096:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
11097:                     item.disabled = '';
11098:                 } else {
11099:                     item.disabled = 'disabled';
11100:                     item.checked = false;
11101:                 }
11102:                 count ++;
11103:             }
11104:         }
11105:     }
11106:     for (var k=0; k<insttypes.length; k++) {
11107:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
11108:             if (ishomedom == 1) {
11109:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11110:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11111:             } else {
11112:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11113:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11114:             }
11115:         } else {
11116:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11117:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11118:         }
11119:         if ((insttypes[k] != '_LC_external') && 
11120:             ((insttypes[k] != '_LC_internetdom') ||
11121:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
11122:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
11123:             item.options.length = 0;
11124:             item.options[0] = new Option("","",true,true);
11125:             var idx = 0;
11126:             for (var m=0; m<alltargets.length; m++) {
11127:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
11128:                     idx ++;
11129:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11130:                 }
11131:             }
11132:         }
11133:     }
11134:     return;
11135: }
11136: 
11137: function hideSpares(balnum) {
11138:     var alltargets = new Array('$alltargets');
11139:     var insttypes = new Array('$allinsttypes');
11140:     var offloadtypes = new Array('primary','default');
11141: 
11142:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
11143:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
11144: 
11145:     var total = alltargets.length - 1;
11146:     for (var i=0; i<offloadtypes; i++) {
11147:         for (var j=0; j<total; j++) {
11148:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
11149:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
11150:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
11151:         }
11152:     }
11153:     for (var k=0; k<insttypes.length; k++) {
11154:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11155:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11156:         if (insttypes[k] != '_LC_external') {
11157:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
11158:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
11159:         }
11160:     }
11161:     return;
11162: }
11163: 
11164: function checkOffloads(item,balnum,type) {
11165:     var alltargets = new Array('$alltargets');
11166:     var offloadtypes = new Array('primary','default');
11167:     if (item.checked) {
11168:         var total = alltargets.length - 1;
11169:         var other;
11170:         if (type == offloadtypes[0]) {
11171:             other = offloadtypes[1];
11172:         } else {
11173:             other = offloadtypes[0];
11174:         }
11175:         for (var i=0; i<total; i++) {
11176:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
11177:             if (server == item.value) {
11178:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
11179:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
11180:                 }
11181:             }
11182:         }
11183:     }
11184:     return;
11185: }
11186: 
11187: function singleServerToggle(balnum,type) {
11188:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
11189:     if (offloadtoSelIdx == 0) {
11190:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
11191:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11192: 
11193:     } else {
11194:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
11195:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11196:     }
11197:     return;
11198: }
11199: 
11200: function balanceruleChange(formname,balnum,type) {
11201:     if (type == '_LC_external') {
11202:         return;
11203:     }
11204:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
11205:     for (var i=0; i<typesRules.length; i++) {
11206:         if (formname.elements[typesRules[i]].checked) {
11207:             if (formname.elements[typesRules[i]].value != 'specific') {
11208:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
11209:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11210:             } else {
11211:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11212:             }
11213:         }
11214:     }
11215:     return;
11216: }
11217: 
11218: function balancerDeleteChange(balnum) {
11219:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11220:     var baltotal = document.getElementById('loadbalancing_total').value;
11221:     var addtarget;
11222:     var removetarget;
11223:     var action = 'delete';
11224:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
11225:         var lonhost = hostitem.value;
11226:         var currIdx = currBalancers.indexOf(lonhost);
11227:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
11228:             if (currIdx != -1) {
11229:                 currBalancers.splice(currIdx,1);
11230:             }
11231:             addtarget = lonhost;
11232:         } else {
11233:             if (currIdx == -1) {
11234:                 currBalancers.push(lonhost);
11235:             }
11236:             removetarget = lonhost;
11237:             action = 'undelete';
11238:         }
11239:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
11240:     }
11241:     return;
11242: }
11243: 
11244: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
11245:     if (baltotal > 1) {
11246:         var offloadtypes = new Array('primary','default');
11247:         var alltargets = new Array('$alltargets');
11248:         var insttypes = new Array('$allinsttypes');
11249:         for (var i=0; i<baltotal; i++) {
11250:             if (i != balnum) {
11251:                 for (var j=0; j<offloadtypes.length; j++) {
11252:                     var total = alltargets.length - 1;
11253:                     for (var k=0; k<total; k++) {
11254:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
11255:                         var server = serveritem.value;
11256:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
11257:                             if (server == addtarget) {
11258:                                 serveritem.disabled = '';
11259:                             }
11260:                         }
11261:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11262:                             if (server == removetarget) {
11263:                                 serveritem.disabled = 'disabled';
11264:                                 serveritem.checked = false;
11265:                             }
11266:                         }
11267:                     }
11268:                 }
11269:                 for (var j=0; j<insttypes.length; j++) {
11270:                     if (insttypes[j] != '_LC_external') {
11271:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
11272:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
11273:                             var currSel = singleserver.selectedIndex;
11274:                             var currVal = singleserver.options[currSel].value;
11275:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
11276:                                 var numoptions = singleserver.options.length;
11277:                                 var needsnew = 1;
11278:                                 for (var k=0; k<numoptions; k++) {
11279:                                     if (singleserver.options[k] == addtarget) {
11280:                                         needsnew = 0;
11281:                                         break;
11282:                                     }
11283:                                 }
11284:                                 if (needsnew == 1) {
11285:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
11286:                                 }
11287:                             }
11288:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11289:                                 singleserver.options.length = 0;
11290:                                 if ((currVal) && (currVal != removetarget)) {
11291:                                     singleserver.options[0] = new Option("","",false,false);
11292:                                 } else {
11293:                                     singleserver.options[0] = new Option("","",true,true);
11294:                                 }
11295:                                 var idx = 0;
11296:                                 for (var m=0; m<alltargets.length; m++) {
11297:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
11298:                                         idx ++;
11299:                                         if (currVal == alltargets[m]) {
11300:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
11301:                                         } else {
11302:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11303:                                         }
11304:                                     }
11305:                                 }
11306:                             }
11307:                         }
11308:                     }
11309:                 }
11310:             }
11311:         }
11312:     }
11313:     return;
11314: }
11315: 
11316: // ]]>
11317: </script>
11318: 
11319: END
11320: }
11321: 
11322: sub new_spares_js {
11323:     my @sparestypes = ('primary','default');
11324:     my $types = join("','",@sparestypes);
11325:     my $select = &mt('Select');
11326:     return <<"END";
11327: 
11328: <script type="text/javascript">
11329: // <![CDATA[
11330: 
11331: function updateNewSpares(formname,lonhost) {
11332:     var types = new Array('$types');
11333:     var include = new Array();
11334:     var exclude = new Array();
11335:     for (var i=0; i<types.length; i++) {
11336:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
11337:         for (var j=0; j<spareboxes.length; j++) {
11338:             if (formname.elements[spareboxes[j]].checked) {
11339:                 exclude.push(formname.elements[spareboxes[j]].value);
11340:             } else {
11341:                 include.push(formname.elements[spareboxes[j]].value);
11342:             }
11343:         }
11344:     }
11345:     for (var i=0; i<types.length; i++) {
11346:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
11347:         var selIdx = newSpare.selectedIndex;
11348:         var currnew = newSpare.options[selIdx].value;
11349:         var okSpares = new Array();
11350:         for (var j=0; j<newSpare.options.length; j++) {
11351:             var possible = newSpare.options[j].value;
11352:             if (possible != '') {
11353:                 if (exclude.indexOf(possible) == -1) {
11354:                     okSpares.push(possible);
11355:                 } else {
11356:                     if (currnew == possible) {
11357:                         selIdx = 0;
11358:                     }
11359:                 }
11360:             }
11361:         }
11362:         for (var k=0; k<include.length; k++) {
11363:             if (okSpares.indexOf(include[k]) == -1) {
11364:                 okSpares.push(include[k]);
11365:             }
11366:         }
11367:         okSpares.sort();
11368:         newSpare.options.length = 0;
11369:         if (selIdx == 0) {
11370:             newSpare.options[0] = new Option("$select","",true,true);
11371:         } else {
11372:             newSpare.options[0] = new Option("$select","",false,false);
11373:         }
11374:         for (var m=0; m<okSpares.length; m++) {
11375:             var idx = m+1;
11376:             var selThis = 0;
11377:             if (selIdx != 0) {
11378:                 if (okSpares[m] == currnew) {
11379:                     selThis = 1;
11380:                 }
11381:             }
11382:             if (selThis == 1) {
11383:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
11384:             } else {
11385:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
11386:             }
11387:         }
11388:     }
11389:     return;
11390: }
11391: 
11392: function checkNewSpares(lonhost,type) {
11393:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
11394:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
11395:     if (chosen != '') { 
11396:         var othertype;
11397:         var othernewSpare;
11398:         if (type == 'primary') {
11399:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
11400:         }
11401:         if (type == 'default') {
11402:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
11403:         }
11404:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
11405:             othernewSpare.selectedIndex = 0;
11406:         }
11407:     }
11408:     return;
11409: }
11410: 
11411: // ]]>
11412: </script>
11413: 
11414: END
11415: 
11416: }
11417: 
11418: sub common_domprefs_js {
11419:     return <<"END";
11420: 
11421: <script type="text/javascript">
11422: // <![CDATA[
11423: 
11424: function getIndicesByName(formname,item) {
11425:     var group = new Array();
11426:     for (var i=0;i<formname.elements.length;i++) {
11427:         if (formname.elements[i].name == item) {
11428:             group.push(formname.elements[i].id);
11429:         }
11430:     }
11431:     return group;
11432: }
11433: 
11434: // ]]>
11435: </script>
11436: 
11437: END
11438: 
11439: }
11440: 
11441: sub recaptcha_js {
11442:     my %lt = &captcha_phrases();
11443:     return <<"END";
11444: 
11445: <script type="text/javascript">
11446: // <![CDATA[
11447: 
11448: function updateCaptcha(caller,context) {
11449:     var privitem;
11450:     var pubitem;
11451:     var privtext;
11452:     var pubtext;
11453:     if (document.getElementById(context+'_recaptchapub')) {
11454:         pubitem = document.getElementById(context+'_recaptchapub');
11455:     } else {
11456:         return;
11457:     }
11458:     if (document.getElementById(context+'_recaptchapriv')) {
11459:         privitem = document.getElementById(context+'_recaptchapriv');
11460:     } else {
11461:         return;
11462:     }
11463:     if (document.getElementById(context+'_recaptchapubtxt')) {
11464:         pubtext = document.getElementById(context+'_recaptchapubtxt');
11465:     } else {
11466:         return;
11467:     }
11468:     if (document.getElementById(context+'_recaptchaprivtxt')) {
11469:         privtext = document.getElementById(context+'_recaptchaprivtxt');
11470:     } else {
11471:         return;
11472:     }
11473:     if (caller.checked) {
11474:         if (caller.value == 'recaptcha') {
11475:             pubitem.type = 'text';
11476:             privitem.type = 'text';
11477:             pubitem.size = '40';
11478:             privitem.size = '40';
11479:             pubtext.innerHTML = "$lt{'pub'}";
11480:             privtext.innerHTML = "$lt{'priv'}";
11481:         } else {
11482:             pubitem.type = 'hidden';
11483:             privitem.type = 'hidden';
11484:             pubtext.innerHTML = '';
11485:             privtext.innerHTML = '';
11486:         }
11487:     }
11488:     return;
11489: }
11490: 
11491: // ]]>
11492: </script>
11493: 
11494: END
11495: 
11496: }
11497: 
11498: sub toggle_display_js {
11499:     return <<"END";
11500: 
11501: <script type="text/javascript">
11502: // <![CDATA[
11503: 
11504: function toggleDisplay(domForm,caller) {
11505:     if (document.getElementById(caller)) {
11506:         var divitem = document.getElementById(caller);
11507:         var optionsElement = domForm.coursecredits;
11508:         if (caller == 'emailoptions') {
11509:             optionsElement = domForm.cancreate_email; 
11510:         }
11511:         if (optionsElement.length) {
11512:             var currval;
11513:             for (var i=0; i<optionsElement.length; i++) {
11514:                 if (optionsElement[i].checked) {
11515:                    currval = optionsElement[i].value;
11516:                 }
11517:             }
11518:             if (currval == 1) {
11519:                 divitem.style.display = 'block';
11520:             } else {
11521:                 divitem.style.display = 'none';
11522:             }
11523:         }
11524:     }
11525:     return;
11526: }
11527: 
11528: // ]]>
11529: </script>
11530: 
11531: END
11532: 
11533: }
11534: 
11535: sub captcha_phrases {
11536:     return &Apache::lonlocal::texthash (
11537:                  priv => 'Private key',
11538:                  pub  => 'Public key',
11539:                  original  => 'original (CAPTCHA)',
11540:                  recaptcha => 'successor (ReCAPTCHA)',
11541:                  notused   => 'unused',
11542:     );
11543: }
11544: 
11545: sub devalidate_remote_domconfs {
11546:     my ($dom,$cachekeys) = @_;
11547:     return unless (ref($cachekeys) eq 'HASH');
11548:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11549:     my %thismachine;
11550:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
11551:     my @posscached = ('domainconfig','domdefaults');
11552:     if (keys(%servers) > 1) {
11553:         foreach my $server (keys(%servers)) {
11554:             next if ($thismachine{$server});
11555:             my @cached;
11556:             foreach my $name (@posscached) {
11557:                 if ($cachekeys->{$name}) {
11558:                     push(@cached,&escape($name).':'.&escape($dom));
11559:                 }
11560:             }
11561:             if (@cached) {
11562:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
11563:             }
11564:         }
11565:     }
11566:     return;
11567: }
11568: 
11569: 1;

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