File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.240: download - view: text, annotated - select for diffs
Mon May 5 21:28:10 2014 UTC (10 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Domain configuration for self-creation of accounts by users authenticated
  via Shibboleth SSO.
  - Set which environment variables provided by Shibboleth map to which
    user information fields.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.240 2014/05/05 21:28:10 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','helpsettings',
  217:                 'coursedefaults','usersessions','loadbalancing',
  218:                 'requestauthor','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','helpsettings',
  224:                        'coursedefaults','selfenrollment','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 => 'Validation (not official courses)',
  373:                               col2 => 'Value'},],
  374:                   print => \&print_quotas,
  375:                   modify => \&modify_quotas,
  376:                  },
  377:         'requestauthor' =>
  378:                  {text => 'Request Authoring Space',
  379:                   help => 'Domain_Configuration_Request_Author',
  380:                   header => [{col1 => 'User affiliation',
  381:                               col2 => 'Availability/Processing of requests',},
  382:                              {col1 => 'Setting',
  383:                               col2 => 'Value'}],
  384:                   print => \&print_quotas,
  385:                   modify => \&modify_quotas,
  386:                  },
  387:         'coursecategories' =>
  388:                   { text => 'Cataloging of courses/communities',
  389:                     help => 'Domain_Configuration_Cataloging_Courses',
  390:                     header => [{col1 => 'Catalog type/availability',
  391:                                 col2 => '',},
  392:                                {col1 => 'Category settings for standard catalog',
  393:                                 col2 => '',},
  394:                                {col1 => 'Categories',
  395:                                 col2 => '',
  396:                                }],
  397:                     print => \&print_coursecategories,
  398:                     modify => \&modify_coursecategories,
  399:                   },
  400:         'serverstatuses' =>
  401:                  {text   => 'Access to server status pages',
  402:                   help   => 'Domain_Configuration_Server_Status',
  403:                   header => [{col1 => 'Status Page',
  404:                               col2 => 'Other named users',
  405:                               col3 => 'Specific IPs',
  406:                             }],
  407:                   print => \&print_serverstatuses,
  408:                   modify => \&modify_serverstatuses,
  409:                  },
  410:         'helpsettings' =>
  411:                  {text   => 'Help page settings',
  412:                   help   => 'Domain_Configuration_Help_Settings',
  413:                   header => [{col1 => 'Help Settings (logged-in users)',
  414:                               col2 => 'Value'}],
  415:                   print  => \&print_helpsettings,
  416:                   modify => \&modify_helpsettings,
  417:                  },
  418:         'coursedefaults' => 
  419:                  {text => 'Course/Community defaults',
  420:                   help => 'Domain_Configuration_Course_Defaults',
  421:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  422:                               col2 => 'Value',},
  423:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  424:                               col2 => 'Value',},],
  425:                   print => \&print_coursedefaults,
  426:                   modify => \&modify_coursedefaults,
  427:                  },
  428:         'selfenrollment' => 
  429:                  {text   => 'Self-enrollment in Course/Community',
  430:                   help   => 'Domain_Configuration_Selfenrollment',
  431:                   header => [{col1 => 'Configuration Rights',
  432:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  433:                              {col1 => 'Defaults',
  434:                               col2 => 'Value'},
  435:                              {col1 => 'Self-enrollment validation (optional)',
  436:                               col2 => 'Value'},],
  437:                   print => \&print_selfenrollment,
  438:                   modify => \&modify_selfenrollment,
  439:                  },
  440:         'privacy' => 
  441:                  {text   => 'User Privacy',
  442:                   help   => 'Domain_Configuration_User_Privacy',
  443:                   header => [{col1 => 'Setting',
  444:                               col2 => 'Value',}],
  445:                   print => \&print_privacy,
  446:                   modify => \&modify_privacy,
  447:                  },
  448:         'usersessions' =>
  449:                  {text  => 'User session hosting/offloading',
  450:                   help  => 'Domain_Configuration_User_Sessions',
  451:                   header => [{col1 => 'Domain server',
  452:                               col2 => 'Servers to offload sessions to when busy'},
  453:                              {col1 => 'Hosting of users from other domains',
  454:                               col2 => 'Rules'},
  455:                              {col1 => "Hosting domain's own users elsewhere",
  456:                               col2 => 'Rules'}],
  457:                   print => \&print_usersessions,
  458:                   modify => \&modify_usersessions,
  459:                  },
  460:          'loadbalancing' =>
  461:                  {text  => 'Dedicated Load Balancer(s)',
  462:                   help  => 'Domain_Configuration_Load_Balancing',
  463:                   header => [{col1 => 'Balancers',
  464:                               col2 => 'Default destinations',
  465:                               col3 => 'User affiliation',
  466:                               col4 => 'Overrides'},
  467:                             ],
  468:                   print => \&print_loadbalancing,
  469:                   modify => \&modify_loadbalancing,
  470:                  },
  471:     );
  472:     if (keys(%servers) > 1) {
  473:         $prefs{'login'}  = { text   => 'Log-in page options',
  474:                              help   => 'Domain_Configuration_Login_Page',
  475:                             header => [{col1 => 'Log-in Service',
  476:                                         col2 => 'Server Setting',},
  477:                                        {col1 => 'Log-in Page Items',
  478:                                         col2 => ''},
  479:                                        {col1 => 'Log-in Help',
  480:                                         col2 => 'Value'}],
  481:                             print => \&print_login,
  482:                             modify => \&modify_login,
  483:                            };
  484:     }
  485: 
  486:     my @roles = ('student','coordinator','author','admin');
  487:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  488:     &Apache::lonhtmlcommon::add_breadcrumb
  489:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  490:       text=>"Settings to display/modify"});
  491:     my $confname = $dom.'-domainconfig';
  492: 
  493:     if ($phase eq 'process') {
  494:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  495:                                                               \%prefs,\%domconfig,$confname,\@roles);
  496:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  497:             $r->rflush();
  498:             &devalidate_remote_domconfs($dom,$result);
  499:         }
  500:     } elsif ($phase eq 'display') {
  501:         my $js = &recaptcha_js().
  502:                  &toggle_display_js();
  503:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  504:             my ($othertitle,$usertypes,$types) =
  505:                 &Apache::loncommon::sorted_inst_types($dom);
  506:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  507:                                           $domconfig{'loadbalancing'}).
  508:                    &new_spares_js().
  509:                    &common_domprefs_js().
  510:                    &Apache::loncommon::javascript_array_indexof();
  511:         }
  512:         if (grep(/^requestcourses$/,@actions)) {
  513:             my $javascript_validations;
  514:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  515:             $js .= <<END;
  516: <script type="text/javascript">
  517: $javascript_validations
  518: </script>
  519: $coursebrowserjs
  520: END
  521:         }
  522:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  523:     } else {
  524: # check if domconfig user exists for the domain.
  525:         my $servadm = $r->dir_config('lonAdmEMail');
  526:         my ($configuserok,$author_ok,$switchserver) =
  527:             &config_check($dom,$confname,$servadm);
  528:         unless ($configuserok eq 'ok') {
  529:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  530:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  531:                           $confname).
  532:                       '<br />'
  533:             );
  534:             if ($switchserver) {
  535:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  536:                           '<br />'.
  537:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  538:                           '<br />'.
  539:                           &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).
  540:                           '<br />'.
  541:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  542:                 );
  543:             } else {
  544:                 $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.').
  545:                           '<br />'.
  546:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  547:                 );
  548:             }
  549:             $r->print(&Apache::loncommon::end_page());
  550:             return OK;
  551:         }
  552:         if (keys(%domconfig) == 0) {
  553:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  554:             my @ids=&Apache::lonnet::current_machine_ids();
  555:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  556:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  557:                 my @loginimages = ('img','logo','domlogo','login');
  558:                 my $custom_img_count = 0;
  559:                 foreach my $img (@loginimages) {
  560:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  561:                         $custom_img_count ++;
  562:                     }
  563:                 }
  564:                 foreach my $role (@roles) {
  565:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  566:                         $custom_img_count ++;
  567:                     }
  568:                 }
  569:                 if ($custom_img_count > 0) {
  570:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  571:                     my $switch_server = &check_switchserver($dom,$confname);
  572:                     $r->print(
  573:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  574:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  575:     &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 />'.
  576:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  577:                     if ($switch_server) {
  578:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  579:                     }
  580:                     $r->print(&Apache::loncommon::end_page());
  581:                     return OK;
  582:                 }
  583:             }
  584:         }
  585:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  586:     }
  587:     return OK;
  588: }
  589: 
  590: sub process_changes {
  591:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  592:     my %domconfig;
  593:     if (ref($values) eq 'HASH') {
  594:         %domconfig = %{$values};
  595:     }
  596:     my $output;
  597:     if ($action eq 'login') {
  598:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  599:     } elsif ($action eq 'rolecolors') {
  600:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  601:                                      $lastactref,%domconfig);
  602:     } elsif ($action eq 'quotas') {
  603:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  604:     } elsif ($action eq 'autoenroll') {
  605:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  606:     } elsif ($action eq 'autoupdate') {
  607:         $output = &modify_autoupdate($dom,%domconfig);
  608:     } elsif ($action eq 'autocreate') {
  609:         $output = &modify_autocreate($dom,%domconfig);
  610:     } elsif ($action eq 'directorysrch') {
  611:         $output = &modify_directorysrch($dom,%domconfig);
  612:     } elsif ($action eq 'usercreation') {
  613:         $output = &modify_usercreation($dom,%domconfig);
  614:     } elsif ($action eq 'selfcreation') {
  615:         $output = &modify_selfcreation($dom,%domconfig);
  616:     } elsif ($action eq 'usermodification') {
  617:         $output = &modify_usermodification($dom,%domconfig);
  618:     } elsif ($action eq 'contacts') {
  619:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  620:     } elsif ($action eq 'defaults') {
  621:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  622:     } elsif ($action eq 'scantron') {
  623:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  624:     } elsif ($action eq 'coursecategories') {
  625:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  626:     } elsif ($action eq 'serverstatuses') {
  627:         $output = &modify_serverstatuses($dom,%domconfig);
  628:     } elsif ($action eq 'requestcourses') {
  629:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  630:     } elsif ($action eq 'requestauthor') {
  631:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  632:     } elsif ($action eq 'helpsettings') {
  633:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  634:     } elsif ($action eq 'coursedefaults') {
  635:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  636:     } elsif ($action eq 'selfenrollment') {
  637:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  638:     } elsif ($action eq 'usersessions') {
  639:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  640:     } elsif ($action eq 'loadbalancing') {
  641:         $output = &modify_loadbalancing($dom,%domconfig);
  642:     }
  643:     return $output;
  644: }
  645: 
  646: sub print_config_box {
  647:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  648:     my $rowtotal = 0;
  649:     my $output;
  650:     if ($action eq 'coursecategories') {
  651:         $output = &coursecategories_javascript($settings);
  652:     } elsif ($action eq 'defaults') {
  653:         $output = &defaults_javascript($settings); 
  654:     }
  655:     $output .=
  656:          '<table class="LC_nested_outer">
  657:           <tr>
  658:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  659:            &mt($item->{text}).'&nbsp;'.
  660:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  661:           '</tr>';
  662:     $rowtotal ++;
  663:     my $numheaders = 1;
  664:     if (ref($item->{'header'}) eq 'ARRAY') {
  665:         $numheaders = scalar(@{$item->{'header'}});
  666:     }
  667:     if ($numheaders > 1) {
  668:         my $colspan = '';
  669:         my $rightcolspan = '';
  670:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  671:             (($action eq 'login') && ($numheaders < 3))) {
  672:             $colspan = ' colspan="2"';
  673:         }
  674:         if ($action eq 'usersessions') {
  675:             $rightcolspan = ' colspan="3"'; 
  676:         }
  677:         $output .= '
  678:           <tr>
  679:            <td>
  680:             <table class="LC_nested">
  681:              <tr class="LC_info_row">
  682:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  683:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  684:              </tr>';
  685:         $rowtotal ++;
  686:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  687:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  688:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
  689:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  690:         } elsif ($action eq 'coursecategories') {
  691:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  692:         } elsif ($action eq 'login') {
  693:             if ($numheaders == 3) {
  694:                 $colspan = ' colspan="2"';
  695:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  696:             } else {
  697:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  698:             }
  699:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  700:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  701:         } elsif ($action eq 'rolecolors') {
  702:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  703:         }
  704:         $output .= '
  705:            </table>
  706:           </td>
  707:          </tr>
  708:          <tr>
  709:            <td>
  710:             <table class="LC_nested">
  711:              <tr class="LC_info_row">
  712:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  713:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  714:              </tr>';
  715:             $rowtotal ++;
  716:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  717:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  718:             ($action eq 'usersessions') || ($action eq 'coursecategories')) {
  719:             if ($action eq 'coursecategories') {
  720:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  721:                 $colspan = ' colspan="2"';
  722:             } else {
  723:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  724:             }
  725:             $output .= '
  726:            </table>
  727:           </td>
  728:          </tr>
  729:          <tr>
  730:            <td>
  731:             <table class="LC_nested">
  732:              <tr class="LC_info_row">
  733:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  734:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  735:              </tr>'."\n";
  736:             if ($action eq 'coursecategories') {
  737:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  738:             } else {
  739:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  740:             }
  741:             $rowtotal ++;
  742:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  743:                   ($action eq 'defaults')) {
  744:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  745:         } elsif ($action eq 'login') {
  746:             if ($numheaders == 3) {
  747:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  748:            </table>
  749:           </td>
  750:          </tr>
  751:          <tr>
  752:            <td>
  753:             <table class="LC_nested">
  754:              <tr class="LC_info_row">
  755:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  756:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  757:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  758:                 $rowtotal ++;
  759:             } else {
  760:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  761:             }
  762:         } elsif ($action eq 'requestcourses') {
  763:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal).
  764:                        &print_studentcode($settings,\$rowtotal).'
  765:            </table>
  766:           </td>
  767:          </tr>
  768:          <tr>
  769:            <td>
  770:             <table class="LC_nested">
  771:              <tr class="LC_info_row">
  772:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  773:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  774:                        &print_textbookcourses($dom,$settings,\$rowtotal).'
  775:             </table>
  776:            </td>
  777:           </tr>
  778:           <tr>
  779:            <td>
  780:             <table class="LC_nested">
  781:              <tr class="LC_info_row">
  782:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  783:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  784:              </tr>'.
  785:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  786:         } elsif ($action eq 'requestauthor') {
  787:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  788:         } elsif ($action eq 'rolecolors') {
  789:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  790:            </table>
  791:           </td>
  792:          </tr>
  793:          <tr>
  794:            <td>
  795:             <table class="LC_nested">
  796:              <tr class="LC_info_row">
  797:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  798:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  799:               <td class="LC_right_item" valign="top">'.
  800:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  801:              </tr>'.
  802:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  803:            </table>
  804:           </td>
  805:          </tr>
  806:          <tr>
  807:            <td>
  808:             <table class="LC_nested">
  809:              <tr class="LC_info_row">
  810:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  811:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  812:              </tr>'.
  813:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  814:             $rowtotal += 2;
  815:         }
  816:     } else {
  817:         $output .= '
  818:           <tr>
  819:            <td>
  820:             <table class="LC_nested">
  821:              <tr class="LC_info_row">';
  822:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  823:             $output .= '  
  824:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  825:         } elsif ($action eq 'serverstatuses') {
  826:             $output .= '
  827:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  828:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  829: 
  830:         } else {
  831:             $output .= '
  832:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  833:         }
  834:         if (defined($item->{'header'}->[0]->{'col3'})) {
  835:             $output .= '<td class="LC_left_item" valign="top">'.
  836:                        &mt($item->{'header'}->[0]->{'col2'});
  837:             if ($action eq 'serverstatuses') {
  838:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  839:             } 
  840:         } else {
  841:             $output .= '<td class="LC_right_item" valign="top">'.
  842:                        &mt($item->{'header'}->[0]->{'col2'});
  843:         }
  844:         $output .= '</td>';
  845:         if ($item->{'header'}->[0]->{'col3'}) {
  846:             if (defined($item->{'header'}->[0]->{'col4'})) {
  847:                 $output .= '<td class="LC_left_item" valign="top">'.
  848:                             &mt($item->{'header'}->[0]->{'col3'});
  849:             } else {
  850:                 $output .= '<td class="LC_right_item" valign="top">'.
  851:                            &mt($item->{'header'}->[0]->{'col3'});
  852:             }
  853:             if ($action eq 'serverstatuses') {
  854:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  855:             }
  856:             $output .= '</td>';
  857:         }
  858:         if ($item->{'header'}->[0]->{'col4'}) {
  859:             $output .= '<td class="LC_right_item" valign="top">'.
  860:                        &mt($item->{'header'}->[0]->{'col4'});
  861:         }
  862:         $output .= '</tr>';
  863:         $rowtotal ++;
  864:         if ($action eq 'quotas') {
  865:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  866:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  867:                  ($action eq 'contacts') || ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  868:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  869:         } elsif ($action eq 'scantron') {
  870:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  871:         } elsif ($action eq 'helpsettings') {
  872:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  873:         }
  874:     }
  875:     $output .= '
  876:    </table>
  877:   </td>
  878:  </tr>
  879: </table><br />';
  880:     return ($output,$rowtotal);
  881: }
  882: 
  883: sub print_login {
  884:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  885:     my ($css_class,$datatable);
  886:     my %choices = &login_choices();
  887: 
  888:     if ($caller eq 'service') {
  889:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  890:         my $choice = $choices{'disallowlogin'};
  891:         $css_class = ' class="LC_odd_row"';
  892:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  893:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  894:                       '<th>'.$choices{'server'}.'</th>'.
  895:                       '<th>'.$choices{'serverpath'}.'</th>'.
  896:                       '<th>'.$choices{'custompath'}.'</th>'.
  897:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  898:         my %disallowed;
  899:         if (ref($settings) eq 'HASH') {
  900:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  901:                %disallowed = %{$settings->{'loginvia'}};
  902:             }
  903:         }
  904:         foreach my $lonhost (sort(keys(%servers))) {
  905:             my $direct = 'selected="selected"';
  906:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  907:                 if ($disallowed{$lonhost}{'server'} ne '') {
  908:                     $direct = '';
  909:                 }
  910:             }
  911:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  912:                           '<td><select name="'.$lonhost.'_server">'.
  913:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  914:                           '</option>';
  915:             foreach my $hostid (sort(keys(%servers))) {
  916:                 next if ($servers{$hostid} eq $servers{$lonhost});
  917:                 my $selected = '';
  918:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  919:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  920:                         $selected = 'selected="selected"';
  921:                     }
  922:                 }
  923:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  924:                               $servers{$hostid}.'</option>';
  925:             }
  926:             $datatable .= '</select></td>'.
  927:                           '<td><select name="'.$lonhost.'_serverpath">';
  928:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  929:                 my $pathname = $path;
  930:                 if ($path eq 'custom') {
  931:                     $pathname = &mt('Custom Path').' ->';
  932:                 }
  933:                 my $selected = '';
  934:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  935:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  936:                         $selected = 'selected="selected"';
  937:                     }
  938:                 } elsif ($path eq '') {
  939:                     $selected = 'selected="selected"';
  940:                 }
  941:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  942:             }
  943:             $datatable .= '</select></td>';
  944:             my ($custom,$exempt);
  945:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  946:                 $custom = $disallowed{$lonhost}{'custompath'};
  947:                 $exempt = $disallowed{$lonhost}{'exempt'};
  948:             }
  949:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  950:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  951:                           '</tr>';
  952:         }
  953:         $datatable .= '</table></td></tr>';
  954:         return $datatable;
  955:     } elsif ($caller eq 'page') {
  956:         my %defaultchecked = ( 
  957:                                'coursecatalog' => 'on',
  958:                                'helpdesk'      => 'on',
  959:                                'adminmail'     => 'off',
  960:                                'newuser'       => 'off',
  961:                              );
  962:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  963:         my (%checkedon,%checkedoff);
  964:         foreach my $item (@toggles) {
  965:             if ($defaultchecked{$item} eq 'on') { 
  966:                 $checkedon{$item} = ' checked="checked" ';
  967:                 $checkedoff{$item} = ' ';
  968:             } elsif ($defaultchecked{$item} eq 'off') {
  969:                 $checkedoff{$item} = ' checked="checked" ';
  970:                 $checkedon{$item} = ' ';
  971:             }
  972:         }
  973:         my @images = ('img','logo','domlogo','login');
  974:         my @logintext = ('textcol','bgcol');
  975:         my @bgs = ('pgbg','mainbg','sidebg');
  976:         my @links = ('link','alink','vlink');
  977:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  978:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  979:         my (%is_custom,%designs);
  980:         my %defaults = (
  981:                        font => $defaultdesign{'login.font'},
  982:                        );
  983:         foreach my $item (@images) {
  984:             $defaults{$item} = $defaultdesign{'login.'.$item};
  985:             $defaults{'showlogo'}{$item} = 1;
  986:         }
  987:         foreach my $item (@bgs) {
  988:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  989:         }
  990:         foreach my $item (@logintext) {
  991:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  992:         }
  993:         foreach my $item (@links) {
  994:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  995:         }
  996:         if (ref($settings) eq 'HASH') {
  997:             foreach my $item (@toggles) {
  998:                 if ($settings->{$item} eq '1') {
  999:                     $checkedon{$item} =  ' checked="checked" ';
 1000:                     $checkedoff{$item} = ' ';
 1001:                 } elsif ($settings->{$item} eq '0') {
 1002:                     $checkedoff{$item} =  ' checked="checked" ';
 1003:                     $checkedon{$item} = ' ';
 1004:                 }
 1005:             }
 1006:             foreach my $item (@images) {
 1007:                 if (defined($settings->{$item})) {
 1008:                     $designs{$item} = $settings->{$item};
 1009:                     $is_custom{$item} = 1;
 1010:                 }
 1011:                 if (defined($settings->{'showlogo'}{$item})) {
 1012:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1013:                 }
 1014:             }
 1015:             foreach my $item (@logintext) {
 1016:                 if ($settings->{$item} ne '') {
 1017:                     $designs{'logintext'}{$item} = $settings->{$item};
 1018:                     $is_custom{$item} = 1;
 1019:                 }
 1020:             }
 1021:             if ($settings->{'font'} ne '') {
 1022:                 $designs{'font'} = $settings->{'font'};
 1023:                 $is_custom{'font'} = 1;
 1024:             }
 1025:             foreach my $item (@bgs) {
 1026:                 if ($settings->{$item} ne '') {
 1027:                     $designs{'bgs'}{$item} = $settings->{$item};
 1028:                     $is_custom{$item} = 1;
 1029:                 }
 1030:             }
 1031:             foreach my $item (@links) {
 1032:                 if ($settings->{$item} ne '') {
 1033:                     $designs{'links'}{$item} = $settings->{$item};
 1034:                     $is_custom{$item} = 1;
 1035:                 }
 1036:             }
 1037:         } else {
 1038:             if ($designhash{$dom.'.login.font'} ne '') {
 1039:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1040:                 $is_custom{'font'} = 1;
 1041:             }
 1042:             foreach my $item (@images) {
 1043:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1044:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1045:                     $is_custom{$item} = 1;
 1046:                 }
 1047:             }
 1048:             foreach my $item (@bgs) {
 1049:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1050:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1051:                     $is_custom{$item} = 1;
 1052:                 }
 1053:             }
 1054:             foreach my $item (@links) {
 1055:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1056:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1057:                     $is_custom{$item} = 1;
 1058:                 }
 1059:             }
 1060:         }
 1061:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1062:                                                       logo => 'Institution Logo',
 1063:                                                       domlogo => 'Domain Logo',
 1064:                                                       login => 'Login box');
 1065:         my $itemcount = 1;
 1066:         foreach my $item (@toggles) {
 1067:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1068:             $datatable .=  
 1069:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1070:                 '</td><td>'.
 1071:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1072:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1073:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1074:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1075:                 '</tr>';
 1076:             $itemcount ++;
 1077:         }
 1078:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1079:         $datatable .= '</tr></table></td></tr>';
 1080:     } elsif ($caller eq 'help') {
 1081:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1082:         my $switchserver = &check_switchserver($dom,$confname);
 1083:         my $itemcount = 1;
 1084:         $defaulturl = '/adm/loginproblems.html';
 1085:         $defaulttype = 'default';
 1086:         %lt = &Apache::lonlocal::texthash (
 1087:                      del     => 'Delete?',
 1088:                      rep     => 'Replace:',
 1089:                      upl     => 'Upload:',
 1090:                      default => 'Default',
 1091:                      custom  => 'Custom',
 1092:                                              );
 1093:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1094:         my @currlangs;
 1095:         if (ref($settings) eq 'HASH') {
 1096:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1097:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1098:                     next if ($settings->{'helpurl'}{$key} eq '');
 1099:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1100:                     $type{$key} = 'custom';
 1101:                     unless ($key eq 'nolang') {
 1102:                         push(@currlangs,$key);
 1103:                     }
 1104:                 }
 1105:             } elsif ($settings->{'helpurl'} ne '') {
 1106:                 $type{'nolang'} = 'custom';
 1107:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1108:             }
 1109:         }
 1110:         foreach my $lang ('nolang',sort(@currlangs)) {
 1111:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1112:             $datatable .= '<tr'.$css_class.'>';
 1113:             if ($url{$lang} eq '') {
 1114:                 $url{$lang} = $defaulturl;
 1115:             }
 1116:             if ($type{$lang} eq '') {
 1117:                 $type{$lang} = $defaulttype;
 1118:             }
 1119:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1120:             if ($lang eq 'nolang') {
 1121:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1122:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1123:             } else {
 1124:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1125:                                   $langchoices{$lang},
 1126:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1127:             }
 1128:             $datatable .= '</span></td>'."\n".
 1129:                           '<td class="LC_left_item">';
 1130:             if ($type{$lang} eq 'custom') {
 1131:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1132:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1133:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1134:             } else {
 1135:                 $datatable .= $lt{'upl'};
 1136:             }
 1137:             $datatable .='<br />';
 1138:             if ($switchserver) {
 1139:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1140:             } else {
 1141:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1142:             }
 1143:             $datatable .= '</td></tr>';
 1144:             $itemcount ++;
 1145:         }
 1146:         my @addlangs;
 1147:         foreach my $lang (sort(keys(%langchoices))) {
 1148:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1149:             push(@addlangs,$lang);
 1150:         }
 1151:         if (@addlangs > 0) {
 1152:             my %toadd;
 1153:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1154:             $toadd{''} = &mt('Select');
 1155:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1156:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1157:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1158:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1159:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1160:             if ($switchserver) {
 1161:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1162:             } else {
 1163:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1164:             }
 1165:             $datatable .= '</td></tr>';
 1166:             $itemcount ++;
 1167:         }
 1168:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1169:     }
 1170:     return $datatable;
 1171: }
 1172: 
 1173: sub login_choices {
 1174:     my %choices =
 1175:         &Apache::lonlocal::texthash (
 1176:             coursecatalog => 'Display Course/Community Catalog link?',
 1177:             adminmail     => "Display Administrator's E-mail Address?",
 1178:             helpdesk      => 'Display "Contact Helpdesk" link',
 1179:             disallowlogin => "Login page requests redirected",
 1180:             hostid        => "Server",
 1181:             server        => "Redirect to:",
 1182:             serverpath    => "Path",
 1183:             custompath    => "Custom", 
 1184:             exempt        => "Exempt IP(s)",
 1185:             directlogin   => "No redirect",
 1186:             newuser       => "Link to create a user account",
 1187:             img           => "Header",
 1188:             logo          => "Main Logo",
 1189:             domlogo       => "Domain Logo",
 1190:             login         => "Log-in Header", 
 1191:             textcol       => "Text color",
 1192:             bgcol         => "Box color",
 1193:             bgs           => "Background colors",
 1194:             links         => "Link colors",
 1195:             font          => "Font color",
 1196:             pgbg          => "Header",
 1197:             mainbg        => "Page",
 1198:             sidebg        => "Login box",
 1199:             link          => "Link",
 1200:             alink         => "Active link",
 1201:             vlink         => "Visited link",
 1202:         );
 1203:     return %choices;
 1204: }
 1205: 
 1206: sub print_rolecolors {
 1207:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1208:     my %choices = &color_font_choices();
 1209:     my @bgs = ('pgbg','tabbg','sidebg');
 1210:     my @links = ('link','alink','vlink');
 1211:     my @images = ('img');
 1212:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1213:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1214:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1215:     my (%is_custom,%designs);
 1216:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1217:     if (ref($settings) eq 'HASH') {
 1218:         if (ref($settings->{$role}) eq 'HASH') {
 1219:             if ($settings->{$role}->{'img'} ne '') {
 1220:                 $designs{'img'} = $settings->{$role}->{'img'};
 1221:                 $is_custom{'img'} = 1;
 1222:             }
 1223:             if ($settings->{$role}->{'font'} ne '') {
 1224:                 $designs{'font'} = $settings->{$role}->{'font'};
 1225:                 $is_custom{'font'} = 1;
 1226:             }
 1227:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1228:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1229:                 $is_custom{'fontmenu'} = 1;
 1230:             }
 1231:             foreach my $item (@bgs) {
 1232:                 if ($settings->{$role}->{$item} ne '') {
 1233:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1234:                     $is_custom{$item} = 1;
 1235:                 }
 1236:             }
 1237:             foreach my $item (@links) {
 1238:                 if ($settings->{$role}->{$item} ne '') {
 1239:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1240:                     $is_custom{$item} = 1;
 1241:                 }
 1242:             }
 1243:         }
 1244:     } else {
 1245:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1246:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1247:             $is_custom{'img'} = 1;
 1248:         }
 1249:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1250:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1251:             $is_custom{'fontmenu'} = 1; 
 1252:         }
 1253:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1254:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1255:             $is_custom{'font'} = 1;
 1256:         }
 1257:         foreach my $item (@bgs) {
 1258:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1259:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1260:                 $is_custom{$item} = 1;
 1261:             
 1262:             }
 1263:         }
 1264:         foreach my $item (@links) {
 1265:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1266:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1267:                 $is_custom{$item} = 1;
 1268:             }
 1269:         }
 1270:     }
 1271:     my $itemcount = 1;
 1272:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1273:     $datatable .= '</tr></table></td></tr>';
 1274:     return $datatable;
 1275: }
 1276: 
 1277: sub role_defaults {
 1278:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1279:     my %defaults;
 1280:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1281:         return %defaults;
 1282:     }
 1283:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1284:     if ($role eq 'login') {
 1285:         %defaults = (
 1286:                        font => $defaultdesign{$role.'.font'},
 1287:                     );
 1288:         if (ref($logintext) eq 'ARRAY') {
 1289:             foreach my $item (@{$logintext}) {
 1290:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1291:             }
 1292:         }
 1293:         foreach my $item (@{$images}) {
 1294:             $defaults{'showlogo'}{$item} = 1;
 1295:         }
 1296:     } else {
 1297:         %defaults = (
 1298:                        img => $defaultdesign{$role.'.img'},
 1299:                        font => $defaultdesign{$role.'.font'},
 1300:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1301:                     );
 1302:     }
 1303:     foreach my $item (@{$bgs}) {
 1304:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1305:     }
 1306:     foreach my $item (@{$links}) {
 1307:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1308:     }
 1309:     foreach my $item (@{$images}) {
 1310:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1311:     }
 1312:     return %defaults;
 1313: }
 1314: 
 1315: sub display_color_options {
 1316:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1317:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1318:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1319:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1320:     my $datatable = '<tr'.$css_class.'>'.
 1321:         '<td>'.$choices->{'font'}.'</td>';
 1322:     if (!$is_custom->{'font'}) {
 1323:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1324:     } else {
 1325:         $datatable .= '<td>&nbsp;</td>';
 1326:     }
 1327:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1328: 
 1329:     $datatable .= '<td><span class="LC_nobreak">'.
 1330:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1331:                   ' value="'.$current_color.'" />&nbsp;'.
 1332:                   '&nbsp;</td></tr>';
 1333:     unless ($role eq 'login') { 
 1334:         $datatable .= '<tr'.$css_class.'>'.
 1335:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1336:         if (!$is_custom->{'fontmenu'}) {
 1337:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1338:         } else {
 1339:             $datatable .= '<td>&nbsp;</td>';
 1340:         }
 1341: 	$current_color = $designs->{'fontmenu'} ?
 1342: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1343:         $datatable .= '<td><span class="LC_nobreak">'.
 1344:                       '<input class="colorchooser" type="text" size="10" name="'
 1345: 		      .$role.'_fontmenu"'.
 1346:                       ' value="'.$current_color.'" />&nbsp;'.
 1347:                       '&nbsp;</td></tr>';
 1348:     }
 1349:     my $switchserver = &check_switchserver($dom,$confname);
 1350:     foreach my $img (@{$images}) {
 1351: 	$itemcount ++;
 1352:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1353:         $datatable .= '<tr'.$css_class.'>'.
 1354:                       '<td>'.$choices->{$img};
 1355:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1356:         if ($role eq 'login') {
 1357:             if ($img eq 'login') {
 1358:                 $login_hdr_pick =
 1359:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1360:                 $logincolors =
 1361:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1362:                                        $designs,$defaults);
 1363:             } elsif ($img ne 'domlogo') {
 1364:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1365:             }
 1366:         }
 1367:         $datatable .= '</td>';
 1368:         if ($designs->{$img} ne '') {
 1369:             $imgfile = $designs->{$img};
 1370: 	    $img_import = ($imgfile =~ m{^/adm/});
 1371:         } else {
 1372:             $imgfile = $defaults->{$img};
 1373:         }
 1374:         if ($imgfile) {
 1375:             my ($showfile,$fullsize);
 1376:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1377:                 my $urldir = $1;
 1378:                 my $filename = $2;
 1379:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1380:                 if (@info) {
 1381:                     my $thumbfile = 'tn-'.$filename;
 1382:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1383:                     if (@thumb) {
 1384:                         $showfile = $urldir.'/'.$thumbfile;
 1385:                     } else {
 1386:                         $showfile = $imgfile;
 1387:                     }
 1388:                 } else {
 1389:                     $showfile = '';
 1390:                 }
 1391:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1392:                 $showfile = $imgfile;
 1393:                 my $imgdir = $1;
 1394:                 my $filename = $2;
 1395:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1396:                     $showfile = "/$imgdir/tn-".$filename;
 1397:                 } else {
 1398:                     my $input = $londocroot.$imgfile;
 1399:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1400:                     if (!-e $output) {
 1401:                         my ($width,$height) = &thumb_dimensions();
 1402:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1403:                         if ($fullwidth ne '' && $fullheight ne '') {
 1404:                             if ($fullwidth > $width && $fullheight > $height) { 
 1405:                                 my $size = $width.'x'.$height;
 1406:                                 system("convert -sample $size $input $output");
 1407:                                 $showfile = "/$imgdir/tn-".$filename;
 1408:                             }
 1409:                         }
 1410:                     }
 1411:                 }
 1412:             }
 1413:             if ($showfile) {
 1414:                 if ($showfile =~ m{^/(adm|res)/}) {
 1415:                     if ($showfile =~ m{^/res/}) {
 1416:                         my $local_showfile =
 1417:                             &Apache::lonnet::filelocation('',$showfile);
 1418:                         &Apache::lonnet::repcopy($local_showfile);
 1419:                     }
 1420:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1421:                 }
 1422:                 if ($imgfile) {
 1423:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1424:                         if ($imgfile =~ m{^/res/}) {
 1425:                             my $local_imgfile =
 1426:                                 &Apache::lonnet::filelocation('',$imgfile);
 1427:                             &Apache::lonnet::repcopy($local_imgfile);
 1428:                         }
 1429:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1430:                     } else {
 1431:                         $fullsize = $imgfile;
 1432:                     }
 1433:                 }
 1434:                 $datatable .= '<td>';
 1435:                 if ($img eq 'login') {
 1436:                     $datatable .= $login_hdr_pick;
 1437:                 } 
 1438:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1439:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1440:             } else {
 1441:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1442:                               &mt('Upload:').'<br />';
 1443:             }
 1444:         } else {
 1445:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1446:                           &mt('Upload:').'<br />';
 1447:         }
 1448:         if ($switchserver) {
 1449:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1450:         } else {
 1451:             if ($img ne 'login') { # suppress file selection for Log-in header
 1452:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1453:             }
 1454:         }
 1455:         $datatable .= '</td></tr>';
 1456:     }
 1457:     $itemcount ++;
 1458:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1459:     $datatable .= '<tr'.$css_class.'>'.
 1460:                   '<td>'.$choices->{'bgs'}.'</td>';
 1461:     my $bgs_def;
 1462:     foreach my $item (@{$bgs}) {
 1463:         if (!$is_custom->{$item}) {
 1464:             $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>';
 1465:         }
 1466:     }
 1467:     if ($bgs_def) {
 1468:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1469:     } else {
 1470:         $datatable .= '<td>&nbsp;</td>';
 1471:     }
 1472:     $datatable .= '<td class="LC_right_item">'.
 1473:                   '<table border="0"><tr>';
 1474: 
 1475:     foreach my $item (@{$bgs}) {
 1476:         $datatable .= '<td align="center">'.$choices->{$item};
 1477: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1478:         if ($designs->{'bgs'}{$item}) {
 1479:             $datatable .= '&nbsp;';
 1480:         }
 1481:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1482:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1483:     }
 1484:     $datatable .= '</tr></table></td></tr>';
 1485:     $itemcount ++;
 1486:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1487:     $datatable .= '<tr'.$css_class.'>'.
 1488:                   '<td>'.$choices->{'links'}.'</td>';
 1489:     my $links_def;
 1490:     foreach my $item (@{$links}) {
 1491:         if (!$is_custom->{$item}) {
 1492:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1493:         }
 1494:     }
 1495:     if ($links_def) {
 1496:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1497:     } else {
 1498:         $datatable .= '<td>&nbsp;</td>';
 1499:     }
 1500:     $datatable .= '<td class="LC_right_item">'.
 1501:                   '<table border="0"><tr>';
 1502:     foreach my $item (@{$links}) {
 1503: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1504:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1505:         if ($designs->{'links'}{$item}) {
 1506:             $datatable.='&nbsp;';
 1507:         }
 1508:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1509:                       '" /></td>';
 1510:     }
 1511:     $$rowtotal += $itemcount;
 1512:     return $datatable;
 1513: }
 1514: 
 1515: sub logo_display_options {
 1516:     my ($img,$defaults,$designs) = @_;
 1517:     my $checkedon;
 1518:     if (ref($defaults) eq 'HASH') {
 1519:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1520:             if ($defaults->{'showlogo'}{$img}) {
 1521:                 $checkedon = 'checked="checked" ';     
 1522:             }
 1523:         } 
 1524:     }
 1525:     if (ref($designs) eq 'HASH') {
 1526:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1527:             if (defined($designs->{'showlogo'}{$img})) {
 1528:                 if ($designs->{'showlogo'}{$img} == 0) {
 1529:                     $checkedon = '';
 1530:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1531:                     $checkedon = 'checked="checked" ';
 1532:                 }
 1533:             }
 1534:         }
 1535:     }
 1536:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1537:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1538:            &mt('show').'</label>'."\n";
 1539: }
 1540: 
 1541: sub login_header_options  {
 1542:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1543:     my $output = '';
 1544:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1545:         $output .= &mt('Text default(s):').'<br />';
 1546:         if (!$is_custom->{'textcol'}) {
 1547:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1548:                        '&nbsp;&nbsp;&nbsp;';
 1549:         }
 1550:         if (!$is_custom->{'bgcol'}) {
 1551:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1552:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1553:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1554:         }
 1555:         $output .= '<br />';
 1556:     }
 1557:     $output .='<br />';
 1558:     return $output;
 1559: }
 1560: 
 1561: sub login_text_colors {
 1562:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1563:     my $color_menu = '<table border="0"><tr>';
 1564:     foreach my $item (@{$logintext}) {
 1565:         $color_menu .= '<td align="center">'.$choices->{$item};
 1566:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1567:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1568:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1569:     }
 1570:     $color_menu .= '</tr></table><br />';
 1571:     return $color_menu;
 1572: }
 1573: 
 1574: sub image_changes {
 1575:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1576:     my $output;
 1577:     if ($img eq 'login') {
 1578:             # suppress image for Log-in header
 1579:     } elsif (!$is_custom) {
 1580:         if ($img ne 'domlogo') {
 1581:             $output .= &mt('Default image:').'<br />';
 1582:         } else {
 1583:             $output .= &mt('Default in use:').'<br />';
 1584:         }
 1585:     }
 1586:     if ($img eq 'login') { # suppress image for Log-in header
 1587:         $output .= '<td>'.$logincolors;
 1588:     } else {
 1589:         if ($img_import) {
 1590:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1591:         }
 1592:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1593:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1594:         if ($is_custom) {
 1595:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1596:                        '<input type="checkbox" name="'.
 1597:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1598:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1599:         } else {
 1600:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1601:         }
 1602:     }
 1603:     return $output;
 1604: }
 1605: 
 1606: sub print_quotas {
 1607:     my ($dom,$settings,$rowtotal,$action) = @_;
 1608:     my $context;
 1609:     if ($action eq 'quotas') {
 1610:         $context = 'tools';
 1611:     } else {
 1612:         $context = $action;
 1613:     }
 1614:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1615:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1616:     my $typecount = 0;
 1617:     my ($css_class,%titles);
 1618:     if ($context eq 'requestcourses') {
 1619:         @usertools = ('official','unofficial','community','textbook');
 1620:         @options =('norequest','approval','validate','autolimit');
 1621:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1622:         %titles = &courserequest_titles();
 1623:     } elsif ($context eq 'requestauthor') {
 1624:         @usertools = ('author');
 1625:         @options = ('norequest','approval','automatic');
 1626:         %titles = &authorrequest_titles();
 1627:     } else {
 1628:         @usertools = ('aboutme','blog','webdav','portfolio');
 1629:         %titles = &tool_titles();
 1630:     }
 1631:     if (ref($types) eq 'ARRAY') {
 1632:         foreach my $type (@{$types}) {
 1633:             my ($currdefquota,$currauthorquota);
 1634:             unless (($context eq 'requestcourses') ||
 1635:                     ($context eq 'requestauthor')) {
 1636:                 if (ref($settings) eq 'HASH') {
 1637:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1638:                         $currdefquota = $settings->{defaultquota}->{$type};
 1639:                     } else {
 1640:                         $currdefquota = $settings->{$type};
 1641:                     }
 1642:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1643:                         $currauthorquota = $settings->{authorquota}->{$type};
 1644:                     }
 1645:                 }
 1646:             }
 1647:             if (defined($usertypes->{$type})) {
 1648:                 $typecount ++;
 1649:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1650:                 $datatable .= '<tr'.$css_class.'>'.
 1651:                               '<td>'.$usertypes->{$type}.'</td>'.
 1652:                               '<td class="LC_left_item">';
 1653:                 if ($context eq 'requestcourses') {
 1654:                     $datatable .= '<table><tr>';
 1655:                 }
 1656:                 my %cell;  
 1657:                 foreach my $item (@usertools) {
 1658:                     if ($context eq 'requestcourses') {
 1659:                         my ($curroption,$currlimit);
 1660:                         if (ref($settings) eq 'HASH') {
 1661:                             if (ref($settings->{$item}) eq 'HASH') {
 1662:                                 $curroption = $settings->{$item}->{$type};
 1663:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1664:                                     $currlimit = $1; 
 1665:                                 }
 1666:                             }
 1667:                         }
 1668:                         if (!$curroption) {
 1669:                             $curroption = 'norequest';
 1670:                         }
 1671:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1672:                         foreach my $option (@options) {
 1673:                             my $val = $option;
 1674:                             if ($option eq 'norequest') {
 1675:                                 $val = 0;  
 1676:                             }
 1677:                             if ($option eq 'validate') {
 1678:                                 my $canvalidate = 0;
 1679:                                 if (ref($validations{$item}) eq 'HASH') { 
 1680:                                     if ($validations{$item}{$type}) {
 1681:                                         $canvalidate = 1;
 1682:                                     }
 1683:                                 }
 1684:                                 next if (!$canvalidate);
 1685:                             }
 1686:                             my $checked = '';
 1687:                             if ($option eq $curroption) {
 1688:                                 $checked = ' checked="checked"';
 1689:                             } elsif ($option eq 'autolimit') {
 1690:                                 if ($curroption =~ /^autolimit/) {
 1691:                                     $checked = ' checked="checked"';
 1692:                                 }                       
 1693:                             } 
 1694:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1695:                                   '<input type="radio" name="crsreq_'.$item.
 1696:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1697:                                   $titles{$option}.'</label>';
 1698:                             if ($option eq 'autolimit') {
 1699:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1700:                                                 $item.'_limit_'.$type.'" size="1" '.
 1701:                                                 'value="'.$currlimit.'" />';
 1702:                             }
 1703:                             $cell{$item} .= '</span> ';
 1704:                             if ($option eq 'autolimit') {
 1705:                                 $cell{$item} .= $titles{'unlimited'};
 1706:                             }
 1707:                         }
 1708:                     } elsif ($context eq 'requestauthor') {
 1709:                         my $curroption;
 1710:                         if (ref($settings) eq 'HASH') {
 1711:                             $curroption = $settings->{$type};
 1712:                         }
 1713:                         if (!$curroption) {
 1714:                             $curroption = 'norequest';
 1715:                         }
 1716:                         foreach my $option (@options) {
 1717:                             my $val = $option;
 1718:                             if ($option eq 'norequest') {
 1719:                                 $val = 0;
 1720:                             }
 1721:                             my $checked = '';
 1722:                             if ($option eq $curroption) {
 1723:                                 $checked = ' checked="checked"';
 1724:                             }
 1725:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1726:                                   '<input type="radio" name="authorreq_'.$type.
 1727:                                   '" value="'.$val.'"'.$checked.' />'.
 1728:                                   $titles{$option}.'</label></span>&nbsp; ';
 1729:                         }
 1730:                     } else {
 1731:                         my $checked = 'checked="checked" ';
 1732:                         if (ref($settings) eq 'HASH') {
 1733:                             if (ref($settings->{$item}) eq 'HASH') {
 1734:                                 if ($settings->{$item}->{$type} == 0) {
 1735:                                     $checked = '';
 1736:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1737:                                     $checked =  'checked="checked" ';
 1738:                                 }
 1739:                             }
 1740:                         }
 1741:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1742:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1743:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1744:                                       '</label></span>&nbsp; ';
 1745:                     }
 1746:                 }
 1747:                 if ($context eq 'requestcourses') {
 1748:                     $datatable .= '</tr><tr>';
 1749:                     foreach my $item (@usertools) {
 1750:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1751:                     }
 1752:                     $datatable .= '</tr></table>';
 1753:                 }
 1754:                 $datatable .= '</td>';
 1755:                 unless (($context eq 'requestcourses') ||
 1756:                         ($context eq 'requestauthor')) {
 1757:                     $datatable .= 
 1758:                               '<td class="LC_right_item">'.
 1759:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1760:                               '<input type="text" name="quota_'.$type.
 1761:                               '" value="'.$currdefquota.
 1762:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1763:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1764:                               '<input type="text" name="authorquota_'.$type.
 1765:                               '" value="'.$currauthorquota.
 1766:                               '" size="5" /></span></td>';
 1767:                 }
 1768:                 $datatable .= '</tr>';
 1769:             }
 1770:         }
 1771:     }
 1772:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1773:         $defaultquota = '20';
 1774:         $authorquota = '500';
 1775:         if (ref($settings) eq 'HASH') {
 1776:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1777:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1778:             } elsif (defined($settings->{'default'})) {
 1779:                 $defaultquota = $settings->{'default'};
 1780:             }
 1781:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1782:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1783:             }
 1784:         }
 1785:     }
 1786:     $typecount ++;
 1787:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1788:     $datatable .= '<tr'.$css_class.'>'.
 1789:                   '<td>'.$othertitle.'</td>'.
 1790:                   '<td class="LC_left_item">';
 1791:     if ($context eq 'requestcourses') {
 1792:         $datatable .= '<table><tr>';
 1793:     }
 1794:     my %defcell;
 1795:     foreach my $item (@usertools) {
 1796:         if ($context eq 'requestcourses') {
 1797:             my ($curroption,$currlimit);
 1798:             if (ref($settings) eq 'HASH') {
 1799:                 if (ref($settings->{$item}) eq 'HASH') {
 1800:                     $curroption = $settings->{$item}->{'default'};
 1801:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1802:                         $currlimit = $1;
 1803:                     }
 1804:                 }
 1805:             }
 1806:             if (!$curroption) {
 1807:                 $curroption = 'norequest';
 1808:             }
 1809:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1810:             foreach my $option (@options) {
 1811:                 my $val = $option;
 1812:                 if ($option eq 'norequest') {
 1813:                     $val = 0;
 1814:                 }
 1815:                 if ($option eq 'validate') {
 1816:                     my $canvalidate = 0;
 1817:                     if (ref($validations{$item}) eq 'HASH') {
 1818:                         if ($validations{$item}{'default'}) {
 1819:                             $canvalidate = 1;
 1820:                         }
 1821:                     }
 1822:                     next if (!$canvalidate);
 1823:                 }
 1824:                 my $checked = '';
 1825:                 if ($option eq $curroption) {
 1826:                     $checked = ' checked="checked"';
 1827:                 } elsif ($option eq 'autolimit') {
 1828:                     if ($curroption =~ /^autolimit/) {
 1829:                         $checked = ' checked="checked"';
 1830:                     }
 1831:                 }
 1832:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1833:                                   '<input type="radio" name="crsreq_'.$item.
 1834:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1835:                                   $titles{$option}.'</label>';
 1836:                 if ($option eq 'autolimit') {
 1837:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1838:                                        $item.'_limit_default" size="1" '.
 1839:                                        'value="'.$currlimit.'" />';
 1840:                 }
 1841:                 $defcell{$item} .= '</span> ';
 1842:                 if ($option eq 'autolimit') {
 1843:                     $defcell{$item} .= $titles{'unlimited'};
 1844:                 }
 1845:             }
 1846:         } elsif ($context eq 'requestauthor') {
 1847:             my $curroption;
 1848:             if (ref($settings) eq 'HASH') {
 1849:                 $curroption = $settings->{'default'};
 1850:             }
 1851:             if (!$curroption) {
 1852:                 $curroption = 'norequest';
 1853:             }
 1854:             foreach my $option (@options) {
 1855:                 my $val = $option;
 1856:                 if ($option eq 'norequest') {
 1857:                     $val = 0;
 1858:                 }
 1859:                 my $checked = '';
 1860:                 if ($option eq $curroption) {
 1861:                     $checked = ' checked="checked"';
 1862:                 }
 1863:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1864:                               '<input type="radio" name="authorreq_default"'.
 1865:                               ' value="'.$val.'"'.$checked.' />'.
 1866:                               $titles{$option}.'</label></span>&nbsp; ';
 1867:             }
 1868:         } else {
 1869:             my $checked = 'checked="checked" ';
 1870:             if (ref($settings) eq 'HASH') {
 1871:                 if (ref($settings->{$item}) eq 'HASH') {
 1872:                     if ($settings->{$item}->{'default'} == 0) {
 1873:                         $checked = '';
 1874:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1875:                         $checked = 'checked="checked" ';
 1876:                     }
 1877:                 }
 1878:             }
 1879:             $datatable .= '<span class="LC_nobreak"><label>'.
 1880:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1881:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1882:                           '</label></span>&nbsp; ';
 1883:         }
 1884:     }
 1885:     if ($context eq 'requestcourses') {
 1886:         $datatable .= '</tr><tr>';
 1887:         foreach my $item (@usertools) {
 1888:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1889:         }
 1890:         $datatable .= '</tr></table>';
 1891:     }
 1892:     $datatable .= '</td>';
 1893:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1894:         $datatable .= '<td class="LC_right_item">'.
 1895:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1896:                       '<input type="text" name="defaultquota" value="'.
 1897:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1898:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1899:                       '<input type="text" name="authorquota" value="'.
 1900:                       $authorquota.'" size="5" /></span></td>';
 1901:     }
 1902:     $datatable .= '</tr>';
 1903:     $typecount ++;
 1904:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1905:     $datatable .= '<tr'.$css_class.'>'.
 1906:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1907:     if ($context eq 'requestcourses') {
 1908:         $datatable .= &mt('(overrides affiliation, if set)').
 1909:                       '</td>'.
 1910:                       '<td class="LC_left_item">'.
 1911:                       '<table><tr>';
 1912:     } else {
 1913:         $datatable .= &mt('(overrides affiliation, if checked)').
 1914:                       '</td>'.
 1915:                       '<td class="LC_left_item" colspan="2">'.
 1916:                       '<br />';
 1917:     }
 1918:     my %advcell;
 1919:     foreach my $item (@usertools) {
 1920:         if ($context eq 'requestcourses') {
 1921:             my ($curroption,$currlimit);
 1922:             if (ref($settings) eq 'HASH') {
 1923:                 if (ref($settings->{$item}) eq 'HASH') {
 1924:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1925:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1926:                         $currlimit = $1;
 1927:                     }
 1928:                 }
 1929:             }
 1930:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1931:             my $checked = '';
 1932:             if ($curroption eq '') {
 1933:                 $checked = ' checked="checked"';
 1934:             }
 1935:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1936:                                '<input type="radio" name="crsreq_'.$item.
 1937:                                '__LC_adv" value=""'.$checked.' />'.
 1938:                                &mt('No override set').'</label></span>&nbsp; ';
 1939:             foreach my $option (@options) {
 1940:                 my $val = $option;
 1941:                 if ($option eq 'norequest') {
 1942:                     $val = 0;
 1943:                 }
 1944:                 if ($option eq 'validate') {
 1945:                     my $canvalidate = 0;
 1946:                     if (ref($validations{$item}) eq 'HASH') {
 1947:                         if ($validations{$item}{'_LC_adv'}) {
 1948:                             $canvalidate = 1;
 1949:                         }
 1950:                     }
 1951:                     next if (!$canvalidate);
 1952:                 }
 1953:                 my $checked = '';
 1954:                 if ($val eq $curroption) {
 1955:                     $checked = ' checked="checked"';
 1956:                 } elsif ($option eq 'autolimit') {
 1957:                     if ($curroption =~ /^autolimit/) {
 1958:                         $checked = ' checked="checked"';
 1959:                     }
 1960:                 }
 1961:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1962:                                   '<input type="radio" name="crsreq_'.$item.
 1963:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1964:                                   $titles{$option}.'</label>';
 1965:                 if ($option eq 'autolimit') {
 1966:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1967:                                        $item.'_limit__LC_adv" size="1" '.
 1968:                                        'value="'.$currlimit.'" />';
 1969:                 }
 1970:                 $advcell{$item} .= '</span> ';
 1971:                 if ($option eq 'autolimit') {
 1972:                     $advcell{$item} .= $titles{'unlimited'};
 1973:                 }
 1974:             }
 1975:         } elsif ($context eq 'requestauthor') {
 1976:             my $curroption;
 1977:             if (ref($settings) eq 'HASH') {
 1978:                 $curroption = $settings->{'_LC_adv'};
 1979:             }
 1980:             my $checked = '';
 1981:             if ($curroption eq '') {
 1982:                 $checked = ' checked="checked"';
 1983:             }
 1984:             $datatable .= '<span class="LC_nobreak"><label>'.
 1985:                           '<input type="radio" name="authorreq__LC_adv"'.
 1986:                           ' value=""'.$checked.' />'.
 1987:                           &mt('No override set').'</label></span>&nbsp; ';
 1988:             foreach my $option (@options) {
 1989:                 my $val = $option;
 1990:                 if ($option eq 'norequest') {
 1991:                     $val = 0;
 1992:                 }
 1993:                 my $checked = '';
 1994:                 if ($val eq $curroption) {
 1995:                     $checked = ' checked="checked"';
 1996:                 }
 1997:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1998:                               '<input type="radio" name="authorreq__LC_adv"'.
 1999:                               ' value="'.$val.'"'.$checked.' />'.
 2000:                               $titles{$option}.'</label></span>&nbsp; ';
 2001:             }
 2002:         } else {
 2003:             my $checked = 'checked="checked" ';
 2004:             if (ref($settings) eq 'HASH') {
 2005:                 if (ref($settings->{$item}) eq 'HASH') {
 2006:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2007:                         $checked = '';
 2008:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2009:                         $checked = 'checked="checked" ';
 2010:                     }
 2011:                 }
 2012:             }
 2013:             $datatable .= '<span class="LC_nobreak"><label>'.
 2014:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2015:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2016:                           '</label></span>&nbsp; ';
 2017:         }
 2018:     }
 2019:     if ($context eq 'requestcourses') {
 2020:         $datatable .= '</tr><tr>';
 2021:         foreach my $item (@usertools) {
 2022:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2023:         }
 2024:         $datatable .= '</tr></table>';
 2025:     }
 2026:     $datatable .= '</td></tr>';
 2027:     $$rowtotal += $typecount;
 2028:     return $datatable;
 2029: }
 2030: 
 2031: sub print_requestmail {
 2032:     my ($dom,$action,$settings,$rowtotal) = @_;
 2033:     my ($now,$datatable,%currapp);
 2034:     $now = time;
 2035:     if (ref($settings) eq 'HASH') {
 2036:         if (ref($settings->{'notify'}) eq 'HASH') {
 2037:             if ($settings->{'notify'}{'approval'} ne '') {
 2038:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2039:             }
 2040:         }
 2041:     }
 2042:     my $numinrow = 2;
 2043:     my $css_class;
 2044:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2045:     my $text;
 2046:     if ($action eq 'requestcourses') {
 2047:         $text = &mt('Receive notification of course requests requiring approval');
 2048:     } elsif ($action eq 'requestauthor') {
 2049:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2050:     } else {
 2051:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2052:     }
 2053:     $datatable = '<tr'.$css_class.'>'.
 2054:                  ' <td>'.$text.'</td>'.
 2055:                  ' <td class="LC_left_item">';
 2056:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2057:                                                  $action.'notifyapproval',%currapp);
 2058:     if ($numdc > 0) {
 2059:         $datatable .= $table;
 2060:     } else {
 2061:         $datatable .= &mt('There are no active Domain Coordinators');
 2062:     }
 2063:     $datatable .='</td></tr>';
 2064:     $$rowtotal += $rows;
 2065:     return $datatable;
 2066: }
 2067: 
 2068: sub print_studentcode {
 2069:     my ($settings,$rowtotal) = @_;
 2070:     my $rownum = 0; 
 2071:     my ($output,%current);
 2072:     my @crstypes = ('official','unofficial','community','textbook');
 2073:     if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2074:         foreach my $type (@crstypes) {
 2075:             $current{$type} = $settings->{'uniquecode'}{$type};
 2076:         }
 2077:     }
 2078:     $output .= '<tr>'.
 2079:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2080:                '<td class="LC_left_item">';
 2081:     foreach my $type (@crstypes) {
 2082:         my $check = ' ';
 2083:         if ($current{$type}) {
 2084:             $check = ' checked="checked" ';
 2085:         }
 2086:         $output .= '<span class="LC_nobreak"><label>'.
 2087:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2088:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2089:     }
 2090:     $output .= '</td></tr>';
 2091:     $$rowtotal ++;
 2092:     return $output;
 2093: }
 2094: 
 2095: sub print_textbookcourses {
 2096:     my ($dom,$settings,$rowtotal) = @_;
 2097:     my $rownum = 0;
 2098:     my $css_class;
 2099:     my $itemcount = 1;
 2100:     my $maxnum = 0;
 2101:     my $bookshash;
 2102:     if (ref($settings) eq 'HASH') {
 2103:         $bookshash = $settings->{'textbooks'};
 2104:     }
 2105:     my %ordered;
 2106:     if (ref($bookshash) eq 'HASH') {
 2107:         foreach my $item (keys(%{$bookshash})) {
 2108:             if (ref($bookshash->{$item}) eq 'HASH') {
 2109:                 my $num = $bookshash->{$item}{'order'};
 2110:                 $ordered{$num} = $item;
 2111:             }
 2112:         }
 2113:     }
 2114:     my $confname = $dom.'-domainconfig';
 2115:     my $switchserver = &check_switchserver($dom,$confname);
 2116:     $maxnum = scalar(keys(%ordered));
 2117:     my $datatable = &textbookcourses_javascript(\%ordered);
 2118:     if (keys(%ordered)) {
 2119:         my @items = sort { $a <=> $b } keys(%ordered);
 2120:         for (my $i=0; $i<@items; $i++) {
 2121:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2122:             my $key = $ordered{$items[$i]};
 2123:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2124:             my $coursetitle = $coursehash{'description'};
 2125:             my ($subject,$title,$author,$image,$imgsrc,$cdom,$cnum);
 2126:             if (ref($bookshash->{$key}) eq 'HASH') {
 2127:                 $subject = $bookshash->{$key}->{'subject'};
 2128:                 $title = $bookshash->{$key}->{'title'};
 2129:                 $author = $bookshash->{$key}->{'author'};
 2130:                 $image = $bookshash->{$key}->{'image'};
 2131:                 if ($image ne '') {
 2132:                     my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2133:                     my $imagethumb = "$path/tn-".$imagefile;
 2134:                     $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2135:                 }
 2136:             }
 2137:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$key'".');"';
 2138:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2139:                          .'<select name="'.$key.'"'.$chgstr.'>';
 2140:             for (my $k=0; $k<=$maxnum; $k++) {
 2141:                 my $vpos = $k+1;
 2142:                 my $selstr;
 2143:                 if ($k == $i) {
 2144:                     $selstr = ' selected="selected" ';
 2145:                 }
 2146:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2147:             }
 2148:             $datatable .= '</select>'.('&nbsp;'x2).
 2149:                 '<label><input type="checkbox" name="book_del" value="'.$key.'" />'.
 2150:                 &mt('Delete?').'</label></span></td>'.
 2151:                 '<td colspan="2">'.
 2152:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="book_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2153:                 ('&nbsp;'x2).
 2154:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="book_title_'.$i.'" value="'.$title.'" /></span> '.
 2155:                 ('&nbsp;'x2).
 2156:                 '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="book_author_'.$i.'" value="'.$author.'" /></span> '.
 2157:                 ('&nbsp;'x2).
 2158:                 '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2159:             if ($image) {
 2160:                 $datatable .= '<span class="LC_nobreak">'.
 2161:                               $imgsrc.
 2162:                               '<label><input type="checkbox" name="book_image_del"'.
 2163:                               ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2164:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2165:             }
 2166:             if ($switchserver) {
 2167:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2168:             } else {
 2169:                 $datatable .= '<input type="file" name="book_image_'.$i.'" value="" />';
 2170:             }
 2171:             $datatable .= '<input type="hidden" name="book_id_'.$i.'" value="'.$key.'" /></span> '.
 2172:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2173:                           $coursetitle.'</span></td></tr>'."\n";
 2174:             $itemcount ++;
 2175:         }
 2176:     }
 2177:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2178:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'addbook_pos'".');"';
 2179:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2180:                   '<input type="hidden" name="book_maxnum" value="'.$maxnum.'" />'."\n".
 2181:                   '<select name="addbook_pos"'.$chgstr.'>';
 2182:     for (my $k=0; $k<$maxnum+1; $k++) {
 2183:         my $vpos = $k+1;
 2184:         my $selstr;
 2185:         if ($k == $maxnum) {
 2186:             $selstr = ' selected="selected" ';
 2187:         }
 2188:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2189:     }
 2190:     $datatable .= '</select>&nbsp;'."\n".
 2191:                   '<input type="checkbox" name="addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2192:                   '<td colspan="2">'.
 2193:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="addbook_subject" value="" /></span> '."\n".
 2194:                   ('&nbsp;'x2).
 2195:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="addbook_title" value="" /></span> '."\n".
 2196:                   ('&nbsp;'x2).
 2197:                   '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="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="addbook_image" value="" />';
 2204:     }
 2205:     $datatable .= '</span>'."\n".
 2206:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2207:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},'addbook_cdom').
 2208:                   '<input type="text" size="25" name="addbook_cnum" value="" />'.
 2209:                   &Apache::loncommon::selectcourse_link
 2210:                       ('display','addbook_cnum','addbook_cdom',undef,undef,undef,'Course');
 2211:                   '</span></td>'."\n".
 2212:                   '</tr>'."\n";
 2213:     $itemcount ++;
 2214:     return $datatable;
 2215: }
 2216: 
 2217: sub textbookcourses_javascript {
 2218:     my ($textbooks) = @_;
 2219:     return unless(ref($textbooks) eq 'HASH');
 2220:     my $num = scalar(keys(%{$textbooks}));
 2221:     my @jsarray;
 2222:     foreach my $item (sort {$a <=> $b } (keys(%{$textbooks}))) {
 2223:         push(@jsarray,$textbooks->{$item});
 2224:     }
 2225:     my $jstext = '    var textbooks = Array('."'".join("','",@jsarray)."'".');'."\n";
 2226:     return <<"ENDSCRIPT";
 2227: <script type="text/javascript">
 2228: // <![CDATA[
 2229: function reorderBooks(form,item) {
 2230:     var changedVal;
 2231: $jstext 
 2232:     var newpos = 'addbook_pos';
 2233:     var current = new Array;
 2234:     var maxh = 1 + $num;
 2235:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2236:     if (item == newpos) {
 2237:         changedVal = newitemVal;
 2238:     } else {
 2239:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2240:         current[newitemVal] = newpos;
 2241:     }
 2242:     for (var i=0; i<textbooks.length; i++) {
 2243:         var elementName = textbooks[i];
 2244:         if (elementName != item) {
 2245:             if (form.elements[elementName]) {
 2246:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2247:                 current[currVal] = elementName;
 2248:             }
 2249:         }
 2250:     }
 2251:     var oldVal;
 2252:     for (var j=0; j<maxh; j++) {
 2253:         if (current[j] == undefined) {
 2254:             oldVal = j;
 2255:         }
 2256:     }
 2257:     if (oldVal < changedVal) {
 2258:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2259:            var elementName = current[k];
 2260:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2261:         }
 2262:     } else {
 2263:         for (var k=changedVal; k<oldVal; k++) {
 2264:             var elementName = current[k];
 2265:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2266:         }
 2267:     }
 2268:     return;
 2269: }
 2270: 
 2271: // ]]>
 2272: </script>
 2273: 
 2274: ENDSCRIPT
 2275: }
 2276: 
 2277: sub print_autoenroll {
 2278:     my ($dom,$settings,$rowtotal) = @_;
 2279:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2280:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2281:     if (ref($settings) eq 'HASH') {
 2282:         if (exists($settings->{'run'})) {
 2283:             if ($settings->{'run'} eq '0') {
 2284:                 $runoff = ' checked="checked" ';
 2285:                 $runon = ' ';
 2286:             } else {
 2287:                 $runon = ' checked="checked" ';
 2288:                 $runoff = ' ';
 2289:             }
 2290:         } else {
 2291:             if ($autorun) {
 2292:                 $runon = ' checked="checked" ';
 2293:                 $runoff = ' ';
 2294:             } else {
 2295:                 $runoff = ' checked="checked" ';
 2296:                 $runon = ' ';
 2297:             }
 2298:         }
 2299:         if (exists($settings->{'co-owners'})) {
 2300:             if ($settings->{'co-owners'} eq '0') {
 2301:                 $coownersoff = ' checked="checked" ';
 2302:                 $coownerson = ' ';
 2303:             } else {
 2304:                 $coownerson = ' checked="checked" ';
 2305:                 $coownersoff = ' ';
 2306:             }
 2307:         } else {
 2308:             $coownersoff = ' checked="checked" ';
 2309:             $coownerson = ' ';
 2310:         }
 2311:         if (exists($settings->{'sender_domain'})) {
 2312:             $defdom = $settings->{'sender_domain'};
 2313:         }
 2314:     } else {
 2315:         if ($autorun) {
 2316:             $runon = ' checked="checked" ';
 2317:             $runoff = ' ';
 2318:         } else {
 2319:             $runoff = ' checked="checked" ';
 2320:             $runon = ' ';
 2321:         }
 2322:     }
 2323:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2324:     my $notif_sender;
 2325:     if (ref($settings) eq 'HASH') {
 2326:         $notif_sender = $settings->{'sender_uname'};
 2327:     }
 2328:     my $datatable='<tr class="LC_odd_row">'.
 2329:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2330:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2331:                   '<input type="radio" name="autoenroll_run"'.
 2332:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2333:                   '<label><input type="radio" name="autoenroll_run"'.
 2334:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2335:                   '</tr><tr>'.
 2336:                   '<td>'.&mt('Notification messages - sender').
 2337:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2338:                   &mt('username').':&nbsp;'.
 2339:                   '<input type="text" name="sender_uname" value="'.
 2340:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2341:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2342:                   '<tr class="LC_odd_row">'.
 2343:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2344:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2345:                   '<input type="radio" name="autoassign_coowners"'.
 2346:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2347:                   '<label><input type="radio" name="autoassign_coowners"'.
 2348:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2349:                   '</tr>';
 2350:     $$rowtotal += 3;
 2351:     return $datatable;
 2352: }
 2353: 
 2354: sub print_autoupdate {
 2355:     my ($position,$dom,$settings,$rowtotal) = @_;
 2356:     my $datatable;
 2357:     if ($position eq 'top') {
 2358:         my $updateon = ' ';
 2359:         my $updateoff = ' checked="checked" ';
 2360:         my $classlistson = ' ';
 2361:         my $classlistsoff = ' checked="checked" ';
 2362:         if (ref($settings) eq 'HASH') {
 2363:             if ($settings->{'run'} eq '1') {
 2364:                 $updateon = $updateoff;
 2365:                 $updateoff = ' ';
 2366:             }
 2367:             if ($settings->{'classlists'} eq '1') {
 2368:                 $classlistson = $classlistsoff;
 2369:                 $classlistsoff = ' ';
 2370:             }
 2371:         }
 2372:         my %title = (
 2373:                    run => 'Auto-update active?',
 2374:                    classlists => 'Update information in classlists?',
 2375:                     );
 2376:         $datatable = '<tr class="LC_odd_row">'. 
 2377:                   '<td>'.&mt($title{'run'}).'</td>'.
 2378:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2379:                   '<input type="radio" name="autoupdate_run"'.
 2380:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2381:                   '<label><input type="radio" name="autoupdate_run"'.
 2382:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2383:                   '</tr><tr>'.
 2384:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2385:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2386:                   '<label><input type="radio" name="classlists"'.
 2387:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2388:                   '<label><input type="radio" name="classlists"'.
 2389:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2390:                   '</tr>';
 2391:         $$rowtotal += 2;
 2392:     } elsif ($position eq 'middle') {
 2393:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2394:         my $numinrow = 3;
 2395:         my $locknamesettings;
 2396:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2397:                                      $dom,$numinrow,$othertitle,
 2398:                                     'lockablenames');
 2399:         $$rowtotal ++;
 2400:     } else {
 2401:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2402:         my @fields = ('lastname','firstname','middlename','generation',
 2403:                       'permanentemail','id');
 2404:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2405:         my $numrows = 0;
 2406:         if (ref($types) eq 'ARRAY') {
 2407:             if (@{$types} > 0) {
 2408:                 $datatable = 
 2409:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2410:                                          \@fields,$types,\$numrows);
 2411:                     $$rowtotal += @{$types}; 
 2412:             }
 2413:         }
 2414:         $datatable .= 
 2415:             &usertype_update_row($settings,{'default' => $othertitle},
 2416:                                  \%fieldtitles,\@fields,['default'],
 2417:                                  \$numrows);
 2418:         $$rowtotal ++;     
 2419:     }
 2420:     return $datatable;
 2421: }
 2422: 
 2423: sub print_autocreate {
 2424:     my ($dom,$settings,$rowtotal) = @_;
 2425:     my (%createon,%createoff,%currhash);
 2426:     my @types = ('xml','req');
 2427:     if (ref($settings) eq 'HASH') {
 2428:         foreach my $item (@types) {
 2429:             $createoff{$item} = ' checked="checked" ';
 2430:             $createon{$item} = ' ';
 2431:             if (exists($settings->{$item})) {
 2432:                 if ($settings->{$item}) {
 2433:                     $createon{$item} = ' checked="checked" ';
 2434:                     $createoff{$item} = ' ';
 2435:                 }
 2436:             }
 2437:         }
 2438:         if ($settings->{'xmldc'} ne '') {
 2439:             $currhash{$settings->{'xmldc'}} = 1;
 2440:         }
 2441:     } else {
 2442:         foreach my $item (@types) {
 2443:             $createoff{$item} = ' checked="checked" ';
 2444:             $createon{$item} = ' ';
 2445:         }
 2446:     }
 2447:     $$rowtotal += 2;
 2448:     my $numinrow = 2;
 2449:     my $datatable='<tr class="LC_odd_row">'.
 2450:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2451:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2452:                   '<input type="radio" name="autocreate_xml"'.
 2453:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2454:                   '<label><input type="radio" name="autocreate_xml"'.
 2455:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2456:                   '</td></tr><tr>'.
 2457:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2458:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2459:                   '<input type="radio" name="autocreate_req"'.
 2460:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2461:                   '<label><input type="radio" name="autocreate_req"'.
 2462:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2463:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2464:                                                    'autocreate_xmldc',%currhash);
 2465:     if ($numdc > 1) {
 2466:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2467:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2468:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2469:     } else {
 2470:         $datatable .= $dctable.'</td></tr>';
 2471:     }
 2472:     $$rowtotal += $rows;
 2473:     return $datatable;
 2474: }
 2475: 
 2476: sub print_directorysrch {
 2477:     my ($dom,$settings,$rowtotal) = @_;
 2478:     my $srchon = ' ';
 2479:     my $srchoff = ' checked="checked" ';
 2480:     my ($exacton,$containson,$beginson);
 2481:     my $localon = ' ';
 2482:     my $localoff = ' checked="checked" ';
 2483:     if (ref($settings) eq 'HASH') {
 2484:         if ($settings->{'available'} eq '1') {
 2485:             $srchon = $srchoff;
 2486:             $srchoff = ' ';
 2487:         }
 2488:         if ($settings->{'localonly'} eq '1') {
 2489:             $localon = $localoff;
 2490:             $localoff = ' ';
 2491:         }
 2492:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2493:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2494:                 if ($type eq 'exact') {
 2495:                     $exacton = ' checked="checked" ';
 2496:                 } elsif ($type eq 'contains') {
 2497:                     $containson = ' checked="checked" ';
 2498:                 } elsif ($type eq 'begins') {
 2499:                     $beginson = ' checked="checked" ';
 2500:                 }
 2501:             }
 2502:         } else {
 2503:             if ($settings->{'searchtypes'} eq 'exact') {
 2504:                 $exacton = ' checked="checked" ';
 2505:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2506:                 $containson = ' checked="checked" ';
 2507:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2508:                 $exacton = ' checked="checked" ';
 2509:                 $containson = ' checked="checked" ';
 2510:             }
 2511:         }
 2512:     }
 2513:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2514:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2515: 
 2516:     my $numinrow = 4;
 2517:     my $cansrchrow = 0;
 2518:     my $datatable='<tr class="LC_odd_row">'.
 2519:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2520:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2521:                   '<input type="radio" name="dirsrch_available"'.
 2522:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2523:                   '<label><input type="radio" name="dirsrch_available"'.
 2524:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2525:                   '</tr><tr>'.
 2526:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2527:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2528:                   '<input type="radio" name="dirsrch_localonly"'.
 2529:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2530:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2531:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2532:                   '</tr>';
 2533:     $$rowtotal += 2;
 2534:     if (ref($usertypes) eq 'HASH') {
 2535:         if (keys(%{$usertypes}) > 0) {
 2536:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2537:                                          $numinrow,$othertitle,'cansearch');
 2538:             $cansrchrow = 1;
 2539:         }
 2540:     }
 2541:     if ($cansrchrow) {
 2542:         $$rowtotal ++;
 2543:         $datatable .= '<tr>';
 2544:     } else {
 2545:         $datatable .= '<tr class="LC_odd_row">';
 2546:     }
 2547:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2548:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2549:     foreach my $title (@{$titleorder}) {
 2550:         if (defined($searchtitles->{$title})) {
 2551:             my $check = ' ';
 2552:             if (ref($settings) eq 'HASH') {
 2553:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2554:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2555:                         $check = ' checked="checked" ';
 2556:                     }
 2557:                 }
 2558:             }
 2559:             $datatable .= '<td class="LC_left_item">'.
 2560:                           '<span class="LC_nobreak"><label>'.
 2561:                           '<input type="checkbox" name="searchby" '.
 2562:                           'value="'.$title.'"'.$check.'/>'.
 2563:                           $searchtitles->{$title}.'</label></span></td>';
 2564:         }
 2565:     }
 2566:     $datatable .= '</tr></table></td></tr>';
 2567:     $$rowtotal ++;
 2568:     if ($cansrchrow) {
 2569:         $datatable .= '<tr class="LC_odd_row">';
 2570:     } else {
 2571:         $datatable .= '<tr>';
 2572:     }
 2573:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2574:                   '<td class="LC_left_item" colspan="2">'.
 2575:                   '<span class="LC_nobreak"><label>'.
 2576:                   '<input type="checkbox" name="searchtypes" '.
 2577:                   $exacton.' value="exact" />'.&mt('Exact match').
 2578:                   '</label>&nbsp;'.
 2579:                   '<label><input type="checkbox" name="searchtypes" '.
 2580:                   $beginson.' value="begins" />'.&mt('Begins with').
 2581:                   '</label>&nbsp;'.
 2582:                   '<label><input type="checkbox" name="searchtypes" '.
 2583:                   $containson.' value="contains" />'.&mt('Contains').
 2584:                   '</label></span></td></tr>';
 2585:     $$rowtotal ++;
 2586:     return $datatable;
 2587: }
 2588: 
 2589: sub print_contacts {
 2590:     my ($dom,$settings,$rowtotal) = @_;
 2591:     my $datatable;
 2592:     my @contacts = ('adminemail','supportemail');
 2593:     my (%checked,%to,%otheremails,%bccemails);
 2594:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2595:                     'requestsmail','updatesmail','idconflictsmail');
 2596:     foreach my $type (@mailings) {
 2597:         $otheremails{$type} = '';
 2598:     }
 2599:     $bccemails{'helpdeskmail'} = '';
 2600:     if (ref($settings) eq 'HASH') {
 2601:         foreach my $item (@contacts) {
 2602:             if (exists($settings->{$item})) {
 2603:                 $to{$item} = $settings->{$item};
 2604:             }
 2605:         }
 2606:         foreach my $type (@mailings) {
 2607:             if (exists($settings->{$type})) {
 2608:                 if (ref($settings->{$type}) eq 'HASH') {
 2609:                     foreach my $item (@contacts) {
 2610:                         if ($settings->{$type}{$item}) {
 2611:                             $checked{$type}{$item} = ' checked="checked" ';
 2612:                         }
 2613:                     }
 2614:                     $otheremails{$type} = $settings->{$type}{'others'};
 2615:                     if ($type eq 'helpdeskmail') {
 2616:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2617:                     }
 2618:                 }
 2619:             } elsif ($type eq 'lonstatusmail') {
 2620:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2621:             }
 2622:         }
 2623:     } else {
 2624:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2625:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2626:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2627:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2628:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2629:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2630:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2631:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2632:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2633:     }
 2634:     my ($titles,$short_titles) = &contact_titles();
 2635:     my $rownum = 0;
 2636:     my $css_class;
 2637:     foreach my $item (@contacts) {
 2638:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2639:         $datatable .= '<tr'.$css_class.'>'. 
 2640:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2641:                   '</span></td><td class="LC_right_item">'.
 2642:                   '<input type="text" name="'.$item.'" value="'.
 2643:                   $to{$item}.'" /></td></tr>';
 2644:         $rownum ++;
 2645:     }
 2646:     foreach my $type (@mailings) {
 2647:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2648:         $datatable .= '<tr'.$css_class.'>'.
 2649:                       '<td><span class="LC_nobreak">'.
 2650:                       $titles->{$type}.': </span></td>'.
 2651:                       '<td class="LC_left_item">'.
 2652:                       '<span class="LC_nobreak">';
 2653:         foreach my $item (@contacts) {
 2654:             $datatable .= '<label>'.
 2655:                           '<input type="checkbox" name="'.$type.'"'.
 2656:                           $checked{$type}{$item}.
 2657:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2658:                           '</label>&nbsp;';
 2659:         }
 2660:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2661:                       '<input type="text" name="'.$type.'_others" '.
 2662:                       'value="'.$otheremails{$type}.'"  />';
 2663:         if ($type eq 'helpdeskmail') {
 2664:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2665:                           '<input type="text" name="'.$type.'_bcc" '.
 2666:                           'value="'.$bccemails{$type}.'"  />';
 2667:         }
 2668:         $datatable .= '</td></tr>'."\n";
 2669:         $rownum ++;
 2670:     }
 2671:     my %choices;
 2672:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2673:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2674:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2675:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2676:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2677:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2678:     my @toggles = ('reporterrors','reportupdates');
 2679:     my %defaultchecked = ('reporterrors'  => 'on',
 2680:                           'reportupdates' => 'on');
 2681:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2682:                                                \%choices,$rownum);
 2683:     $datatable .= $reports;
 2684:     $$rowtotal += $rownum;
 2685:     return $datatable;
 2686: }
 2687: 
 2688: sub print_helpsettings {
 2689:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2690:     my ($datatable,$itemcount);
 2691:     $itemcount = 1;
 2692:     my (%choices,%defaultchecked,@toggles);
 2693:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2694:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2695:                                  &mt('LON-CAPA bug tracker'),600,500));
 2696:     %defaultchecked = ('submitbugs' => 'on');
 2697:     @toggles = ('submitbugs',);
 2698: 
 2699:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2700:                                                  \%choices,$itemcount);
 2701:     return $datatable;
 2702: }
 2703: 
 2704: sub radiobutton_prefs {
 2705:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2706:         $additional) = @_;
 2707:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2708:                    (ref($choices) eq 'HASH'));
 2709: 
 2710:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2711: 
 2712:     foreach my $item (@{$toggles}) {
 2713:         if ($defaultchecked->{$item} eq 'on') {
 2714:             $checkedon{$item} = ' checked="checked" ';
 2715:             $checkedoff{$item} = ' ';
 2716:         } elsif ($defaultchecked->{$item} eq 'off') {
 2717:             $checkedoff{$item} = ' checked="checked" ';
 2718:             $checkedon{$item} = ' ';
 2719:         }
 2720:     }
 2721:     if (ref($settings) eq 'HASH') {
 2722:         foreach my $item (@{$toggles}) {
 2723:             if ($settings->{$item} eq '1') {
 2724:                 $checkedon{$item} =  ' checked="checked" ';
 2725:                 $checkedoff{$item} = ' ';
 2726:             } elsif ($settings->{$item} eq '0') {
 2727:                 $checkedoff{$item} =  ' checked="checked" ';
 2728:                 $checkedon{$item} = ' ';
 2729:             }
 2730:         }
 2731:     }
 2732:     if ($onclick) {
 2733:         $onclick = ' onclick="'.$onclick.'"';
 2734:     }
 2735:     foreach my $item (@{$toggles}) {
 2736:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2737:         $datatable .=
 2738:             '<tr'.$css_class.'><td valign="top">'.
 2739:             '<span class="LC_nobreak">'.$choices->{$item}.
 2740:             '</span></td>'.
 2741:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2742:             '<label><input type="radio" name="'.
 2743:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2744:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2745:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2746:             '</span>'.$additional.
 2747:             '</td>'.
 2748:             '</tr>';
 2749:         $itemcount ++;
 2750:     }
 2751:     return ($datatable,$itemcount);
 2752: }
 2753: 
 2754: sub print_coursedefaults {
 2755:     my ($position,$dom,$settings,$rowtotal) = @_;
 2756:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2757:     my $itemcount = 1;
 2758:     my %choices =  &Apache::lonlocal::texthash (
 2759:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2760:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2761:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2762:         coursecredits        => 'Credits can be specified for courses',
 2763:     );
 2764:     my %staticdefaults = (
 2765:                            anonsurvey_threshold => 10,
 2766:                            uploadquota          => 500,
 2767:                          );
 2768:     if ($position eq 'top') {
 2769:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2770:         @toggles = ('canuse_pdfforms');
 2771:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2772:                                                  \%choices,$itemcount);
 2773:     } else {
 2774:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2775:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2776:             %curruploadquota);
 2777:         my $currusecredits = 0;
 2778:         my @types = ('official','unofficial','community','textbook');
 2779:         if (ref($settings) eq 'HASH') {
 2780:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2781:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2782:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2783:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2784:                 }
 2785:             }
 2786:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2787:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2788:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2789:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2790:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2791:                     ($def_textbook_credits ne '')) {
 2792:                     $currusecredits = 1;
 2793:                 }
 2794:             }
 2795:         }
 2796:         if (!$currdefresponder) {
 2797:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2798:         } elsif ($currdefresponder < 1) {
 2799:             $currdefresponder = 1;
 2800:         }
 2801:         foreach my $type (@types) {
 2802:             if ($curruploadquota{$type} eq '') {
 2803:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2804:             }
 2805:         }
 2806:         $datatable .=
 2807:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2808:                 $choices{'anonsurvey_threshold'}.
 2809:                 '</span></td>'.
 2810:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2811:                 '<input type="text" name="anonsurvey_threshold"'.
 2812:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2813:                 '</td></tr>'."\n";
 2814:         $itemcount ++;
 2815:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2816:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2817:                       $choices{'uploadquota'}.
 2818:                       '</span></td>'.
 2819:                       '<td align="right" class="LC_right_item">'.
 2820:                       '<table><tr>';
 2821:         foreach my $type (@types) {
 2822:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2823:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2824:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2825:         }
 2826:         $datatable .= '</tr></table></td></tr>'."\n";
 2827:         $itemcount ++;
 2828:         my $onclick = "toggleDisplay(this.form,'credits');";
 2829:         my $display = 'none';
 2830:         if ($currusecredits) {
 2831:             $display = 'block';
 2832:         }
 2833:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2834:                          '<span class="LC_nobreak">'.
 2835:                          &mt('Default credits for official courses [_1]',
 2836:                          '<input type="text" name="official_credits" value="'.
 2837:                          $def_official_credits.'" size="3" />').
 2838:                          '</span><br />'.
 2839:                          '<span class="LC_nobreak">'.
 2840:                          &mt('Default credits for unofficial courses [_1]',
 2841:                          '<input type="text" name="unofficial_credits" value="'.
 2842:                          $def_unofficial_credits.'" size="3" />').
 2843:                          '</span><br />'.
 2844:                          '<span class="LC_nobreak">'.
 2845:                          &mt('Default credits for textbook courses [_1]',
 2846:                          '<input type="text" name="textbook_credits" value="'.
 2847:                          $def_textbook_credits.'" size="3" />').
 2848:                          '</span></div>'."\n";
 2849:         %defaultchecked = ('coursecredits' => 'off');
 2850:         @toggles = ('coursecredits');
 2851:         my $current = {
 2852:                         'coursecredits' => $currusecredits,
 2853:                       };
 2854:         (my $table,$itemcount) =
 2855:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2856:                                \%choices,$itemcount,$onclick,$additional);
 2857:         $datatable .= $table;
 2858:         $itemcount ++;
 2859:     }
 2860:     $$rowtotal += $itemcount;
 2861:     return $datatable;
 2862: }
 2863: 
 2864: sub print_selfenrollment {
 2865:     my ($position,$dom,$settings,$rowtotal) = @_;
 2866:     my ($css_class,$datatable);
 2867:     my $itemcount = 1;
 2868:     my @types = ('official','unofficial','community','textbook');
 2869:     if (($position eq 'top') || ($position eq 'middle')) {
 2870:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 2871:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 2872:         my @rows;
 2873:         my $key;
 2874:         if ($position eq 'top') {
 2875:             $key = 'admin'; 
 2876:             if (ref($rowsref) eq 'ARRAY') {
 2877:                 @rows = @{$rowsref};
 2878:             }
 2879:         } elsif ($position eq 'middle') {
 2880:             $key = 'default';
 2881:             @rows = ('types','registered','approval','limit');
 2882:         }
 2883:         foreach my $row (@rows) {
 2884:             if (defined($titlesref->{$row})) {
 2885:                 $itemcount ++;
 2886:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2887:                 $datatable .= '<tr'.$css_class.'>'.
 2888:                               '<td>'.$titlesref->{$row}.'</td>'.
 2889:                               '<td class="LC_left_item">'.
 2890:                               '<table><tr>';
 2891:                 my (%current,%currentcap);
 2892:                 if (ref($settings) eq 'HASH') {
 2893:                     if (ref($settings->{$key}) eq 'HASH') {
 2894:                         foreach my $type (@types) {
 2895:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2896:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 2897:                             }
 2898:                             if (($row eq 'limit') && ($key eq 'default')) {
 2899:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2900:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 2901:                                 }
 2902:                             }
 2903:                         }
 2904:                     }
 2905:                 }
 2906:                 my %roles = (
 2907:                              '0' => &Apache::lonnet::plaintext('dc'),
 2908:                             ); 
 2909:             
 2910:                 foreach my $type (@types) {
 2911:                     unless (($row eq 'registered') && ($key eq 'default')) {
 2912:                         $datatable .= '<th>'.&mt($type).'</th>';
 2913:                     }
 2914:                 }
 2915:                 unless (($row eq 'registered') && ($key eq 'default')) {
 2916:                     $datatable .= '</tr><tr>';
 2917:                 }
 2918:                 foreach my $type (@types) {
 2919:                     if ($type eq 'community') {
 2920:                         $roles{'1'} = &mt('Community personnel');
 2921:                     } else {
 2922:                         $roles{'1'} = &mt('Course personnel');
 2923:                     }
 2924:                     $datatable .= '<td style="vertical-align: top">';
 2925:                     if ($position eq 'top') {
 2926:                         my %checked;
 2927:                         if ($current{$type} eq '0') {
 2928:                             $checked{'0'} = ' checked="checked"';
 2929:                         } else {
 2930:                             $checked{'1'} = ' checked="checked"';
 2931:                         }
 2932:                         foreach my $role ('1','0') {
 2933:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2934:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 2935:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 2936:                                           $roles{$role}.'</label></span> ';
 2937:                         }
 2938:                     } else {
 2939:                         if ($row eq 'types') {
 2940:                             my %checked;
 2941:                             if ($current{$type} =~ /^(all|dom)$/) {
 2942:                                 $checked{$1} = ' checked="checked"';
 2943:                             } else {
 2944:                                 $checked{''} = ' checked="checked"';
 2945:                             }
 2946:                             foreach my $val ('','dom','all') {
 2947:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2948:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2949:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2950:                             }
 2951:                         } elsif ($row eq 'registered') {
 2952:                             my %checked;
 2953:                             if ($current{$type} eq '1') {
 2954:                                 $checked{'1'} = ' checked="checked"';
 2955:                             } else {
 2956:                                 $checked{'0'} = ' checked="checked"';
 2957:                             }
 2958:                             foreach my $val ('0','1') {
 2959:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2960:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2961:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2962:                             }
 2963:                         } elsif ($row eq 'approval') {
 2964:                             my %checked;
 2965:                             if ($current{$type} =~ /^([12])$/) {
 2966:                                 $checked{$1} = ' checked="checked"';
 2967:                             } else {
 2968:                                 $checked{'0'} = ' checked="checked"';
 2969:                             }
 2970:                             for my $val (0..2) {
 2971:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2972:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2973:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2974:                             }
 2975:                         } elsif ($row eq 'limit') {
 2976:                             my %checked;
 2977:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 2978:                                 $checked{$1} = ' checked="checked"';
 2979:                             } else {
 2980:                                 $checked{'none'} = ' checked="checked"';
 2981:                             }
 2982:                             my $cap;
 2983:                             if ($currentcap{$type} =~ /^\d+$/) {
 2984:                                 $cap = $currentcap{$type};
 2985:                             }
 2986:                             foreach my $val ('none','allstudents','selfenrolled') {
 2987:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2988:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2989:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2990:                             }
 2991:                             $datatable .= '<br />'.
 2992:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 2993:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 2994:                                           '</span>'; 
 2995:                         }
 2996:                     }
 2997:                     $datatable .= '</td>';
 2998:                 }
 2999:                 $datatable .= '</tr>';
 3000:             }
 3001:             $datatable .= '</table></td></tr>';
 3002:         }
 3003:     } elsif ($position eq 'bottom') {
 3004:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3005:     }
 3006:     $$rowtotal += $itemcount;
 3007:     return $datatable;
 3008: }
 3009: 
 3010: sub print_validation_rows {
 3011:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3012:     my ($itemsref,$namesref,$fieldsref);
 3013:     if ($caller eq 'selfenroll') { 
 3014:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3015:     } elsif ($caller eq 'requestcourses') {
 3016:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3017:     }
 3018:     my %currvalidation;
 3019:     if (ref($settings) eq 'HASH') {
 3020:         if (ref($settings->{'validation'}) eq 'HASH') {
 3021:             %currvalidation = %{$settings->{'validation'}};
 3022:         }
 3023:     }
 3024:     my $datatable;
 3025:     my $itemcount = 0;
 3026:     foreach my $item (@{$itemsref}) {
 3027:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3028:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3029:                       $namesref->{$item}.
 3030:                       '</span></td>'.
 3031:                       '<td class="LC_left_item">';
 3032:         if (($item eq 'url') || ($item eq 'button')) {
 3033:             $datatable .= '<span class="LC_nobreak">'.
 3034:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3035:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3036:         } elsif ($item eq 'fields') {
 3037:             my @currfields;
 3038:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3039:                 @currfields = @{$currvalidation{$item}};
 3040:             }
 3041:             foreach my $field (@{$fieldsref}) {
 3042:                 my $check = '';
 3043:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3044:                     $check = ' checked="checked"';
 3045:                 }
 3046:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3047:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3048:                               ' value="'.$field.'"'.$check.' />'.$field.
 3049:                               '</label></span> ';
 3050:             }
 3051:         } elsif ($item eq 'markup') {
 3052:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3053:                            $currvalidation{$item}.
 3054:                               '</textarea>';
 3055:         }
 3056:         $datatable .= '</td></tr>'."\n";
 3057:         if (ref($rowtotal)) {
 3058:             $itemcount ++;
 3059:         }
 3060:     }
 3061:     if ($caller eq 'requestcourses') {
 3062:         my %currhash;
 3063:         if (ref($settings->{'validation'}) eq 'HASH') {
 3064:             if ($settings->{'validation'}{'dc'} ne '') {
 3065:                 $currhash{$settings->{'validation'}{'dc'}} = 1;
 3066:             }
 3067:         }
 3068:         my $numinrow = 2;
 3069:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3070:                                                        'validationdc',%currhash);
 3071:         if ($numdc > 1) {
 3072:             $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 3073:                           &mt('Course creation processed as: (choose Dom. Coord.)').
 3074:                           '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3075:         } else {
 3076:             $datatable .= $dctable.'</td></tr>';
 3077:         }
 3078:         $itemcount ++;
 3079:     }
 3080:     if (ref($rowtotal)) {
 3081:         $$rowtotal += $itemcount;
 3082:     }
 3083:     return $datatable;
 3084: }
 3085: 
 3086: sub print_usersessions {
 3087:     my ($position,$dom,$settings,$rowtotal) = @_;
 3088:     my ($css_class,$datatable,%checked,%choices);
 3089:     my (%by_ip,%by_location,@intdoms);
 3090:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3091: 
 3092:     my @alldoms = &Apache::lonnet::all_domains();
 3093:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3094:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3095:     my %altids = &id_for_thisdom(%servers);
 3096:     my $itemcount = 1;
 3097:     if ($position eq 'top') {
 3098:         if (keys(%serverhomes) > 1) {
 3099:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3100:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 3101:         } else {
 3102:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3103:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3104:         }
 3105:     } else {
 3106:         if (keys(%by_location) == 0) {
 3107:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3108:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3109:         } else {
 3110:             my %lt = &usersession_titles();
 3111:             my $numinrow = 5;
 3112:             my $prefix;
 3113:             my @types;
 3114:             if ($position eq 'bottom') {
 3115:                 $prefix = 'remote';
 3116:                 @types = ('version','excludedomain','includedomain');
 3117:             } else {
 3118:                 $prefix = 'hosted';
 3119:                 @types = ('excludedomain','includedomain');
 3120:             }
 3121:             my (%current,%checkedon,%checkedoff);
 3122:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3123:             my @locations = sort(keys(%by_location));
 3124:             foreach my $type (@types) {
 3125:                 $checkedon{$type} = '';
 3126:                 $checkedoff{$type} = ' checked="checked"';
 3127:             }
 3128:             if (ref($settings) eq 'HASH') {
 3129:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3130:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3131:                         $current{$key} = $settings->{$prefix}{$key};
 3132:                         if ($key eq 'version') {
 3133:                             if ($current{$key} ne '') {
 3134:                                 $checkedon{$key} = ' checked="checked"';
 3135:                                 $checkedoff{$key} = '';
 3136:                             }
 3137:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3138:                             $checkedon{$key} = ' checked="checked"';
 3139:                             $checkedoff{$key} = '';
 3140:                         }
 3141:                     }
 3142:                 }
 3143:             }
 3144:             foreach my $type (@types) {
 3145:                 next if ($type ne 'version' && !@locations);
 3146:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3147:                 $datatable .= '<tr'.$css_class.'>
 3148:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3149:                                <span class="LC_nobreak">&nbsp;
 3150:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3151:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3152:                 if ($type eq 'version') {
 3153:                     my $selector = '<select name="'.$prefix.'_version">';
 3154:                     foreach my $version (@lcversions) {
 3155:                         my $selected = '';
 3156:                         if ($current{'version'} eq $version) {
 3157:                             $selected = ' selected="selected"';
 3158:                         }
 3159:                         $selector .= ' <option value="'.$version.'"'.
 3160:                                      $selected.'>'.$version.'</option>';
 3161:                     }
 3162:                     $selector .= '</select> ';
 3163:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3164:                 } else {
 3165:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3166:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3167:                                  ' />'.('&nbsp;'x2).
 3168:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3169:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3170:                                  "\n".
 3171:                                  '</div><div><table>';
 3172:                     my $rem;
 3173:                     for (my $i=0; $i<@locations; $i++) {
 3174:                         my ($showloc,$value,$checkedtype);
 3175:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3176:                             my $ip = $by_location{$locations[$i]}->[0];
 3177:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3178:                                  $value = join(':',@{$by_ip{$ip}});
 3179:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3180:                                 if (ref($current{$type}) eq 'ARRAY') {
 3181:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3182:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3183:                                             $checkedtype = ' checked="checked"';
 3184:                                             last;
 3185:                                         }
 3186:                                     }
 3187:                                 }
 3188:                             }
 3189:                         }
 3190:                         $rem = $i%($numinrow);
 3191:                         if ($rem == 0) {
 3192:                             if ($i > 0) {
 3193:                                 $datatable .= '</tr>';
 3194:                             }
 3195:                             $datatable .= '<tr>';
 3196:                         }
 3197:                         $datatable .= '<td class="LC_left_item">'.
 3198:                                       '<span class="LC_nobreak"><label>'.
 3199:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3200:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3201:                                       '</label></span></td>';
 3202:                     }
 3203:                     $rem = @locations%($numinrow);
 3204:                     my $colsleft = $numinrow - $rem;
 3205:                     if ($colsleft > 1 ) {
 3206:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3207:                                       '&nbsp;</td>';
 3208:                     } elsif ($colsleft == 1) {
 3209:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3210:                     }
 3211:                     $datatable .= '</tr></table>';
 3212:                 }
 3213:                 $datatable .= '</td></tr>';
 3214:                 $itemcount ++;
 3215:             }
 3216:         }
 3217:     }
 3218:     $$rowtotal += $itemcount;
 3219:     return $datatable;
 3220: }
 3221: 
 3222: sub build_location_hashes {
 3223:     my ($intdoms,$by_ip,$by_location) = @_;
 3224:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3225:                   (ref($by_location) eq 'HASH')); 
 3226:     my %iphost = &Apache::lonnet::get_iphost();
 3227:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3228:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3229:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3230:         foreach my $id (@{$iphost{$primary_ip}}) {
 3231:             my $intdom = &Apache::lonnet::internet_dom($id);
 3232:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3233:                 push(@{$intdoms},$intdom);
 3234:             }
 3235:         }
 3236:     }
 3237:     foreach my $ip (keys(%iphost)) {
 3238:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3239:             foreach my $id (@{$iphost{$ip}}) {
 3240:                 my $location = &Apache::lonnet::internet_dom($id);
 3241:                 if ($location) {
 3242:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3243:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3244:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3245:                             push(@{$by_ip->{$ip}},$location);
 3246:                         }
 3247:                     } else {
 3248:                         $by_ip->{$ip} = [$location];
 3249:                     }
 3250:                 }
 3251:             }
 3252:         }
 3253:     }
 3254:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3255:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3256:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3257:             my $first = $by_ip->{$ip}->[0];
 3258:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3259:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3260:                     push(@{$by_location->{$first}},$ip);
 3261:                 }
 3262:             } else {
 3263:                 $by_location->{$first} = [$ip];
 3264:             }
 3265:         }
 3266:     }
 3267:     return;
 3268: }
 3269: 
 3270: sub current_offloads_to {
 3271:     my ($dom,$settings,$servers) = @_;
 3272:     my (%spareid,%otherdomconfigs);
 3273:     if (ref($servers) eq 'HASH') {
 3274:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3275:             my $gotspares;
 3276:             if (ref($settings) eq 'HASH') {
 3277:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3278:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3279:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3280:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3281:                         $gotspares = 1;
 3282:                     }
 3283:                 }
 3284:             }
 3285:             unless ($gotspares) {
 3286:                 my $gotspares;
 3287:                 my $serverhomeID =
 3288:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3289:                 my $serverhomedom =
 3290:                     &Apache::lonnet::host_domain($serverhomeID);
 3291:                 if ($serverhomedom ne $dom) {
 3292:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3293:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3294:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3295:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3296:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3297:                                 $gotspares = 1;
 3298:                             }
 3299:                         }
 3300:                     } else {
 3301:                         $otherdomconfigs{$serverhomedom} =
 3302:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3303:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3304:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3305:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3306:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3307:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3308:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3309:                                         $gotspares = 1;
 3310:                                     }
 3311:                                 }
 3312:                             }
 3313:                         }
 3314:                     }
 3315:                 }
 3316:             }
 3317:             unless ($gotspares) {
 3318:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3319:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3320:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3321:                } else {
 3322:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3323:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3324:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3325:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3326:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3327:                     } else {
 3328:                         my %what = (
 3329:                              spareid => 1,
 3330:                         );
 3331:                         my ($result,$returnhash) = 
 3332:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3333:                         if ($result eq 'ok') { 
 3334:                             if (ref($returnhash) eq 'HASH') {
 3335:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3336:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3337:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3338:                                 }
 3339:                             }
 3340:                         }
 3341:                     }
 3342:                 }
 3343:             }
 3344:         }
 3345:     }
 3346:     return %spareid;
 3347: }
 3348: 
 3349: sub spares_row {
 3350:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3351:     my $css_class;
 3352:     my $numinrow = 4;
 3353:     my $itemcount = 1;
 3354:     my $datatable;
 3355:     my %typetitles = &sparestype_titles();
 3356:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3357:         foreach my $server (sort(keys(%{$servers}))) {
 3358:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3359:             my ($othercontrol,$serverdom);
 3360:             if ($serverhome ne $server) {
 3361:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3362:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3363:             } else {
 3364:                 $serverdom = &Apache::lonnet::host_domain($server);
 3365:                 if ($serverdom ne $dom) {
 3366:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3367:                 }
 3368:             }
 3369:             next unless (ref($spareid->{$server}) eq 'HASH');
 3370:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3371:             $datatable .= '<tr'.$css_class.'>
 3372:                            <td rowspan="2">
 3373:                             <span class="LC_nobreak">'.
 3374:                           &mt('[_1] when busy, offloads to:'
 3375:                               ,'<b>'.$server.'</b>').
 3376:                           "\n";
 3377:             my (%current,%canselect);
 3378:             my @choices = 
 3379:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3380:             foreach my $type ('primary','default') {
 3381:                 if (ref($spareid->{$server}) eq 'HASH') {
 3382:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3383:                         my @spares = @{$spareid->{$server}{$type}};
 3384:                         if (@spares > 0) {
 3385:                             if ($othercontrol) {
 3386:                                 $current{$type} = join(', ',@spares);
 3387:                             } else {
 3388:                                 $current{$type} .= '<table>';
 3389:                                 my $numspares = scalar(@spares);
 3390:                                 for (my $i=0;  $i<@spares; $i++) {
 3391:                                     my $rem = $i%($numinrow);
 3392:                                     if ($rem == 0) {
 3393:                                         if ($i > 0) {
 3394:                                             $current{$type} .= '</tr>';
 3395:                                         }
 3396:                                         $current{$type} .= '<tr>';
 3397:                                     }
 3398:                                     $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;'.
 3399:                                                        $spareid->{$server}{$type}[$i].
 3400:                                                        '</label></td>'."\n";
 3401:                                 }
 3402:                                 my $rem = @spares%($numinrow);
 3403:                                 my $colsleft = $numinrow - $rem;
 3404:                                 if ($colsleft > 1 ) {
 3405:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3406:                                                        '" class="LC_left_item">'.
 3407:                                                        '&nbsp;</td>';
 3408:                                 } elsif ($colsleft == 1) {
 3409:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3410:                                 }
 3411:                                 $current{$type} .= '</tr></table>';
 3412:                             }
 3413:                         }
 3414:                     }
 3415:                     if ($current{$type} eq '') {
 3416:                         $current{$type} = &mt('None specified');
 3417:                     }
 3418:                     if ($othercontrol) {
 3419:                         if ($type eq 'primary') {
 3420:                             $canselect{$type} = $othercontrol;
 3421:                         }
 3422:                     } else {
 3423:                         $canselect{$type} = 
 3424:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3425:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3426:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3427:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3428:                         if (@choices > 0) {
 3429:                             foreach my $lonhost (@choices) {
 3430:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3431:                             }
 3432:                         }
 3433:                         $canselect{$type} .= '</select>'."\n";
 3434:                     }
 3435:                 } else {
 3436:                     $current{$type} = &mt('Could not be determined');
 3437:                     if ($type eq 'primary') {
 3438:                         $canselect{$type} =  $othercontrol;
 3439:                     }
 3440:                 }
 3441:                 if ($type eq 'default') {
 3442:                     $datatable .= '<tr'.$css_class.'>';
 3443:                 }
 3444:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3445:                               '<td>'.$current{$type}.'</td>'."\n".
 3446:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3447:             }
 3448:             $itemcount ++;
 3449:         }
 3450:     }
 3451:     $$rowtotal += $itemcount;
 3452:     return $datatable;
 3453: }
 3454: 
 3455: sub possible_newspares {
 3456:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3457:     my $serverhostname = &Apache::lonnet::hostname($server);
 3458:     my %excluded;
 3459:     if ($serverhostname ne '') {
 3460:         %excluded = (
 3461:                        $serverhostname => 1,
 3462:                     );
 3463:     }
 3464:     if (ref($currspares) eq 'HASH') {
 3465:         foreach my $type (keys(%{$currspares})) {
 3466:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3467:                 if (@{$currspares->{$type}} > 0) {
 3468:                     foreach my $curr (@{$currspares->{$type}}) {
 3469:                         my $hostname = &Apache::lonnet::hostname($curr);
 3470:                         $excluded{$hostname} = 1;
 3471:                     }
 3472:                 }
 3473:             }
 3474:         }
 3475:     }
 3476:     my @choices;
 3477:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3478:         if (keys(%{$serverhomes}) > 1) {
 3479:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3480:                 unless ($excluded{$name}) {
 3481:                     if (exists($altids->{$serverhomes->{$name}})) {
 3482:                         push(@choices,$altids->{$serverhomes->{$name}});
 3483:                     } else {
 3484:                         push(@choices,$serverhomes->{$name});
 3485:                     }
 3486:                 }
 3487:             }
 3488:         }
 3489:     }
 3490:     return sort(@choices);
 3491: }
 3492: 
 3493: sub print_loadbalancing {
 3494:     my ($dom,$settings,$rowtotal) = @_;
 3495:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3496:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3497:     my $numinrow = 1;
 3498:     my $datatable;
 3499:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3500:     my (%currbalancer,%currtargets,%currrules,%existing);
 3501:     if (ref($settings) eq 'HASH') {
 3502:         %existing = %{$settings};
 3503:     }
 3504:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3505:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3506:                                   \%currtargets,\%currrules);
 3507:     } else {
 3508:         return;
 3509:     }
 3510:     my ($othertitle,$usertypes,$types) =
 3511:         &Apache::loncommon::sorted_inst_types($dom);
 3512:     my $rownum = 8;
 3513:     if (ref($types) eq 'ARRAY') {
 3514:         $rownum += scalar(@{$types});
 3515:     }
 3516:     my @css_class = ('LC_odd_row','LC_even_row');
 3517:     my $balnum = 0;
 3518:     my $islast;
 3519:     my (@toshow,$disabledtext);
 3520:     if (keys(%currbalancer) > 0) {
 3521:         @toshow = sort(keys(%currbalancer));
 3522:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3523:             push(@toshow,'');
 3524:         }
 3525:     } else {
 3526:         @toshow = ('');
 3527:         $disabledtext = &mt('No existing load balancer');
 3528:     }
 3529:     foreach my $lonhost (@toshow) {
 3530:         if ($balnum == scalar(@toshow)-1) {
 3531:             $islast = 1;
 3532:         } else {
 3533:             $islast = 0;
 3534:         }
 3535:         my $cssidx = $balnum%2;
 3536:         my $targets_div_style = 'display: none';
 3537:         my $disabled_div_style = 'display: block';
 3538:         my $homedom_div_style = 'display: none';
 3539:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3540:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3541:                       '<p>';
 3542:         if ($lonhost eq '') {
 3543:             $datatable .= '<span class="LC_nobreak">';
 3544:             if (keys(%currbalancer) > 0) {
 3545:                 $datatable .= &mt('Add balancer:');
 3546:             } else {
 3547:                 $datatable .= &mt('Enable balancer:');
 3548:             }
 3549:             $datatable .= '&nbsp;'.
 3550:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3551:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3552:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3553:                           '<option value="" selected="selected">'.&mt('None').
 3554:                           '</option>'."\n";
 3555:             foreach my $server (sort(keys(%servers))) {
 3556:                 next if ($currbalancer{$server});
 3557:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3558:             }
 3559:             $datatable .=
 3560:                 '</select>'."\n".
 3561:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3562:         } else {
 3563:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3564:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3565:                            &mt('Stop balancing').'</label>'.
 3566:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3567:             $targets_div_style = 'display: block';
 3568:             $disabled_div_style = 'display: none';
 3569:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3570:                 $homedom_div_style = 'display: block';
 3571:             }
 3572:         }
 3573:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3574:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3575:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3576:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3577:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3578:         my @sparestypes = ('primary','default');
 3579:         my %typetitles = &sparestype_titles();
 3580:         foreach my $sparetype (@sparestypes) {
 3581:             my $targettable;
 3582:             for (my $i=0; $i<$numspares; $i++) {
 3583:                 my $checked;
 3584:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3585:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3586:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3587:                             $checked = ' checked="checked"';
 3588:                         }
 3589:                     }
 3590:                 }
 3591:                 my ($chkboxval,$disabled);
 3592:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3593:                     $chkboxval = $spares[$i];
 3594:                 }
 3595:                 if (exists($currbalancer{$spares[$i]})) {
 3596:                     $disabled = ' disabled="disabled"';
 3597:                 }
 3598:                 $targettable .=
 3599:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3600:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3601:                     '</span></label></td>';
 3602:                 my $rem = $i%($numinrow);
 3603:                 if ($rem == 0) {
 3604:                     if (($i > 0) && ($i < $numspares-1)) {
 3605:                         $targettable .= '</tr>';
 3606:                     }
 3607:                     if ($i < $numspares-1) {
 3608:                         $targettable .= '<tr>';
 3609:                     }
 3610:                 }
 3611:             }
 3612:             if ($targettable ne '') {
 3613:                 my $rem = $numspares%($numinrow);
 3614:                 my $colsleft = $numinrow - $rem;
 3615:                 if ($colsleft > 1 ) {
 3616:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3617:                                     '&nbsp;</td>';
 3618:                 } elsif ($colsleft == 1) {
 3619:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3620:                 }
 3621:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3622:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3623:             }
 3624:         }
 3625:         $datatable .= '</div></td></tr>'.
 3626:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3627:                                            $othertitle,$usertypes,$types,\%servers,
 3628:                                            \%currbalancer,$lonhost,
 3629:                                            $targets_div_style,$homedom_div_style,
 3630:                                            $css_class[$cssidx],$balnum,$islast);
 3631:         $$rowtotal += $rownum;
 3632:         $balnum ++;
 3633:     }
 3634:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3635:     return $datatable;
 3636: }
 3637: 
 3638: sub get_loadbalancers_config {
 3639:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3640:     return unless ((ref($servers) eq 'HASH') &&
 3641:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3642:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3643:     if (keys(%{$existing}) > 0) {
 3644:         my $oldlonhost;
 3645:         foreach my $key (sort(keys(%{$existing}))) {
 3646:             if ($key eq 'lonhost') {
 3647:                 $oldlonhost = $existing->{'lonhost'};
 3648:                 $currbalancer->{$oldlonhost} = 1;
 3649:             } elsif ($key eq 'targets') {
 3650:                 if ($oldlonhost) {
 3651:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3652:                 }
 3653:             } elsif ($key eq 'rules') {
 3654:                 if ($oldlonhost) {
 3655:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3656:                 }
 3657:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3658:                 $currbalancer->{$key} = 1;
 3659:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3660:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3661:             }
 3662:         }
 3663:     } else {
 3664:         my ($balancerref,$targetsref) =
 3665:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3666:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3667:             foreach my $server (sort(keys(%{$balancerref}))) {
 3668:                 $currbalancer->{$server} = 1;
 3669:                 $currtargets->{$server} = $targetsref->{$server};
 3670:             }
 3671:         }
 3672:     }
 3673:     return;
 3674: }
 3675: 
 3676: sub loadbalancing_rules {
 3677:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3678:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3679:         $css_class,$balnum,$islast) = @_;
 3680:     my $output;
 3681:     my $num = 0;
 3682:     my ($alltypes,$othertypes,$titles) =
 3683:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3684:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3685:         foreach my $type (@{$alltypes}) {
 3686:             $num ++;
 3687:             my $current;
 3688:             if (ref($currrules) eq 'HASH') {
 3689:                 $current = $currrules->{$type};
 3690:             }
 3691:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3692:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3693:                     $current = '';
 3694:                 }
 3695:             }
 3696:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3697:                                              $servers,$currbalancer,$lonhost,$dom,
 3698:                                              $targets_div_style,$homedom_div_style,
 3699:                                              $css_class,$balnum,$num,$islast);
 3700:         }
 3701:     }
 3702:     return $output;
 3703: }
 3704: 
 3705: sub loadbalancing_titles {
 3706:     my ($dom,$intdom,$usertypes,$types) = @_;
 3707:     my %othertypes = (
 3708:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3709:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3710:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3711:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3712:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3713:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3714:                      );
 3715:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3716:     if (ref($types) eq 'ARRAY') {
 3717:         unshift(@alltypes,@{$types},'default');
 3718:     }
 3719:     my %titles;
 3720:     foreach my $type (@alltypes) {
 3721:         if ($type =~ /^_LC_/) {
 3722:             $titles{$type} = $othertypes{$type};
 3723:         } elsif ($type eq 'default') {
 3724:             $titles{$type} = &mt('All users from [_1]',$dom);
 3725:             if (ref($types) eq 'ARRAY') {
 3726:                 if (@{$types} > 0) {
 3727:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3728:                 }
 3729:             }
 3730:         } elsif (ref($usertypes) eq 'HASH') {
 3731:             $titles{$type} = $usertypes->{$type};
 3732:         }
 3733:     }
 3734:     return (\@alltypes,\%othertypes,\%titles);
 3735: }
 3736: 
 3737: sub loadbalance_rule_row {
 3738:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3739:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3740:     my @rulenames;
 3741:     my %ruletitles = &offloadtype_text();
 3742:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3743:         @rulenames = ('balancer','offloadedto');
 3744:     } else {
 3745:         @rulenames = ('default','homeserver');
 3746:         if ($type eq '_LC_external') {
 3747:             push(@rulenames,'externalbalancer');
 3748:         } else {
 3749:             push(@rulenames,'specific');
 3750:         }
 3751:         push(@rulenames,'none');
 3752:     }
 3753:     my $style = $targets_div_style;
 3754:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3755:         $style = $homedom_div_style;
 3756:     }
 3757:     my $space;
 3758:     if ($islast && $num == 1) {
 3759:         $space = '<div display="inline-block">&nbsp;</div>';
 3760:     }
 3761:     my $output =
 3762:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3763:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3764:         '<td valaign="top">'.$space.
 3765:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3766:     for (my $i=0; $i<@rulenames; $i++) {
 3767:         my $rule = $rulenames[$i];
 3768:         my ($checked,$extra);
 3769:         if ($rulenames[$i] eq 'default') {
 3770:             $rule = '';
 3771:         }
 3772:         if ($rulenames[$i] eq 'specific') {
 3773:             if (ref($servers) eq 'HASH') {
 3774:                 my $default;
 3775:                 if (($current ne '') && (exists($servers->{$current}))) {
 3776:                     $checked = ' checked="checked"';
 3777:                 }
 3778:                 unless ($checked) {
 3779:                     $default = ' selected="selected"';
 3780:                 }
 3781:                 $extra =
 3782:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3783:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3784:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3785:                     '<option value=""'.$default.'></option>'."\n";
 3786:                 foreach my $server (sort(keys(%{$servers}))) {
 3787:                     if (ref($currbalancer) eq 'HASH') {
 3788:                         next if (exists($currbalancer->{$server}));
 3789:                     }
 3790:                     my $selected;
 3791:                     if ($server eq $current) {
 3792:                         $selected = ' selected="selected"';
 3793:                     }
 3794:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3795:                 }
 3796:                 $extra .= '</select>';
 3797:             }
 3798:         } elsif ($rule eq $current) {
 3799:             $checked = ' checked="checked"';
 3800:         }
 3801:         $output .= '<span class="LC_nobreak"><label>'.
 3802:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3803:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3804:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3805:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3806:                    '</label>'.$extra.'</span><br />'."\n";
 3807:     }
 3808:     $output .= '</div></td></tr>'."\n";
 3809:     return $output;
 3810: }
 3811: 
 3812: sub offloadtype_text {
 3813:     my %ruletitles = &Apache::lonlocal::texthash (
 3814:            'default'          => 'Offloads to default destinations',
 3815:            'homeserver'       => "Offloads to user's home server",
 3816:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3817:            'specific'         => 'Offloads to specific server',
 3818:            'none'             => 'No offload',
 3819:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3820:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3821:     );
 3822:     return %ruletitles;
 3823: }
 3824: 
 3825: sub sparestype_titles {
 3826:     my %typestitles = &Apache::lonlocal::texthash (
 3827:                           'primary' => 'primary',
 3828:                           'default' => 'default',
 3829:                       );
 3830:     return %typestitles;
 3831: }
 3832: 
 3833: sub contact_titles {
 3834:     my %titles = &Apache::lonlocal::texthash (
 3835:                    'supportemail' => 'Support E-mail address',
 3836:                    'adminemail'   => 'Default Server Admin E-mail address',
 3837:                    'errormail'    => 'Error reports to be e-mailed to',
 3838:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3839:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3840:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3841:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3842:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3843:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3844:                  );
 3845:     my %short_titles = &Apache::lonlocal::texthash (
 3846:                            adminemail   => 'Admin E-mail address',
 3847:                            supportemail => 'Support E-mail',
 3848:                        );   
 3849:     return (\%titles,\%short_titles);
 3850: }
 3851: 
 3852: sub tool_titles {
 3853:     my %titles = &Apache::lonlocal::texthash (
 3854:                      aboutme    => 'Personal web page',
 3855:                      blog       => 'Blog',
 3856:                      webdav     => 'WebDAV',
 3857:                      portfolio  => 'Portfolio',
 3858:                      official   => 'Official courses (with institutional codes)',
 3859:                      unofficial => 'Unofficial courses',
 3860:                      community  => 'Communities',
 3861:                      textbook   => 'Textbook courses',
 3862:                  );
 3863:     return %titles;
 3864: }
 3865: 
 3866: sub courserequest_titles {
 3867:     my %titles = &Apache::lonlocal::texthash (
 3868:                                    official   => 'Official',
 3869:                                    unofficial => 'Unofficial',
 3870:                                    community  => 'Communities',
 3871:                                    textbook   => 'Textbook',
 3872:                                    norequest  => 'Not allowed',
 3873:                                    approval   => 'Approval by Dom. Coord.',
 3874:                                    validate   => 'With validation',
 3875:                                    autolimit  => 'Numerical limit',
 3876:                                    unlimited  => '(blank for unlimited)',
 3877:                  );
 3878:     return %titles;
 3879: }
 3880: 
 3881: sub authorrequest_titles {
 3882:     my %titles = &Apache::lonlocal::texthash (
 3883:                                    norequest  => 'Not allowed',
 3884:                                    approval   => 'Approval by Dom. Coord.',
 3885:                                    automatic  => 'Automatic approval',
 3886:                  );
 3887:     return %titles;
 3888: }
 3889: 
 3890: sub courserequest_conditions {
 3891:     my %conditions = &Apache::lonlocal::texthash (
 3892:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3893:        validate   => '(Processing of request subject to institutional validation).',
 3894:                  );
 3895:     return %conditions;
 3896: }
 3897: 
 3898: 
 3899: sub print_usercreation {
 3900:     my ($position,$dom,$settings,$rowtotal) = @_;
 3901:     my $numinrow = 4;
 3902:     my $datatable;
 3903:     if ($position eq 'top') {
 3904:         $$rowtotal ++;
 3905:         my $rowcount = 0;
 3906:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3907:         if (ref($rules) eq 'HASH') {
 3908:             if (keys(%{$rules}) > 0) {
 3909:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3910:                                                 $ruleorder,$numinrow,$rowcount);
 3911:                 $$rowtotal ++;
 3912:                 $rowcount ++;
 3913:             }
 3914:         }
 3915:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3916:         if (ref($idrules) eq 'HASH') {
 3917:             if (keys(%{$idrules}) > 0) {
 3918:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3919:                                                 $idruleorder,$numinrow,$rowcount);
 3920:                 $$rowtotal ++;
 3921:                 $rowcount ++;
 3922:             }
 3923:         }
 3924:         if ($rowcount == 0) {
 3925:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3926:             $$rowtotal ++;
 3927:             $rowcount ++;
 3928:         }
 3929:     } elsif ($position eq 'middle') {
 3930:         my @creators = ('author','course','requestcrs');
 3931:         my ($rules,$ruleorder) =
 3932:             &Apache::lonnet::inst_userrules($dom,'username');
 3933:         my %lt = &usercreation_types();
 3934:         my %checked;
 3935:         if (ref($settings) eq 'HASH') {
 3936:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3937:                 foreach my $item (@creators) {
 3938:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3939:                 }
 3940:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3941:                 foreach my $item (@creators) {
 3942:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3943:                         $checked{$item} = 'none';
 3944:                     }
 3945:                 }
 3946:             }
 3947:         }
 3948:         my $rownum = 0;
 3949:         foreach my $item (@creators) {
 3950:             $rownum ++;
 3951:             if ($checked{$item} eq '') {
 3952:                 $checked{$item} = 'any';
 3953:             }
 3954:             my $css_class;
 3955:             if ($rownum%2) {
 3956:                 $css_class = '';
 3957:             } else {
 3958:                 $css_class = ' class="LC_odd_row" ';
 3959:             }
 3960:             $datatable .= '<tr'.$css_class.'>'.
 3961:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3962:                          '</span></td><td align="right">';
 3963:             my @options = ('any');
 3964:             if (ref($rules) eq 'HASH') {
 3965:                 if (keys(%{$rules}) > 0) {
 3966:                     push(@options,('official','unofficial'));
 3967:                 }
 3968:             }
 3969:             push(@options,'none');
 3970:             foreach my $option (@options) {
 3971:                 my $type = 'radio';
 3972:                 my $check = ' ';
 3973:                 if ($checked{$item} eq $option) {
 3974:                     $check = ' checked="checked" ';
 3975:                 } 
 3976:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3977:                               '<input type="'.$type.'" name="can_createuser_'.
 3978:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3979:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3980:             }
 3981:             $datatable .= '</td></tr>';
 3982:         }
 3983:     } else {
 3984:         my @contexts = ('author','course','domain');
 3985:         my @authtypes = ('int','krb4','krb5','loc');
 3986:         my %checked;
 3987:         if (ref($settings) eq 'HASH') {
 3988:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3989:                 foreach my $item (@contexts) {
 3990:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3991:                         foreach my $auth (@authtypes) {
 3992:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3993:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3994:                             }
 3995:                         }
 3996:                     }
 3997:                 }
 3998:             }
 3999:         } else {
 4000:             foreach my $item (@contexts) {
 4001:                 foreach my $auth (@authtypes) {
 4002:                     $checked{$item}{$auth} = ' checked="checked" ';
 4003:                 }
 4004:             }
 4005:         }
 4006:         my %title = &context_names();
 4007:         my %authname = &authtype_names();
 4008:         my $rownum = 0;
 4009:         my $css_class; 
 4010:         foreach my $item (@contexts) {
 4011:             if ($rownum%2) {
 4012:                 $css_class = '';
 4013:             } else {
 4014:                 $css_class = ' class="LC_odd_row" ';
 4015:             }
 4016:             $datatable .=   '<tr'.$css_class.'>'.
 4017:                             '<td>'.$title{$item}.
 4018:                             '</td><td class="LC_left_item">'.
 4019:                             '<span class="LC_nobreak">';
 4020:             foreach my $auth (@authtypes) {
 4021:                 $datatable .= '<label>'. 
 4022:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4023:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4024:                               $authname{$auth}.'</label>&nbsp;';
 4025:             }
 4026:             $datatable .= '</span></td></tr>';
 4027:             $rownum ++;
 4028:         }
 4029:         $$rowtotal += $rownum;
 4030:     }
 4031:     return $datatable;
 4032: }
 4033: 
 4034: sub print_selfcreation {
 4035:     my ($position,$dom,$settings,$rowtotal) = @_;
 4036:     my (@selfcreate,$createsettings,$processing,$datatable);
 4037:     if (ref($settings) eq 'HASH') {
 4038:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4039:             $createsettings = $settings->{'cancreate'};
 4040:             if (ref($createsettings) eq 'HASH') {
 4041:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4042:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4043:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4044:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4045:                         @selfcreate = ('email','login','sso');
 4046:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4047:                         @selfcreate = ($createsettings->{'selfcreate'});
 4048:                     }
 4049:                 }
 4050:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4051:                     $processing = $createsettings->{'selfcreateprocessing'};
 4052:                 }
 4053:             }
 4054:         }
 4055:     }
 4056:     my %radiohash;
 4057:     my $numinrow = 4;
 4058:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4059:     if ($position eq 'top') {
 4060:         my %choices = &Apache::lonlocal::texthash (
 4061:                                                       cancreate_login      => 'Institutional Login',
 4062:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4063:                                                   );
 4064:         my @toggles = sort(keys(%choices));
 4065:         my %defaultchecked = (
 4066:                                'cancreate_login' => 'off',
 4067:                                'cancreate_sso'   => 'off',
 4068:                              );
 4069:         my ($onclick,$itemcount);
 4070:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4071:                                                      \%choices,$itemcount,$onclick);
 4072:         $$rowtotal += $itemcount;
 4073:         
 4074:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4075: 
 4076:         if (ref($usertypes) eq 'HASH') {
 4077:             if (keys(%{$usertypes}) > 0) {
 4078:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4079:                                              $dom,$numinrow,$othertitle,
 4080:                                              'statustocreate',$$rowtotal);
 4081:                 $$rowtotal ++;
 4082:             }
 4083:         }
 4084:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 4085:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4086:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 4087:         my $rem;
 4088:         my $numperrow = 2;
 4089:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4090:         $datatable .= '<tr'.$css_class.'>'.
 4091:                      '<td class="LC_left_item">'.&mt('Shibboleth (SSO) Data').'</td>'.
 4092:                      '<td class="LC_left_item">'."\n".
 4093:                      '<table><tr><td>'."\n";
 4094:         for (my $i=0; $i<@fields; $i++) {
 4095:             $rem = $i%($numperrow);
 4096:             if ($rem == 0) {
 4097:                 if ($i > 0) {
 4098:                     $datatable .= '</tr>';
 4099:                 }
 4100:                 $datatable .= '<tr>';
 4101:             }
 4102:             my $currval;
 4103:             if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 4104:                 $currval = $createsettings->{'shibenv'}{$fields[$i]};
 4105:             }
 4106:             $datatable .= '<td class="LC_left_item">'.
 4107:                           '<span class="LC_nobreak">'.
 4108:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 4109:                           'value="'.$currval.'" size="10" />&nbsp;'.
 4110:                           $fieldtitles{$fields[$i]}.'</span></td>';
 4111:         }
 4112:         my $colsleft = $numperrow - $rem;
 4113:         if ($colsleft > 1 ) {
 4114:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4115:                          '&nbsp;</td>';
 4116:         } elsif ($colsleft == 1) {
 4117:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4118:         }
 4119:         $datatable .= '</tr></table></td></tr>';
 4120:         $$rowtotal ++;
 4121:     } elsif ($position eq 'middle') {
 4122:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4123:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4124:         $usertypes->{'default'} = $othertitle;
 4125:         if (ref($types) eq 'ARRAY') {
 4126:             push(@{$types},'default');
 4127:             $usertypes->{'default'} = $othertitle;
 4128:             foreach my $status (@{$types}) {
 4129:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4130:                                                        $numinrow,$$rowtotal,$usertypes);
 4131:                 $$rowtotal ++;
 4132:             }
 4133:         }
 4134:     } else {
 4135:         my %choices = &Apache::lonlocal::texthash (
 4136:                                                       cancreate_email => 'E-mail address as username',
 4137:                                                   );
 4138:         my @toggles = sort(keys(%choices));
 4139:         my %defaultchecked = (
 4140:                                'cancreate_email' => 'off',
 4141:                              );
 4142:         my $itemcount = 0;
 4143:         my $display = 'none';
 4144:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 4145:             $display = 'block';
 4146:         }
 4147:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 4148:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 4149:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4150:         my $usertypes = {};
 4151:         my $order = [];
 4152:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 4153:             $usertypes = $domdefaults{'inststatustypes'};
 4154:             $order = $domdefaults{'inststatusguest'};
 4155:         }
 4156:         if (ref($order) eq 'ARRAY') {
 4157:             push(@{$order},'default');
 4158:             if (@{$order} > 1) {
 4159:                 $usertypes->{'default'} = &mt('Other users');
 4160:                 $additional .= '<table><tr>';
 4161:                 foreach my $status (@{$order}) {
 4162:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 4163:                 }
 4164:                 $additional .= '</tr><tr>';
 4165:                 foreach my $status (@{$order}) {
 4166:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 4167:                 }
 4168:                 $additional .= '</tr></table>';
 4169:             } else {
 4170:                 $usertypes->{'default'} = &mt('All users');
 4171:                 $additional .= &email_as_username($rowtotal,$processing);
 4172:             }
 4173:         }
 4174:         $additional .= '</div>'."\n";
 4175: 
 4176:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4177:                                                      \%choices,$$rowtotal,$onclick,$additional);
 4178:         $$rowtotal ++;
 4179:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4180:         $$rowtotal ++;
 4181:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4182:         $numinrow = 1;
 4183:         if (ref($order) eq 'ARRAY') {
 4184:             foreach my $status (@{$order}) {
 4185:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4186:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4187:                 $$rowtotal ++;
 4188:             }
 4189:         }
 4190:         my ($emailrules,$emailruleorder) =
 4191:             &Apache::lonnet::inst_userrules($dom,'email');
 4192:         if (ref($emailrules) eq 'HASH') {
 4193:             if (keys(%{$emailrules}) > 0) {
 4194:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4195:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4196:                 $$rowtotal ++;
 4197:             }
 4198:         }
 4199:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4200:     }
 4201:     return $datatable;
 4202: }
 4203: 
 4204: sub email_as_username {
 4205:     my ($rowtotal,$processing,$type) = @_;
 4206:     my %choices =
 4207:         &Apache::lonlocal::texthash (
 4208:                                       automatic => 'Automatic approval',
 4209:                                       approval  => 'Queued for approval',
 4210:                                     );
 4211:     my $output;
 4212:     foreach my $option ('automatic','approval') {
 4213:         my $checked;
 4214:         if (ref($processing) eq 'HASH') {
 4215:             if ($type eq '') {   
 4216:                 if (!exists($processing->{'default'})) {
 4217:                     if ($option eq 'automatic') {
 4218:                         $checked = ' checked="checked"';
 4219:                     }
 4220:                 } else {
 4221:                     if ($processing->{'default'} eq $option) {
 4222:                         $checked = ' checked="checked"';
 4223:                     }
 4224:                 }
 4225:             } else {
 4226:                 if (!exists($processing->{$type})) {
 4227:                     if ($option eq 'automatic') {
 4228:                         $checked = ' checked="checked"';
 4229:                     }
 4230:                 } else {
 4231:                     if ($processing->{$type} eq $option) {
 4232:                         $checked = ' checked="checked"';
 4233:                     }
 4234:                 }
 4235:             }
 4236:         } elsif ($option eq 'automatic') {
 4237:             $checked = ' checked="checked"'; 
 4238:         }
 4239:         my $name = 'cancreate_emailprocess';
 4240:         if (($type ne '') && ($type ne 'default')) {
 4241:             $name .= '_'.$type;
 4242:         }
 4243:         $output .= '<span class="LC_nobreak"><label>'.
 4244:                    '<input type="radio" name="'.$name.'"'.
 4245:                    $checked.' value="'.$option.'" />'.
 4246:                    $choices{$option}.'</label></span>';
 4247:         if ($type eq '') {
 4248:             $output .= '&nbsp;';
 4249:         } else {
 4250:             $output .= '<br />';
 4251:         }
 4252:     }
 4253:     $$rowtotal ++;
 4254:     return $output;
 4255: }
 4256: 
 4257: sub captcha_choice {
 4258:     my ($context,$settings,$itemcount) = @_;
 4259:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4260:     my %lt = &captcha_phrases();
 4261:     $keyentry = 'hidden';
 4262:     if ($context eq 'cancreate') {
 4263:         $rowname = &mt('CAPTCHA validation');
 4264:     } elsif ($context eq 'login') {
 4265:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4266:     }
 4267:     if (ref($settings) eq 'HASH') {
 4268:         if ($settings->{'captcha'}) {
 4269:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4270:         } else {
 4271:             $checked{'original'} = ' checked="checked"';
 4272:         }
 4273:         if ($settings->{'captcha'} eq 'recaptcha') {
 4274:             $pubtext = $lt{'pub'};
 4275:             $privtext = $lt{'priv'};
 4276:             $keyentry = 'text';
 4277:         }
 4278:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4279:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4280:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4281:         }
 4282:     } else {
 4283:         $checked{'original'} = ' checked="checked"';
 4284:     }
 4285:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4286:     my $output = '<tr'.$css_class.'>'.
 4287:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4288:                  '<table><tr><td>'."\n";
 4289:     foreach my $option ('original','recaptcha','notused') {
 4290:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4291:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4292:                    $lt{$option}.'</label></span>';
 4293:         unless ($option eq 'notused') {
 4294:             $output .= ('&nbsp;'x2)."\n";
 4295:         }
 4296:     }
 4297: #
 4298: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4299: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4300: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4301: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4302: #
 4303:     $output .= '</td></tr>'."\n".
 4304:                '<tr><td>'."\n".
 4305:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4306:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4307:                $currpub.'" size="40" /></span><br />'."\n".
 4308:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4309:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4310:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4311:                '</td></tr>';
 4312:     return $output;
 4313: }
 4314: 
 4315: sub user_formats_row {
 4316:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4317:     my $output;
 4318:     my %text = (
 4319:                    'username' => 'new usernames',
 4320:                    'id'       => 'IDs',
 4321:                    'email'    => 'self-created accounts (e-mail)',
 4322:                );
 4323:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4324:     $output = '<tr '.$css_class.'>'.
 4325:               '<td><span class="LC_nobreak">';
 4326:     if ($type eq 'email') {
 4327:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4328:     } else {
 4329:         $output .= &mt("Format rules to check for $text{$type}: ");
 4330:     }
 4331:     $output .= '</span></td>'.
 4332:                '<td class="LC_left_item" colspan="2"><table>';
 4333:     my $rem;
 4334:     if (ref($ruleorder) eq 'ARRAY') {
 4335:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4336:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4337:                 my $rem = $i%($numinrow);
 4338:                 if ($rem == 0) {
 4339:                     if ($i > 0) {
 4340:                         $output .= '</tr>';
 4341:                     }
 4342:                     $output .= '<tr>';
 4343:                 }
 4344:                 my $check = ' ';
 4345:                 if (ref($settings) eq 'HASH') {
 4346:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4347:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4348:                             $check = ' checked="checked" ';
 4349:                         }
 4350:                     }
 4351:                 }
 4352:                 $output .= '<td class="LC_left_item">'.
 4353:                            '<span class="LC_nobreak"><label>'.
 4354:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4355:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4356:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4357:             }
 4358:         }
 4359:         $rem = @{$ruleorder}%($numinrow);
 4360:     }
 4361:     my $colsleft = $numinrow - $rem;
 4362:     if ($colsleft > 1 ) {
 4363:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4364:                    '&nbsp;</td>';
 4365:     } elsif ($colsleft == 1) {
 4366:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4367:     }
 4368:     $output .= '</tr></table></td></tr>';
 4369:     return $output;
 4370: }
 4371: 
 4372: sub usercreation_types {
 4373:     my %lt = &Apache::lonlocal::texthash (
 4374:                     author     => 'When adding a co-author',
 4375:                     course     => 'When adding a user to a course',
 4376:                     requestcrs => 'When requesting a course',
 4377:                     any        => 'Any',
 4378:                     official   => 'Institutional only ',
 4379:                     unofficial => 'Non-institutional only',
 4380:                     none       => 'None',
 4381:     );
 4382:     return %lt;
 4383: }
 4384: 
 4385: sub selfcreation_types {
 4386:     my %lt = &Apache::lonlocal::texthash (
 4387:                     selfcreate => 'User creates own account',
 4388:                     any        => 'Any',
 4389:                     official   => 'Institutional only ',
 4390:                     unofficial => 'Non-institutional only',
 4391:                     email      => 'E-mail address',
 4392:                     login      => 'Institutional Login',
 4393:                     sso        => 'SSO',
 4394:              );
 4395: }
 4396: 
 4397: sub authtype_names {
 4398:     my %lt = &Apache::lonlocal::texthash(
 4399:                       int    => 'Internal',
 4400:                       krb4   => 'Kerberos 4',
 4401:                       krb5   => 'Kerberos 5',
 4402:                       loc    => 'Local',
 4403:                   );
 4404:     return %lt;
 4405: }
 4406: 
 4407: sub context_names {
 4408:     my %context_title = &Apache::lonlocal::texthash(
 4409:        author => 'Creating users when an Author',
 4410:        course => 'Creating users when in a course',
 4411:        domain => 'Creating users when a Domain Coordinator',
 4412:     );
 4413:     return %context_title;
 4414: }
 4415: 
 4416: sub print_usermodification {
 4417:     my ($position,$dom,$settings,$rowtotal) = @_;
 4418:     my $numinrow = 4;
 4419:     my ($context,$datatable,$rowcount);
 4420:     if ($position eq 'top') {
 4421:         $rowcount = 0;
 4422:         $context = 'author'; 
 4423:         foreach my $role ('ca','aa') {
 4424:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4425:                                                    $numinrow,$rowcount);
 4426:             $$rowtotal ++;
 4427:             $rowcount ++;
 4428:         }
 4429:     } elsif ($position eq 'bottom') {
 4430:         $context = 'course';
 4431:         $rowcount = 0;
 4432:         foreach my $role ('st','ep','ta','in','cr') {
 4433:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4434:                                                    $numinrow,$rowcount);
 4435:             $$rowtotal ++;
 4436:             $rowcount ++;
 4437:         }
 4438:     }
 4439:     return $datatable;
 4440: }
 4441: 
 4442: sub print_defaults {
 4443:     my ($position,$dom,$settings,$rowtotal) = @_;
 4444:     my $rownum = 0;
 4445:     my ($datatable,$css_class);
 4446:     if ($position eq 'top') {
 4447:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4448:                      'datelocale_def','portal_def');
 4449:         my %defaults;
 4450:         if (ref($settings) eq 'HASH') {
 4451:             %defaults = %{$settings};
 4452:         } else {
 4453:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4454:             foreach my $item (@items) {
 4455:                 $defaults{$item} = $domdefaults{$item};
 4456:             }
 4457:         }
 4458:         my $titles = &defaults_titles($dom);
 4459:         foreach my $item (@items) {
 4460:             if ($rownum%2) {
 4461:                 $css_class = '';
 4462:             } else {
 4463:                 $css_class = ' class="LC_odd_row" ';
 4464:             }
 4465:             $datatable .= '<tr'.$css_class.'>'.
 4466:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4467:                           '</span></td><td class="LC_right_item" colspan="3">';
 4468:             if ($item eq 'auth_def') {
 4469:                 my @authtypes = ('internal','krb4','krb5','localauth');
 4470:                 my %shortauth = (
 4471:                                  internal => 'int',
 4472:                                  krb4 => 'krb4',
 4473:                                  krb5 => 'krb5',
 4474:                                  localauth  => 'loc'
 4475:                                 );
 4476:                 my %authnames = &authtype_names();
 4477:                 foreach my $auth (@authtypes) {
 4478:                     my $checked = ' ';
 4479:                     if ($defaults{$item} eq $auth) {
 4480:                         $checked = ' checked="checked" ';
 4481:                     }
 4482:                     $datatable .= '<label><input type="radio" name="'.$item.
 4483:                                   '" value="'.$auth.'"'.$checked.'/>'.
 4484:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4485:                 }
 4486:             } elsif ($item eq 'timezone_def') {
 4487:                 my $includeempty = 1;
 4488:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4489:             } elsif ($item eq 'datelocale_def') {
 4490:                 my $includeempty = 1;
 4491:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4492:             } elsif ($item eq 'lang_def') {
 4493:                 my %langchoices = &get_languages_hash();
 4494:                 $langchoices{''} = 'No language preference';
 4495:                 %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4496:                 $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4497:                                                               \%langchoices);
 4498:             } else {
 4499:                 my $size;
 4500:                 if ($item eq 'portal_def') {
 4501:                     $size = ' size="25"';
 4502:                 }
 4503:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4504:                               $defaults{$item}.'"'.$size.' />';
 4505:             }
 4506:             $datatable .= '</td></tr>';
 4507:             $rownum ++;
 4508:         }
 4509:     } else {
 4510:         my (%defaults);
 4511:         if (ref($settings) eq 'HASH') {
 4512:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 4513:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 4514:                 my $maxnum = @{$settings->{'inststatusorder'}};
 4515:                 for (my $i=0; $i<$maxnum; $i++) {
 4516:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 4517:                     my $item = $settings->{'inststatusorder'}->[$i];
 4518:                     my $title = $settings->{'inststatustypes'}->{$item};
 4519:                     my $guestok;
 4520:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 4521:                         $guestok = 1;
 4522:                     }
 4523:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 4524:                     $datatable .= '<tr'.$css_class.'>'.
 4525:                                   '<td><span class="LC_nobreak">'.
 4526:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 4527:                     for (my $k=0; $k<=$maxnum; $k++) {
 4528:                         my $vpos = $k+1;
 4529:                         my $selstr;
 4530:                         if ($k == $i) {
 4531:                             $selstr = ' selected="selected" ';
 4532:                         }
 4533:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4534:                     }
 4535:                     my ($checkedon,$checkedoff);
 4536:                     $checkedoff = ' checked="checked"';
 4537:                     if ($guestok) {
 4538:                         $checkedon = $checkedoff;
 4539:                         $checkedoff = ''; 
 4540:                     }
 4541:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 4542:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 4543:                                   &mt('delete').'</span></td>'.
 4544:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 4545:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 4546:                                   '</span></td>'.
 4547:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4548:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 4549:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 4550:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 4551:                                   &mt('No').'</label></span></td></tr>';
 4552:                 }
 4553:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 4554:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 4555:                 $datatable .= '<tr '.$css_class.'>'.
 4556:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 4557:                 for (my $k=0; $k<=$maxnum; $k++) {
 4558:                     my $vpos = $k+1;
 4559:                     my $selstr;
 4560:                     if ($k == $maxnum) {
 4561:                         $selstr = ' selected="selected" ';
 4562:                     }
 4563:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4564:                 }
 4565:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 4566:                               '<input type="text" size="10" name="addinststatus" value="" /></span>'.
 4567:                               '&nbsp;'.&mt('(new)').
 4568:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 4569:                               &mt('Name displayed:').
 4570:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 4571:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 4572:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 4573:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 4574:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 4575:                               &mt('No').'</label></span></td></tr>';
 4576:                               '</tr>'."\n";
 4577:                 $rownum ++;
 4578:             }
 4579:         }
 4580:     }
 4581:     $$rowtotal += $rownum;
 4582:     return $datatable;
 4583: }
 4584: 
 4585: sub get_languages_hash {
 4586:     my %langchoices;
 4587:     foreach my $id (&Apache::loncommon::languageids()) {
 4588:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4589:         if ($code ne '') {
 4590:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4591:         }
 4592:     }
 4593:     return %langchoices;
 4594: }
 4595: 
 4596: sub defaults_titles {
 4597:     my ($dom) = @_;
 4598:     my %titles = &Apache::lonlocal::texthash (
 4599:                    'auth_def'      => 'Default authentication type',
 4600:                    'auth_arg_def'  => 'Default authentication argument',
 4601:                    'lang_def'      => 'Default language',
 4602:                    'timezone_def'  => 'Default timezone',
 4603:                    'datelocale_def' => 'Default locale for dates',
 4604:                    'portal_def'     => 'Portal/Default URL',
 4605:                  );
 4606:     if ($dom) {
 4607:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4608:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4609:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4610:         $protocol = 'http' if ($protocol ne 'https');
 4611:         if ($uint_dom) {
 4612:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4613:                                          $uint_dom);
 4614:         }
 4615:     }
 4616:     return (\%titles);
 4617: }
 4618: 
 4619: sub print_scantronformat {
 4620:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4621:     my $itemcount = 1;
 4622:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4623:         %confhash);
 4624:     my $switchserver = &check_switchserver($dom,$confname);
 4625:     my %lt = &Apache::lonlocal::texthash (
 4626:                 default => 'Default bubblesheet format file error',
 4627:                 custom  => 'Custom bubblesheet format file error',
 4628:              );
 4629:     my %scantronfiles = (
 4630:         default => 'default.tab',
 4631:         custom => 'custom.tab',
 4632:     );
 4633:     foreach my $key (keys(%scantronfiles)) {
 4634:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4635:                               .$scantronfiles{$key};
 4636:     }
 4637:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4638:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4639:         if (!$switchserver) {
 4640:             my $servadm = $r->dir_config('lonAdmEMail');
 4641:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4642:             if ($configuserok eq 'ok') {
 4643:                 if ($author_ok eq 'ok') {
 4644:                     my %legacyfile = (
 4645:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4646:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4647:                     );
 4648:                     my %md5chk;
 4649:                     foreach my $type (keys(%legacyfile)) {
 4650:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4651:                         chomp($md5chk{$type});
 4652:                     }
 4653:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4654:                         foreach my $type (keys(%legacyfile)) {
 4655:                             ($scantronurls{$type},my $error) = 
 4656:                                 &legacy_scantronformat($r,$dom,$confname,
 4657:                                                  $type,$legacyfile{$type},
 4658:                                                  $scantronurls{$type},
 4659:                                                  $scantronfiles{$type});
 4660:                             if ($error ne '') {
 4661:                                 $error{$type} = $error;
 4662:                             }
 4663:                         }
 4664:                         if (keys(%error) == 0) {
 4665:                             $is_custom = 1;
 4666:                             $confhash{'scantron'}{'scantronformat'} = 
 4667:                                 $scantronurls{'custom'};
 4668:                             my $putresult = 
 4669:                                 &Apache::lonnet::put_dom('configuration',
 4670:                                                          \%confhash,$dom);
 4671:                             if ($putresult ne 'ok') {
 4672:                                 $error{'custom'} = 
 4673:                                     '<span class="LC_error">'.
 4674:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4675:                             }
 4676:                         }
 4677:                     } else {
 4678:                         ($scantronurls{'default'},my $error) =
 4679:                             &legacy_scantronformat($r,$dom,$confname,
 4680:                                           'default',$legacyfile{'default'},
 4681:                                           $scantronurls{'default'},
 4682:                                           $scantronfiles{'default'});
 4683:                         if ($error eq '') {
 4684:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4685:                             my $putresult =
 4686:                                 &Apache::lonnet::put_dom('configuration',
 4687:                                                          \%confhash,$dom);
 4688:                             if ($putresult ne 'ok') {
 4689:                                 $error{'default'} =
 4690:                                     '<span class="LC_error">'.
 4691:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4692:                             }
 4693:                         } else {
 4694:                             $error{'default'} = $error;
 4695:                         }
 4696:                     }
 4697:                 }
 4698:             }
 4699:         } else {
 4700:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4701:         }
 4702:     }
 4703:     if (ref($settings) eq 'HASH') {
 4704:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4705:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4706:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4707:                 $scantronurl = '';
 4708:             } else {
 4709:                 $scantronurl = $settings->{'scantronformat'};
 4710:             }
 4711:             $is_custom = 1;
 4712:         } else {
 4713:             $scantronurl = $scantronurls{'default'};
 4714:         }
 4715:     } else {
 4716:         if ($is_custom) {
 4717:             $scantronurl = $scantronurls{'custom'};
 4718:         } else {
 4719:             $scantronurl = $scantronurls{'default'};
 4720:         }
 4721:     }
 4722:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4723:     $datatable .= '<tr'.$css_class.'>';
 4724:     if (!$is_custom) {
 4725:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4726:                       '<span class="LC_nobreak">';
 4727:         if ($scantronurl) {
 4728:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4729:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4730:         } else {
 4731:             $datatable = &mt('File unavailable for display');
 4732:         }
 4733:         $datatable .= '</span></td>';
 4734:         if (keys(%error) == 0) { 
 4735:             $datatable .= '<td valign="bottom">';
 4736:             if (!$switchserver) {
 4737:                 $datatable .= &mt('Upload:').'<br />';
 4738:             }
 4739:         } else {
 4740:             my $errorstr;
 4741:             foreach my $key (sort(keys(%error))) {
 4742:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4743:             }
 4744:             $datatable .= '<td>'.$errorstr;
 4745:         }
 4746:     } else {
 4747:         if (keys(%error) > 0) {
 4748:             my $errorstr;
 4749:             foreach my $key (sort(keys(%error))) {
 4750:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4751:             } 
 4752:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4753:         } elsif ($scantronurl) {
 4754:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4755:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4756:             $datatable .= '<td><span class="LC_nobreak">'.
 4757:                           $link.
 4758:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4759:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4760:                           '<td><span class="LC_nobreak">&nbsp;'.
 4761:                           &mt('Replace:').'</span><br />';
 4762:         }
 4763:     }
 4764:     if (keys(%error) == 0) {
 4765:         if ($switchserver) {
 4766:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4767:         } else {
 4768:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4769:                          '<input type="file" name="scantronformat" /></span>';
 4770:         }
 4771:     }
 4772:     $datatable .= '</td></tr>';
 4773:     $$rowtotal ++;
 4774:     return $datatable;
 4775: }
 4776: 
 4777: sub legacy_scantronformat {
 4778:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4779:     my ($url,$error);
 4780:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4781:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4782:         (my $result,$url) =
 4783:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4784:                          '','',$newfile);
 4785:         if ($result ne 'ok') {
 4786:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4787:         }
 4788:     }
 4789:     return ($url,$error);
 4790: }
 4791: 
 4792: sub print_coursecategories {
 4793:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4794:     my $datatable;
 4795:     if ($position eq 'top') {
 4796:         my (%checked);
 4797:         my @catitems = ('unauth','auth');
 4798:         my @cattypes = ('std','domonly','codesrch','none');
 4799:         $checked{'unauth'} = 'std';
 4800:         $checked{'auth'} = 'std';
 4801:         if (ref($settings) eq 'HASH') {
 4802:             foreach my $type (@cattypes) {
 4803:                 if ($type eq $settings->{'unauth'}) {
 4804:                     $checked{'unauth'} = $type;
 4805:                 }
 4806:                 if ($type eq $settings->{'auth'}) {
 4807:                     $checked{'auth'} = $type;
 4808:                 }
 4809:             }
 4810:         }
 4811:         my %lt = &Apache::lonlocal::texthash (
 4812:                                                unauth   => 'Catalog type for unauthenticated users',
 4813:                                                auth     => 'Catalog type for authenticated users',
 4814:                                                none     => 'No catalog',
 4815:                                                std      => 'Standard catalog',
 4816:                                                domonly  => 'Domain-only catalog',
 4817:                                                codesrch => "Code search form",
 4818:                                              );
 4819:        my $itemcount = 0;
 4820:        foreach my $item (@catitems) {
 4821:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 4822:            $datatable .= '<tr '.$css_class.'>'.
 4823:                          '<td>'.$lt{$item}.'</td>'.
 4824:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 4825:            foreach my $type (@cattypes) {
 4826:                my $ischecked;
 4827:                if ($checked{$item} eq $type) {
 4828:                    $ischecked=' checked="checked"';
 4829:                }
 4830:                $datatable .= '<label>'.
 4831:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 4832:                              ' />'.$lt{$type}.'</label>&nbsp;';
 4833:            }
 4834:            $datatable .= '</td></tr>';
 4835:            $itemcount ++;
 4836:         }
 4837:         $$rowtotal += $itemcount;
 4838:     } elsif ($position eq 'middle') {
 4839:         my $toggle_cats_crs = ' ';
 4840:         my $toggle_cats_dom = ' checked="checked" ';
 4841:         my $can_cat_crs = ' ';
 4842:         my $can_cat_dom = ' checked="checked" ';
 4843:         my $toggle_catscomm_comm = ' ';
 4844:         my $toggle_catscomm_dom = ' checked="checked" ';
 4845:         my $can_catcomm_comm = ' ';
 4846:         my $can_catcomm_dom = ' checked="checked" ';
 4847: 
 4848:         if (ref($settings) eq 'HASH') {
 4849:             if ($settings->{'togglecats'} eq 'crs') {
 4850:                 $toggle_cats_crs = $toggle_cats_dom;
 4851:                 $toggle_cats_dom = ' ';
 4852:             }
 4853:             if ($settings->{'categorize'} eq 'crs') {
 4854:                 $can_cat_crs = $can_cat_dom;
 4855:                 $can_cat_dom = ' ';
 4856:             }
 4857:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4858:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4859:                 $toggle_catscomm_dom = ' ';
 4860:             }
 4861:             if ($settings->{'categorizecomm'} eq 'comm') {
 4862:                 $can_catcomm_comm = $can_catcomm_dom;
 4863:                 $can_catcomm_dom = ' ';
 4864:             }
 4865:         }
 4866:         my %title = &Apache::lonlocal::texthash (
 4867:                      togglecats     => 'Show/Hide a course in catalog',
 4868:                      togglecatscomm => 'Show/Hide a community in catalog',
 4869:                      categorize     => 'Assign a category to a course',
 4870:                      categorizecomm => 'Assign a category to a community',
 4871:                     );
 4872:         my %level = &Apache::lonlocal::texthash (
 4873:                      dom  => 'Set in Domain',
 4874:                      crs  => 'Set in Course',
 4875:                      comm => 'Set in Community',
 4876:                     );
 4877:         $datatable = '<tr class="LC_odd_row">'.
 4878:                   '<td>'.$title{'togglecats'}.'</td>'.
 4879:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4880:                   '<input type="radio" name="togglecats"'.
 4881:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4882:                   '<label><input type="radio" name="togglecats"'.
 4883:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4884:                   '</tr><tr>'.
 4885:                   '<td>'.$title{'categorize'}.'</td>'.
 4886:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4887:                   '<label><input type="radio" name="categorize"'.
 4888:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4889:                   '<label><input type="radio" name="categorize"'.
 4890:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4891:                   '</tr><tr class="LC_odd_row">'.
 4892:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4893:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4894:                   '<input type="radio" name="togglecatscomm"'.
 4895:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4896:                   '<label><input type="radio" name="togglecatscomm"'.
 4897:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4898:                   '</tr><tr>'.
 4899:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4900:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4901:                   '<label><input type="radio" name="categorizecomm"'.
 4902:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4903:                   '<label><input type="radio" name="categorizecomm"'.
 4904:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4905:                   '</tr>';
 4906:         $$rowtotal += 4;
 4907:     } else {
 4908:         my $css_class;
 4909:         my $itemcount = 1;
 4910:         my $cathash; 
 4911:         if (ref($settings) eq 'HASH') {
 4912:             $cathash = $settings->{'cats'};
 4913:         }
 4914:         if (ref($cathash) eq 'HASH') {
 4915:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4916:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4917:                                                    \%allitems,\%idx,\@jsarray);
 4918:             my $maxdepth = scalar(@cats);
 4919:             my $colattrib = '';
 4920:             if ($maxdepth > 2) {
 4921:                 $colattrib = ' colspan="2" ';
 4922:             }
 4923:             my @path;
 4924:             if (@cats > 0) {
 4925:                 if (ref($cats[0]) eq 'ARRAY') {
 4926:                     my $numtop = @{$cats[0]};
 4927:                     my $maxnum = $numtop;
 4928:                     my %default_names = (
 4929:                           instcode    => &mt('Official courses'),
 4930:                           communities => &mt('Communities'),
 4931:                     );
 4932: 
 4933:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4934:                         ($cathash->{'instcode::0'} eq '') ||
 4935:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4936:                         ($cathash->{'communities::0'} eq '')) {
 4937:                         $maxnum ++;
 4938:                     }
 4939:                     my $lastidx;
 4940:                     for (my $i=0; $i<$numtop; $i++) {
 4941:                         my $parent = $cats[0][$i];
 4942:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4943:                         my $item = &escape($parent).'::0';
 4944:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4945:                         $lastidx = $idx{$item};
 4946:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4947:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4948:                         for (my $k=0; $k<=$maxnum; $k++) {
 4949:                             my $vpos = $k+1;
 4950:                             my $selstr;
 4951:                             if ($k == $i) {
 4952:                                 $selstr = ' selected="selected" ';
 4953:                             }
 4954:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4955:                         }
 4956:                         $datatable .= '</select></span></td><td>';
 4957:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4958:                             $datatable .=  '<span class="LC_nobreak">'
 4959:                                            .$default_names{$parent}.'</span>';
 4960:                             if ($parent eq 'instcode') {
 4961:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4962:                                               .&mt('with institutional codes')
 4963:                                               .')</span></td><td'.$colattrib.'>';
 4964:                             } else {
 4965:                                 $datatable .= '<table><tr><td>';
 4966:                             }
 4967:                             $datatable .= '<span class="LC_nobreak">'
 4968:                                           .'<label><input type="radio" name="'
 4969:                                           .$parent.'" value="1" checked="checked" />'
 4970:                                           .&mt('Display').'</label>';
 4971:                             if ($parent eq 'instcode') {
 4972:                                 $datatable .= '&nbsp;';
 4973:                             } else {
 4974:                                 $datatable .= '</span></td></tr><tr><td>'
 4975:                                               .'<span class="LC_nobreak">';
 4976:                             }
 4977:                             $datatable .= '<label><input type="radio" name="'
 4978:                                           .$parent.'" value="0" />'
 4979:                                           .&mt('Do not display').'</label></span>';
 4980:                             if ($parent eq 'communities') {
 4981:                                 $datatable .= '</td></tr></table>';
 4982:                             }
 4983:                             $datatable .= '</td>';
 4984:                         } else {
 4985:                             $datatable .= $parent
 4986:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 4987:                                           .'<input type="checkbox" name="deletecategory" '
 4988:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4989:                         }
 4990:                         my $depth = 1;
 4991:                         push(@path,$parent);
 4992:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4993:                         pop(@path);
 4994:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4995:                         $itemcount ++;
 4996:                     }
 4997:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4998:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4999:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 5000:                     for (my $k=0; $k<=$maxnum; $k++) {
 5001:                         my $vpos = $k+1;
 5002:                         my $selstr;
 5003:                         if ($k == $numtop) {
 5004:                             $selstr = ' selected="selected" ';
 5005:                         }
 5006:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5007:                     }
 5008:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 5009:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 5010:                                   .'</tr>'."\n";
 5011:                     $itemcount ++;
 5012:                     foreach my $default ('instcode','communities') {
 5013:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 5014:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5015:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 5016:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 5017:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5018:                             for (my $k=0; $k<=$maxnum; $k++) {
 5019:                                 my $vpos = $k+1;
 5020:                                 my $selstr;
 5021:                                 if ($k == $maxnum) {
 5022:                                     $selstr = ' selected="selected" ';
 5023:                                 }
 5024:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5025:                             }
 5026:                             $datatable .= '</select></span></td>'.
 5027:                                           '<td><span class="LC_nobreak">'.
 5028:                                           $default_names{$default}.'</span>';
 5029:                             if ($default eq 'instcode') {
 5030:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5031:                                               .&mt('with institutional codes').')</span>';
 5032:                             }
 5033:                             $datatable .= '</td>'
 5034:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5035:                                           .&mt('Display').'</label>&nbsp;'
 5036:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5037:                                           .&mt('Do not display').'</label></span></td></tr>';
 5038:                         }
 5039:                     }
 5040:                 }
 5041:             } else {
 5042:                 $datatable .= &initialize_categories($itemcount);
 5043:             }
 5044:         } else {
 5045:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 5046:                           .&initialize_categories($itemcount);
 5047:         }
 5048:         $$rowtotal += $itemcount;
 5049:     }
 5050:     return $datatable;
 5051: }
 5052: 
 5053: sub print_serverstatuses {
 5054:     my ($dom,$settings,$rowtotal) = @_;
 5055:     my $datatable;
 5056:     my @pages = &serverstatus_pages();
 5057:     my (%namedaccess,%machineaccess);
 5058:     foreach my $type (@pages) {
 5059:         $namedaccess{$type} = '';
 5060:         $machineaccess{$type}= '';
 5061:     }
 5062:     if (ref($settings) eq 'HASH') {
 5063:         foreach my $type (@pages) {
 5064:             if (exists($settings->{$type})) {
 5065:                 if (ref($settings->{$type}) eq 'HASH') {
 5066:                     foreach my $key (keys(%{$settings->{$type}})) {
 5067:                         if ($key eq 'namedusers') {
 5068:                             $namedaccess{$type} = $settings->{$type}->{$key};
 5069:                         } elsif ($key eq 'machines') {
 5070:                             $machineaccess{$type} = $settings->{$type}->{$key};
 5071:                         }
 5072:                     }
 5073:                 }
 5074:             }
 5075:         }
 5076:     }
 5077:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 5078:     my $rownum = 0;
 5079:     my $css_class;
 5080:     foreach my $type (@pages) {
 5081:         $rownum ++;
 5082:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 5083:         $datatable .= '<tr'.$css_class.'>'.
 5084:                       '<td><span class="LC_nobreak">'.
 5085:                       $titles->{$type}.'</span></td>'.
 5086:                       '<td class="LC_left_item">'.
 5087:                       '<input type="text" name="'.$type.'_namedusers" '.
 5088:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 5089:                       '<td class="LC_right_item">'.
 5090:                       '<span class="LC_nobreak">'.
 5091:                       '<input type="text" name="'.$type.'_machines" '.
 5092:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 5093:                       '</td></tr>'."\n";
 5094:     }
 5095:     $$rowtotal += $rownum;
 5096:     return $datatable;
 5097: }
 5098: 
 5099: sub serverstatus_pages {
 5100:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 5101:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 5102:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 5103:             'uniquecodes','diskusage');
 5104: }
 5105: 
 5106: sub defaults_javascript {
 5107:     my ($settings) = @_;
 5108:     my ($output,$jstext); 
 5109:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5110:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 5111:         if ($maxnum eq '') {
 5112:             $maxnum = 0;
 5113:         }
 5114:         $maxnum ++;
 5115:         $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 5116:         return <<"ENDSCRIPT";
 5117: <script type="text/javascript">
 5118: // <![CDATA[
 5119: function reorderTypes(form,caller) {
 5120:     var changedVal;
 5121: $jstext 
 5122:     var newpos = 'addinststatus_pos';
 5123:     var current = new Array;
 5124:     var maxh = $maxnum;
 5125:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5126:     var oldVal;
 5127:     if (caller == newpos) {
 5128:         changedVal = newitemVal;
 5129:     } else {
 5130:         var curritem = 'inststatus_pos_'+caller;
 5131:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 5132:         current[newitemVal] = newpos;
 5133:     }
 5134:     for (var i=0; i<inststatuses.length; i++) {
 5135:         if (inststatuses[i] != caller) {
 5136:             var elementName = 'inststatus_pos_'+inststatuses[i];
 5137:             if (form.elements[elementName]) {
 5138:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5139:                 current[currVal] = elementName;
 5140:             }
 5141:         }
 5142:     }
 5143:     for (var j=0; j<maxh; j++) {
 5144:         if (current[j] == undefined) {
 5145:             oldVal = j;
 5146:         }
 5147:     }
 5148:     if (oldVal < changedVal) {
 5149:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5150:            var elementName = current[k];
 5151:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5152:         }
 5153:     } else {
 5154:         for (var k=changedVal; k<oldVal; k++) {
 5155:             var elementName = current[k];
 5156:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5157:         }
 5158:     }
 5159:     return;
 5160: }
 5161: 
 5162: // ]]>
 5163: </script>
 5164: 
 5165: ENDSCRIPT
 5166:     }
 5167: }
 5168: 
 5169: sub coursecategories_javascript {
 5170:     my ($settings) = @_;
 5171:     my ($output,$jstext,$cathash);
 5172:     if (ref($settings) eq 'HASH') {
 5173:         $cathash = $settings->{'cats'};
 5174:     }
 5175:     if (ref($cathash) eq 'HASH') {
 5176:         my (@cats,@jsarray,%idx);
 5177:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 5178:         if (@jsarray > 0) {
 5179:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 5180:             for (my $i=0; $i<@jsarray; $i++) {
 5181:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 5182:                     my $catstr = join('","',@{$jsarray[$i]});
 5183:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 5184:                 }
 5185:             }
 5186:         }
 5187:     } else {
 5188:         $jstext  = '    var categories = Array(1);'."\n".
 5189:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 5190:     }
 5191:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 5192:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 5193:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category.'); 
 5194:     $output = <<"ENDSCRIPT";
 5195: <script type="text/javascript">
 5196: // <![CDATA[
 5197: function reorderCats(form,parent,item,idx) {
 5198:     var changedVal;
 5199: $jstext
 5200:     var newpos = 'addcategory_pos';
 5201:     if (parent == '') {
 5202:         var has_instcode = 0;
 5203:         var maxtop = categories[idx].length;
 5204:         for (var j=0; j<maxtop; j++) {
 5205:             if (categories[idx][j] == 'instcode::0') {
 5206:                 has_instcode == 1;
 5207:             }
 5208:         }
 5209:         if (has_instcode == 0) {
 5210:             categories[idx][maxtop] = 'instcode_pos';
 5211:         }
 5212:     } else {
 5213:         newpos += '_'+parent;
 5214:     }
 5215:     var maxh = 1 + categories[idx].length;
 5216:     var current = new Array;
 5217:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5218:     if (item == newpos) {
 5219:         changedVal = newitemVal;
 5220:     } else {
 5221:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5222:         current[newitemVal] = newpos;
 5223:     }
 5224:     for (var i=0; i<categories[idx].length; i++) {
 5225:         var elementName = categories[idx][i];
 5226:         if (elementName != item) {
 5227:             if (form.elements[elementName]) {
 5228:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5229:                 current[currVal] = elementName;
 5230:             }
 5231:         }
 5232:     }
 5233:     var oldVal;
 5234:     for (var j=0; j<maxh; j++) {
 5235:         if (current[j] == undefined) {
 5236:             oldVal = j;
 5237:         }
 5238:     }
 5239:     if (oldVal < changedVal) {
 5240:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5241:            var elementName = current[k];
 5242:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5243:         }
 5244:     } else {
 5245:         for (var k=changedVal; k<oldVal; k++) {
 5246:             var elementName = current[k];
 5247:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5248:         }
 5249:     }
 5250:     return;
 5251: }
 5252: 
 5253: function categoryCheck(form) {
 5254:     if (form.elements['addcategory_name'].value == 'instcode') {
 5255:         alert('$instcode_reserved\\n$choose_again');
 5256:         return false;
 5257:     }
 5258:     if (form.elements['addcategory_name'].value == 'communities') {
 5259:         alert('$communities_reserved\\n$choose_again');
 5260:         return false;
 5261:     }
 5262:     return true;
 5263: }
 5264: 
 5265: // ]]>
 5266: </script>
 5267: 
 5268: ENDSCRIPT
 5269:     return $output;
 5270: }
 5271: 
 5272: sub initialize_categories {
 5273:     my ($itemcount) = @_;
 5274:     my ($datatable,$css_class,$chgstr);
 5275:     my %default_names = (
 5276:                       instcode    => 'Official courses (with institutional codes)',
 5277:                       communities => 'Communities',
 5278:                         );
 5279:     my $select0 = ' selected="selected"';
 5280:     my $select1 = '';
 5281:     foreach my $default ('instcode','communities') {
 5282:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5283:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 5284:         if ($default eq 'communities') {
 5285:             $select1 = $select0;
 5286:             $select0 = '';
 5287:         }
 5288:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5289:                      .'<select name="'.$default.'_pos">'
 5290:                      .'<option value="0"'.$select0.'>1</option>'
 5291:                      .'<option value="1"'.$select1.'>2</option>'
 5292:                      .'<option value="2">3</option></select>&nbsp;'
 5293:                      .$default_names{$default}
 5294:                      .'</span></td><td><span class="LC_nobreak">'
 5295:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5296:                      .&mt('Display').'</label>&nbsp;<label>'
 5297:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5298:                  .'</label></span></td></tr>';
 5299:         $itemcount ++;
 5300:     }
 5301:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5302:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5303:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5304:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5305:                   .'<option value="0">1</option>'
 5306:                   .'<option value="1">2</option>'
 5307:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5308:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5309:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5310:     return $datatable;
 5311: }
 5312: 
 5313: sub build_category_rows {
 5314:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5315:     my ($text,$name,$item,$chgstr);
 5316:     if (ref($cats) eq 'ARRAY') {
 5317:         my $maxdepth = scalar(@{$cats});
 5318:         if (ref($cats->[$depth]) eq 'HASH') {
 5319:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5320:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5321:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5322:                 $text .= '<td><table class="LC_data_table">';
 5323:                 my ($idxnum,$parent_name,$parent_item);
 5324:                 my $higher = $depth - 1;
 5325:                 if ($higher == 0) {
 5326:                     $parent_name = &escape($parent).'::'.$higher;
 5327:                 } else {
 5328:                     if (ref($path) eq 'ARRAY') {
 5329:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5330:                     }
 5331:                 }
 5332:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5333:                 for (my $j=0; $j<=$numchildren; $j++) {
 5334:                     if ($j < $numchildren) {
 5335:                         $name = $cats->[$depth]{$parent}[$j];
 5336:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5337:                         $idxnum = $idx->{$item};
 5338:                     } else {
 5339:                         $name = $parent_name;
 5340:                         $item = $parent_item;
 5341:                     }
 5342:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5343:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5344:                     for (my $i=0; $i<=$numchildren; $i++) {
 5345:                         my $vpos = $i+1;
 5346:                         my $selstr;
 5347:                         if ($j == $i) {
 5348:                             $selstr = ' selected="selected" ';
 5349:                         }
 5350:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5351:                     }
 5352:                     $text .= '</select>&nbsp;';
 5353:                     if ($j < $numchildren) {
 5354:                         my $deeper = $depth+1;
 5355:                         $text .= $name.'&nbsp;'
 5356:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5357:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5358:                         if(ref($path) eq 'ARRAY') {
 5359:                             push(@{$path},$name);
 5360:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5361:                             pop(@{$path});
 5362:                         }
 5363:                     } else {
 5364:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5365:                         if ($j == $numchildren) {
 5366:                             $text .= $name;
 5367:                         } else {
 5368:                             $text .= $item;
 5369:                         }
 5370:                         $text .= '" value="" />';
 5371:                     }
 5372:                     $text .= '</td></tr>';
 5373:                 }
 5374:                 $text .= '</table></td>';
 5375:             } else {
 5376:                 my $higher = $depth-1;
 5377:                 if ($higher == 0) {
 5378:                     $name = &escape($parent).'::'.$higher;
 5379:                 } else {
 5380:                     if (ref($path) eq 'ARRAY') {
 5381:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5382:                     }
 5383:                 }
 5384:                 my $colspan;
 5385:                 if ($parent ne 'instcode') {
 5386:                     $colspan = $maxdepth - $depth - 1;
 5387:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5388:                 }
 5389:             }
 5390:         }
 5391:     }
 5392:     return $text;
 5393: }
 5394: 
 5395: sub modifiable_userdata_row {
 5396:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5397:     my ($role,$rolename,$statustype);
 5398:     $role = $item;
 5399:     if ($context eq 'cancreate') {
 5400:         if ($item =~ /^emailusername_(.+)$/) {
 5401:             $statustype = $1;
 5402:             $role = 'emailusername';
 5403:             if (ref($usertypes) eq 'HASH') {
 5404:                 if ($usertypes->{$statustype}) {
 5405:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5406:                 } else {
 5407:                     $rolename = &mt('Data provided by user');
 5408:                 }
 5409:             }
 5410:         }
 5411:     } elsif ($context eq 'selfcreate') {
 5412:         if (ref($usertypes) eq 'HASH') {
 5413:             $rolename = $usertypes->{$role};
 5414:         } else {
 5415:             $rolename = $role;
 5416:         }
 5417:     } else {
 5418:         if ($role eq 'cr') {
 5419:             $rolename = &mt('Custom role');
 5420:         } else {
 5421:             $rolename = &Apache::lonnet::plaintext($role);
 5422:         }
 5423:     }
 5424:     my (@fields,%fieldtitles);
 5425:     if (ref($fieldsref) eq 'ARRAY') {
 5426:         @fields = @{$fieldsref};
 5427:     } else {
 5428:         @fields = ('lastname','firstname','middlename','generation',
 5429:                    'permanentemail','id');
 5430:     }
 5431:     if ((ref($titlesref) eq 'HASH')) {
 5432:         %fieldtitles = %{$titlesref};
 5433:     } else {
 5434:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5435:     }
 5436:     my $output;
 5437:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5438:     $output = '<tr '.$css_class.'>'.
 5439:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5440:               '<td class="LC_left_item" colspan="2"><table>';
 5441:     my $rem;
 5442:     my %checks;
 5443:     if (ref($settings) eq 'HASH') {
 5444:         if (ref($settings->{$context}) eq 'HASH') {
 5445:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5446:                 my $hashref = $settings->{$context}->{$role};
 5447:                 if ($role eq 'emailusername') {
 5448:                     if ($statustype) {
 5449:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5450:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5451:                             if (ref($hashref) eq 'HASH') { 
 5452:                                 foreach my $field (@fields) {
 5453:                                     if ($hashref->{$field}) {
 5454:                                         $checks{$field} = $hashref->{$field};
 5455:                                     }
 5456:                                 }
 5457:                             }
 5458:                         }
 5459:                     }
 5460:                 } else {
 5461:                     if (ref($hashref) eq 'HASH') {
 5462:                         foreach my $field (@fields) {
 5463:                             if ($hashref->{$field}) {
 5464:                                 $checks{$field} = ' checked="checked" ';
 5465:                             }
 5466:                         }
 5467:                     }
 5468:                 }
 5469:             }
 5470:         }
 5471:     }
 5472:      
 5473:     for (my $i=0; $i<@fields; $i++) {
 5474:         my $rem = $i%($numinrow);
 5475:         if ($rem == 0) {
 5476:             if ($i > 0) {
 5477:                 $output .= '</tr>';
 5478:             }
 5479:             $output .= '<tr>';
 5480:         }
 5481:         my $check = ' ';
 5482:         unless ($role eq 'emailusername') {
 5483:             if (exists($checks{$fields[$i]})) {
 5484:                 $check = $checks{$fields[$i]}
 5485:             } else {
 5486:                 if ($role eq 'st') {
 5487:                     if (ref($settings) ne 'HASH') {
 5488:                         $check = ' checked="checked" '; 
 5489:                     }
 5490:                 }
 5491:             }
 5492:         }
 5493:         $output .= '<td class="LC_left_item">'.
 5494:                    '<span class="LC_nobreak">';
 5495:         if ($role eq 'emailusername') {
 5496:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5497:                 $checks{$fields[$i]} = 'omit';
 5498:             }
 5499:             foreach my $option ('required','optional','omit') {
 5500:                 my $checked='';
 5501:                 if ($checks{$fields[$i]} eq $option) {
 5502:                     $checked='checked="checked" ';
 5503:                 }
 5504:                 $output .= '<label>'.
 5505:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5506:                            &mt($option).'</label>'.('&nbsp;' x2);
 5507:             }
 5508:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5509:         } else {
 5510:             $output .= '<label>'.
 5511:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5512:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5513:                        '</label>';
 5514:         }
 5515:         $output .= '</span></td>';
 5516:         $rem = @fields%($numinrow);
 5517:     }
 5518:     my $colsleft = $numinrow - $rem;
 5519:     if ($colsleft > 1 ) {
 5520:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5521:                    '&nbsp;</td>';
 5522:     } elsif ($colsleft == 1) {
 5523:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5524:     }
 5525:     $output .= '</tr></table></td></tr>';
 5526:     return $output;
 5527: }
 5528: 
 5529: sub insttypes_row {
 5530:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5531:     my %lt = &Apache::lonlocal::texthash (
 5532:                       cansearch => 'Users allowed to search',
 5533:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5534:                       lockablenames => 'User preference to lock name',
 5535:              );
 5536:     my $showdom;
 5537:     if ($context eq 'cansearch') {
 5538:         $showdom = ' ('.$dom.')';
 5539:     }
 5540:     my $class = 'LC_left_item';
 5541:     if ($context eq 'statustocreate') {
 5542:         $class = 'LC_right_item';
 5543:     }
 5544:     my $css_class = ' class="LC_odd_row"';
 5545:     if ($rownum ne '') { 
 5546:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5547:     }
 5548:     my $output = '<tr'.$css_class.'>'.
 5549:                  '<td>'.$lt{$context}.$showdom.
 5550:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5551:     my $rem;
 5552:     if (ref($types) eq 'ARRAY') {
 5553:         for (my $i=0; $i<@{$types}; $i++) {
 5554:             if (defined($usertypes->{$types->[$i]})) {
 5555:                 my $rem = $i%($numinrow);
 5556:                 if ($rem == 0) {
 5557:                     if ($i > 0) {
 5558:                         $output .= '</tr>';
 5559:                     }
 5560:                     $output .= '<tr>';
 5561:                 }
 5562:                 my $check = ' ';
 5563:                 if (ref($settings) eq 'HASH') {
 5564:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5565:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5566:                             $check = ' checked="checked" ';
 5567:                         }
 5568:                     } elsif ($context eq 'statustocreate') {
 5569:                         $check = ' checked="checked" ';
 5570:                     }
 5571:                 }
 5572:                 $output .= '<td class="LC_left_item">'.
 5573:                            '<span class="LC_nobreak"><label>'.
 5574:                            '<input type="checkbox" name="'.$context.'" '.
 5575:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5576:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5577:             }
 5578:         }
 5579:         $rem = @{$types}%($numinrow);
 5580:     }
 5581:     my $colsleft = $numinrow - $rem;
 5582:     if (($rem == 0) && (@{$types} > 0)) {
 5583:         $output .= '<tr>';
 5584:     }
 5585:     if ($colsleft > 1) {
 5586:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5587:     } else {
 5588:         $output .= '<td class="LC_left_item">';
 5589:     }
 5590:     my $defcheck = ' ';
 5591:     if (ref($settings) eq 'HASH') {  
 5592:         if (ref($settings->{$context}) eq 'ARRAY') {
 5593:             if (grep(/^default$/,@{$settings->{$context}})) {
 5594:                 $defcheck = ' checked="checked" ';
 5595:             }
 5596:         } elsif ($context eq 'statustocreate') {
 5597:             $defcheck = ' checked="checked" ';
 5598:         }
 5599:     }
 5600:     $output .= '<span class="LC_nobreak"><label>'.
 5601:                '<input type="checkbox" name="'.$context.'" '.
 5602:                'value="default"'.$defcheck.'/>'.
 5603:                $othertitle.'</label></span></td>'.
 5604:                '</tr></table></td></tr>';
 5605:     return $output;
 5606: }
 5607: 
 5608: sub sorted_searchtitles {
 5609:     my %searchtitles = &Apache::lonlocal::texthash(
 5610:                          'uname' => 'username',
 5611:                          'lastname' => 'last name',
 5612:                          'lastfirst' => 'last name, first name',
 5613:                      );
 5614:     my @titleorder = ('uname','lastname','lastfirst');
 5615:     return (\%searchtitles,\@titleorder);
 5616: }
 5617: 
 5618: sub sorted_searchtypes {
 5619:     my %srchtypes_desc = (
 5620:                            exact    => 'is exact match',
 5621:                            contains => 'contains ..',
 5622:                            begins   => 'begins with ..',
 5623:                          );
 5624:     my @srchtypeorder = ('exact','begins','contains');
 5625:     return (\%srchtypes_desc,\@srchtypeorder);
 5626: }
 5627: 
 5628: sub usertype_update_row {
 5629:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5630:     my $datatable;
 5631:     my $numinrow = 4;
 5632:     foreach my $type (@{$types}) {
 5633:         if (defined($usertypes->{$type})) {
 5634:             $$rownums ++;
 5635:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5636:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5637:                           '</td><td class="LC_left_item"><table>';
 5638:             for (my $i=0; $i<@{$fields}; $i++) {
 5639:                 my $rem = $i%($numinrow);
 5640:                 if ($rem == 0) {
 5641:                     if ($i > 0) {
 5642:                         $datatable .= '</tr>';
 5643:                     }
 5644:                     $datatable .= '<tr>';
 5645:                 }
 5646:                 my $check = ' ';
 5647:                 if (ref($settings) eq 'HASH') {
 5648:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5649:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5650:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5651:                                 $check = ' checked="checked" ';
 5652:                             }
 5653:                         }
 5654:                     }
 5655:                 }
 5656: 
 5657:                 if ($i == @{$fields}-1) {
 5658:                     my $colsleft = $numinrow - $rem;
 5659:                     if ($colsleft > 1) {
 5660:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5661:                     } else {
 5662:                         $datatable .= '<td>';
 5663:                     }
 5664:                 } else {
 5665:                     $datatable .= '<td>';
 5666:                 }
 5667:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5668:                               '<input type="checkbox" name="updateable_'.$type.
 5669:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5670:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5671:             }
 5672:             $datatable .= '</tr></table></td></tr>';
 5673:         }
 5674:     }
 5675:     return $datatable;
 5676: }
 5677: 
 5678: sub modify_login {
 5679:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5680:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5681:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5682:     %title = ( coursecatalog => 'Display course catalog',
 5683:                adminmail => 'Display administrator E-mail address',
 5684:                helpdesk  => 'Display "Contact Helpdesk" link',
 5685:                newuser => 'Link for visitors to create a user account',
 5686:                loginheader => 'Log-in box header');
 5687:     @offon = ('off','on');
 5688:     if (ref($domconfig{login}) eq 'HASH') {
 5689:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5690:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5691:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5692:             }
 5693:         }
 5694:     }
 5695:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5696:                                            \%domconfig,\%loginhash);
 5697:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5698:     foreach my $item (@toggles) {
 5699:         $loginhash{login}{$item} = $env{'form.'.$item};
 5700:     }
 5701:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5702:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5703:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5704:                                          \%loginhash);
 5705:     }
 5706: 
 5707:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5708:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5709:     if (keys(%servers) > 1) {
 5710:         foreach my $lonhost (keys(%servers)) {
 5711:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5712:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5713:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5714:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5715:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5716:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5717:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5718:                         $changes{'loginvia'}{$lonhost} = 1;
 5719:                     } else {
 5720:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5721:                         $changes{'loginvia'}{$lonhost} = 1;
 5722:                     }
 5723:                 } else {
 5724:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5725:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5726:                         $changes{'loginvia'}{$lonhost} = 1;
 5727:                     }
 5728:                 }
 5729:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5730:                     foreach my $item (@loginvia_attribs) {
 5731:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5732:                     }
 5733:                 } else {
 5734:                     foreach my $item (@loginvia_attribs) {
 5735:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5736:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5737:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5738:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5739:                                 $new = '/';
 5740:                             }
 5741:                         }
 5742:                         if (($item eq 'custompath') && 
 5743:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5744:                             $new = '';
 5745:                         }
 5746:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5747:                             $changes{'loginvia'}{$lonhost} = 1;
 5748:                         }
 5749:                         if ($item eq 'exempt') {
 5750:                             $new =~ s/^\s+//;
 5751:                             $new =~ s/\s+$//;
 5752:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5753:                             my @okips;
 5754:                             foreach my $ip (@poss_ips) {
 5755:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5756:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5757:                                         push(@okips,$ip); 
 5758:                                     }
 5759:                                 }
 5760:                             }
 5761:                             if (@okips > 0) {
 5762:                                 $new = join(',',@okips); 
 5763:                             } else {
 5764:                                 $new = ''; 
 5765:                             }
 5766:                         }
 5767:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5768:                     }
 5769:                 }
 5770:             } else {
 5771:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5772:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5773:                     $changes{'loginvia'}{$lonhost} = 1;
 5774:                     foreach my $item (@loginvia_attribs) {
 5775:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5776:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5777:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5778:                                 $new = '/';
 5779:                             }
 5780:                         }
 5781:                         if (($item eq 'custompath') && 
 5782:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5783:                             $new = '';
 5784:                         }
 5785:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5786:                     }
 5787:                 }
 5788:             }
 5789:         }
 5790:     }
 5791: 
 5792:     my $servadm = $r->dir_config('lonAdmEMail');
 5793:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5794:     if (ref($domconfig{'login'}) eq 'HASH') {
 5795:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5796:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5797:                 if ($lang eq 'nolang') {
 5798:                     push(@currlangs,$lang);
 5799:                 } elsif (defined($langchoices{$lang})) {
 5800:                     push(@currlangs,$lang);
 5801:                 } else {
 5802:                     next;
 5803:                 }
 5804:             }
 5805:         }
 5806:     }
 5807:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5808:     if (@currlangs > 0) {
 5809:         foreach my $lang (@currlangs) {
 5810:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5811:                 $changes{'helpurl'}{$lang} = 1;
 5812:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5813:                 $changes{'helpurl'}{$lang} = 1;
 5814:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5815:                 push(@newlangs,$lang);
 5816:             } else {
 5817:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5818:             }
 5819:         }
 5820:     }
 5821:     unless (grep(/^nolang$/,@currlangs)) {
 5822:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5823:             $changes{'helpurl'}{'nolang'} = 1;
 5824:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5825:             push(@newlangs,'nolang');
 5826:         }
 5827:     }
 5828:     if ($env{'form.loginhelpurl_add_lang'}) {
 5829:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5830:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5831:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5832:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5833:         }
 5834:     }
 5835:     if ((@newlangs > 0) || ($addedfile)) {
 5836:         my $error;
 5837:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5838:         if ($configuserok eq 'ok') {
 5839:             if ($switchserver) {
 5840:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5841:             } elsif ($author_ok eq 'ok') {
 5842:                 my @allnew = @newlangs;
 5843:                 if ($addedfile ne '') {
 5844:                     push(@allnew,$addedfile);
 5845:                 }
 5846:                 foreach my $lang (@allnew) {
 5847:                     my $formelem = 'loginhelpurl_'.$lang;
 5848:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5849:                         $formelem = 'loginhelpurl_add_file';
 5850:                     }
 5851:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5852:                                                                "help/$lang",'','',$newfile{$lang});
 5853:                     if ($result eq 'ok') {
 5854:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5855:                         $changes{'helpurl'}{$lang} = 1;
 5856:                     } else {
 5857:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5858:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5859:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5860:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5861: 
 5862:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5863:                         }
 5864:                     }
 5865:                 }
 5866:             } else {
 5867:                 $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);
 5868:             }
 5869:         } else {
 5870:             $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);
 5871:         }
 5872:         if ($error) {
 5873:             &Apache::lonnet::logthis($error);
 5874:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5875:         }
 5876:     }
 5877:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5878: 
 5879:     my $defaulthelpfile = '/adm/loginproblems.html';
 5880:     my $defaulttext = &mt('Default in use');
 5881: 
 5882:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5883:                                              $dom);
 5884:     if ($putresult eq 'ok') {
 5885:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5886:         my %defaultchecked = (
 5887:                     'coursecatalog' => 'on',
 5888:                     'helpdesk'      => 'on',
 5889:                     'adminmail'     => 'off',
 5890:                     'newuser'       => 'off',
 5891:         );
 5892:         if (ref($domconfig{'login'}) eq 'HASH') {
 5893:             foreach my $item (@toggles) {
 5894:                 if ($defaultchecked{$item} eq 'on') { 
 5895:                     if (($domconfig{'login'}{$item} eq '0') &&
 5896:                         ($env{'form.'.$item} eq '1')) {
 5897:                         $changes{$item} = 1;
 5898:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5899:                               $domconfig{'login'}{$item} eq '1') &&
 5900:                              ($env{'form.'.$item} eq '0')) {
 5901:                         $changes{$item} = 1;
 5902:                     }
 5903:                 } elsif ($defaultchecked{$item} eq 'off') {
 5904:                     if (($domconfig{'login'}{$item} eq '1') &&
 5905:                         ($env{'form.'.$item} eq '0')) {
 5906:                         $changes{$item} = 1;
 5907:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5908:                               $domconfig{'login'}{$item} eq '0') &&
 5909:                              ($env{'form.'.$item} eq '1')) {
 5910:                         $changes{$item} = 1;
 5911:                     }
 5912:                 }
 5913:             }
 5914:         }
 5915:         if (keys(%changes) > 0 || $colchgtext) {
 5916:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5917:             if (ref($lastactref) eq 'HASH') {
 5918:                 $lastactref->{'domainconfig'} = 1;
 5919:             }
 5920:             $resulttext = &mt('Changes made:').'<ul>';
 5921:             foreach my $item (sort(keys(%changes))) {
 5922:                 if ($item eq 'loginvia') {
 5923:                     if (ref($changes{$item}) eq 'HASH') {
 5924:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5925:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5926:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5927:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5928:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5929:                                     $protocol = 'http' if ($protocol ne 'https');
 5930:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5931: 
 5932:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5933:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5934:                                     } else {
 5935:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5936:                                     }
 5937:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5938:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5939:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5940:                                     }
 5941:                                     $resulttext .= '</li>';
 5942:                                 } else {
 5943:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5944:                                 }
 5945:                             } else {
 5946:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5947:                             }
 5948:                         }
 5949:                         $resulttext .= '</ul></li>';
 5950:                     }
 5951:                 } elsif ($item eq 'helpurl') {
 5952:                     if (ref($changes{$item}) eq 'HASH') {
 5953:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5954:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5955:                                 my ($chg,$link);
 5956:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5957:                                 if ($lang eq 'nolang') {
 5958:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5959:                                 } else {
 5960:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5961:                                 }
 5962:                                 $resulttext .= '<li>'.$chg.'</li>';
 5963:                             } else {
 5964:                                 my $chg;
 5965:                                 if ($lang eq 'nolang') {
 5966:                                     $chg = &mt('custom log-in help file for no preferred language');
 5967:                                 } else {
 5968:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 5969:                                 }
 5970:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 5971:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 5972:                                                       '?inhibitmenu=yes',$chg,600,500).
 5973:                                                '</li>';
 5974:                             }
 5975:                         }
 5976:                     }
 5977:                 } elsif ($item eq 'captcha') {
 5978:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5979:                         my $chgtxt;
 5980:                         if ($loginhash{'login'}{$item} eq 'notused') {
 5981:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 5982:                         } else {
 5983:                             my %captchas = &captcha_phrases();
 5984:                             if ($captchas{$loginhash{'login'}{$item}}) {
 5985:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 5986:                             } else {
 5987:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 5988:                             }
 5989:                         }
 5990:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5991:                     }
 5992:                 } elsif ($item eq 'recaptchakeys') {
 5993:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5994:                         my ($privkey,$pubkey);
 5995:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 5996:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 5997:                             $privkey = $loginhash{'login'}{$item}{'private'};
 5998:                         }
 5999:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 6000:                         if (!$pubkey) {
 6001:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 6002:                         } else {
 6003:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6004:                         }
 6005:                         if (!$privkey) {
 6006:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 6007:                         } else {
 6008:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6009:                         }
 6010:                         $chgtxt .= '</ul>';
 6011:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6012:                     }
 6013:                 } else {
 6014:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 6015:                 }
 6016:             }
 6017:             $resulttext .= $colchgtext.'</ul>';
 6018:         } else {
 6019:             $resulttext = &mt('No changes made to log-in page settings');
 6020:         }
 6021:     } else {
 6022:         $resulttext = '<span class="LC_error">'.
 6023: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6024:     }
 6025:     if ($errors) {
 6026:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 6027:                        $errors.'</ul>';
 6028:     }
 6029:     return $resulttext;
 6030: }
 6031: 
 6032: sub color_font_choices {
 6033:     my %choices =
 6034:         &Apache::lonlocal::texthash (
 6035:             img => "Header",
 6036:             bgs => "Background colors",
 6037:             links => "Link colors",
 6038:             images => "Images",
 6039:             font => "Font color",
 6040:             fontmenu => "Font menu",
 6041:             pgbg => "Page",
 6042:             tabbg => "Header",
 6043:             sidebg => "Border",
 6044:             link => "Link",
 6045:             alink => "Active link",
 6046:             vlink => "Visited link",
 6047:         );
 6048:     return %choices;
 6049: }
 6050: 
 6051: sub modify_rolecolors {
 6052:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 6053:     my ($resulttext,%rolehash);
 6054:     $rolehash{'rolecolors'} = {};
 6055:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 6056:         if ($domconfig{'rolecolors'} eq '') {
 6057:             $domconfig{'rolecolors'} = {};
 6058:         }
 6059:     }
 6060:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 6061:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 6062:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 6063:                                              $dom);
 6064:     if ($putresult eq 'ok') {
 6065:         if (keys(%changes) > 0) {
 6066:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6067:             if (ref($lastactref) eq 'HASH') {
 6068:                 $lastactref->{'domainconfig'} = 1;
 6069:             }
 6070:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 6071:                                              $rolehash{'rolecolors'});
 6072:         } else {
 6073:             $resulttext = &mt('No changes made to default color schemes');
 6074:         }
 6075:     } else {
 6076:         $resulttext = '<span class="LC_error">'.
 6077: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6078:     }
 6079:     if ($errors) {
 6080:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6081:                        $errors.'</ul>';
 6082:     }
 6083:     return $resulttext;
 6084: }
 6085: 
 6086: sub modify_colors {
 6087:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 6088:     my (%changes,%choices);
 6089:     my @bgs;
 6090:     my @links = ('link','alink','vlink');
 6091:     my @logintext;
 6092:     my @images;
 6093:     my $servadm = $r->dir_config('lonAdmEMail');
 6094:     my $errors;
 6095:     my %defaults;
 6096:     foreach my $role (@{$roles}) {
 6097:         if ($role eq 'login') {
 6098:             %choices = &login_choices();
 6099:             @logintext = ('textcol','bgcol');
 6100:         } else {
 6101:             %choices = &color_font_choices();
 6102:         }
 6103:         if ($role eq 'login') {
 6104:             @images = ('img','logo','domlogo','login');
 6105:             @bgs = ('pgbg','mainbg','sidebg');
 6106:         } else {
 6107:             @images = ('img');
 6108:             @bgs = ('pgbg','tabbg','sidebg');
 6109:         }
 6110:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 6111:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 6112:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 6113:         }
 6114:         if ($role eq 'login') {
 6115:             foreach my $item (@logintext) {
 6116:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6117:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6118:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6119:                 }
 6120:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 6121:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6122:                 }
 6123:             }
 6124:         } else {
 6125:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 6126:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 6127:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 6128:             }
 6129:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 6130:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 6131:             }
 6132:         }
 6133:         foreach my $item (@bgs) {
 6134:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6135:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6136:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6137:             }
 6138:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 6139:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6140:             }
 6141:         }
 6142:         foreach my $item (@links) {
 6143:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6144:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6145:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6146:             }
 6147:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 6148:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6149:             }
 6150:         }
 6151:         my ($configuserok,$author_ok,$switchserver) = 
 6152:             &config_check($dom,$confname,$servadm);
 6153:         my ($width,$height) = &thumb_dimensions();
 6154:         if (ref($domconfig->{$role}) ne 'HASH') {
 6155:             $domconfig->{$role} = {};
 6156:         }
 6157:         foreach my $img (@images) {
 6158:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 6159:                 if (defined($env{'form.login_showlogo_'.$img})) {
 6160:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 6161:                 } else { 
 6162:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 6163:                 }
 6164:             } 
 6165: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 6166: 		 && !defined($domconfig->{$role}{$img})
 6167: 		 && !$env{'form.'.$role.'_del_'.$img}
 6168: 		 && $env{'form.'.$role.'_import_'.$img}) {
 6169: 		# import the old configured image from the .tab setting
 6170: 		# if they haven't provided a new one 
 6171: 		$domconfig->{$role}{$img} = 
 6172: 		    $env{'form.'.$role.'_import_'.$img};
 6173: 	    }
 6174:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 6175:                 my $error;
 6176:                 if ($configuserok eq 'ok') {
 6177:                     if ($switchserver) {
 6178:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 6179:                     } else {
 6180:                         if ($author_ok eq 'ok') {
 6181:                             my ($result,$logourl) = 
 6182:                                 &publishlogo($r,'upload',$role.'_'.$img,
 6183:                                            $dom,$confname,$img,$width,$height);
 6184:                             if ($result eq 'ok') {
 6185:                                 $confhash->{$role}{$img} = $logourl;
 6186:                                 $changes{$role}{'images'}{$img} = 1;
 6187:                             } else {
 6188:                                 $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);
 6189:                             }
 6190:                         } else {
 6191:                             $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);
 6192:                         }
 6193:                     }
 6194:                 } else {
 6195:                     $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);
 6196:                 }
 6197:                 if ($error) {
 6198:                     &Apache::lonnet::logthis($error);
 6199:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6200:                 }
 6201:             } elsif ($domconfig->{$role}{$img} ne '') {
 6202:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 6203:                     my $error;
 6204:                     if ($configuserok eq 'ok') {
 6205: # is confname an author?
 6206:                         if ($switchserver eq '') {
 6207:                             if ($author_ok eq 'ok') {
 6208:                                 my ($result,$logourl) = 
 6209:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 6210:                                             $dom,$confname,$img,$width,$height);
 6211:                                 if ($result eq 'ok') {
 6212:                                     $confhash->{$role}{$img} = $logourl;
 6213: 				    $changes{$role}{'images'}{$img} = 1;
 6214:                                 }
 6215:                             }
 6216:                         }
 6217:                     }
 6218:                 }
 6219:             }
 6220:         }
 6221:         if (ref($domconfig) eq 'HASH') {
 6222:             if (ref($domconfig->{$role}) eq 'HASH') {
 6223:                 foreach my $img (@images) {
 6224:                     if ($domconfig->{$role}{$img} ne '') {
 6225:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6226:                             $confhash->{$role}{$img} = '';
 6227:                             $changes{$role}{'images'}{$img} = 1;
 6228:                         } else {
 6229:                             if ($confhash->{$role}{$img} eq '') {
 6230:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 6231:                             }
 6232:                         }
 6233:                     } else {
 6234:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6235:                             $confhash->{$role}{$img} = '';
 6236:                             $changes{$role}{'images'}{$img} = 1;
 6237:                         } 
 6238:                     }
 6239:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 6240:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 6241:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 6242:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 6243:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 6244:                             }
 6245:                         } else {
 6246:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6247:                                 $changes{$role}{'showlogo'}{$img} = 1;
 6248:                             }
 6249:                         }
 6250:                     }
 6251:                 }
 6252:                 if ($domconfig->{$role}{'font'} ne '') {
 6253:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 6254:                         $changes{$role}{'font'} = 1;
 6255:                     }
 6256:                 } else {
 6257:                     if ($confhash->{$role}{'font'}) {
 6258:                         $changes{$role}{'font'} = 1;
 6259:                     }
 6260:                 }
 6261:                 if ($role ne 'login') {
 6262:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 6263:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 6264:                             $changes{$role}{'fontmenu'} = 1;
 6265:                         }
 6266:                     } else {
 6267:                         if ($confhash->{$role}{'fontmenu'}) {
 6268:                             $changes{$role}{'fontmenu'} = 1;
 6269:                         }
 6270:                     }
 6271:                 }
 6272:                 foreach my $item (@bgs) {
 6273:                     if ($domconfig->{$role}{$item} ne '') {
 6274:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6275:                             $changes{$role}{'bgs'}{$item} = 1;
 6276:                         } 
 6277:                     } else {
 6278:                         if ($confhash->{$role}{$item}) {
 6279:                             $changes{$role}{'bgs'}{$item} = 1;
 6280:                         }
 6281:                     }
 6282:                 }
 6283:                 foreach my $item (@links) {
 6284:                     if ($domconfig->{$role}{$item} ne '') {
 6285:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6286:                             $changes{$role}{'links'}{$item} = 1;
 6287:                         }
 6288:                     } else {
 6289:                         if ($confhash->{$role}{$item}) {
 6290:                             $changes{$role}{'links'}{$item} = 1;
 6291:                         }
 6292:                     }
 6293:                 }
 6294:                 foreach my $item (@logintext) {
 6295:                     if ($domconfig->{$role}{$item} ne '') {
 6296:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6297:                             $changes{$role}{'logintext'}{$item} = 1;
 6298:                         }
 6299:                     } else {
 6300:                         if ($confhash->{$role}{$item}) {
 6301:                             $changes{$role}{'logintext'}{$item} = 1;
 6302:                         }
 6303:                     }
 6304:                 }
 6305:             } else {
 6306:                 &default_change_checker($role,\@images,\@links,\@bgs,
 6307:                                         \@logintext,$confhash,\%changes); 
 6308:             }
 6309:         } else {
 6310:             &default_change_checker($role,\@images,\@links,\@bgs,
 6311:                                     \@logintext,$confhash,\%changes); 
 6312:         }
 6313:     }
 6314:     return ($errors,%changes);
 6315: }
 6316: 
 6317: sub config_check {
 6318:     my ($dom,$confname,$servadm) = @_;
 6319:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6320:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6321:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6322:                                                    $confname,$servadm);
 6323:     if ($configuserok eq 'ok') {
 6324:         $switchserver = &check_switchserver($dom,$confname);
 6325:         if ($switchserver eq '') {
 6326:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6327:         }
 6328:     }
 6329:     return ($configuserok,$author_ok,$switchserver);
 6330: }
 6331: 
 6332: sub default_change_checker {
 6333:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6334:     foreach my $item (@{$links}) {
 6335:         if ($confhash->{$role}{$item}) {
 6336:             $changes->{$role}{'links'}{$item} = 1;
 6337:         }
 6338:     }
 6339:     foreach my $item (@{$bgs}) {
 6340:         if ($confhash->{$role}{$item}) {
 6341:             $changes->{$role}{'bgs'}{$item} = 1;
 6342:         }
 6343:     }
 6344:     foreach my $item (@{$logintext}) {
 6345:         if ($confhash->{$role}{$item}) {
 6346:             $changes->{$role}{'logintext'}{$item} = 1;
 6347:         }
 6348:     }
 6349:     foreach my $img (@{$images}) {
 6350:         if ($env{'form.'.$role.'_del_'.$img}) {
 6351:             $confhash->{$role}{$img} = '';
 6352:             $changes->{$role}{'images'}{$img} = 1;
 6353:         }
 6354:         if ($role eq 'login') {
 6355:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6356:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6357:             }
 6358:         }
 6359:     }
 6360:     if ($confhash->{$role}{'font'}) {
 6361:         $changes->{$role}{'font'} = 1;
 6362:     }
 6363: }
 6364: 
 6365: sub display_colorchgs {
 6366:     my ($dom,$changes,$roles,$confhash) = @_;
 6367:     my (%choices,$resulttext);
 6368:     if (!grep(/^login$/,@{$roles})) {
 6369:         $resulttext = &mt('Changes made:').'<br />';
 6370:     }
 6371:     foreach my $role (@{$roles}) {
 6372:         if ($role eq 'login') {
 6373:             %choices = &login_choices();
 6374:         } else {
 6375:             %choices = &color_font_choices();
 6376:         }
 6377:         if (ref($changes->{$role}) eq 'HASH') {
 6378:             if ($role ne 'login') {
 6379:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6380:             }
 6381:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6382:                 if ($role ne 'login') {
 6383:                     $resulttext .= '<ul>';
 6384:                 }
 6385:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6386:                     if ($role ne 'login') {
 6387:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6388:                     }
 6389:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6390:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6391:                             if ($confhash->{$role}{$key}{$item}) {
 6392:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6393:                             } else {
 6394:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6395:                             }
 6396:                         } elsif ($confhash->{$role}{$item} eq '') {
 6397:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6398:                         } else {
 6399:                             my $newitem = $confhash->{$role}{$item};
 6400:                             if ($key eq 'images') {
 6401:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6402:                             }
 6403:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6404:                         }
 6405:                     }
 6406:                     if ($role ne 'login') {
 6407:                         $resulttext .= '</ul></li>';
 6408:                     }
 6409:                 } else {
 6410:                     if ($confhash->{$role}{$key} eq '') {
 6411:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6412:                     } else {
 6413:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6414:                     }
 6415:                 }
 6416:                 if ($role ne 'login') {
 6417:                     $resulttext .= '</ul>';
 6418:                 }
 6419:             }
 6420:         }
 6421:     }
 6422:     return $resulttext;
 6423: }
 6424: 
 6425: sub thumb_dimensions {
 6426:     return ('200','50');
 6427: }
 6428: 
 6429: sub check_dimensions {
 6430:     my ($inputfile) = @_;
 6431:     my ($fullwidth,$fullheight);
 6432:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6433:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6434:             my $imageinfo = <PIPE>;
 6435:             if (!close(PIPE)) {
 6436:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6437:             }
 6438:             chomp($imageinfo);
 6439:             my ($fullsize) = 
 6440:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6441:             if ($fullsize) {
 6442:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6443:             }
 6444:         }
 6445:     }
 6446:     return ($fullwidth,$fullheight);
 6447: }
 6448: 
 6449: sub check_configuser {
 6450:     my ($uhome,$dom,$confname,$servadm) = @_;
 6451:     my ($configuserok,%currroles);
 6452:     if ($uhome eq 'no_host') {
 6453:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6454:         my $configpass = &LONCAPA::Enrollment::create_password();
 6455:         $configuserok = 
 6456:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6457:                              $configpass,'','','','','',undef,$servadm);
 6458:     } else {
 6459:         $configuserok = 'ok';
 6460:         %currroles = 
 6461:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6462:     }
 6463:     return ($configuserok,%currroles);
 6464: }
 6465: 
 6466: sub check_authorstatus {
 6467:     my ($dom,$confname,%currroles) = @_;
 6468:     my $author_ok;
 6469:     if (!$currroles{':'.$dom.':au'}) {
 6470:         my $start = time;
 6471:         my $end = 0;
 6472:         $author_ok = 
 6473:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6474:                                         'au',$end,$start,'','','domconfig');
 6475:     } else {
 6476:         $author_ok = 'ok';
 6477:     }
 6478:     return $author_ok;
 6479: }
 6480: 
 6481: sub publishlogo {
 6482:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6483:     my ($output,$fname,$logourl);
 6484:     if ($action eq 'upload') {
 6485:         $fname=$env{'form.'.$formname.'.filename'};
 6486:         chop($env{'form.'.$formname});
 6487:     } else {
 6488:         ($fname) = ($formname =~ /([^\/]+)$/);
 6489:     }
 6490:     if ($savefileas ne '') {
 6491:         $fname = $savefileas;
 6492:     }
 6493:     $fname=&Apache::lonnet::clean_filename($fname);
 6494: # See if there is anything left
 6495:     unless ($fname) { return ('error: no uploaded file'); }
 6496:     $fname="$subdir/$fname";
 6497:     my $docroot=$r->dir_config('lonDocRoot');
 6498:     my $filepath="$docroot/priv";
 6499:     my $relpath = "$dom/$confname";
 6500:     my ($fnamepath,$file,$fetchthumb);
 6501:     $file=$fname;
 6502:     if ($fname=~m|/|) {
 6503:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6504:     }
 6505:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6506:     my $count;
 6507:     for ($count=5;$count<=$#parts;$count++) {
 6508:         $filepath.="/$parts[$count]";
 6509:         if ((-e $filepath)!=1) {
 6510:             mkdir($filepath,02770);
 6511:         }
 6512:     }
 6513:     # Check for bad extension and disallow upload
 6514:     if ($file=~/\.(\w+)$/ &&
 6515:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6516:         $output = 
 6517:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6518:     } elsif ($file=~/\.(\w+)$/ &&
 6519:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6520:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6521:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6522:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6523:     } elsif (-d "$filepath/$file") {
 6524:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6525:     } else {
 6526:         my $source = $filepath.'/'.$file;
 6527:         my $logfile;
 6528:         if (!open($logfile,">>$source".'.log')) {
 6529:             return (&mt('No write permission to Authoring Space'));
 6530:         }
 6531:         print $logfile
 6532: "\n================= Publish ".localtime()." ================\n".
 6533: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6534: # Save the file
 6535:         if (!open(FH,'>'.$source)) {
 6536:             &Apache::lonnet::logthis('Failed to create '.$source);
 6537:             return (&mt('Failed to create file'));
 6538:         }
 6539:         if ($action eq 'upload') {
 6540:             if (!print FH ($env{'form.'.$formname})) {
 6541:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6542:                 return (&mt('Failed to write file'));
 6543:             }
 6544:         } else {
 6545:             my $original = &Apache::lonnet::filelocation('',$formname);
 6546:             if(!copy($original,$source)) {
 6547:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6548:                 return (&mt('Failed to write file'));
 6549:             }
 6550:         }
 6551:         close(FH);
 6552:         chmod(0660, $source); # Permissions to rw-rw---.
 6553: 
 6554:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6555:         my $copyfile=$targetdir.'/'.$file;
 6556: 
 6557:         my @parts=split(/\//,$targetdir);
 6558:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6559:         for (my $count=5;$count<=$#parts;$count++) {
 6560:             $path.="/$parts[$count]";
 6561:             if (!-e $path) {
 6562:                 print $logfile "\nCreating directory ".$path;
 6563:                 mkdir($path,02770);
 6564:             }
 6565:         }
 6566:         my $versionresult;
 6567:         if (-e $copyfile) {
 6568:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6569:         } else {
 6570:             $versionresult = 'ok';
 6571:         }
 6572:         if ($versionresult eq 'ok') {
 6573:             if (copy($source,$copyfile)) {
 6574:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6575:                 $output = 'ok';
 6576:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6577:                 push(@{$modified_urls},[$copyfile,$source]);
 6578:                 my $metaoutput = 
 6579:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6580:                 unless ($registered_cleanup) {
 6581:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6582:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6583:                     $registered_cleanup=1;
 6584:                 }
 6585:             } else {
 6586:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6587:                 $output = &mt('Failed to copy file to RES space').", $!";
 6588:             }
 6589:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6590:                 my $inputfile = $filepath.'/'.$file;
 6591:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6592:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6593:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6594:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6595:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6596:                         system("convert -sample $thumbsize $inputfile $outfile");
 6597:                         chmod(0660, $filepath.'/tn-'.$file);
 6598:                         if (-e $outfile) {
 6599:                             my $copyfile=$targetdir.'/tn-'.$file;
 6600:                             if (copy($outfile,$copyfile)) {
 6601:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6602:                                 my $thumb_metaoutput = 
 6603:                                     &write_metadata($dom,$confname,$formname,
 6604:                                                     $targetdir,'tn-'.$file,$logfile);
 6605:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6606:                                 unless ($registered_cleanup) {
 6607:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6608:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6609:                                     $registered_cleanup=1;
 6610:                                 }
 6611:                             } else {
 6612:                                 print $logfile "\nUnable to write ".$copyfile.
 6613:                                                ':'.$!."\n";
 6614:                             }
 6615:                         }
 6616:                     }
 6617:                 }
 6618:             }
 6619:         } else {
 6620:             $output = $versionresult;
 6621:         }
 6622:     }
 6623:     return ($output,$logourl);
 6624: }
 6625: 
 6626: sub logo_versioning {
 6627:     my ($targetdir,$file,$logfile) = @_;
 6628:     my $target = $targetdir.'/'.$file;
 6629:     my ($maxversion,$fn,$extn,$output);
 6630:     $maxversion = 0;
 6631:     if ($file =~ /^(.+)\.(\w+)$/) {
 6632:         $fn=$1;
 6633:         $extn=$2;
 6634:     }
 6635:     opendir(DIR,$targetdir);
 6636:     while (my $filename=readdir(DIR)) {
 6637:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6638:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6639:         }
 6640:     }
 6641:     $maxversion++;
 6642:     print $logfile "\nCreating old version ".$maxversion."\n";
 6643:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6644:     if (copy($target,$copyfile)) {
 6645:         print $logfile "Copied old target to ".$copyfile."\n";
 6646:         $copyfile=$copyfile.'.meta';
 6647:         if (copy($target.'.meta',$copyfile)) {
 6648:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6649:             $output = 'ok';
 6650:         } else {
 6651:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6652:             $output = &mt('Failed to copy old meta').", $!, ";
 6653:         }
 6654:     } else {
 6655:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6656:         $output = &mt('Failed to copy old target').", $!, ";
 6657:     }
 6658:     return $output;
 6659: }
 6660: 
 6661: sub write_metadata {
 6662:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6663:     my (%metadatafields,%metadatakeys,$output);
 6664:     $metadatafields{'title'}=$formname;
 6665:     $metadatafields{'creationdate'}=time;
 6666:     $metadatafields{'lastrevisiondate'}=time;
 6667:     $metadatafields{'copyright'}='public';
 6668:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6669:                                          $env{'user.domain'};
 6670:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6671:     $metadatafields{'domain'}=$dom;
 6672:     {
 6673:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6674:         my $mfh;
 6675:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6676:             foreach (sort(keys(%metadatafields))) {
 6677:                 unless ($_=~/\./) {
 6678:                     my $unikey=$_;
 6679:                     $unikey=~/^([A-Za-z]+)/;
 6680:                     my $tag=$1;
 6681:                     $tag=~tr/A-Z/a-z/;
 6682:                     print $mfh "\n\<$tag";
 6683:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6684:                         my $value=$metadatafields{$unikey.'.'.$_};
 6685:                         $value=~s/\"/\'\'/g;
 6686:                         print $mfh ' '.$_.'="'.$value.'"';
 6687:                     }
 6688:                     print $mfh '>'.
 6689:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6690:                             .'</'.$tag.'>';
 6691:                 }
 6692:             }
 6693:             $output = 'ok';
 6694:             print $logfile "\nWrote metadata";
 6695:             close($mfh);
 6696:         } else {
 6697:             print $logfile "\nFailed to open metadata file";
 6698:             $output = &mt('Could not write metadata');
 6699:         }
 6700:     }
 6701:     return $output;
 6702: }
 6703: 
 6704: sub notifysubscribed {
 6705:     foreach my $targetsource (@{$modified_urls}){
 6706:         next unless (ref($targetsource) eq 'ARRAY');
 6707:         my ($target,$source)=@{$targetsource};
 6708:         if ($source ne '') {
 6709:             if (open(my $logfh,'>>'.$source.'.log')) {
 6710:                 print $logfh "\nCleanup phase: Notifications\n";
 6711:                 my @subscribed=&subscribed_hosts($target);
 6712:                 foreach my $subhost (@subscribed) {
 6713:                     print $logfh "\nNotifying host ".$subhost.':';
 6714:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6715:                     print $logfh $reply;
 6716:                 }
 6717:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6718:                 foreach my $subhost (@subscribedmeta) {
 6719:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6720:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6721:                                                         $subhost);
 6722:                     print $logfh $reply;
 6723:                 }
 6724:                 print $logfh "\n============ Done ============\n";
 6725:                 close($logfh);
 6726:             }
 6727:         }
 6728:     }
 6729:     return OK;
 6730: }
 6731: 
 6732: sub subscribed_hosts {
 6733:     my ($target) = @_;
 6734:     my @subscribed;
 6735:     if (open(my $fh,"<$target.subscription")) {
 6736:         while (my $subline=<$fh>) {
 6737:             if ($subline =~ /^($match_lonid):/) {
 6738:                 my $host = $1;
 6739:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6740:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6741:                         push(@subscribed,$host);
 6742:                     }
 6743:                 }
 6744:             }
 6745:         }
 6746:     }
 6747:     return @subscribed;
 6748: }
 6749: 
 6750: sub check_switchserver {
 6751:     my ($dom,$confname) = @_;
 6752:     my ($allowed,$switchserver);
 6753:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6754:     if ($home eq 'no_host') {
 6755:         $home = &Apache::lonnet::domain($dom,'primary');
 6756:     }
 6757:     my @ids=&Apache::lonnet::current_machine_ids();
 6758:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6759:     if (!$allowed) {
 6760: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6761:     }
 6762:     return $switchserver;
 6763: }
 6764: 
 6765: sub modify_quotas {
 6766:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6767:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6768:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6769:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 6770:         $validationfieldsref);
 6771:     if ($action eq 'quotas') {
 6772:         $context = 'tools'; 
 6773:     } else {
 6774:         $context = $action;
 6775:     }
 6776:     if ($context eq 'requestcourses') {
 6777:         @usertools = ('official','unofficial','community','textbook');
 6778:         @options =('norequest','approval','validate','autolimit');
 6779:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6780:         %titles = &courserequest_titles();
 6781:         $toolregexp = join('|',@usertools);
 6782:         %conditions = &courserequest_conditions();
 6783:         $confname = $dom.'-domainconfig';
 6784:         my $servadm = $r->dir_config('lonAdmEMail');
 6785:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6786:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 6787:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6788:     } elsif ($context eq 'requestauthor') {
 6789:         @usertools = ('author');
 6790:         %titles = &authorrequest_titles();
 6791:     } else {
 6792:         @usertools = ('aboutme','blog','webdav','portfolio');
 6793:         %titles = &tool_titles();
 6794:     }
 6795:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6796:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6797:     foreach my $key (keys(%env)) {
 6798:         if ($context eq 'requestcourses') {
 6799:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6800:                 my $item = $1;
 6801:                 my $type = $2;
 6802:                 if ($type =~ /^limit_(.+)/) {
 6803:                     $limithash{$item}{$1} = $env{$key};
 6804:                 } else {
 6805:                     $confhash{$item}{$type} = $env{$key};
 6806:                 }
 6807:             }
 6808:         } elsif ($context eq 'requestauthor') {
 6809:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6810:                 $confhash{$1} = $env{$key};
 6811:             }
 6812:         } else {
 6813:             if ($key =~ /^form\.quota_(.+)$/) {
 6814:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6815:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6816:                 $confhash{'authorquota'}{$1} = $env{$key};
 6817:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6818:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6819:             }
 6820:         }
 6821:     }
 6822:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6823:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6824:         @approvalnotify = sort(@approvalnotify);
 6825:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6826:         my @crstypes = ('official','unofficial','community','textbook');
 6827:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6828:         foreach my $type (@hasuniquecode) {
 6829:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6830:                 $confhash{'uniquecode'}{$type} = 1;
 6831:             }
 6832:         }
 6833:         my ($newbook,@allpos);
 6834:         if ($context eq 'requestcourses') {
 6835:             if ($env{'form.addbook'}) {
 6836:                 if (($env{'form.addbook_cnum'} =~ /^$match_courseid$/) &&
 6837:                     ($env{'form.addbook_cdom'} =~ /^$match_domain$/)) {
 6838:                     if (&Apache::lonnet::homeserver($env{'form.addbook_cnum'},
 6839:                                                     $env{'form.addbook_cdom'}) eq 'no_host') {
 6840:                         $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6841:                                    '</span></li>';
 6842:                     } else {
 6843:                         $newbook = $env{'form.addbook_cdom'}.'_'.$env{'form.addbook_cnum'};
 6844:                         my $position = $env{'form.addbook_pos'};
 6845:                         $position =~ s/\D+//g;
 6846:                         if ($position ne '') {
 6847:                             $allpos[$position] = $newbook;
 6848:                         }
 6849:                     }
 6850:                 } else {
 6851:                     $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6852:                                '</span></li>';
 6853:                 }
 6854:             }
 6855: 
 6856:         }
 6857:         if (ref($domconfig{$action}) eq 'HASH') {
 6858:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6859:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6860:                     $changes{'notify'}{'approval'} = 1;
 6861:                 }
 6862:             } else {
 6863:                 if ($confhash{'notify'}{'approval'}) {
 6864:                     $changes{'notify'}{'approval'} = 1;
 6865:                 }
 6866:             }
 6867:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6868:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6869:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6870:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6871:                             $changes{'uniquecode'} = 1;
 6872:                         }
 6873:                     }
 6874:                     unless ($changes{'uniquecode'}) {
 6875:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6876:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6877:                                 $changes{'uniquecode'} = 1;
 6878:                             }
 6879:                         }
 6880:                     }
 6881:                } else {
 6882:                    $changes{'uniquecode'} = 1;
 6883:                }
 6884:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6885:                 $changes{'uniquecode'} = 1;
 6886:             }
 6887:             if ($context eq 'requestcourses') {
 6888:                 if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6889:                     my %deletions;
 6890:                     my @todelete = &Apache::loncommon::get_env_multiple('form.book_del');
 6891:                     if (@todelete) {
 6892:                         map { $deletions{$_} = 1; } @todelete;
 6893:                     }
 6894:                     my %imgdeletions;
 6895:                     my @todeleteimages = &Apache::loncommon::get_env_multiple('form.book_image_del');
 6896:                     if (@todeleteimages) {
 6897:                         map { $imgdeletions{$_} = 1; } @todeleteimages;
 6898:                     }
 6899:                     my $maxnum = $env{'form.book_maxnum'};
 6900:                     for (my $i=0; $i<=$maxnum; $i++) {
 6901:                         my $key = $env{'form.book_id_'.$i};
 6902:                         if (ref($domconfig{$action}{'textbooks'}{$key}) eq 'HASH') {
 6903:                             if ($deletions{$key}) {
 6904:                                 if ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6905:                                     #FIXME need to obsolete item in RES space
 6906:                                 }
 6907:                                 next;
 6908:                             } else {
 6909:                                 my $newpos = $env{'form.'.$key};
 6910:                                 $newpos =~ s/\D+//g;
 6911:                                 foreach my $item ('subject','title','author') {
 6912:                                     $confhash{'textbooks'}{$key}{$item} = $env{'form.book_'.$item.'_'.$i};
 6913:                                     if ($domconfig{$action}{'textbooks'}{$key}{$item} ne $confhash{'textbooks'}{$key}{$item}) {
 6914:                                         $changes{'textbooks'}{$key} = 1;
 6915:                                     }
 6916:                                 }
 6917:                                 $allpos[$newpos] = $key;
 6918:                             }
 6919:                             if ($imgdeletions{$key}) {
 6920:                                 $changes{'textbooks'}{$key} = 1;
 6921:                                 #FIXME need to obsolete item in RES space
 6922:                             } elsif ($env{'form.book_image_'.$i.'.filename'}) {
 6923:                                 my ($cdom,$cnum) = split(/_/,$key);
 6924:                                 my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,'book_image_'.$i,
 6925:                                                                               $cdom,$cnum,$configuserok,
 6926:                                                                               $switchserver,$author_ok);
 6927:                                 if ($imgurl) {
 6928:                                     $confhash{'textbooks'}{$key}{'image'} = $imgurl;
 6929:                                     $changes{'textbooks'}{$key} = 1; 
 6930:                                 }
 6931:                                 if ($error) {
 6932:                                     &Apache::lonnet::logthis($error);
 6933:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6934:                                 } 
 6935:                             } elsif ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6936:                                 $confhash{'textbooks'}{$key}{'image'} = 
 6937:                                     $domconfig{$action}{'textbooks'}{$key}{'image'};
 6938:                             }
 6939:                         }
 6940:                     }
 6941:                 }
 6942:             }
 6943:         } else {
 6944:             if ($confhash{'notify'}{'approval'}) {
 6945:                 $changes{'notify'}{'approval'} = 1;
 6946:             }
 6947:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 6948:                 $changes{'uniquecode'} = 1;
 6949:             }
 6950:         }
 6951:         if ($context eq 'requestcourses') {
 6952:             if ($newbook) {
 6953:                 $changes{'textbooks'}{$newbook} = 1;
 6954:                 foreach my $item ('subject','title','author') {
 6955:                     $env{'form.addbook_'.$item} =~ s/(`)/'/g;
 6956:                     if ($env{'form.addbook_'.$item}) {
 6957:                         $confhash{'textbooks'}{$newbook}{$item} = $env{'form.addbook_'.$item};
 6958:                     }
 6959:                 }
 6960:                 if ($env{'form.addbook_image.filename'} ne '') {
 6961:                     my ($cdom,$cnum) = split(/_/,$newbook);
 6962:                     my ($imageurl,$error) =
 6963:                         &process_textbook_image($r,$dom,$confname,'addbook_image',$cdom,$cnum,$configuserok,
 6964:                                                 $switchserver,$author_ok);
 6965:                     if ($imageurl) {
 6966:                         $confhash{'textbooks'}{$newbook}{'image'} = $imageurl;
 6967:                     }
 6968:                     if ($error) {
 6969:                         &Apache::lonnet::logthis($error);
 6970:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6971:                     }
 6972:                 }
 6973:             }
 6974:             if (@allpos > 0) {
 6975:                 my $idx = 0;
 6976:                 foreach my $item (@allpos) {
 6977:                     if ($item ne '') {
 6978:                         $confhash{'textbooks'}{$item}{'order'} = $idx;
 6979:                         if (ref($domconfig{$action}) eq 'HASH') {
 6980:                             if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6981:                                 if (ref($domconfig{$action}{'textbooks'}{$item}) eq 'HASH') {
 6982:                                     if ($domconfig{$action}{'textbooks'}{$item}{'order'} ne $idx) {
 6983:                                         $changes{'textbooks'}{$item} = 1;
 6984:                                     }
 6985:                                 }
 6986:                             }
 6987:                         }
 6988:                         $idx ++;
 6989:                     }
 6990:                 }
 6991:             }
 6992:             if (ref($validationitemsref) eq 'ARRAY') {
 6993:                 foreach my $item (@{$validationitemsref}) {
 6994:                     if ($item eq 'fields') {
 6995:                         my @changed;
 6996:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 6997:                         if (@{$confhash{'validation'}{$item}} > 0) {
 6998:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 6999:                         }
 7000:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7001:                             if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 7002:                                 @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 7003:                                                                               $domconfig{'requestcourses'}{'validation'}{$item});
 7004:                             } else {
 7005:                                 @changed = @{$confhash{'validation'}{$item}};
 7006:                             }
 7007:                         } else {
 7008:                             @changed = @{$confhash{'validation'}{$item}};
 7009:                         }
 7010:                         if (@changed) {
 7011:                             if ($confhash{'validation'}{$item}) {
 7012:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 7013:                             } else {
 7014:                                 $changes{'validation'}{$item} = &mt('None');
 7015:                             }
 7016:                         }
 7017:                     } else {
 7018:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 7019:                         if ($item eq 'markup') {
 7020:                             if ($env{'form.requestcourses_validation_'.$item}) {
 7021:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 7022:                             }
 7023:                         }
 7024:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7025:                             if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 7026:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7027:                             }
 7028:                         } else {
 7029:                             if ($confhash{'validation'}{$item} ne '') {
 7030:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7031:                             }
 7032:                         }
 7033:                     }
 7034:                 }
 7035:             }
 7036:             if ($env{'form.validationdc'}) {
 7037:                 my $newval = $env{'form.validationdc'};
 7038:                 my %domcoords = &get_active_dcs($dom);
 7039:                 if (exists($domcoords{$newval})) {
 7040:                     $confhash{'validation'}{'dc'} = $newval;
 7041:                 }
 7042:             }
 7043:             if (ref($confhash{'validation'}) eq 'HASH') {
 7044:                 if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7045:                     if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7046:                         unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7047:                             if ($confhash{'validation'}{'dc'} eq '') {
 7048:                                 $changes{'validation'}{'dc'} = &mt('None');
 7049:                             } else {
 7050:                                 $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7051:                             }
 7052:                         }
 7053:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 7054:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7055:                     }
 7056:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 7057:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7058:                 }
 7059:             } elsif (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7060:                 if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7061:                     $changes{'validation'}{'dc'} = &mt('None');
 7062:                 }
 7063:             }
 7064:         }
 7065:     } else {
 7066:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 7067:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 7068:     }
 7069:     foreach my $item (@usertools) {
 7070:         foreach my $type (@{$types},'default','_LC_adv') {
 7071:             my $unset; 
 7072:             if ($context eq 'requestcourses') {
 7073:                 $unset = '0';
 7074:                 if ($type eq '_LC_adv') {
 7075:                     $unset = '';
 7076:                 }
 7077:                 if ($confhash{$item}{$type} eq 'autolimit') {
 7078:                     $confhash{$item}{$type} .= '=';
 7079:                     unless ($limithash{$item}{$type} =~ /\D/) {
 7080:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 7081:                     }
 7082:                 }
 7083:             } elsif ($context eq 'requestauthor') {
 7084:                 $unset = '0';
 7085:                 if ($type eq '_LC_adv') {
 7086:                     $unset = '';
 7087:                 }
 7088:             } else {
 7089:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 7090:                     $confhash{$item}{$type} = 1;
 7091:                 } else {
 7092:                     $confhash{$item}{$type} = 0;
 7093:                 }
 7094:             }
 7095:             if (ref($domconfig{$action}) eq 'HASH') {
 7096:                 if ($action eq 'requestauthor') {
 7097:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 7098:                         $changes{$type} = 1;
 7099:                     }
 7100:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 7101:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 7102:                         $changes{$item}{$type} = 1;
 7103:                     }
 7104:                 } else {
 7105:                     if ($context eq 'requestcourses') {
 7106:                         if ($confhash{$item}{$type} ne $unset) {
 7107:                             $changes{$item}{$type} = 1;
 7108:                         }
 7109:                     } else {
 7110:                         if (!$confhash{$item}{$type}) {
 7111:                             $changes{$item}{$type} = 1;
 7112:                         }
 7113:                     }
 7114:                 }
 7115:             } else {
 7116:                 if ($context eq 'requestcourses') {
 7117:                     if ($confhash{$item}{$type} ne $unset) {
 7118:                         $changes{$item}{$type} = 1;
 7119:                     }
 7120:                 } elsif ($context eq 'requestauthor') {
 7121:                     if ($confhash{$type} ne $unset) {
 7122:                         $changes{$type} = 1;
 7123:                     }
 7124:                 } else {
 7125:                     if (!$confhash{$item}{$type}) {
 7126:                         $changes{$item}{$type} = 1;
 7127:                     }
 7128:                 }
 7129:             }
 7130:         }
 7131:     }
 7132:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7133:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 7134:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7135:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 7136:                     if (exists($confhash{'defaultquota'}{$key})) {
 7137:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 7138:                             $changes{'defaultquota'}{$key} = 1;
 7139:                         }
 7140:                     } else {
 7141:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 7142:                     }
 7143:                 }
 7144:             } else {
 7145:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 7146:                     if (exists($confhash{'defaultquota'}{$key})) {
 7147:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 7148:                             $changes{'defaultquota'}{$key} = 1;
 7149:                         }
 7150:                     } else {
 7151:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 7152:                     }
 7153:                 }
 7154:             }
 7155:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7156:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 7157:                     if (exists($confhash{'authorquota'}{$key})) {
 7158:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 7159:                             $changes{'authorquota'}{$key} = 1;
 7160:                         }
 7161:                     } else {
 7162:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 7163:                     }
 7164:                 }
 7165:             }
 7166:         }
 7167:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 7168:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 7169:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7170:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7171:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 7172:                             $changes{'defaultquota'}{$key} = 1;
 7173:                         }
 7174:                     } else {
 7175:                         if (!exists($domconfig{'quotas'}{$key})) {
 7176:                             $changes{'defaultquota'}{$key} = 1;
 7177:                         }
 7178:                     }
 7179:                 } else {
 7180:                     $changes{'defaultquota'}{$key} = 1;
 7181:                 }
 7182:             }
 7183:         }
 7184:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 7185:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 7186:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7187:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7188:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 7189:                             $changes{'authorquota'}{$key} = 1;
 7190:                         }
 7191:                     } else {
 7192:                         $changes{'authorquota'}{$key} = 1;
 7193:                     }
 7194:                 } else {
 7195:                     $changes{'authorquota'}{$key} = 1;
 7196:                 }
 7197:             }
 7198:         }
 7199:     }
 7200: 
 7201:     if ($context eq 'requestauthor') {
 7202:         $domdefaults{'requestauthor'} = \%confhash;
 7203:     } else {
 7204:         foreach my $key (keys(%confhash)) {
 7205:             unless (($context eq 'requestcourses') && ($key eq 'textbooks')) {
 7206:                 $domdefaults{$key} = $confhash{$key};
 7207:             }
 7208:         }
 7209:     }
 7210: 
 7211:     my %quotahash = (
 7212:                       $action => { %confhash }
 7213:                     );
 7214:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 7215:                                              $dom);
 7216:     if ($putresult eq 'ok') {
 7217:         if (keys(%changes) > 0) {
 7218:             my $cachetime = 24*60*60;
 7219:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7220:             if (ref($lastactref) eq 'HASH') {
 7221:                 $lastactref->{'domdefaults'} = 1;
 7222:             }
 7223:             $resulttext = &mt('Changes made:').'<ul>';
 7224:             unless (($context eq 'requestcourses') ||
 7225:                     ($context eq 'requestauthor')) {
 7226:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 7227:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 7228:                     foreach my $type (@{$types},'default') {
 7229:                         if (defined($changes{'defaultquota'}{$type})) {
 7230:                             my $typetitle = $usertypes->{$type};
 7231:                             if ($type eq 'default') {
 7232:                                 $typetitle = $othertitle;
 7233:                             }
 7234:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 7235:                         }
 7236:                     }
 7237:                     $resulttext .= '</ul></li>';
 7238:                 }
 7239:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 7240:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 7241:                     foreach my $type (@{$types},'default') {
 7242:                         if (defined($changes{'authorquota'}{$type})) {
 7243:                             my $typetitle = $usertypes->{$type};
 7244:                             if ($type eq 'default') {
 7245:                                 $typetitle = $othertitle;
 7246:                             }
 7247:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 7248:                         }
 7249:                     }
 7250:                     $resulttext .= '</ul></li>';
 7251:                 }
 7252:             }
 7253:             my %newenv;
 7254:             foreach my $item (@usertools) {
 7255:                 my (%haschgs,%inconf);
 7256:                 if ($context eq 'requestauthor') {
 7257:                     %haschgs = %changes;
 7258:                     %inconf = %confhash;
 7259:                 } else {
 7260:                     if (ref($changes{$item}) eq 'HASH') {
 7261:                         %haschgs = %{$changes{$item}};
 7262:                     }
 7263:                     if (ref($confhash{$item}) eq 'HASH') {
 7264:                         %inconf = %{$confhash{$item}};
 7265:                     }
 7266:                 }
 7267:                 if (keys(%haschgs) > 0) {
 7268:                     my $newacc = 
 7269:                         &Apache::lonnet::usertools_access($env{'user.name'},
 7270:                                                           $env{'user.domain'},
 7271:                                                           $item,'reload',$context);
 7272:                     if (($context eq 'requestcourses') ||
 7273:                         ($context eq 'requestauthor')) {
 7274:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 7275:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 7276:                         }
 7277:                     } else {
 7278:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 7279:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 7280:                         }
 7281:                     }
 7282:                     unless ($context eq 'requestauthor') {
 7283:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 7284:                     }
 7285:                     foreach my $type (@{$types},'default','_LC_adv') {
 7286:                         if ($haschgs{$type}) {
 7287:                             my $typetitle = $usertypes->{$type};
 7288:                             if ($type eq 'default') {
 7289:                                 $typetitle = $othertitle;
 7290:                             } elsif ($type eq '_LC_adv') {
 7291:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 7292:                             }
 7293:                             if ($inconf{$type}) {
 7294:                                 if ($context eq 'requestcourses') {
 7295:                                     my $cond;
 7296:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 7297:                                         if ($1 eq '') {
 7298:                                             $cond = &mt('(Automatic processing of any request).');
 7299:                                         } else {
 7300:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 7301:                                         }
 7302:                                     } else { 
 7303:                                         $cond = $conditions{$inconf{$type}};
 7304:                                     }
 7305:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 7306:                                 } elsif ($context eq 'requestauthor') {
 7307:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 7308:                                                              $titles{$inconf{$type}},$typetitle);
 7309: 
 7310:                                 } else {
 7311:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7312:                                 }
 7313:                             } else {
 7314:                                 if ($type eq '_LC_adv') {
 7315:                                     if ($inconf{$type} eq '0') {
 7316:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7317:                                     } else { 
 7318:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7319:                                     }
 7320:                                 } else {
 7321:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7322:                                 }
 7323:                             }
 7324:                         }
 7325:                     }
 7326:                     unless ($context eq 'requestauthor') {
 7327:                         $resulttext .= '</ul></li>';
 7328:                     }
 7329:                 }
 7330:             }
 7331:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7332:                 if (ref($changes{'notify'}) eq 'HASH') {
 7333:                     if ($changes{'notify'}{'approval'}) {
 7334:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7335:                             if ($confhash{'notify'}{'approval'}) {
 7336:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7337:                             } else {
 7338:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7339:                             }
 7340:                         }
 7341:                     }
 7342:                 }
 7343:             }
 7344:             if ($action eq 'requestcourses') {
 7345:                 my @offon = ('off','on');
 7346:                 if ($changes{'uniquecode'}) {
 7347:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7348:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7349:                         $resulttext .= '<li>'.
 7350:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7351:                                        '</li>';
 7352:                     } else {
 7353:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7354:                                        '</li>';
 7355:                     }
 7356:                 }
 7357:                 if (ref($changes{'textbooks'}) eq 'HASH') {
 7358:                     $resulttext .= '<li>'.&mt('Available textbooks updated').'<ul>';
 7359:                     foreach my $key (sort(keys(%{$changes{'textbooks'}}))) {
 7360:                         my %coursehash = &Apache::lonnet::coursedescription($key);
 7361:                         my $coursetitle = $coursehash{'description'};
 7362:                         my $position = $confhash{'textbooks'}{$key}{'order'} + 1;
 7363:                         $resulttext .= '<li>';
 7364:                         foreach my $item ('subject','title','author') {
 7365:                             my $name = $item.':';
 7366:                             $name =~ s/^(\w)/\U$1/;
 7367:                             $resulttext .= &mt($name).' '.$confhash{'textbooks'}{$key}{$item}.'<br />';
 7368:                         }
 7369:                         $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7370:                         if ($confhash{'textbooks'}{$key}{'image'}) {
 7371:                             $resulttext .= ' '.&mt('Image: [_1]',
 7372:                                                '<img src="'.$confhash{'textbooks'}{$key}{'image'}.'"'.
 7373:                                                ' alt="Textbook cover" />').'<br />';
 7374:                         }
 7375:                         $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7376:                     }
 7377:                     $resulttext .= '</ul></li>';
 7378:                 }
 7379:                 if (ref($changes{'validation'}) eq 'HASH') {
 7380:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7381:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7382:                         foreach my $item (@{$validationitemsref}) {
 7383:                             if (exists($changes{'validation'}{$item})) {
 7384:                                 if ($item eq 'markup') {
 7385:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7386:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7387:                                 } else {
 7388:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7389:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7390:                                 }
 7391:                             }
 7392:                         }
 7393:                         if (exists($changes{'validation'}{'dc'})) {
 7394:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7395:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7396:                         }
 7397:                     }
 7398:                 }
 7399:             }
 7400:             $resulttext .= '</ul>';
 7401:             if (keys(%newenv)) {
 7402:                 &Apache::lonnet::appenv(\%newenv);
 7403:             }
 7404:         } else {
 7405:             if ($context eq 'requestcourses') {
 7406:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7407:             } elsif ($context eq 'requestauthor') {
 7408:                 $resulttext = &mt('No changes made to rights to request author space.');
 7409:             } else {
 7410:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7411:             }
 7412:         }
 7413:     } else {
 7414:         $resulttext = '<span class="LC_error">'.
 7415: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7416:     }
 7417:     if ($errors) {
 7418:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7419:                        '<ul>'.$errors.'</ul></p>';
 7420:     }
 7421:     return $resulttext;
 7422: }
 7423: 
 7424: sub process_textbook_image {
 7425:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$configuserok,$switchserver,$author_ok) = @_;
 7426:     my $filename = $env{'form.'.$caller.'.filename'};
 7427:     my ($error,$url);
 7428:     my ($width,$height) = (50,50);
 7429:     if ($configuserok eq 'ok') {
 7430:         if ($switchserver) {
 7431:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7432:                          $switchserver);
 7433:         } elsif ($author_ok eq 'ok') {
 7434:             my ($result,$imageurl) =
 7435:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7436:                              "textbooks/$dom/$cnum/cover",$width,$height);
 7437:             if ($result eq 'ok') {
 7438:                 $url = $imageurl;
 7439:             } else {
 7440:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7441:             }
 7442:         } else {
 7443:             $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);
 7444:         }
 7445:     } else {
 7446:         $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);
 7447:     }
 7448:     return ($url,$error);
 7449: }
 7450: 
 7451: sub modify_autoenroll {
 7452:     my ($dom,$lastactref,%domconfig) = @_;
 7453:     my ($resulttext,%changes);
 7454:     my %currautoenroll;
 7455:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7456:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7457:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7458:         }
 7459:     }
 7460:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7461:     my %title = ( run => 'Auto-enrollment active',
 7462:                   sender => 'Sender for notification messages',
 7463:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7464:     my @offon = ('off','on');
 7465:     my $sender_uname = $env{'form.sender_uname'};
 7466:     my $sender_domain = $env{'form.sender_domain'};
 7467:     if ($sender_domain eq '') {
 7468:         $sender_uname = '';
 7469:     } elsif ($sender_uname eq '') {
 7470:         $sender_domain = '';
 7471:     }
 7472:     my $coowners = $env{'form.autoassign_coowners'};
 7473:     my %autoenrollhash =  (
 7474:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7475:                                        'sender_uname' => $sender_uname,
 7476:                                        'sender_domain' => $sender_domain,
 7477:                                        'co-owners' => $coowners,
 7478:                                 }
 7479:                      );
 7480:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7481:                                              $dom);
 7482:     if ($putresult eq 'ok') {
 7483:         if (exists($currautoenroll{'run'})) {
 7484:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7485:                  $changes{'run'} = 1;
 7486:              }
 7487:         } elsif ($autorun) {
 7488:             if ($env{'form.autoenroll_run'} ne '1') {
 7489:                  $changes{'run'} = 1;
 7490:             }
 7491:         }
 7492:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7493:             $changes{'sender'} = 1;
 7494:         }
 7495:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7496:             $changes{'sender'} = 1;
 7497:         }
 7498:         if ($currautoenroll{'co-owners'} ne '') {
 7499:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7500:                 $changes{'coowners'} = 1;
 7501:             }
 7502:         } elsif ($coowners) {
 7503:             $changes{'coowners'} = 1;
 7504:         }      
 7505:         if (keys(%changes) > 0) {
 7506:             $resulttext = &mt('Changes made:').'<ul>';
 7507:             if ($changes{'run'}) {
 7508:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7509:             }
 7510:             if ($changes{'sender'}) {
 7511:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7512:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7513:                 } else {
 7514:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7515:                 }
 7516:             }
 7517:             if ($changes{'coowners'}) {
 7518:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7519:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7520:                 if (ref($lastactref) eq 'HASH') {
 7521:                     $lastactref->{'domainconfig'} = 1;
 7522:                 }
 7523:             }
 7524:             $resulttext .= '</ul>';
 7525:         } else {
 7526:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7527:         }
 7528:     } else {
 7529:         $resulttext = '<span class="LC_error">'.
 7530: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7531:     }
 7532:     return $resulttext;
 7533: }
 7534: 
 7535: sub modify_autoupdate {
 7536:     my ($dom,%domconfig) = @_;
 7537:     my ($resulttext,%currautoupdate,%fields,%changes);
 7538:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7539:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7540:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7541:         }
 7542:     }
 7543:     my @offon = ('off','on');
 7544:     my %title = &Apache::lonlocal::texthash (
 7545:                    run => 'Auto-update:',
 7546:                    classlists => 'Updates to user information in classlists?'
 7547:                 );
 7548:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7549:     my %fieldtitles = &Apache::lonlocal::texthash (
 7550:                         id => 'Student/Employee ID',
 7551:                         permanentemail => 'E-mail address',
 7552:                         lastname => 'Last Name',
 7553:                         firstname => 'First Name',
 7554:                         middlename => 'Middle Name',
 7555:                         generation => 'Generation',
 7556:                       );
 7557:     $othertitle = &mt('All users');
 7558:     if (keys(%{$usertypes}) >  0) {
 7559:         $othertitle = &mt('Other users');
 7560:     }
 7561:     foreach my $key (keys(%env)) {
 7562:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7563:             my ($usertype,$item) = ($1,$2);
 7564:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7565:                 if ($usertype eq 'default') {   
 7566:                     push(@{$fields{$1}},$2);
 7567:                 } elsif (ref($types) eq 'ARRAY') {
 7568:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7569:                         push(@{$fields{$1}},$2);
 7570:                     }
 7571:                 }
 7572:             }
 7573:         }
 7574:     }
 7575:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 7576:     @lockablenames = sort(@lockablenames);
 7577:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 7578:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7579:         if (@changed) {
 7580:             $changes{'lockablenames'} = 1;
 7581:         }
 7582:     } else {
 7583:         if (@lockablenames) {
 7584:             $changes{'lockablenames'} = 1;
 7585:         }
 7586:     }
 7587:     my %updatehash = (
 7588:                       autoupdate => { run => $env{'form.autoupdate_run'},
 7589:                                       classlists => $env{'form.classlists'},
 7590:                                       fields => {%fields},
 7591:                                       lockablenames => \@lockablenames,
 7592:                                     }
 7593:                      );
 7594:     foreach my $key (keys(%currautoupdate)) {
 7595:         if (($key eq 'run') || ($key eq 'classlists')) {
 7596:             if (exists($updatehash{autoupdate}{$key})) {
 7597:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 7598:                     $changes{$key} = 1;
 7599:                 }
 7600:             }
 7601:         } elsif ($key eq 'fields') {
 7602:             if (ref($currautoupdate{$key}) eq 'HASH') {
 7603:                 foreach my $item (@{$types},'default') {
 7604:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 7605:                         my $change = 0;
 7606:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 7607:                             if (!exists($fields{$item})) {
 7608:                                 $change = 1;
 7609:                                 last;
 7610:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 7611:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 7612:                                     $change = 1;
 7613:                                     last;
 7614:                                 }
 7615:                             }
 7616:                         }
 7617:                         if ($change) {
 7618:                             push(@{$changes{$key}},$item);
 7619:                         }
 7620:                     } 
 7621:                 }
 7622:             }
 7623:         } elsif ($key eq 'lockablenames') {
 7624:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 7625:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7626:                 if (@changed) {
 7627:                     $changes{'lockablenames'} = 1;
 7628:                 }
 7629:             } else {
 7630:                 if (@lockablenames) {
 7631:                     $changes{'lockablenames'} = 1;
 7632:                 }
 7633:             }
 7634:         }
 7635:     }
 7636:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 7637:         if (@lockablenames) {
 7638:             $changes{'lockablenames'} = 1;
 7639:         }
 7640:     }
 7641:     foreach my $item (@{$types},'default') {
 7642:         if (defined($fields{$item})) {
 7643:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 7644:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 7645:                     my $change = 0;
 7646:                     if (ref($fields{$item}) eq 'ARRAY') {
 7647:                         foreach my $type (@{$fields{$item}}) {
 7648:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 7649:                                 $change = 1;
 7650:                                 last;
 7651:                             }
 7652:                         }
 7653:                     }
 7654:                     if ($change) {
 7655:                         push(@{$changes{'fields'}},$item);
 7656:                     }
 7657:                 } else {
 7658:                     push(@{$changes{'fields'}},$item);
 7659:                 }
 7660:             } else {
 7661:                 push(@{$changes{'fields'}},$item);
 7662:             }
 7663:         }
 7664:     }
 7665:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7666:                                              $dom);
 7667:     if ($putresult eq 'ok') {
 7668:         if (keys(%changes) > 0) {
 7669:             $resulttext = &mt('Changes made:').'<ul>';
 7670:             foreach my $key (sort(keys(%changes))) {
 7671:                 if ($key eq 'lockablenames') {
 7672:                     $resulttext .= '<li>';
 7673:                     if (@lockablenames) {
 7674:                         $usertypes->{'default'} = $othertitle;
 7675:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7676:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7677:                     } else {
 7678:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7679:                     }
 7680:                     $resulttext .= '</li>';
 7681:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7682:                     foreach my $item (@{$changes{$key}}) {
 7683:                         my @newvalues;
 7684:                         foreach my $type (@{$fields{$item}}) {
 7685:                             push(@newvalues,$fieldtitles{$type});
 7686:                         }
 7687:                         my $newvaluestr;
 7688:                         if (@newvalues > 0) {
 7689:                             $newvaluestr = join(', ',@newvalues);
 7690:                         } else {
 7691:                             $newvaluestr = &mt('none');
 7692:                         }
 7693:                         if ($item eq 'default') {
 7694:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7695:                         } else {
 7696:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7697:                         }
 7698:                     }
 7699:                 } else {
 7700:                     my $newvalue;
 7701:                     if ($key eq 'run') {
 7702:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7703:                     } else {
 7704:                         $newvalue = $offon[$env{'form.'.$key}];
 7705:                     }
 7706:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7707:                 }
 7708:             }
 7709:             $resulttext .= '</ul>';
 7710:         } else {
 7711:             $resulttext = &mt('No changes made to autoupdates');
 7712:         }
 7713:     } else {
 7714:         $resulttext = '<span class="LC_error">'.
 7715: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7716:     }
 7717:     return $resulttext;
 7718: }
 7719: 
 7720: sub modify_autocreate {
 7721:     my ($dom,%domconfig) = @_;
 7722:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7723:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7724:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7725:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7726:         }
 7727:     }
 7728:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7729:                  req => 'Auto-creation of validated requests for official courses',
 7730:                  xmldc => 'Identity of course creator of courses from XML files',
 7731:                );
 7732:     my @types = ('xml','req');
 7733:     foreach my $item (@types) {
 7734:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7735:         $newvals{$item} =~ s/\D//g;
 7736:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7737:     }
 7738:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7739:     my %domcoords = &get_active_dcs($dom);
 7740:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7741:         $newvals{'xmldc'} = '';
 7742:     } 
 7743:     %autocreatehash =  (
 7744:                         autocreate => { xml => $newvals{'xml'},
 7745:                                         req => $newvals{'req'},
 7746:                                       }
 7747:                        );
 7748:     if ($newvals{'xmldc'} ne '') {
 7749:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7750:     }
 7751:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7752:                                              $dom);
 7753:     if ($putresult eq 'ok') {
 7754:         my @items = @types;
 7755:         if ($newvals{'xml'}) {
 7756:             push(@items,'xmldc');
 7757:         }
 7758:         foreach my $item (@items) {
 7759:             if (exists($currautocreate{$item})) {
 7760:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7761:                     $changes{$item} = 1;
 7762:                 }
 7763:             } elsif ($newvals{$item}) {
 7764:                 $changes{$item} = 1;
 7765:             }
 7766:         }
 7767:         if (keys(%changes) > 0) {
 7768:             my @offon = ('off','on'); 
 7769:             $resulttext = &mt('Changes made:').'<ul>';
 7770:             foreach my $item (@types) {
 7771:                 if ($changes{$item}) {
 7772:                     my $newtxt = $offon[$newvals{$item}];
 7773:                     $resulttext .= '<li>'.
 7774:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7775:                                        '<b>','</b>').
 7776:                                    '</li>';
 7777:                 }
 7778:             }
 7779:             if ($changes{'xmldc'}) {
 7780:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7781:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7782:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7783:             }
 7784:             $resulttext .= '</ul>';
 7785:         } else {
 7786:             $resulttext = &mt('No changes made to auto-creation settings');
 7787:         }
 7788:     } else {
 7789:         $resulttext = '<span class="LC_error">'.
 7790:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7791:     }
 7792:     return $resulttext;
 7793: }
 7794: 
 7795: sub modify_directorysrch {
 7796:     my ($dom,%domconfig) = @_;
 7797:     my ($resulttext,%changes);
 7798:     my %currdirsrch;
 7799:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7800:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7801:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7802:         }
 7803:     }
 7804:     my %title = ( available => 'Directory search available',
 7805:                   localonly => 'Other domains can search',
 7806:                   searchby => 'Search types',
 7807:                   searchtypes => 'Search latitude');
 7808:     my @offon = ('off','on');
 7809:     my @otherdoms = ('Yes','No');
 7810: 
 7811:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7812:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7813:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7814: 
 7815:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7816:     if (keys(%{$usertypes}) == 0) {
 7817:         @cansearch = ('default');
 7818:     } else {
 7819:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7820:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7821:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7822:                     push(@{$changes{'cansearch'}},$type);
 7823:                 }
 7824:             }
 7825:             foreach my $type (@cansearch) {
 7826:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7827:                     push(@{$changes{'cansearch'}},$type);
 7828:                 }
 7829:             }
 7830:         } else {
 7831:             push(@{$changes{'cansearch'}},@cansearch);
 7832:         }
 7833:     }
 7834: 
 7835:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7836:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7837:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7838:                 push(@{$changes{'searchby'}},$by);
 7839:             }
 7840:         }
 7841:         foreach my $by (@searchby) {
 7842:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7843:                 push(@{$changes{'searchby'}},$by);
 7844:             }
 7845:         }
 7846:     } else {
 7847:         push(@{$changes{'searchby'}},@searchby);
 7848:     }
 7849: 
 7850:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7851:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7852:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7853:                 push(@{$changes{'searchtypes'}},$type);
 7854:             }
 7855:         }
 7856:         foreach my $type (@searchtypes) {
 7857:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7858:                 push(@{$changes{'searchtypes'}},$type);
 7859:             }
 7860:         }
 7861:     } else {
 7862:         if (exists($currdirsrch{'searchtypes'})) {
 7863:             foreach my $type (@searchtypes) {  
 7864:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7865:                     push(@{$changes{'searchtypes'}},$type);
 7866:                 }
 7867:             }
 7868:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7869:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7870:             }   
 7871:         } else {
 7872:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7873:         }
 7874:     }
 7875: 
 7876:     my %dirsrch_hash =  (
 7877:             directorysrch => { available => $env{'form.dirsrch_available'},
 7878:                                cansearch => \@cansearch,
 7879:                                localonly => $env{'form.dirsrch_localonly'},
 7880:                                searchby => \@searchby,
 7881:                                searchtypes => \@searchtypes,
 7882:                              }
 7883:             );
 7884:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7885:                                              $dom);
 7886:     if ($putresult eq 'ok') {
 7887:         if (exists($currdirsrch{'available'})) {
 7888:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7889:                  $changes{'available'} = 1;
 7890:              }
 7891:         } else {
 7892:             if ($env{'form.dirsrch_available'} eq '1') {
 7893:                 $changes{'available'} = 1;
 7894:             }
 7895:         }
 7896:         if (exists($currdirsrch{'localonly'})) {
 7897:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7898:                  $changes{'localonly'} = 1;
 7899:              }
 7900:         } else {
 7901:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7902:                 $changes{'localonly'} = 1;
 7903:             }
 7904:         }
 7905:         if (keys(%changes) > 0) {
 7906:             $resulttext = &mt('Changes made:').'<ul>';
 7907:             if ($changes{'available'}) {
 7908:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7909:             }
 7910:             if ($changes{'localonly'}) {
 7911:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7912:             }
 7913: 
 7914:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7915:                 my $chgtext;
 7916:                 if (ref($usertypes) eq 'HASH') {
 7917:                     if (keys(%{$usertypes}) > 0) {
 7918:                         foreach my $type (@{$types}) {
 7919:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7920:                                 $chgtext .= $usertypes->{$type}.'; ';
 7921:                             }
 7922:                         }
 7923:                         if (grep(/^default$/,@cansearch)) {
 7924:                             $chgtext .= $othertitle;
 7925:                         } else {
 7926:                             $chgtext =~ s/\; $//;
 7927:                         }
 7928:                         $resulttext .=
 7929:                             '<li>'.
 7930:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 7931:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 7932:                             '</li>';
 7933:                     }
 7934:                 }
 7935:             }
 7936:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 7937:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 7938:                 my $chgtext;
 7939:                 foreach my $type (@{$titleorder}) {
 7940:                     if (grep(/^\Q$type\E$/,@searchby)) {
 7941:                         if (defined($searchtitles->{$type})) {
 7942:                             $chgtext .= $searchtitles->{$type}.'; ';
 7943:                         }
 7944:                     }
 7945:                 }
 7946:                 $chgtext =~ s/\; $//;
 7947:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 7948:             }
 7949:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 7950:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 7951:                 my $chgtext;
 7952:                 foreach my $type (@{$srchtypeorder}) {
 7953:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 7954:                         if (defined($srchtypes_desc->{$type})) {
 7955:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 7956:                         }
 7957:                     }
 7958:                 }
 7959:                 $chgtext =~ s/\; $//;
 7960:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 7961:             }
 7962:             $resulttext .= '</ul>';
 7963:         } else {
 7964:             $resulttext = &mt('No changes made to institution directory search settings');
 7965:         }
 7966:     } else {
 7967:         $resulttext = '<span class="LC_error">'.
 7968:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 7969:     }
 7970:     return $resulttext;
 7971: }
 7972: 
 7973: sub modify_contacts {
 7974:     my ($dom,$lastactref,%domconfig) = @_;
 7975:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 7976:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 7977:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 7978:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 7979:         }
 7980:     }
 7981:     my (%others,%to,%bcc);
 7982:     my @contacts = ('supportemail','adminemail');
 7983:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 7984:                     'requestsmail','updatesmail','idconflictsmail');
 7985:     my @toggles = ('reporterrors','reportupdates');
 7986:     foreach my $type (@mailings) {
 7987:         @{$newsetting{$type}} = 
 7988:             &Apache::loncommon::get_env_multiple('form.'.$type);
 7989:         foreach my $item (@contacts) {
 7990:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 7991:                 $contacts_hash{contacts}{$type}{$item} = 1;
 7992:             } else {
 7993:                 $contacts_hash{contacts}{$type}{$item} = 0;
 7994:             }
 7995:         }  
 7996:         $others{$type} = $env{'form.'.$type.'_others'};
 7997:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 7998:         if ($type eq 'helpdeskmail') {
 7999:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 8000:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 8001:         }
 8002:     }
 8003:     foreach my $item (@contacts) {
 8004:         $to{$item} = $env{'form.'.$item};
 8005:         $contacts_hash{'contacts'}{$item} = $to{$item};
 8006:     }
 8007:     foreach my $item (@toggles) {
 8008:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 8009:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 8010:         }
 8011:     }
 8012:     if (keys(%currsetting) > 0) {
 8013:         foreach my $item (@contacts) {
 8014:             if ($to{$item} ne $currsetting{$item}) {
 8015:                 $changes{$item} = 1;
 8016:             }
 8017:         }
 8018:         foreach my $type (@mailings) {
 8019:             foreach my $item (@contacts) {
 8020:                 if (ref($currsetting{$type}) eq 'HASH') {
 8021:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 8022:                         push(@{$changes{$type}},$item);
 8023:                     }
 8024:                 } else {
 8025:                     push(@{$changes{$type}},@{$newsetting{$type}});
 8026:                 }
 8027:             }
 8028:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 8029:                 push(@{$changes{$type}},'others');
 8030:             }
 8031:             if ($type eq 'helpdeskmail') {   
 8032:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 8033:                     push(@{$changes{$type}},'bcc'); 
 8034:                 }
 8035:             }
 8036:         }
 8037:     } else {
 8038:         my %default;
 8039:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 8040:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 8041:         $default{'errormail'} = 'adminemail';
 8042:         $default{'packagesmail'} = 'adminemail';
 8043:         $default{'helpdeskmail'} = 'supportemail';
 8044:         $default{'lonstatusmail'} = 'adminemail';
 8045:         $default{'requestsmail'} = 'adminemail';
 8046:         $default{'updatesmail'} = 'adminemail';
 8047:         foreach my $item (@contacts) {
 8048:            if ($to{$item} ne $default{$item}) {
 8049:               $changes{$item} = 1;
 8050:            }
 8051:         }
 8052:         foreach my $type (@mailings) {
 8053:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 8054:                
 8055:                 push(@{$changes{$type}},@{$newsetting{$type}});
 8056:             }
 8057:             if ($others{$type} ne '') {
 8058:                 push(@{$changes{$type}},'others');
 8059:             }
 8060:             if ($type eq 'helpdeskmail') {
 8061:                 if ($bcc{$type} ne '') {
 8062:                     push(@{$changes{$type}},'bcc');
 8063:                 }
 8064:             }
 8065:         }
 8066:     }
 8067:     foreach my $item (@toggles) {
 8068:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 8069:             $changes{$item} = 1;
 8070:         } elsif ((!$env{'form.'.$item}) &&
 8071:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 8072:             $changes{$item} = 1;
 8073:         }
 8074:     }
 8075:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 8076:                                              $dom);
 8077:     if ($putresult eq 'ok') {
 8078:         if (keys(%changes) > 0) {
 8079:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8080:             if (ref($lastactref) eq 'HASH') {
 8081:                 $lastactref->{'domainconfig'} = 1;
 8082:             }
 8083:             my ($titles,$short_titles)  = &contact_titles();
 8084:             $resulttext = &mt('Changes made:').'<ul>';
 8085:             foreach my $item (@contacts) {
 8086:                 if ($changes{$item}) {
 8087:                     $resulttext .= '<li>'.$titles->{$item}.
 8088:                                     &mt(' set to: ').
 8089:                                     '<span class="LC_cusr_emph">'.
 8090:                                     $to{$item}.'</span></li>';
 8091:                 }
 8092:             }
 8093:             foreach my $type (@mailings) {
 8094:                 if (ref($changes{$type}) eq 'ARRAY') {
 8095:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 8096:                     my @text;
 8097:                     foreach my $item (@{$newsetting{$type}}) {
 8098:                         push(@text,$short_titles->{$item});
 8099:                     }
 8100:                     if ($others{$type} ne '') {
 8101:                         push(@text,$others{$type});
 8102:                     }
 8103:                     $resulttext .= '<span class="LC_cusr_emph">'.
 8104:                                    join(', ',@text).'</span>';
 8105:                     if ($type eq 'helpdeskmail') {
 8106:                         if ($bcc{$type} ne '') {
 8107:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 8108:                         }
 8109:                     }
 8110:                     $resulttext .= '</li>';
 8111:                 }
 8112:             }
 8113:             my @offon = ('off','on');
 8114:             if ($changes{'reporterrors'}) {
 8115:                 $resulttext .= '<li>'.
 8116:                                &mt('E-mail error reports to [_1] set to "'.
 8117:                                    $offon[$env{'form.reporterrors'}].'".',
 8118:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8119:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 8120:                                '</li>';
 8121:             }
 8122:             if ($changes{'reportupdates'}) {
 8123:                 $resulttext .= '<li>'.
 8124:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 8125:                                     $offon[$env{'form.reportupdates'}].'".',
 8126:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8127:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 8128:                                 '</li>';
 8129:             }
 8130:             $resulttext .= '</ul>';
 8131:         } else {
 8132:             $resulttext = &mt('No changes made to contact information');
 8133:         }
 8134:     } else {
 8135:         $resulttext = '<span class="LC_error">'.
 8136:             &mt('An error occurred: [_1].',$putresult).'</span>';
 8137:     }
 8138:     return $resulttext;
 8139: }
 8140: 
 8141: sub modify_usercreation {
 8142:     my ($dom,%domconfig) = @_;
 8143:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 8144:     my $warningmsg;
 8145:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8146:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8147:             if ($key eq 'cancreate') {
 8148:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8149:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8150:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8151:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 8152:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8153:                         } else {
 8154:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8155:                         }
 8156:                     }
 8157:                 }
 8158:             } elsif ($key eq 'email_rule') {
 8159:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8160:             } else {
 8161:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8162:             }
 8163:         }
 8164:     }
 8165:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 8166:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 8167:     my @contexts = ('author','course','requestcrs');
 8168:     foreach my $item(@contexts) {
 8169:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 8170:     }
 8171:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8172:         foreach my $item (@contexts) {
 8173:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 8174:                 push(@{$changes{'cancreate'}},$item);
 8175:             }
 8176:         }
 8177:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 8178:         foreach my $item (@contexts) {
 8179:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 8180:                 if ($cancreate{$item} ne 'any') {
 8181:                     push(@{$changes{'cancreate'}},$item);
 8182:                 }
 8183:             } else {
 8184:                 if ($cancreate{$item} ne 'none') {
 8185:                     push(@{$changes{'cancreate'}},$item);
 8186:                 }
 8187:             }
 8188:         }
 8189:     } else {
 8190:         foreach my $item (@contexts)  {
 8191:             push(@{$changes{'cancreate'}},$item);
 8192:         }
 8193:     }
 8194: 
 8195:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 8196:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 8197:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 8198:                 push(@{$changes{'username_rule'}},$type);
 8199:             }
 8200:         }
 8201:         foreach my $type (@username_rule) {
 8202:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 8203:                 push(@{$changes{'username_rule'}},$type);
 8204:             }
 8205:         }
 8206:     } else {
 8207:         push(@{$changes{'username_rule'}},@username_rule);
 8208:     }
 8209: 
 8210:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 8211:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 8212:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 8213:                 push(@{$changes{'id_rule'}},$type);
 8214:             }
 8215:         }
 8216:         foreach my $type (@id_rule) {
 8217:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 8218:                 push(@{$changes{'id_rule'}},$type);
 8219:             }
 8220:         }
 8221:     } else {
 8222:         push(@{$changes{'id_rule'}},@id_rule);
 8223:     }
 8224: 
 8225:     my @authen_contexts = ('author','course','domain');
 8226:     my @authtypes = ('int','krb4','krb5','loc');
 8227:     my %authhash;
 8228:     foreach my $item (@authen_contexts) {
 8229:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 8230:         foreach my $auth (@authtypes) {
 8231:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 8232:                 $authhash{$item}{$auth} = 1;
 8233:             } else {
 8234:                 $authhash{$item}{$auth} = 0;
 8235:             }
 8236:         }
 8237:     }
 8238:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 8239:         foreach my $item (@authen_contexts) {
 8240:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 8241:                 foreach my $auth (@authtypes) {
 8242:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 8243:                         push(@{$changes{'authtypes'}},$item);
 8244:                         last;
 8245:                     }
 8246:                 }
 8247:             }
 8248:         }
 8249:     } else {
 8250:         foreach my $item (@authen_contexts) {
 8251:             push(@{$changes{'authtypes'}},$item);
 8252:         }
 8253:     }
 8254: 
 8255:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 8256:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 8257:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 8258:     $save_usercreate{'id_rule'} = \@id_rule;
 8259:     $save_usercreate{'username_rule'} = \@username_rule,
 8260:     $save_usercreate{'authtypes'} = \%authhash;
 8261: 
 8262:     my %usercreation_hash =  (
 8263:         usercreation     => \%save_usercreate,
 8264:     );
 8265: 
 8266:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 8267:                                              $dom);
 8268: 
 8269:     if ($putresult eq 'ok') {
 8270:         if (keys(%changes) > 0) {
 8271:             $resulttext = &mt('Changes made:').'<ul>';
 8272:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8273:                 my %lt = &usercreation_types();
 8274:                 foreach my $type (@{$changes{'cancreate'}}) {
 8275:                     my $chgtext = $lt{$type}.', ';
 8276:                     if ($cancreate{$type} eq 'none') {
 8277:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 8278:                     } elsif ($cancreate{$type} eq 'any') {
 8279:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 8280:                     } elsif ($cancreate{$type} eq 'official') {
 8281:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 8282:                     } elsif ($cancreate{$type} eq 'unofficial') {
 8283:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 8284:                     }
 8285:                     $resulttext .= '<li>'.$chgtext.'</li>';
 8286:                 }
 8287:             }
 8288:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 8289:                 my ($rules,$ruleorder) = 
 8290:                     &Apache::lonnet::inst_userrules($dom,'username');
 8291:                 my $chgtext = '<ul>';
 8292:                 foreach my $type (@username_rule) {
 8293:                     if (ref($rules->{$type}) eq 'HASH') {
 8294:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 8295:                     }
 8296:                 }
 8297:                 $chgtext .= '</ul>';
 8298:                 if (@username_rule > 0) {
 8299:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 8300:                 } else {
 8301:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 8302:                 }
 8303:             }
 8304:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 8305:                 my ($idrules,$idruleorder) = 
 8306:                     &Apache::lonnet::inst_userrules($dom,'id');
 8307:                 my $chgtext = '<ul>';
 8308:                 foreach my $type (@id_rule) {
 8309:                     if (ref($idrules->{$type}) eq 'HASH') {
 8310:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8311:                     }
 8312:                 }
 8313:                 $chgtext .= '</ul>';
 8314:                 if (@id_rule > 0) {
 8315:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8316:                 } else {
 8317:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8318:                 }
 8319:             }
 8320:             my %authname = &authtype_names();
 8321:             my %context_title = &context_names();
 8322:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8323:                 my $chgtext = '<ul>';
 8324:                 foreach my $type (@{$changes{'authtypes'}}) {
 8325:                     my @allowed;
 8326:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8327:                     foreach my $auth (@authtypes) {
 8328:                         if ($authhash{$type}{$auth}) {
 8329:                             push(@allowed,$authname{$auth});
 8330:                         }
 8331:                     }
 8332:                     if (@allowed > 0) {
 8333:                         $chgtext .= join(', ',@allowed).'</li>';
 8334:                     } else {
 8335:                         $chgtext .= &mt('none').'</li>';
 8336:                     }
 8337:                 }
 8338:                 $chgtext .= '</ul>';
 8339:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8340:                 $resulttext .= '</li>';
 8341:             }
 8342:             $resulttext .= '</ul>';
 8343:         } else {
 8344:             $resulttext = &mt('No changes made to user creation settings');
 8345:         }
 8346:     } else {
 8347:         $resulttext = '<span class="LC_error">'.
 8348:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8349:     }
 8350:     if ($warningmsg ne '') {
 8351:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8352:     }
 8353:     return $resulttext;
 8354: }
 8355: 
 8356: sub modify_selfcreation {
 8357:     my ($dom,%domconfig) = @_;
 8358:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8359:     my (%save_usercreate,%save_usermodify);
 8360:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8361:     if (ref($types) eq 'ARRAY') {
 8362:         $usertypes->{'default'} = $othertitle;
 8363:         push(@{$types},'default');
 8364:     }
 8365: #
 8366: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8367: #
 8368:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8369:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8370:             if ($key eq 'cancreate') {
 8371:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8372:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8373:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8374:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8375:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 8376:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 8377:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8378:                         } else {
 8379:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8380:                         }
 8381:                     }
 8382:                 }
 8383:             } elsif ($key eq 'email_rule') {
 8384:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8385:             } else {
 8386:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8387:             }
 8388:         }
 8389:     }
 8390: #
 8391: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8392: #
 8393:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8394:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8395:             if ($key eq 'selfcreate') {
 8396:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8397:             } else {
 8398:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8399:             }
 8400:         }
 8401:     }
 8402: 
 8403:     my @contexts = ('selfcreate');
 8404:     @{$cancreate{'selfcreate'}} = ();
 8405:     %{$cancreate{'emailusername'}} = ();
 8406:     @{$cancreate{'statustocreate'}} = ();
 8407:     %{$cancreate{'selfcreateprocessing'}} = ();
 8408:     %{$cancreate{'shibenv'}} = ();
 8409:     my %selfcreatetypes = (
 8410:                              sso   => 'users authenticated by institutional single sign on',
 8411:                              login => 'users authenticated by institutional log-in',
 8412:                              email => 'users who provide a valid e-mail address for use as username',
 8413:                           );
 8414: #
 8415: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8416: # is permitted.
 8417: #
 8418: 
 8419:     my @statuses;
 8420:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 8421:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 8422:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 8423:         }
 8424:     }
 8425:     push(@statuses,'default');
 8426: 
 8427:     foreach my $item ('login','sso','email') {
 8428:         if ($item eq 'email') {
 8429:             if ($env{'form.cancreate_email'}) {
 8430:                 push(@{$cancreate{'selfcreate'}},'email');
 8431:                 push(@contexts,'selfcreateprocessing');
 8432:                 foreach my $type (@statuses) {
 8433:                     if ($type eq 'default') {
 8434:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 8435:                     } else { 
 8436:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 8437:                     }
 8438:                 }
 8439:             }
 8440:         } else {
 8441:             if ($env{'form.cancreate_'.$item}) {
 8442:                 push(@{$cancreate{'selfcreate'}},$item);
 8443:             }
 8444:         }
 8445:     }
 8446:     my (@email_rule,%userinfo,%savecaptcha);
 8447:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8448: #
 8449: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8450: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8451: #
 8452: 
 8453:     if ($env{'form.cancreate_email'} eq 'email') {
 8454:         push(@contexts,'emailusername');
 8455:         if (ref($types) eq 'ARRAY') {
 8456:             foreach my $type (@{$types}) {
 8457:                 if (ref($infofields) eq 'ARRAY') {
 8458:                     foreach my $field (@{$infofields}) {
 8459:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8460:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8461:                         }
 8462:                     }
 8463:                 }
 8464:             }
 8465:         }
 8466: #
 8467: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8468: # queued requests for self-creation of account using e-mail address as username
 8469: #
 8470: 
 8471:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8472:         @approvalnotify = sort(@approvalnotify);
 8473:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8474:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8475:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8476:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8477:                     push(@{$changes{'cancreate'}},'notify');
 8478:                 }
 8479:             } else {
 8480:                 if ($cancreate{'notify'}{'approval'}) {
 8481:                     push(@{$changes{'cancreate'}},'notify');
 8482:                 }
 8483:             }
 8484:         } elsif ($cancreate{'notify'}{'approval'}) {
 8485:             push(@{$changes{'cancreate'}},'notify');
 8486:         }
 8487: 
 8488: #
 8489: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8490: #
 8491:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8492:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8493:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8494:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8495:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8496:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8497:                         push(@{$changes{'email_rule'}},$type);
 8498:                     }
 8499:                 }
 8500:             }
 8501:             if (@email_rule > 0) {
 8502:                 foreach my $type (@email_rule) {
 8503:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8504:                         push(@{$changes{'email_rule'}},$type);
 8505:                     }
 8506:                 }
 8507:             }
 8508:         } elsif (@email_rule > 0) {
 8509:             push(@{$changes{'email_rule'}},@email_rule);
 8510:         }
 8511:     }
 8512: #  
 8513: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 8514: # institutional log-in.
 8515: #
 8516:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8517:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8518:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8519:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8520:             $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.').' '.
 8521:                           &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.');
 8522:         }
 8523:     }
 8524:     my @fields = ('lastname','firstname','middlename','generation',
 8525:                   'permanentemail','id');
 8526:     my @shibfields = (@fields,'inststatus');
 8527:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8528: #
 8529: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8530: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8531: # may self-create accounts 
 8532: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8533: # which the user may supply, if institutional data is unavailable.
 8534: #
 8535:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8536:         if (ref($types) eq 'ARRAY') {
 8537:             if (@{$types} > 1) {
 8538:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8539:                 push(@contexts,'statustocreate');
 8540:             } else {
 8541:                 undef($cancreate{'statustocreate'});
 8542:             } 
 8543:             foreach my $type (@{$types}) {
 8544:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8545:                 foreach my $field (@fields) {
 8546:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8547:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8548:                     } else {
 8549:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8550:                     }
 8551:                 }
 8552:             }
 8553:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8554:                 foreach my $type (@{$types}) {
 8555:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8556:                         foreach my $field (@fields) {
 8557:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8558:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8559:                                 push(@{$changes{'selfcreate'}},$type);
 8560:                                 last;
 8561:                             }
 8562:                         }
 8563:                     }
 8564:                 }
 8565:             } else {
 8566:                 foreach my $type (@{$types}) {
 8567:                     push(@{$changes{'selfcreate'}},$type);
 8568:                 }
 8569:             }
 8570:         }
 8571:         foreach my $field (@shibfields) {
 8572:             if ($env{'form.shibenv_'.$field} ne '') {
 8573:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 8574:             }
 8575:         }
 8576:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8577:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 8578:                 foreach my $field (@shibfields) {
 8579:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 8580:                         push(@{$changes{'cancreate'}},'shibenv');
 8581:                     }
 8582:                 }
 8583:             } else {
 8584:                 foreach my $field (@shibfields) {
 8585:                     if ($env{'form.shibenv_'.$field}) {
 8586:                         push(@{$changes{'cancreate'}},'shibenv');
 8587:                         last;
 8588:                     }
 8589:                 }
 8590:             }
 8591:         }
 8592:     }
 8593:     foreach my $item (@contexts) {
 8594:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 8595:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 8596:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 8597:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 8598:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8599:                             push(@{$changes{'cancreate'}},$item);
 8600:                         }
 8601:                     }
 8602:                 }
 8603:             }
 8604:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8605:                 foreach my $type (@{$cancreate{$item}}) {
 8606:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 8607:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8608:                             push(@{$changes{'cancreate'}},$item);
 8609:                         }
 8610:                     }
 8611:                 }
 8612:             }
 8613:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 8614:             if (ref($cancreate{$item}) eq 'HASH') {
 8615:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 8616:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 8617:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 8618:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 8619:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8620:                                     push(@{$changes{'cancreate'}},$item);
 8621:                                 }
 8622:                             }
 8623:                         }
 8624:                     } elsif ($item eq 'selfcreateprocessing') {
 8625:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 8626:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8627:                                 push(@{$changes{'cancreate'}},$item);
 8628:                             }
 8629:                         }
 8630:                     } else {
 8631:                         if (!$cancreate{$item}{$curr}) {
 8632:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8633:                                 push(@{$changes{'cancreate'}},$item);
 8634:                             }
 8635:                         }
 8636:                     }
 8637:                 }
 8638:                 foreach my $field (keys(%{$cancreate{$item}})) {
 8639:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 8640:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 8641:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 8642:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 8643:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8644:                                         push(@{$changes{'cancreate'}},$item);
 8645:                                     }
 8646:                                 }
 8647:                             } else {
 8648:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8649:                                     push(@{$changes{'cancreate'}},$item);
 8650:                                 }
 8651:                             }
 8652:                         }
 8653:                     } elsif ($item eq 'selfcreateprocessing') {
 8654:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 8655:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8656:                                 push(@{$changes{'cancreate'}},$item);
 8657:                             }
 8658:                         }
 8659:                     } else {
 8660:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 8661:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8662:                                 push(@{$changes{'cancreate'}},$item);
 8663:                             }
 8664:                         }
 8665:                     }
 8666:                 }
 8667:             }
 8668:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 8669:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8670:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 8671:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8672:                         push(@{$changes{'cancreate'}},$item);
 8673:                     }
 8674:                 }
 8675:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 8676:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 8677:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8678:                         push(@{$changes{'cancreate'}},$item);
 8679:                     }
 8680:                 }
 8681:             }
 8682:         } elsif ($item eq 'emailusername') {
 8683:             if (ref($cancreate{$item}) eq 'HASH') {
 8684:                 foreach my $type (keys(%{$cancreate{$item}})) {
 8685:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 8686:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 8687:                             if ($cancreate{$item}{$type}{$field}) {
 8688:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8689:                                     push(@{$changes{'cancreate'}},$item);
 8690:                                 }
 8691:                                 last;
 8692:                             }
 8693:                         }
 8694:                     }
 8695:                 }
 8696:             }
 8697:         }
 8698:     }
 8699: #
 8700: # Populate %save_usercreate hash with updates to self-creation configuration.
 8701: #
 8702:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 8703:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 8704:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 8705:     if (ref($cancreate{'notify'}) eq 'HASH') {
 8706:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 8707:     }
 8708:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
 8709:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
 8710:     }
 8711:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8712:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 8713:     }
 8714:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
 8715:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
 8716:     }
 8717:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 8718:     $save_usercreate{'emailrule'} = \@email_rule;
 8719: 
 8720:     my %userconfig_hash = (
 8721:             usercreation     => \%save_usercreate,
 8722:             usermodification => \%save_usermodify,
 8723:     );
 8724:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8725:                                              $dom);
 8726: #
 8727: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8728: #
 8729:     if ($putresult eq 'ok') {
 8730:         if (keys(%changes) > 0) {
 8731:             $resulttext = &mt('Changes made:').'<ul>';
 8732:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8733:                 my %lt = &selfcreation_types();
 8734:                 foreach my $type (@{$changes{'cancreate'}}) {
 8735:                     my $chgtext;
 8736:                     if ($type eq 'selfcreate') {
 8737:                         if (@{$cancreate{$type}} == 0) {
 8738:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 8739:                         } else {
 8740:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 8741:                                         '<ul>';
 8742:                             foreach my $case (@{$cancreate{$type}}) {
 8743:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 8744:                             }
 8745:                             $chgtext .= '</ul>';
 8746:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 8747:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 8748:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8749:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 8750:                                             $chgtext .= '<br />'.
 8751:                                                         '<span class="LC_warning">'.
 8752:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8753:                                                         '</span>';
 8754:                                         }
 8755:                                     }
 8756:                                 }
 8757:                             }
 8758:                         }
 8759:                     } elsif ($type eq 'shibenv') {
 8760:                         if (keys(%{$cancreate{$type}}) == 0) {
 8761:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
 8762:                         } else {
 8763:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
 8764:                                         '<ul>';
 8765:                             foreach my $field (@shibfields) {
 8766:                                 next if ($cancreate{$type}{$field} eq '');
 8767:                                 if ($field eq 'inststatus') {
 8768:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
 8769:                                 } else {
 8770:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
 8771:                                 }
 8772:                             }
 8773:                             $chgtext .= '</ul>';
 8774:                         }  
 8775:                     } elsif ($type eq 'statustocreate') {
 8776:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 8777:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 8778:                             if (@{$cancreate{'selfcreate'}} > 0) {
 8779:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 8780:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 8781:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 8782:                                         $chgtext .= '<br />'.
 8783:                                                     '<span class="LC_warning">'.
 8784:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8785:                                                     '</span>';
 8786:                                     }
 8787:                                 } elsif (ref($usertypes) eq 'HASH') {
 8788:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8789:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 8790:                                     } else {
 8791:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 8792:                                     }
 8793:                                     $chgtext .= '<ul>';
 8794:                                     foreach my $case (@{$cancreate{$type}}) {
 8795:                                         if ($case eq 'default') {
 8796:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 8797:                                         } else {
 8798:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 8799:                                         }
 8800:                                     }
 8801:                                     $chgtext .= '</ul>';
 8802:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8803:                                         $chgtext .= '<br /><span class="LC_warning">'.
 8804:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 8805:                                                     '</span>';
 8806:                                     }
 8807:                                 }
 8808:                             } else {
 8809:                                 if (@{$cancreate{$type}} == 0) {
 8810:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8811:                                 } else {
 8812:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8813:                                 }
 8814:                             }
 8815:                         }
 8816:                     } elsif ($type eq 'selfcreateprocessing') {
 8817:                         my %choices = &Apache::lonlocal::texthash (
 8818:                                                                     automatic => 'Automatic approval',
 8819:                                                                     approval  => 'Queued for approval',
 8820:                                                                   );
 8821:                         if (@statuses > 1) {
 8822:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
 8823:                                         '<ul>';
 8824:                            foreach my $type (@statuses) {
 8825:                                if ($type eq 'default') {
 8826:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8827:                                } else {
 8828:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8829:                                }
 8830:                            }
 8831:                            $chgtext .= '</ul>';
 8832:                         } else {
 8833:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
 8834:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
 8835:                         }
 8836:                     } elsif ($type eq 'captcha') {
 8837:                         if ($savecaptcha{$type} eq 'notused') {
 8838:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8839:                         } else {
 8840:                             my %captchas = &captcha_phrases();
 8841:                             if ($captchas{$savecaptcha{$type}}) {
 8842:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8843:                             } else {
 8844:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8845:                             }
 8846:                         }
 8847:                     } elsif ($type eq 'recaptchakeys') {
 8848:                         my ($privkey,$pubkey);
 8849:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8850:                             $pubkey = $savecaptcha{$type}{'public'};
 8851:                             $privkey = $savecaptcha{$type}{'private'};
 8852:                         }
 8853:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8854:                         if (!$pubkey) {
 8855:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8856:                         } else {
 8857:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8858:                         }
 8859:                         if (!$privkey) {
 8860:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8861:                         } else {
 8862:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8863:                         }
 8864:                         $chgtext .= '</ul>';
 8865:                     } elsif ($type eq 'emailusername') {
 8866:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8867:                             if (ref($types) eq 'ARRAY') {
 8868:                                 foreach my $type (@{$types}) {
 8869:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 8870:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 8871:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',$usertypes->{$type}).
 8872:                                                     '<ul>';
 8873:                                             foreach my $field (@{$infofields}) {
 8874:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 8875:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8876:                                                 }
 8877:                                             }
 8878:                                         }
 8879:                                         $chgtext .= '</ul>';
 8880:                                     } else {
 8881:                                         $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 />';
 8882:                                     }
 8883:                                 }
 8884:                             }
 8885:                         }
 8886:                     } elsif ($type eq 'notify') {
 8887:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8888:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8889:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8890:                                 if ($cancreate{'notify'}{'approval'}) {
 8891:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8892:                                 }
 8893:                             }
 8894:                         }
 8895:                     }
 8896:                     if ($chgtext) {
 8897:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8898:                     }
 8899:                 }
 8900:             }
 8901:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8902:                 my ($emailrules,$emailruleorder) =
 8903:                     &Apache::lonnet::inst_userrules($dom,'email');
 8904:                 my $chgtext = '<ul>';
 8905:                 foreach my $type (@email_rule) {
 8906:                     if (ref($emailrules->{$type}) eq 'HASH') {
 8907:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 8908:                     }
 8909:                 }
 8910:                 $chgtext .= '</ul>';
 8911:                 if (@email_rule > 0) {
 8912:                     $resulttext .= '<li>'.
 8913:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 8914:                                        $chgtext.
 8915:                                    '</li>';
 8916:                 } else {
 8917:                     $resulttext .= '<li>'.
 8918:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 8919:                                    '</li>';
 8920:                 }
 8921:             }
 8922:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 8923:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 8924:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8925:                 foreach my $type (@{$changes{'selfcreate'}}) {
 8926:                     my $typename = $type;
 8927:                     if (ref($usertypes) eq 'HASH') {
 8928:                         if ($usertypes->{$type} ne '') {
 8929:                             $typename = $usertypes->{$type};
 8930:                         }
 8931:                     }
 8932:                     my @modifiable;
 8933:                     $resulttext .= '<li>'.
 8934:                                     &mt('Self-creation of account by users with status: [_1]',
 8935:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 8936:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 8937:                     foreach my $field (@fields) {
 8938:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 8939:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 8940:                         }
 8941:                     }
 8942:                     if (@modifiable > 0) {
 8943:                         $resulttext .= join(', ',@modifiable);
 8944:                     } else {
 8945:                         $resulttext .= &mt('none');
 8946:                     }
 8947:                     $resulttext .= '</li>';
 8948:                 }
 8949:                 $resulttext .= '</ul></li>';
 8950:             }
 8951:             $resulttext .= '</ul>';
 8952:         } else {
 8953:             $resulttext = &mt('No changes made to self-creation settings');
 8954:         }
 8955:     } else {
 8956:         $resulttext = '<span class="LC_error">'.
 8957:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8958:     }
 8959:     if ($warningmsg ne '') {
 8960:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8961:     }
 8962:     return $resulttext;
 8963: }
 8964: 
 8965: sub process_captcha {
 8966:     my ($container,$changes,$newsettings,$current) = @_;
 8967:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 8968:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 8969:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 8970:         $newsettings->{'captcha'} = 'original';
 8971:     }
 8972:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 8973:         if ($container eq 'cancreate') {
 8974:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8975:                 push(@{$changes->{'cancreate'}},'captcha');
 8976:             } elsif (!defined($changes->{'cancreate'})) {
 8977:                 $changes->{'cancreate'} = ['captcha'];
 8978:             }
 8979:         } else {
 8980:             $changes->{'captcha'} = 1;
 8981:         }
 8982:     }
 8983:     my ($newpub,$newpriv,$currpub,$currpriv);
 8984:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 8985:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 8986:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 8987:         $newpub =~ s/\W//g;
 8988:         $newpriv =~ s/\W//g;
 8989:         $newsettings->{'recaptchakeys'} = {
 8990:                                              public  => $newpub,
 8991:                                              private => $newpriv,
 8992:                                           };
 8993:     }
 8994:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 8995:         $currpub = $current->{'recaptchakeys'}{'public'};
 8996:         $currpriv = $current->{'recaptchakeys'}{'private'};
 8997:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 8998:             $newsettings->{'recaptchakeys'} = {
 8999:                                                  public  => '',
 9000:                                                  private => '',
 9001:                                               }
 9002:         }
 9003:     }
 9004:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 9005:         if ($container eq 'cancreate') {
 9006:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9007:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 9008:             } elsif (!defined($changes->{'cancreate'})) {
 9009:                 $changes->{'cancreate'} = ['recaptchakeys'];
 9010:             }
 9011:         } else {
 9012:             $changes->{'recaptchakeys'} = 1;
 9013:         }
 9014:     }
 9015:     return;
 9016: }
 9017: 
 9018: sub modify_usermodification {
 9019:     my ($dom,%domconfig) = @_;
 9020:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 9021:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9022:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9023:             if ($key eq 'selfcreate') {
 9024:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 9025:             } else {  
 9026:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 9027:             }
 9028:         }
 9029:     }
 9030:     my @contexts = ('author','course');
 9031:     my %context_title = (
 9032:                            author => 'In author context',
 9033:                            course => 'In course context',
 9034:                         );
 9035:     my @fields = ('lastname','firstname','middlename','generation',
 9036:                   'permanentemail','id');
 9037:     my %roles = (
 9038:                   author => ['ca','aa'],
 9039:                   course => ['st','ep','ta','in','cr'],
 9040:                 );
 9041:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9042:     foreach my $context (@contexts) {
 9043:         foreach my $role (@{$roles{$context}}) {
 9044:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 9045:             foreach my $item (@fields) {
 9046:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 9047:                     $modifyhash{$context}{$role}{$item} = 1;
 9048:                 } else {
 9049:                     $modifyhash{$context}{$role}{$item} = 0;
 9050:                 }
 9051:             }
 9052:         }
 9053:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 9054:             foreach my $role (@{$roles{$context}}) {
 9055:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 9056:                     foreach my $field (@fields) {
 9057:                         if ($modifyhash{$context}{$role}{$field} ne 
 9058:                                 $curr_usermodification{$context}{$role}{$field}) {
 9059:                             push(@{$changes{$context}},$role);
 9060:                             last;
 9061:                         }
 9062:                     }
 9063:                 }
 9064:             }
 9065:         } else {
 9066:             foreach my $context (@contexts) {
 9067:                 foreach my $role (@{$roles{$context}}) {
 9068:                     push(@{$changes{$context}},$role);
 9069:                 }
 9070:             }
 9071:         }
 9072:     }
 9073:     my %usermodification_hash =  (
 9074:                                    usermodification => \%modifyhash,
 9075:                                  );
 9076:     my $putresult = &Apache::lonnet::put_dom('configuration',
 9077:                                              \%usermodification_hash,$dom);
 9078:     if ($putresult eq 'ok') {
 9079:         if (keys(%changes) > 0) {
 9080:             $resulttext = &mt('Changes made: ').'<ul>';
 9081:             foreach my $context (@contexts) {
 9082:                 if (ref($changes{$context}) eq 'ARRAY') {
 9083:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 9084:                     if (ref($changes{$context}) eq 'ARRAY') {
 9085:                         foreach my $role (@{$changes{$context}}) {
 9086:                             my $rolename;
 9087:                             if ($role eq 'cr') {
 9088:                                 $rolename = &mt('Custom');
 9089:                             } else {
 9090:                                 $rolename = &Apache::lonnet::plaintext($role);
 9091:                             }
 9092:                             my @modifiable;
 9093:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 9094:                             foreach my $field (@fields) {
 9095:                                 if ($modifyhash{$context}{$role}{$field}) {
 9096:                                     push(@modifiable,$fieldtitles{$field});
 9097:                                 }
 9098:                             }
 9099:                             if (@modifiable > 0) {
 9100:                                 $resulttext .= join(', ',@modifiable);
 9101:                             } else {
 9102:                                 $resulttext .= &mt('none'); 
 9103:                             }
 9104:                             $resulttext .= '</li>';
 9105:                         }
 9106:                         $resulttext .= '</ul></li>';
 9107:                     }
 9108:                 }
 9109:             }
 9110:             $resulttext .= '</ul>';
 9111:         } else {
 9112:             $resulttext = &mt('No changes made to user modification settings');
 9113:         }
 9114:     } else {
 9115:         $resulttext = '<span class="LC_error">'.
 9116:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9117:     }
 9118:     return $resulttext;
 9119: }
 9120: 
 9121: sub modify_defaults {
 9122:     my ($dom,$lastactref,%domconfig) = @_;
 9123:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 9124:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9125:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 9126:     my @authtypes = ('internal','krb4','krb5','localauth');
 9127:     foreach my $item (@items) {
 9128:         $newvalues{$item} = $env{'form.'.$item};
 9129:         if ($item eq 'auth_def') {
 9130:             if ($newvalues{$item} ne '') {
 9131:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 9132:                     push(@errors,$item);
 9133:                 }
 9134:             }
 9135:         } elsif ($item eq 'lang_def') {
 9136:             if ($newvalues{$item} ne '') {
 9137:                 if ($newvalues{$item} =~ /^(\w+)/) {
 9138:                     my $langcode = $1;
 9139:                     if ($langcode ne 'x_chef') {
 9140:                         if (code2language($langcode) eq '') {
 9141:                             push(@errors,$item);
 9142:                         }
 9143:                     }
 9144:                 } else {
 9145:                     push(@errors,$item);
 9146:                 }
 9147:             }
 9148:         } elsif ($item eq 'timezone_def') {
 9149:             if ($newvalues{$item} ne '') {
 9150:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 9151:                     push(@errors,$item);   
 9152:                 }
 9153:             }
 9154:         } elsif ($item eq 'datelocale_def') {
 9155:             if ($newvalues{$item} ne '') {
 9156:                 my @datelocale_ids = DateTime::Locale->ids();
 9157:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 9158:                     push(@errors,$item);
 9159:                 }
 9160:             }
 9161:         } elsif ($item eq 'portal_def') {
 9162:             if ($newvalues{$item} ne '') {
 9163:                 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])\/?$/) {
 9164:                     push(@errors,$item);
 9165:                 }
 9166:             }
 9167:         }
 9168:         if (grep(/^\Q$item\E$/,@errors)) {
 9169:             $newvalues{$item} = $domdefaults{$item};
 9170:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 9171:             $changes{$item} = 1;
 9172:         }
 9173:         $domdefaults{$item} = $newvalues{$item};
 9174:     }
 9175:     my %defaults_hash = (
 9176:                          defaults => \%newvalues,
 9177:                         );
 9178:     my $title = &defaults_titles();
 9179: 
 9180:     my $currinststatus;
 9181:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9182:         $currinststatus = $domconfig{'inststatus'};
 9183:     } else {
 9184:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9185:         $currinststatus = {
 9186:                              inststatustypes => $usertypes,
 9187:                              inststatusorder => $types,
 9188:                              inststatusguest => [],
 9189:                           };
 9190:     }
 9191:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
 9192:     my @allpos;
 9193:     my %guests;
 9194:     my %alltypes;
 9195:     my ($currtitles,$currguests,$currorder);
 9196:     if (ref($currinststatus) eq 'HASH') {
 9197:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
 9198:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
 9199:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
 9200:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
 9201:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
 9202:                     }
 9203:                 }
 9204:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
 9205:                     my $position = $env{'form.inststatus_pos_'.$type};
 9206:                     $position =~ s/\D+//g;
 9207:                     $allpos[$position] = $type;
 9208:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
 9209:                     $alltypes{$type} =~ s/`//g;
 9210:                     if ($env{'form.inststatus_guest_'.$type}) {
 9211:                         $guests{$type} = 1;
 9212:                     }
 9213:                 }
 9214:             }
 9215:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
 9216:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
 9217:             }
 9218:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
 9219:             $currtitles =~ s/,$//;
 9220:         }
 9221:     }
 9222:     if ($env{'form.addinststatus'}) {
 9223:         my $newtype = $env{'form.addinststatus'};
 9224:         $newtype =~ s/\W//g;
 9225:         unless (exists($alltypes{$newtype})) {
 9226:             if ($env{'form.addinststatus_guest'}) {
 9227:                 $guests{$newtype} = 1;
 9228:             }
 9229:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
 9230:             $alltypes{$newtype} =~ s/`//g; 
 9231:             my $position = $env{'form.addinststatus_pos'};
 9232:             $position =~ s/\D+//g;
 9233:             if ($position ne '') {
 9234:                 $allpos[$position] = $newtype;
 9235:             }
 9236:         }
 9237:     }
 9238:     my (@orderedstatus,@orderedguests);
 9239:     foreach my $type (@allpos) {
 9240:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
 9241:             push(@orderedstatus,$type);
 9242:             if ($guests{$type}) {
 9243:                 push(@orderedguests,$type);
 9244:             }
 9245:         }
 9246:     }
 9247:     foreach my $type (keys(%alltypes)) {
 9248:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
 9249:             delete($alltypes{$type});
 9250:         }
 9251:     }
 9252:     $defaults_hash{'inststatus'} = {
 9253:                                      inststatustypes => \%alltypes,
 9254:                                      inststatusorder => \@orderedstatus,
 9255:                                      inststatusguest => \@orderedguests,
 9256:                                    };
 9257:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
 9258:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 9259:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
 9260:         }
 9261:     }
 9262:     if ($currorder ne join(',',@orderedstatus)) {
 9263:         $changes{'inststatus'}{'inststatusorder'} = 1;
 9264:     }
 9265:     if ($currguests ne join(',',@orderedguests)) {
 9266:         $changes{'inststatus'}{'inststatusguest'} = 1;
 9267:     }
 9268:     my $newtitles;
 9269:     foreach my $item (@orderedstatus) {
 9270:         $newtitles .= $alltypes{$item}.',';
 9271:     }
 9272:     $newtitles =~ s/,$//;
 9273:     if ($currtitles ne $newtitles) {
 9274:         $changes{'inststatus'}{'inststatustypes'} = 1;
 9275:     }
 9276:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 9277:                                              $dom);
 9278:     if ($putresult eq 'ok') {
 9279:         if (keys(%changes) > 0) {
 9280:             $resulttext = &mt('Changes made:').'<ul>';
 9281:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 9282:             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";
 9283:             foreach my $item (sort(keys(%changes))) {
 9284:                 if ($item eq 'inststatus') {
 9285:                     if (ref($changes{'inststatus'}) eq 'HASH') {
 9286:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
 9287:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
 9288:                             foreach my $type (@orderedstatus) { 
 9289:                                 $resulttext .= $alltypes{$type}.', ';
 9290:                             }
 9291:                             $resulttext =~ s/, $//;
 9292:                             $resulttext .= '</li>';
 9293:                         }
 9294:                         if ($changes{'inststatus'}{'inststatusguest'}) {
 9295:                             $resulttext .= '<li>'; 
 9296:                             if (@orderedguests) {
 9297:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
 9298:                                 foreach my $type (@orderedguests) {
 9299:                                     $resulttext .= $alltypes{$type}.', ';
 9300:                                 }
 9301:                                 $resulttext =~ s/, $//;
 9302:                             } else {
 9303:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
 9304:                             }
 9305:                             $resulttext .= '</li>';
 9306:                         }
 9307:                     }
 9308:                 } else {
 9309:                     my $value = $env{'form.'.$item};
 9310:                     if ($value eq '') {
 9311:                         $value = &mt('none');
 9312:                     } elsif ($item eq 'auth_def') {
 9313:                         my %authnames = &authtype_names();
 9314:                         my %shortauth = (
 9315:                                           internal   => 'int',
 9316:                                           krb4       => 'krb4',
 9317:                                           krb5       => 'krb5',
 9318:                                           localauth  => 'loc',
 9319:                         );
 9320:                         $value = $authnames{$shortauth{$value}};
 9321:                     }
 9322:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 9323:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
 9324:                 }
 9325:             }
 9326:             $resulttext .= '</ul>';
 9327:             $mailmsgtext .= "\n";
 9328:             my $cachetime = 24*60*60;
 9329:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9330:             if (ref($lastactref) eq 'HASH') {
 9331:                 $lastactref->{'domdefaults'} = 1;
 9332:             }
 9333:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 9334:                 my $notify = 1;
 9335:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 9336:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 9337:                         $notify = 0;
 9338:                     }
 9339:                 }
 9340:                 if ($notify) {
 9341:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 9342:                                                "LON-CAPA Domain Settings Change - $dom",
 9343:                                                $mailmsgtext);
 9344:                 }
 9345:             }
 9346:         } else {
 9347:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 9348:         }
 9349:     } else {
 9350:         $resulttext = '<span class="LC_error">'.
 9351:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9352:     }
 9353:     if (@errors > 0) {
 9354:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 9355:         foreach my $item (@errors) {
 9356:             $resulttext .= ' "'.$title->{$item}.'",';
 9357:         }
 9358:         $resulttext =~ s/,$//;
 9359:     }
 9360:     return $resulttext;
 9361: }
 9362: 
 9363: sub modify_scantron {
 9364:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9365:     my ($resulttext,%confhash,%changes,$errors);
 9366:     my $custom = 'custom.tab';
 9367:     my $default = 'default.tab';
 9368:     my $servadm = $r->dir_config('lonAdmEMail');
 9369:     my ($configuserok,$author_ok,$switchserver) = 
 9370:         &config_check($dom,$confname,$servadm);
 9371:     if ($env{'form.scantronformat.filename'} ne '') {
 9372:         my $error;
 9373:         if ($configuserok eq 'ok') {
 9374:             if ($switchserver) {
 9375:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 9376:             } else {
 9377:                 if ($author_ok eq 'ok') {
 9378:                     my ($result,$scantronurl) =
 9379:                         &publishlogo($r,'upload','scantronformat',$dom,
 9380:                                      $confname,'scantron','','',$custom);
 9381:                     if ($result eq 'ok') {
 9382:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 9383:                         $changes{'scantronformat'} = 1;
 9384:                     } else {
 9385:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 9386:                     }
 9387:                 } else {
 9388:                     $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);
 9389:                 }
 9390:             }
 9391:         } else {
 9392:             $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);
 9393:         }
 9394:         if ($error) {
 9395:             &Apache::lonnet::logthis($error);
 9396:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9397:         }
 9398:     }
 9399:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 9400:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 9401:             if ($env{'form.scantronformat_del'}) {
 9402:                 $confhash{'scantron'}{'scantronformat'} = '';
 9403:                 $changes{'scantronformat'} = 1;
 9404:             }
 9405:         }
 9406:     }
 9407:     if (keys(%confhash) > 0) {
 9408:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 9409:                                                  $dom);
 9410:         if ($putresult eq 'ok') {
 9411:             if (keys(%changes) > 0) {
 9412:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 9413:                     $resulttext = &mt('Changes made:').'<ul>';
 9414:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 9415:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 9416:                     } else {
 9417:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 9418:                     }
 9419:                     $resulttext .= '</ul>';
 9420:                 } else {
 9421:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 9422:                 }
 9423:                 $resulttext .= '</ul>';
 9424:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9425:                 if (ref($lastactref) eq 'HASH') {
 9426:                     $lastactref->{'domainconfig'} = 1;
 9427:                 }
 9428:             } else {
 9429:                 $resulttext = &mt('No changes made to bubblesheet format file');
 9430:             }
 9431:         } else {
 9432:             $resulttext = '<span class="LC_error">'.
 9433:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 9434:         }
 9435:     } else {
 9436:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 9437:     }
 9438:     if ($errors) {
 9439:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9440:                        $errors.'</ul>';
 9441:     }
 9442:     return $resulttext;
 9443: }
 9444: 
 9445: sub modify_coursecategories {
 9446:     my ($dom,$lastactref,%domconfig) = @_;
 9447:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 9448:         $cathash);
 9449:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 9450:     my @catitems = ('unauth','auth');
 9451:     my @cattypes = ('std','domonly','codesrch','none');
 9452:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9453:         $cathash = $domconfig{'coursecategories'}{'cats'};
 9454:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 9455:             $changes{'togglecats'} = 1;
 9456:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 9457:         }
 9458:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 9459:             $changes{'categorize'} = 1;
 9460:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 9461:         }
 9462:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 9463:             $changes{'togglecatscomm'} = 1;
 9464:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 9465:         }
 9466:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 9467:             $changes{'categorizecomm'} = 1;
 9468:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 9469:         }
 9470:         foreach my $item (@catitems) {
 9471:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9472:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
 9473:                     $changes{$item} = 1;
 9474:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9475:                 }
 9476:             }
 9477:         }
 9478:     } else {
 9479:         $changes{'togglecats'} = 1;
 9480:         $changes{'categorize'} = 1;
 9481:         $changes{'togglecatscomm'} = 1;
 9482:         $changes{'categorizecomm'} = 1;
 9483:         $domconfig{'coursecategories'} = {
 9484:                                              togglecats => $env{'form.togglecats'},
 9485:                                              categorize => $env{'form.categorize'},
 9486:                                              togglecatscomm => $env{'form.togglecatscomm'},
 9487:                                              categorizecomm => $env{'form.categorizecomm'},
 9488:                                          };
 9489:         foreach my $item (@catitems) {
 9490:             if ($env{'form.coursecat_'.$item} ne 'std') {
 9491:                 $changes{$item} = 1;
 9492:             }
 9493:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9494:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9495:             }
 9496:         }
 9497:     }
 9498:     if (ref($cathash) eq 'HASH') {
 9499:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 9500:             push (@deletecategory,'instcode::0');
 9501:         }
 9502:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 9503:             push(@deletecategory,'communities::0');
 9504:         }
 9505:     }
 9506:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 9507:     if (ref($cathash) eq 'HASH') {
 9508:         if (@deletecategory > 0) {
 9509:             #FIXME Need to remove category from all courses using a deleted category 
 9510:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 9511:             foreach my $item (@deletecategory) {
 9512:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 9513:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 9514:                     $deletions{$item} = 1;
 9515:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 9516:                 }
 9517:             }
 9518:         }
 9519:         foreach my $item (keys(%{$cathash})) {
 9520:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9521:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 9522:                 $reorderings{$item} = 1;
 9523:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 9524:             }
 9525:             if ($env{'form.addcategory_name_'.$item} ne '') {
 9526:                 my $newcat = $env{'form.addcategory_name_'.$item};
 9527:                 my $newdepth = $depth+1;
 9528:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9529:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 9530:                 $adds{$newitem} = 1; 
 9531:             }
 9532:             if ($env{'form.subcat_'.$item} ne '') {
 9533:                 my $newcat = $env{'form.subcat_'.$item};
 9534:                 my $newdepth = $depth+1;
 9535:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9536:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 9537:                 $adds{$newitem} = 1;
 9538:             }
 9539:         }
 9540:     }
 9541:     if ($env{'form.instcode'} eq '1') {
 9542:         if (ref($cathash) eq 'HASH') {
 9543:             my $newitem = 'instcode::0';
 9544:             if ($cathash->{$newitem} eq '') {  
 9545:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9546:                 $adds{$newitem} = 1;
 9547:             }
 9548:         } else {
 9549:             my $newitem = 'instcode::0';
 9550:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9551:             $adds{$newitem} = 1;
 9552:         }
 9553:     }
 9554:     if ($env{'form.communities'} eq '1') {
 9555:         if (ref($cathash) eq 'HASH') {
 9556:             my $newitem = 'communities::0';
 9557:             if ($cathash->{$newitem} eq '') {
 9558:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9559:                 $adds{$newitem} = 1;
 9560:             }
 9561:         } else {
 9562:             my $newitem = 'communities::0';
 9563:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9564:             $adds{$newitem} = 1;
 9565:         }
 9566:     }
 9567:     if ($env{'form.addcategory_name'} ne '') {
 9568:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 9569:             ($env{'form.addcategory_name'} ne 'communities')) {
 9570:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 9571:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 9572:             $adds{$newitem} = 1;
 9573:         }
 9574:     }
 9575:     my $putresult;
 9576:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9577:         if (keys(%deletions) > 0) {
 9578:             foreach my $key (keys(%deletions)) {
 9579:                 if ($predelallitems{$key} ne '') {
 9580:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 9581:                 }
 9582:             }
 9583:         }
 9584:         my (@chkcats,@chktrails,%chkallitems);
 9585:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 9586:         if (ref($chkcats[0]) eq 'ARRAY') {
 9587:             my $depth = 0;
 9588:             my $chg = 0;
 9589:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 9590:                 my $name = $chkcats[0][$i];
 9591:                 my $item;
 9592:                 if ($name eq '') {
 9593:                     $chg ++;
 9594:                 } else {
 9595:                     $item = &escape($name).'::0';
 9596:                     if ($chg) {
 9597:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 9598:                     }
 9599:                     $depth ++; 
 9600:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 9601:                     $depth --;
 9602:                 }
 9603:             }
 9604:         }
 9605:     }
 9606:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9607:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 9608:         if ($putresult eq 'ok') {
 9609:             my %title = (
 9610:                          togglecats     => 'Show/Hide a course in catalog',
 9611:                          categorize     => 'Assign a category to a course',
 9612:                          togglecatscomm => 'Show/Hide a community in catalog',
 9613:                          categorizecomm => 'Assign a category to a community',
 9614:                         );
 9615:             my %level = (
 9616:                          dom  => 'set in Domain ("Modify Course/Community")',
 9617:                          crs  => 'set in Course ("Course Configuration")',
 9618:                          comm => 'set in Community ("Community Configuration")',
 9619:                          none     => 'No catalog',
 9620:                          std      => 'Standard catalog',
 9621:                          domonly  => 'Domain-only catalog',
 9622:                          codesrch => 'Code search form',
 9623:                         );
 9624:             $resulttext = &mt('Changes made:').'<ul>';
 9625:             if ($changes{'togglecats'}) {
 9626:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 9627:             }
 9628:             if ($changes{'categorize'}) {
 9629:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 9630:             }
 9631:             if ($changes{'togglecatscomm'}) {
 9632:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 9633:             }
 9634:             if ($changes{'categorizecomm'}) {
 9635:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 9636:             }
 9637:             if ($changes{'unauth'}) {
 9638:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
 9639:             }
 9640:             if ($changes{'auth'}) {
 9641:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
 9642:             }
 9643:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9644:                 my $cathash;
 9645:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9646:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 9647:                 } else {
 9648:                     $cathash = {};
 9649:                 } 
 9650:                 my (@cats,@trails,%allitems);
 9651:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 9652:                 if (keys(%deletions) > 0) {
 9653:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 9654:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 9655:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 9656:                     }
 9657:                     $resulttext .= '</ul></li>';
 9658:                 }
 9659:                 if (keys(%reorderings) > 0) {
 9660:                     my %sort_by_trail;
 9661:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 9662:                     foreach my $key (keys(%reorderings)) {
 9663:                         if ($allitems{$key} ne '') {
 9664:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9665:                         }
 9666:                     }
 9667:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9668:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9669:                     }
 9670:                     $resulttext .= '</ul></li>';
 9671:                 }
 9672:                 if (keys(%adds) > 0) {
 9673:                     my %sort_by_trail;
 9674:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 9675:                     foreach my $key (keys(%adds)) {
 9676:                         if ($allitems{$key} ne '') {
 9677:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9678:                         }
 9679:                     }
 9680:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9681:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9682:                     }
 9683:                     $resulttext .= '</ul></li>';
 9684:                 }
 9685:             }
 9686:             $resulttext .= '</ul>';
 9687:             if ($changes{'unauth'} || $changes{'auth'}) {
 9688:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9689:                 if (ref($lastactref) eq 'HASH') {
 9690:                     $lastactref->{'domainconfig'} = 1;
 9691:                 }
 9692:             }
 9693:         } else {
 9694:             $resulttext = '<span class="LC_error">'.
 9695:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9696:         }
 9697:     } else {
 9698:         $resulttext = &mt('No changes made to course and community categories');
 9699:     }
 9700:     return $resulttext;
 9701: }
 9702: 
 9703: sub modify_serverstatuses {
 9704:     my ($dom,%domconfig) = @_;
 9705:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 9706:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 9707:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 9708:     }
 9709:     my @pages = &serverstatus_pages();
 9710:     foreach my $type (@pages) {
 9711:         $newserverstatus{$type}{'namedusers'} = '';
 9712:         $newserverstatus{$type}{'machines'} = '';
 9713:         if (defined($env{'form.'.$type.'_namedusers'})) {
 9714:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 9715:             my @okusers;
 9716:             foreach my $user (@users) {
 9717:                 my ($uname,$udom) = split(/:/,$user);
 9718:                 if (($udom =~ /^$match_domain$/) &&   
 9719:                     (&Apache::lonnet::domain($udom)) &&
 9720:                     ($uname =~ /^$match_username$/)) {
 9721:                     if (!grep(/^\Q$user\E/,@okusers)) {
 9722:                         push(@okusers,$user);
 9723:                     }
 9724:                 }
 9725:             }
 9726:             if (@okusers > 0) {
 9727:                  @okusers = sort(@okusers);
 9728:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 9729:             }
 9730:         }
 9731:         if (defined($env{'form.'.$type.'_machines'})) {
 9732:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 9733:             my @okmachines;
 9734:             foreach my $ip (@machines) {
 9735:                 my @parts = split(/\./,$ip);
 9736:                 next if (@parts < 4);
 9737:                 my $badip = 0;
 9738:                 for (my $i=0; $i<4; $i++) {
 9739:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 9740:                         $badip = 1;
 9741:                         last;
 9742:                     }
 9743:                 }
 9744:                 if (!$badip) {
 9745:                     push(@okmachines,$ip);     
 9746:                 }
 9747:             }
 9748:             @okmachines = sort(@okmachines);
 9749:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 9750:         }
 9751:     }
 9752:     my %serverstatushash =  (
 9753:                                 serverstatuses => \%newserverstatus,
 9754:                             );
 9755:     foreach my $type (@pages) {
 9756:         foreach my $setting ('namedusers','machines') {
 9757:             my (@current,@new);
 9758:             if (ref($currserverstatus{$type}) eq 'HASH') {
 9759:                 if ($currserverstatus{$type}{$setting} ne '') { 
 9760:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 9761:                 }
 9762:             }
 9763:             if ($newserverstatus{$type}{$setting} ne '') {
 9764:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 9765:             }
 9766:             if (@current > 0) {
 9767:                 if (@new > 0) {
 9768:                     foreach my $item (@current) {
 9769:                         if (!grep(/^\Q$item\E$/,@new)) {
 9770:                             $changes{$type}{$setting} = 1;
 9771:                             last;
 9772:                         }
 9773:                     }
 9774:                     foreach my $item (@new) {
 9775:                         if (!grep(/^\Q$item\E$/,@current)) {
 9776:                             $changes{$type}{$setting} = 1;
 9777:                             last;
 9778:                         }
 9779:                     }
 9780:                 } else {
 9781:                     $changes{$type}{$setting} = 1;
 9782:                 }
 9783:             } elsif (@new > 0) {
 9784:                 $changes{$type}{$setting} = 1;
 9785:             }
 9786:         }
 9787:     }
 9788:     if (keys(%changes) > 0) {
 9789:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9790:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9791:                                                  \%serverstatushash,$dom);
 9792:         if ($putresult eq 'ok') {
 9793:             $resulttext .= &mt('Changes made:').'<ul>';
 9794:             foreach my $type (@pages) {
 9795:                 if (ref($changes{$type}) eq 'HASH') {
 9796:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 9797:                     if ($changes{$type}{'namedusers'}) {
 9798:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 9799:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 9800:                         } else {
 9801:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 9802:                         }
 9803:                     }
 9804:                     if ($changes{$type}{'machines'}) {
 9805:                         if ($newserverstatus{$type}{'machines'} eq '') {
 9806:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 9807:                         } else {
 9808:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 9809:                         }
 9810: 
 9811:                     }
 9812:                     $resulttext .= '</ul></li>';
 9813:                 }
 9814:             }
 9815:             $resulttext .= '</ul>';
 9816:         } else {
 9817:             $resulttext = '<span class="LC_error">'.
 9818:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 9819: 
 9820:         }
 9821:     } else {
 9822:         $resulttext = &mt('No changes made to access to server status pages');
 9823:     }
 9824:     return $resulttext;
 9825: }
 9826: 
 9827: sub modify_helpsettings {
 9828:     my ($r,$dom,$confname,%domconfig) = @_;
 9829:     my ($resulttext,$errors,%changes,%helphash);
 9830:     my %defaultchecked = ('submitbugs' => 'on');
 9831:     my @offon = ('off','on');
 9832:     my @toggles = ('submitbugs');
 9833:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 9834:         foreach my $item (@toggles) {
 9835:             if ($defaultchecked{$item} eq 'on') { 
 9836:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9837:                     if ($env{'form.'.$item} eq '0') {
 9838:                         $changes{$item} = 1;
 9839:                     }
 9840:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9841:                     $changes{$item} = 1;
 9842:                 }
 9843:             } elsif ($defaultchecked{$item} eq 'off') {
 9844:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9845:                     if ($env{'form.'.$item} eq '1') {
 9846:                         $changes{$item} = 1;
 9847:                     }
 9848:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9849:                     $changes{$item} = 1;
 9850:                 }
 9851:             }
 9852:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 9853:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 9854:             }
 9855:         }
 9856:     }
 9857:     my $putresult;
 9858:     if (keys(%changes) > 0) {
 9859:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 9860:         if ($putresult eq 'ok') {
 9861:             $resulttext = &mt('Changes made:').'<ul>';
 9862:             foreach my $item (sort(keys(%changes))) {
 9863:                 if ($item eq 'submitbugs') {
 9864:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 9865:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 9866:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 9867:                 }
 9868:             }
 9869:             $resulttext .= '</ul>';
 9870:         } else {
 9871:             $resulttext = &mt('No changes made to help settings');
 9872:             $errors .= '<li><span class="LC_error">'.
 9873:                        &mt('An error occurred storing the settings: [_1]',
 9874:                            $putresult).'</span></li>';
 9875:         }
 9876:     }
 9877:     if ($errors) {
 9878:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9879:                        $errors.'</ul>';
 9880:     }
 9881:     return $resulttext;
 9882: }
 9883: 
 9884: sub modify_coursedefaults {
 9885:     my ($dom,$lastactref,%domconfig) = @_;
 9886:     my ($resulttext,$errors,%changes,%defaultshash);
 9887:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 9888:     my @toggles = ('canuse_pdfforms');
 9889:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 9890:                    'uploadquota_community','uploadquota_textbook');
 9891:     my @types = ('official','unofficial','community','textbook');
 9892:     my %staticdefaults = (
 9893:                            anonsurvey_threshold => 10,
 9894:                            uploadquota          => 500,
 9895:                          );
 9896: 
 9897:     $defaultshash{'coursedefaults'} = {};
 9898: 
 9899:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 9900:         if ($domconfig{'coursedefaults'} eq '') {
 9901:             $domconfig{'coursedefaults'} = {};
 9902:         }
 9903:     }
 9904: 
 9905:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 9906:         foreach my $item (@toggles) {
 9907:             if ($defaultchecked{$item} eq 'on') {
 9908:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9909:                     ($env{'form.'.$item} eq '0')) {
 9910:                     $changes{$item} = 1;
 9911:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9912:                     $changes{$item} = 1;
 9913:                 }
 9914:             } elsif ($defaultchecked{$item} eq 'off') {
 9915:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9916:                     ($env{'form.'.$item} eq '1')) {
 9917:                     $changes{$item} = 1;
 9918:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9919:                     $changes{$item} = 1;
 9920:                 }
 9921:             }
 9922:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 9923:         }
 9924:         foreach my $item (@numbers) {
 9925:             my ($currdef,$newdef);
 9926:             $newdef = $env{'form.'.$item};
 9927:             if ($item eq 'anonsurvey_threshold') {
 9928:                 $currdef = $domconfig{'coursedefaults'}{$item};
 9929:                 $newdef =~ s/\D//g;
 9930:                 if ($newdef eq '' || $newdef < 1) {
 9931:                     $newdef = 1;
 9932:                 }
 9933:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 9934:             } else {
 9935:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 9936:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9937:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 9938:                 }
 9939:                 $newdef =~ s/[^\w.\-]//g;
 9940:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 9941:             }
 9942:             if ($currdef ne $newdef) {
 9943:                 my $staticdef;
 9944:                 if ($item eq 'anonsurvey_threshold') {
 9945:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
 9946:                         $changes{$item} = 1;
 9947:                     }
 9948:                 } else {
 9949:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
 9950:                         $changes{'uploadquota'} = 1;
 9951:                     }
 9952:                 }
 9953:             }
 9954:         }
 9955: 
 9956:         my $officialcreds = $env{'form.official_credits'};
 9957:         $officialcreds =~ s/[^\d.]+//g;
 9958:         my $unofficialcreds = $env{'form.unofficial_credits'};
 9959:         $unofficialcreds =~ s/[^\d.]+//g;
 9960:         my $textbookcreds = $env{'form.textbook_credits'};
 9961:         $textbookcreds =~ s/[^\d.]+//g;
 9962:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 9963:                 ($env{'form.coursecredits'} eq '1')) {
 9964:                 $changes{'coursecredits'} = 1;
 9965:         } else {
 9966:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 9967:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
 9968:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
 9969:                 $changes{'coursecredits'} = 1;
 9970:             }
 9971:         }
 9972:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 9973:             official   => $officialcreds,
 9974:             unofficial => $unofficialcreds,
 9975:             textbook   => $textbookcreds,
 9976:         }
 9977:     }
 9978:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9979:                                              $dom);
 9980:     if ($putresult eq 'ok') {
 9981:         if (keys(%changes) > 0) {
 9982:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9983:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || 
 9984:                 ($changes{'uploadquota'})) { 
 9985:                 if ($changes{'canuse_pdfforms'}) {
 9986:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 9987:                 }
 9988:                 if ($changes{'coursecredits'}) {
 9989:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 9990:                         $domdefaults{'officialcredits'} =
 9991:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 9992:                         $domdefaults{'unofficialcredits'} =
 9993:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 9994:                         $domdefaults{'textbookcredits'} =
 9995:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
 9996:                     }
 9997:                 }
 9998:                 if ($changes{'uploadquota'}) {
 9999:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10000:                         foreach my $type (@types) {
10001:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
10002:                         }
10003:                     }
10004:                 }
10005:                 my $cachetime = 24*60*60;
10006:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10007:                 if (ref($lastactref) eq 'HASH') {
10008:                     $lastactref->{'domdefaults'} = 1;
10009:                 }
10010:             }
10011:             $resulttext = &mt('Changes made:').'<ul>';
10012:             foreach my $item (sort(keys(%changes))) {
10013:                 if ($item eq 'canuse_pdfforms') {
10014:                     if ($env{'form.'.$item} eq '1') {
10015:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
10016:                     } else {
10017:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
10018:                     }
10019:                 } elsif ($item eq 'anonsurvey_threshold') {
10020:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
10021:                 } elsif ($item eq 'uploadquota') {
10022:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10023:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
10024:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
10025:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
10026:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
10027: 
10028:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
10029:                                        '</ul>'.
10030:                                        '</li>';
10031:                     } else {
10032:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
10033:                     }
10034:                 } elsif ($item eq 'coursecredits') {
10035:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10036:                         if (($domdefaults{'officialcredits'} eq '') &&
10037:                             ($domdefaults{'unofficialcredits'} eq '') &&
10038:                             ($domdefaults{'textbookcredits'} eq '')) {
10039:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10040:                         } else {
10041:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
10042:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
10043:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
10044:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
10045:                                            '</ul>'.
10046:                                            '</li>';
10047:                         }
10048:                     } else {
10049:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10050:                     }
10051:                 }
10052:             }
10053:             $resulttext .= '</ul>';
10054:         } else {
10055:             $resulttext = &mt('No changes made to course defaults');
10056:         }
10057:     } else {
10058:         $resulttext = '<span class="LC_error">'.
10059:             &mt('An error occurred: [_1]',$putresult).'</span>';
10060:     }
10061:     return $resulttext;
10062: }
10063: 
10064: sub modify_selfenrollment {
10065:     my ($dom,$lastactref,%domconfig) = @_;
10066:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
10067:     my @types = ('official','unofficial','community','textbook');
10068:     my %titles = &tool_titles();
10069:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
10070:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
10071:     $ordered{'default'} = ['types','registered','approval','limit'];
10072: 
10073:     my (%roles,%shown,%toplevel);
10074:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
10075: 
10076:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
10077:         if ($domconfig{'selfenrollment'} eq '') {
10078:             $domconfig{'selfenrollment'} = {};
10079:         }
10080:     }
10081:     %toplevel = (
10082:                   admin      => 'Configuration Rights',
10083:                   default    => 'Default settings',
10084:                   validation => 'Validation of self-enrollment requests',
10085:                 );
10086:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
10087: 
10088:     if (ref($ordered{'admin'}) eq 'ARRAY') {
10089:         foreach my $item (@{$ordered{'admin'}}) {
10090:             foreach my $type (@types) {
10091:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
10092:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
10093:                 } else {
10094:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
10095:                 }
10096:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
10097:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
10098:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
10099:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
10100:                             push(@{$changes{'admin'}{$type}},$item);
10101:                         }
10102:                     } else {
10103:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
10104:                             push(@{$changes{'admin'}{$type}},$item);
10105:                         }
10106:                     }
10107:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
10108:                     push(@{$changes{'admin'}{$type}},$item);
10109:                 }
10110:             }
10111:         }
10112:     }
10113: 
10114:     foreach my $item (@{$ordered{'default'}}) {
10115:         foreach my $type (@types) {
10116:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
10117:             if ($item eq 'types') {
10118:                 unless (($value eq 'all') || ($value eq 'dom')) {
10119:                     $value = '';
10120:                 }
10121:             } elsif ($item eq 'registered') {
10122:                 unless ($value eq '1') {
10123:                     $value = 0;
10124:                 }
10125:             } elsif ($item eq 'approval') {
10126:                 unless ($value =~ /^[012]$/) {
10127:                     $value = 0;
10128:                 }
10129:             } else {
10130:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10131:                     $value = 'none';
10132:                 }
10133:             }
10134:             $selfenrollhash{'default'}{$type}{$item} = $value;
10135:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
10136:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10137:                     if ($selfenrollhash{'default'}{$type}{$item} ne
10138:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
10139:                          push(@{$changes{'default'}{$type}},$item);
10140:                     }
10141:                 } else {
10142:                     push(@{$changes{'default'}{$type}},$item);
10143:                 }
10144:             } else {
10145:                 push(@{$changes{'default'}{$type}},$item);
10146:             }
10147:             if ($item eq 'limit') {
10148:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10149:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
10150:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
10151:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
10152:                     }
10153:                 } else {
10154:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
10155:                 }
10156:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10157:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
10158:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
10159:                          push(@{$changes{'default'}{$type}},'cap');
10160:                     }
10161:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
10162:                     push(@{$changes{'default'}{$type}},'cap');
10163:                 }
10164:             }
10165:         }
10166:     }
10167: 
10168:     foreach my $item (@{$itemsref}) {
10169:         if ($item eq 'fields') {
10170:             my @changed;
10171:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
10172:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
10173:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
10174:             }
10175:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10176:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
10177:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
10178:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
10179:                 } else {
10180:                     @changed = @{$selfenrollhash{'validation'}{$item}};
10181:                 }
10182:             } else {
10183:                 @changed = @{$selfenrollhash{'validation'}{$item}};
10184:             }
10185:             if (@changed) {
10186:                 if ($selfenrollhash{'validation'}{$item}) { 
10187:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
10188:                 } else {
10189:                     $changes{'validation'}{$item} = &mt('None');
10190:                 }
10191:             }
10192:         } else {
10193:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
10194:             if ($item eq 'markup') {
10195:                if ($env{'form.selfenroll_validation_'.$item}) {
10196:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10197:                }
10198:             }
10199:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10200:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
10201:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
10202:                 }
10203:             }
10204:         }
10205:     }
10206: 
10207:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
10208:                                              $dom);
10209:     if ($putresult eq 'ok') {
10210:         if (keys(%changes) > 0) {
10211:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10212:             $resulttext = &mt('Changes made:').'<ul>';
10213:             foreach my $key ('admin','default','validation') {
10214:                 if (ref($changes{$key}) eq 'HASH') {
10215:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
10216:                     if ($key eq 'validation') {
10217:                         foreach my $item (@{$itemsref}) {
10218:                             if (exists($changes{$key}{$item})) {
10219:                                 if ($item eq 'markup') {
10220:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10221:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
10222:                                 } else {  
10223:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10224:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
10225:                                 }
10226:                             }
10227:                         }
10228:                     } else {
10229:                         foreach my $type (@types) {
10230:                             if ($type eq 'community') {
10231:                                 $roles{'1'} = &mt('Community personnel');
10232:                             } else {
10233:                                 $roles{'1'} = &mt('Course personnel');
10234:                             }
10235:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
10236:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
10237:                                     if ($key eq 'admin') {
10238:                                         my @mgrdc = ();
10239:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10240:                                             foreach my $item (@{$ordered{'admin'}}) {
10241:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
10242:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
10243:                                                         push(@mgrdc,$item);
10244:                                                     }
10245:                                                 }
10246:                                             }
10247:                                             if (@mgrdc) {
10248:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
10249:                                             } else {
10250:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
10251:                                             }
10252:                                         }
10253:                                     } else {
10254:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10255:                                             foreach my $item (@{$ordered{$key}}) {
10256:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10257:                                                     $domdefaults{$type.'selfenroll'.$item} =
10258:                                                         $selfenrollhash{$key}{$type}{$item};
10259:                                                 }
10260:                                             }
10261:                                         }
10262:                                     }
10263:                                 }
10264:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
10265:                                 foreach my $item (@{$ordered{$key}}) {
10266:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10267:                                         $resulttext .= '<li>';
10268:                                         if ($key eq 'admin') {
10269:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
10270:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
10271:                                         } else {
10272:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
10273:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
10274:                                         }
10275:                                         $resulttext .= '</li>';
10276:                                     }
10277:                                 }
10278:                                 $resulttext .= '</ul></li>';
10279:                             }
10280:                         }
10281:                         $resulttext .= '</ul></li>'; 
10282:                     }
10283:                 }
10284:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
10285:                     my $cachetime = 24*60*60;
10286:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10287:                     if (ref($lastactref) eq 'HASH') {
10288:                         $lastactref->{'domdefaults'} = 1;
10289:                     }
10290:                 }
10291:             }
10292:             $resulttext .= '</ul>';
10293:         } else {
10294:             $resulttext = &mt('No changes made to self-enrollment settings');
10295:         }
10296:     } else {
10297:         $resulttext = '<span class="LC_error">'.
10298:             &mt('An error occurred: [_1]',$putresult).'</span>';
10299:     }
10300:     return $resulttext;
10301: }
10302: 
10303: sub modify_usersessions {
10304:     my ($dom,$lastactref,%domconfig) = @_;
10305:     my @hostingtypes = ('version','excludedomain','includedomain');
10306:     my @offloadtypes = ('primary','default');
10307:     my %types = (
10308:                   remote => \@hostingtypes,
10309:                   hosted => \@hostingtypes,
10310:                   spares => \@offloadtypes,
10311:                 );
10312:     my @prefixes = ('remote','hosted','spares');
10313:     my @lcversions = &Apache::lonnet::all_loncaparevs();
10314:     my (%by_ip,%by_location,@intdoms);
10315:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
10316:     my @locations = sort(keys(%by_location));
10317:     my (%defaultshash,%changes);
10318:     foreach my $prefix (@prefixes) {
10319:         $defaultshash{'usersessions'}{$prefix} = {};
10320:     }
10321:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10322:     my $resulttext;
10323:     my %iphost = &Apache::lonnet::get_iphost();
10324:     foreach my $prefix (@prefixes) {
10325:         next if ($prefix eq 'spares');
10326:         foreach my $type (@{$types{$prefix}}) {
10327:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
10328:             if ($type eq 'version') {
10329:                 my $value = $env{'form.'.$prefix.'_'.$type};
10330:                 my $okvalue;
10331:                 if ($value ne '') {
10332:                     if (grep(/^\Q$value\E$/,@lcversions)) {
10333:                         $okvalue = $value;
10334:                     }
10335:                 }
10336:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10337:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10338:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
10339:                             if ($inuse == 0) {
10340:                                 $changes{$prefix}{$type} = 1;
10341:                             } else {
10342:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
10343:                                     $changes{$prefix}{$type} = 1;
10344:                                 }
10345:                                 if ($okvalue ne '') {
10346:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10347:                                 } 
10348:                             }
10349:                         } else {
10350:                             if (($inuse == 1) && ($okvalue ne '')) {
10351:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10352:                                 $changes{$prefix}{$type} = 1;
10353:                             }
10354:                         }
10355:                     } else {
10356:                         if (($inuse == 1) && ($okvalue ne '')) {
10357:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10358:                             $changes{$prefix}{$type} = 1;
10359:                         }
10360:                     }
10361:                 } else {
10362:                     if (($inuse == 1) && ($okvalue ne '')) {
10363:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10364:                         $changes{$prefix}{$type} = 1;
10365:                     }
10366:                 }
10367:             } else {
10368:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
10369:                 my @okvals;
10370:                 foreach my $val (@vals) {
10371:                     if ($val =~ /:/) {
10372:                         my @items = split(/:/,$val);
10373:                         foreach my $item (@items) {
10374:                             if (ref($by_location{$item}) eq 'ARRAY') {
10375:                                 push(@okvals,$item);
10376:                             }
10377:                         }
10378:                     } else {
10379:                         if (ref($by_location{$val}) eq 'ARRAY') {
10380:                             push(@okvals,$val);
10381:                         }
10382:                     }
10383:                 }
10384:                 @okvals = sort(@okvals);
10385:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10386:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10387:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10388:                             if ($inuse == 0) {
10389:                                 $changes{$prefix}{$type} = 1; 
10390:                             } else {
10391:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10392:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
10393:                                 if (@changed > 0) {
10394:                                     $changes{$prefix}{$type} = 1;
10395:                                 }
10396:                             }
10397:                         } else {
10398:                             if ($inuse == 1) {
10399:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10400:                                 $changes{$prefix}{$type} = 1;
10401:                             }
10402:                         } 
10403:                     } else {
10404:                         if ($inuse == 1) {
10405:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10406:                             $changes{$prefix}{$type} = 1;
10407:                         }
10408:                     }
10409:                 } else {
10410:                     if ($inuse == 1) {
10411:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10412:                         $changes{$prefix}{$type} = 1;
10413:                     }
10414:                 }
10415:             }
10416:         }
10417:     }
10418: 
10419:     my @alldoms = &Apache::lonnet::all_domains();
10420:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10421:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
10422:     my $savespares;
10423: 
10424:     foreach my $lonhost (sort(keys(%servers))) {
10425:         my $serverhomeID =
10426:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
10427:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
10428:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
10429:         my %spareschg;
10430:         foreach my $type (@{$types{'spares'}}) {
10431:             my @okspares;
10432:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
10433:             foreach my $server (@checked) {
10434:                 if (&Apache::lonnet::hostname($server) ne '') {
10435:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
10436:                         unless (grep(/^\Q$server\E$/,@okspares)) {
10437:                             push(@okspares,$server);
10438:                         }
10439:                     }
10440:                 }
10441:             }
10442:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
10443:             my $newspare;
10444:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
10445:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
10446:                     $newspare = $new;
10447:                 }
10448:             }
10449:             my @spares;
10450:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
10451:                 @spares = sort(@okspares,$newspare);
10452:             } else {
10453:                 @spares = sort(@okspares);
10454:             }
10455:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
10456:             if (ref($spareid{$lonhost}) eq 'HASH') {
10457:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
10458:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
10459:                     if (@diffs > 0) {
10460:                         $spareschg{$type} = 1;
10461:                     }
10462:                 }
10463:             }
10464:         }
10465:         if (keys(%spareschg) > 0) {
10466:             $changes{'spares'}{$lonhost} = \%spareschg;
10467:         }
10468:     }
10469: 
10470:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
10471:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
10472:             if (ref($changes{'spares'}) eq 'HASH') {
10473:                 if (keys(%{$changes{'spares'}}) > 0) {
10474:                     $savespares = 1;
10475:                 }
10476:             }
10477:         } else {
10478:             $savespares = 1;
10479:         }
10480:     }
10481: 
10482:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
10483:     if ((keys(%changes) > 0) || ($savespares)) {
10484:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10485:                                                  $dom);
10486:         if ($putresult eq 'ok') {
10487:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10488:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
10489:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
10490:                 }
10491:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
10492:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
10493:                 }
10494:             }
10495:             my $cachetime = 24*60*60;
10496:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10497:             if (ref($lastactref) eq 'HASH') {
10498:                 $lastactref->{'domdefaults'} = 1;
10499:             }
10500:             if (keys(%changes) > 0) {
10501:                 my %lt = &usersession_titles();
10502:                 $resulttext = &mt('Changes made:').'<ul>';
10503:                 foreach my $prefix (@prefixes) {
10504:                     if (ref($changes{$prefix}) eq 'HASH') {
10505:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
10506:                         if ($prefix eq 'spares') {
10507:                             if (ref($changes{$prefix}) eq 'HASH') {
10508:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
10509:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
10510:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
10511:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
10512:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
10513:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
10514:                                         foreach my $type (@{$types{$prefix}}) {
10515:                                             if ($changes{$prefix}{$lonhost}{$type}) {
10516:                                                 my $offloadto = &mt('None');
10517:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
10518:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
10519:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
10520:                                                     }
10521:                                                 }
10522:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
10523:                                             }
10524:                                         }
10525:                                     }
10526:                                     $resulttext .= '</li>';
10527:                                 }
10528:                             }
10529:                         } else {
10530:                             foreach my $type (@{$types{$prefix}}) {
10531:                                 if (defined($changes{$prefix}{$type})) {
10532:                                     my $newvalue;
10533:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10534:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
10535:                                             if ($type eq 'version') {
10536:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
10537:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10538:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
10539:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
10540:                                                 }
10541:                                             }
10542:                                         }
10543:                                     }
10544:                                     if ($newvalue eq '') {
10545:                                         if ($type eq 'version') {
10546:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
10547:                                         } else {
10548:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
10549:                                         }
10550:                                     } else {
10551:                                         if ($type eq 'version') {
10552:                                             $newvalue .= ' '.&mt('(or later)'); 
10553:                                         }
10554:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
10555:                                     }
10556:                                 }
10557:                             }
10558:                         }
10559:                         $resulttext .= '</ul>';
10560:                     }
10561:                 }
10562:                 $resulttext .= '</ul>';
10563:             } else {
10564:                 $resulttext = $nochgmsg;
10565:             }
10566:         } else {
10567:             $resulttext = '<span class="LC_error">'.
10568:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10569:         }
10570:     } else {
10571:         $resulttext = $nochgmsg;
10572:     }
10573:     return $resulttext;
10574: }
10575: 
10576: sub modify_loadbalancing {
10577:     my ($dom,%domconfig) = @_;
10578:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
10579:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
10580:     my ($othertitle,$usertypes,$types) =
10581:         &Apache::loncommon::sorted_inst_types($dom);
10582:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10583:     my @sparestypes = ('primary','default');
10584:     my %typetitles = &sparestype_titles();
10585:     my $resulttext;
10586:     my (%currbalancer,%currtargets,%currrules,%existing);
10587:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10588:         %existing = %{$domconfig{'loadbalancing'}};
10589:     }
10590:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
10591:                               \%currtargets,\%currrules);
10592:     my ($saveloadbalancing,%defaultshash,%changes);
10593:     my ($alltypes,$othertypes,$titles) =
10594:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
10595:     my %ruletitles = &offloadtype_text();
10596:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
10597:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
10598:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
10599:         if ($balancer eq '') {
10600:             next;
10601:         }
10602:         if (!exists($servers{$balancer})) {
10603:             if (exists($currbalancer{$balancer})) {
10604:                 push(@{$changes{'delete'}},$balancer);
10605:             }
10606:             next;
10607:         }
10608:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
10609:             push(@{$changes{'delete'}},$balancer);
10610:             next;
10611:         }
10612:         if (!exists($currbalancer{$balancer})) {
10613:             push(@{$changes{'add'}},$balancer);
10614:         }
10615:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
10616:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
10617:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
10618:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10619:             $saveloadbalancing = 1;
10620:         }
10621:         foreach my $sparetype (@sparestypes) {
10622:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
10623:             my @offloadto;
10624:             foreach my $target (@targets) {
10625:                 if (($servers{$target}) && ($target ne $balancer)) {
10626:                     if ($sparetype eq 'default') {
10627:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
10628:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
10629:                         }
10630:                     }
10631:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
10632:                         push(@offloadto,$target);
10633:                     }
10634:                 }
10635:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
10636:             }
10637:         }
10638:         if (ref($currtargets{$balancer}) eq 'HASH') {
10639:             foreach my $sparetype (@sparestypes) {
10640:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
10641:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
10642:                     if (@targetdiffs > 0) {
10643:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10644:                     }
10645:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10646:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10647:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10648:                     }
10649:                 }
10650:             }
10651:         } else {
10652:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
10653:                 foreach my $sparetype (@sparestypes) {
10654:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10655:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10656:                             $changes{'curr'}{$balancer}{'targets'} = 1;
10657:                         }
10658:                     }
10659:                 }
10660:             }
10661:         }
10662:         my $ishomedom;
10663:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
10664:             $ishomedom = 1;
10665:         }
10666:         if (ref($alltypes) eq 'ARRAY') {
10667:             foreach my $type (@{$alltypes}) {
10668:                 my $rule;
10669:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
10670:                          (!$ishomedom)) {
10671:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
10672:                 }
10673:                 if ($rule eq 'specific') {
10674:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
10675:                 }
10676:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
10677:                 if (ref($currrules{$balancer}) eq 'HASH') {
10678:                     if ($rule ne $currrules{$balancer}{$type}) {
10679:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10680:                     }
10681:                 } elsif ($rule ne '') {
10682:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10683:                 }
10684:             }
10685:         }
10686:     }
10687:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
10688:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
10689:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
10690:             $defaultshash{'loadbalancing'} = {};
10691:         }
10692:         my $putresult = &Apache::lonnet::put_dom('configuration',
10693:                                                  \%defaultshash,$dom);
10694:         if ($putresult eq 'ok') {
10695:             if (keys(%changes) > 0) {
10696:                 if (ref($changes{'delete'}) eq 'ARRAY') {
10697:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
10698:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
10699:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10700:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10701:                     }
10702:                 }
10703:                 if (ref($changes{'add'}) eq 'ARRAY') {
10704:                     foreach my $balancer (sort(@{$changes{'add'}})) {
10705:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
10706:                     }
10707:                 }
10708:                 if (ref($changes{'curr'}) eq 'HASH') {
10709:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
10710:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
10711:                             if ($changes{'curr'}{$balancer}{'targets'}) {
10712:                                 my %offloadstr;
10713:                                 foreach my $sparetype (@sparestypes) {
10714:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10715:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10716:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10717:                                         }
10718:                                     }
10719:                                 }
10720:                                 if (keys(%offloadstr) == 0) {
10721:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
10722:                                 } else {
10723:                                     my $showoffload;
10724:                                     foreach my $sparetype (@sparestypes) {
10725:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
10726:                                         if (defined($offloadstr{$sparetype})) {
10727:                                             $showoffload .= $offloadstr{$sparetype};
10728:                                         } else {
10729:                                             $showoffload .= &mt('None');
10730:                                         }
10731:                                         $showoffload .= ('&nbsp;'x3);
10732:                                     }
10733:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
10734:                                 }
10735:                             }
10736:                         }
10737:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
10738:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
10739:                                 foreach my $type (@{$alltypes}) {
10740:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
10741:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
10742:                                         my $balancetext;
10743:                                         if ($rule eq '') {
10744:                                             $balancetext =  $ruletitles{'default'};
10745:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
10746:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
10747:                                             $balancetext =  $ruletitles{$rule};
10748:                                         } else {
10749:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
10750:                                         }
10751:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
10752:                                     }
10753:                                 }
10754:                             }
10755:                         }
10756:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10757:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10758:                     }
10759:                 }
10760:                 if ($resulttext ne '') {
10761:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
10762:                 } else {
10763:                     $resulttext = $nochgmsg;
10764:                 }
10765:             } else {
10766:                 $resulttext = $nochgmsg;
10767:             }
10768:         } else {
10769:             $resulttext = '<span class="LC_error">'.
10770:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10771:         }
10772:     } else {
10773:         $resulttext = $nochgmsg;
10774:     }
10775:     return $resulttext;
10776: }
10777: 
10778: sub recurse_check {
10779:     my ($chkcats,$categories,$depth,$name) = @_;
10780:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
10781:         my $chg = 0;
10782:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
10783:             my $category = $chkcats->[$depth]{$name}[$j];
10784:             my $item;
10785:             if ($category eq '') {
10786:                 $chg ++;
10787:             } else {
10788:                 my $deeper = $depth + 1;
10789:                 $item = &escape($category).':'.&escape($name).':'.$depth;
10790:                 if ($chg) {
10791:                     $categories->{$item} -= $chg;
10792:                 }
10793:                 &recurse_check($chkcats,$categories,$deeper,$category);
10794:                 $deeper --;
10795:             }
10796:         }
10797:     }
10798:     return;
10799: }
10800: 
10801: sub recurse_cat_deletes {
10802:     my ($item,$coursecategories,$deletions) = @_;
10803:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10804:     my $subdepth = $depth + 1;
10805:     if (ref($coursecategories) eq 'HASH') {
10806:         foreach my $subitem (keys(%{$coursecategories})) {
10807:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
10808:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
10809:                 delete($coursecategories->{$subitem});
10810:                 $deletions->{$subitem} = 1;
10811:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
10812:             }
10813:         }
10814:     }
10815:     return;
10816: }
10817: 
10818: sub get_active_dcs {
10819:     my ($dom) = @_;
10820:     my $now = time;
10821:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
10822:     my %domcoords;
10823:     my $numdcs = 0;
10824:     foreach my $server (keys(%dompersonnel)) {
10825:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
10826:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
10827:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
10828:         }
10829:     }
10830:     return %domcoords;
10831: }
10832: 
10833: sub active_dc_picker {
10834:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
10835:     my %domcoords = &get_active_dcs($dom);
10836:     my @domcoord = keys(%domcoords);
10837:     if (keys(%currhash)) {
10838:         foreach my $dc (keys(%currhash)) {
10839:             unless (exists($domcoords{$dc})) {
10840:                 push(@domcoord,$dc);
10841:             }
10842:         }
10843:     }
10844:     @domcoord = sort(@domcoord);
10845:     my $numdcs = scalar(@domcoord);
10846:     my $rows = 0;
10847:     my $table;
10848:     if ($numdcs > 1) {
10849:         $table = '<table>';
10850:         for (my $i=0; $i<@domcoord; $i++) {
10851:             my $rem = $i%($numinrow);
10852:             if ($rem == 0) {
10853:                 if ($i > 0) {
10854:                     $table .= '</tr>';
10855:                 }
10856:                 $table .= '<tr>';
10857:                 $rows ++;
10858:             }
10859:             my $check = '';
10860:             if ($inputtype eq 'radio') {
10861:                 if (keys(%currhash) == 0) {
10862:                     if (!$i) {
10863:                         $check = ' checked="checked"';
10864:                     }
10865:                 } elsif (exists($currhash{$domcoord[$i]})) {
10866:                     $check = ' checked="checked"';
10867:                 }
10868:             } else {
10869:                 if (exists($currhash{$domcoord[$i]})) {
10870:                     $check = ' checked="checked"';
10871:                 }
10872:             }
10873:             if ($i == @domcoord - 1) {
10874:                 my $colsleft = $numinrow - $rem;
10875:                 if ($colsleft > 1) {
10876:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
10877:                 } else {
10878:                     $table .= '<td class="LC_left_item">';
10879:                 }
10880:             } else {
10881:                 $table .= '<td class="LC_left_item">';
10882:             }
10883:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
10884:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10885:             $table .= '<span class="LC_nobreak"><label>'.
10886:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
10887:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
10888:             if ($user ne $dcname.':'.$dcdom) {
10889:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10890:             }
10891:             $table .= '</label></span></td>';
10892:         }
10893:         $table .= '</tr></table>';
10894:     } elsif ($numdcs == 1) {
10895:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
10896:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10897:         if ($inputtype eq 'radio') {
10898:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
10899:             if ($user ne $dcname.':'.$dcdom) {
10900:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10901:             }
10902:         } else {
10903:             my $check;
10904:             if (exists($currhash{$domcoord[0]})) {
10905:                 $check = ' checked="checked"';
10906:             }
10907:             $table .= '<span class="LC_nobreak"><label>'.
10908:                       '<input type="checkbox" name="'.$name.'" '.
10909:                       'value="'.$domcoord[0].'"'.$check.' />'.$user;
10910:             if ($user ne $dcname.':'.$dcdom) {
10911:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10912:             }
10913:             $table .= '</label></span>';
10914:             $rows ++;
10915:         }
10916:     }
10917:     return ($numdcs,$table,$rows);
10918: }
10919: 
10920: sub usersession_titles {
10921:     return &Apache::lonlocal::texthash(
10922:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
10923:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
10924:                spares => 'Servers offloaded to, when busy',
10925:                version => 'LON-CAPA version requirement',
10926:                excludedomain => 'Allow all, but exclude specific domains',
10927:                includedomain => 'Deny all, but include specific domains',
10928:                primary => 'Primary (checked first)',
10929:                default => 'Default',
10930:            );
10931: }
10932: 
10933: sub id_for_thisdom {
10934:     my (%servers) = @_;
10935:     my %altids;
10936:     foreach my $server (keys(%servers)) {
10937:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
10938:         if ($serverhome ne $server) {
10939:             $altids{$serverhome} = $server;
10940:         }
10941:     }
10942:     return %altids;
10943: }
10944: 
10945: sub count_servers {
10946:     my ($currbalancer,%servers) = @_;
10947:     my (@spares,$numspares);
10948:     foreach my $lonhost (sort(keys(%servers))) {
10949:         next if ($currbalancer eq $lonhost);
10950:         push(@spares,$lonhost);
10951:     }
10952:     if ($currbalancer) {
10953:         $numspares = scalar(@spares);
10954:     } else {
10955:         $numspares = scalar(@spares) - 1;
10956:     }
10957:     return ($numspares,@spares);
10958: }
10959: 
10960: sub lonbalance_targets_js {
10961:     my ($dom,$types,$servers,$settings) = @_;
10962:     my $select = &mt('Select');
10963:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
10964:     if (ref($servers) eq 'HASH') {
10965:         $alltargets = join("','",sort(keys(%{$servers})));
10966:         my @homedoms;
10967:         foreach my $server (sort(keys(%{$servers}))) {
10968:             if (&Apache::lonnet::host_domain($server) eq $dom) {
10969:                 push(@homedoms,'1');
10970:             } else {
10971:                 push(@homedoms,'0');
10972:             }
10973:         }
10974:         $allishome = join("','",@homedoms);
10975:     }
10976:     if (ref($types) eq 'ARRAY') {
10977:         if (@{$types} > 0) {
10978:             @alltypes = @{$types};
10979:         }
10980:     }
10981:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
10982:     $allinsttypes = join("','",@alltypes);
10983:     my (%currbalancer,%currtargets,%currrules,%existing);
10984:     if (ref($settings) eq 'HASH') {
10985:         %existing = %{$settings};
10986:     }
10987:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
10988:                               \%currtargets,\%currrules);
10989:     my $balancers = join("','",sort(keys(%currbalancer)));
10990:     return <<"END";
10991: 
10992: <script type="text/javascript">
10993: // <![CDATA[
10994: 
10995: currBalancers = new Array('$balancers');
10996: 
10997: function toggleTargets(balnum) {
10998:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
10999:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
11000:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
11001:     var prevbalancer = prevhostitem.value;
11002:     var baltotal = document.getElementById('loadbalancing_total').value;
11003:     prevhostitem.value = balancer;
11004:     if (prevbalancer != '') {
11005:         var prevIdx = currBalancers.indexOf(prevbalancer);
11006:         if (prevIdx != -1) {
11007:             currBalancers.splice(prevIdx,1);
11008:         }
11009:     }
11010:     if (balancer == '') {
11011:         hideSpares(balnum);
11012:     } else {
11013:         var currIdx = currBalancers.indexOf(balancer);
11014:         if (currIdx == -1) {
11015:             currBalancers.push(balancer);
11016:         }
11017:         var homedoms = new Array('$allishome');
11018:         var ishomedom = homedoms[lonhostitem.selectedIndex];
11019:         showSpares(balancer,ishomedom,balnum);
11020:     }
11021:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
11022:     return;
11023: }
11024: 
11025: function showSpares(balancer,ishomedom,balnum) {
11026:     var alltargets = new Array('$alltargets');
11027:     var insttypes = new Array('$allinsttypes');
11028:     var offloadtypes = new Array('primary','default');
11029: 
11030:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
11031:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
11032:  
11033:     for (var i=0; i<offloadtypes.length; i++) {
11034:         var count = 0;
11035:         for (var j=0; j<alltargets.length; j++) {
11036:             if (alltargets[j] != balancer) {
11037:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
11038:                 item.value = alltargets[j];
11039:                 item.style.textAlign='left';
11040:                 item.style.textFace='normal';
11041:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
11042:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
11043:                     item.disabled = '';
11044:                 } else {
11045:                     item.disabled = 'disabled';
11046:                     item.checked = false;
11047:                 }
11048:                 count ++;
11049:             }
11050:         }
11051:     }
11052:     for (var k=0; k<insttypes.length; k++) {
11053:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
11054:             if (ishomedom == 1) {
11055:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11056:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11057:             } else {
11058:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11059:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11060:             }
11061:         } else {
11062:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11063:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11064:         }
11065:         if ((insttypes[k] != '_LC_external') && 
11066:             ((insttypes[k] != '_LC_internetdom') ||
11067:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
11068:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
11069:             item.options.length = 0;
11070:             item.options[0] = new Option("","",true,true);
11071:             var idx = 0;
11072:             for (var m=0; m<alltargets.length; m++) {
11073:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
11074:                     idx ++;
11075:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11076:                 }
11077:             }
11078:         }
11079:     }
11080:     return;
11081: }
11082: 
11083: function hideSpares(balnum) {
11084:     var alltargets = new Array('$alltargets');
11085:     var insttypes = new Array('$allinsttypes');
11086:     var offloadtypes = new Array('primary','default');
11087: 
11088:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
11089:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
11090: 
11091:     var total = alltargets.length - 1;
11092:     for (var i=0; i<offloadtypes; i++) {
11093:         for (var j=0; j<total; j++) {
11094:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
11095:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
11096:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
11097:         }
11098:     }
11099:     for (var k=0; k<insttypes.length; k++) {
11100:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11101:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11102:         if (insttypes[k] != '_LC_external') {
11103:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
11104:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
11105:         }
11106:     }
11107:     return;
11108: }
11109: 
11110: function checkOffloads(item,balnum,type) {
11111:     var alltargets = new Array('$alltargets');
11112:     var offloadtypes = new Array('primary','default');
11113:     if (item.checked) {
11114:         var total = alltargets.length - 1;
11115:         var other;
11116:         if (type == offloadtypes[0]) {
11117:             other = offloadtypes[1];
11118:         } else {
11119:             other = offloadtypes[0];
11120:         }
11121:         for (var i=0; i<total; i++) {
11122:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
11123:             if (server == item.value) {
11124:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
11125:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
11126:                 }
11127:             }
11128:         }
11129:     }
11130:     return;
11131: }
11132: 
11133: function singleServerToggle(balnum,type) {
11134:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
11135:     if (offloadtoSelIdx == 0) {
11136:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
11137:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11138: 
11139:     } else {
11140:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
11141:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11142:     }
11143:     return;
11144: }
11145: 
11146: function balanceruleChange(formname,balnum,type) {
11147:     if (type == '_LC_external') {
11148:         return;
11149:     }
11150:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
11151:     for (var i=0; i<typesRules.length; i++) {
11152:         if (formname.elements[typesRules[i]].checked) {
11153:             if (formname.elements[typesRules[i]].value != 'specific') {
11154:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
11155:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11156:             } else {
11157:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11158:             }
11159:         }
11160:     }
11161:     return;
11162: }
11163: 
11164: function balancerDeleteChange(balnum) {
11165:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11166:     var baltotal = document.getElementById('loadbalancing_total').value;
11167:     var addtarget;
11168:     var removetarget;
11169:     var action = 'delete';
11170:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
11171:         var lonhost = hostitem.value;
11172:         var currIdx = currBalancers.indexOf(lonhost);
11173:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
11174:             if (currIdx != -1) {
11175:                 currBalancers.splice(currIdx,1);
11176:             }
11177:             addtarget = lonhost;
11178:         } else {
11179:             if (currIdx == -1) {
11180:                 currBalancers.push(lonhost);
11181:             }
11182:             removetarget = lonhost;
11183:             action = 'undelete';
11184:         }
11185:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
11186:     }
11187:     return;
11188: }
11189: 
11190: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
11191:     if (baltotal > 1) {
11192:         var offloadtypes = new Array('primary','default');
11193:         var alltargets = new Array('$alltargets');
11194:         var insttypes = new Array('$allinsttypes');
11195:         for (var i=0; i<baltotal; i++) {
11196:             if (i != balnum) {
11197:                 for (var j=0; j<offloadtypes.length; j++) {
11198:                     var total = alltargets.length - 1;
11199:                     for (var k=0; k<total; k++) {
11200:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
11201:                         var server = serveritem.value;
11202:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
11203:                             if (server == addtarget) {
11204:                                 serveritem.disabled = '';
11205:                             }
11206:                         }
11207:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11208:                             if (server == removetarget) {
11209:                                 serveritem.disabled = 'disabled';
11210:                                 serveritem.checked = false;
11211:                             }
11212:                         }
11213:                     }
11214:                 }
11215:                 for (var j=0; j<insttypes.length; j++) {
11216:                     if (insttypes[j] != '_LC_external') {
11217:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
11218:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
11219:                             var currSel = singleserver.selectedIndex;
11220:                             var currVal = singleserver.options[currSel].value;
11221:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
11222:                                 var numoptions = singleserver.options.length;
11223:                                 var needsnew = 1;
11224:                                 for (var k=0; k<numoptions; k++) {
11225:                                     if (singleserver.options[k] == addtarget) {
11226:                                         needsnew = 0;
11227:                                         break;
11228:                                     }
11229:                                 }
11230:                                 if (needsnew == 1) {
11231:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
11232:                                 }
11233:                             }
11234:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11235:                                 singleserver.options.length = 0;
11236:                                 if ((currVal) && (currVal != removetarget)) {
11237:                                     singleserver.options[0] = new Option("","",false,false);
11238:                                 } else {
11239:                                     singleserver.options[0] = new Option("","",true,true);
11240:                                 }
11241:                                 var idx = 0;
11242:                                 for (var m=0; m<alltargets.length; m++) {
11243:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
11244:                                         idx ++;
11245:                                         if (currVal == alltargets[m]) {
11246:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
11247:                                         } else {
11248:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11249:                                         }
11250:                                     }
11251:                                 }
11252:                             }
11253:                         }
11254:                     }
11255:                 }
11256:             }
11257:         }
11258:     }
11259:     return;
11260: }
11261: 
11262: // ]]>
11263: </script>
11264: 
11265: END
11266: }
11267: 
11268: sub new_spares_js {
11269:     my @sparestypes = ('primary','default');
11270:     my $types = join("','",@sparestypes);
11271:     my $select = &mt('Select');
11272:     return <<"END";
11273: 
11274: <script type="text/javascript">
11275: // <![CDATA[
11276: 
11277: function updateNewSpares(formname,lonhost) {
11278:     var types = new Array('$types');
11279:     var include = new Array();
11280:     var exclude = new Array();
11281:     for (var i=0; i<types.length; i++) {
11282:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
11283:         for (var j=0; j<spareboxes.length; j++) {
11284:             if (formname.elements[spareboxes[j]].checked) {
11285:                 exclude.push(formname.elements[spareboxes[j]].value);
11286:             } else {
11287:                 include.push(formname.elements[spareboxes[j]].value);
11288:             }
11289:         }
11290:     }
11291:     for (var i=0; i<types.length; i++) {
11292:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
11293:         var selIdx = newSpare.selectedIndex;
11294:         var currnew = newSpare.options[selIdx].value;
11295:         var okSpares = new Array();
11296:         for (var j=0; j<newSpare.options.length; j++) {
11297:             var possible = newSpare.options[j].value;
11298:             if (possible != '') {
11299:                 if (exclude.indexOf(possible) == -1) {
11300:                     okSpares.push(possible);
11301:                 } else {
11302:                     if (currnew == possible) {
11303:                         selIdx = 0;
11304:                     }
11305:                 }
11306:             }
11307:         }
11308:         for (var k=0; k<include.length; k++) {
11309:             if (okSpares.indexOf(include[k]) == -1) {
11310:                 okSpares.push(include[k]);
11311:             }
11312:         }
11313:         okSpares.sort();
11314:         newSpare.options.length = 0;
11315:         if (selIdx == 0) {
11316:             newSpare.options[0] = new Option("$select","",true,true);
11317:         } else {
11318:             newSpare.options[0] = new Option("$select","",false,false);
11319:         }
11320:         for (var m=0; m<okSpares.length; m++) {
11321:             var idx = m+1;
11322:             var selThis = 0;
11323:             if (selIdx != 0) {
11324:                 if (okSpares[m] == currnew) {
11325:                     selThis = 1;
11326:                 }
11327:             }
11328:             if (selThis == 1) {
11329:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
11330:             } else {
11331:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
11332:             }
11333:         }
11334:     }
11335:     return;
11336: }
11337: 
11338: function checkNewSpares(lonhost,type) {
11339:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
11340:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
11341:     if (chosen != '') { 
11342:         var othertype;
11343:         var othernewSpare;
11344:         if (type == 'primary') {
11345:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
11346:         }
11347:         if (type == 'default') {
11348:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
11349:         }
11350:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
11351:             othernewSpare.selectedIndex = 0;
11352:         }
11353:     }
11354:     return;
11355: }
11356: 
11357: // ]]>
11358: </script>
11359: 
11360: END
11361: 
11362: }
11363: 
11364: sub common_domprefs_js {
11365:     return <<"END";
11366: 
11367: <script type="text/javascript">
11368: // <![CDATA[
11369: 
11370: function getIndicesByName(formname,item) {
11371:     var group = new Array();
11372:     for (var i=0;i<formname.elements.length;i++) {
11373:         if (formname.elements[i].name == item) {
11374:             group.push(formname.elements[i].id);
11375:         }
11376:     }
11377:     return group;
11378: }
11379: 
11380: // ]]>
11381: </script>
11382: 
11383: END
11384: 
11385: }
11386: 
11387: sub recaptcha_js {
11388:     my %lt = &captcha_phrases();
11389:     return <<"END";
11390: 
11391: <script type="text/javascript">
11392: // <![CDATA[
11393: 
11394: function updateCaptcha(caller,context) {
11395:     var privitem;
11396:     var pubitem;
11397:     var privtext;
11398:     var pubtext;
11399:     if (document.getElementById(context+'_recaptchapub')) {
11400:         pubitem = document.getElementById(context+'_recaptchapub');
11401:     } else {
11402:         return;
11403:     }
11404:     if (document.getElementById(context+'_recaptchapriv')) {
11405:         privitem = document.getElementById(context+'_recaptchapriv');
11406:     } else {
11407:         return;
11408:     }
11409:     if (document.getElementById(context+'_recaptchapubtxt')) {
11410:         pubtext = document.getElementById(context+'_recaptchapubtxt');
11411:     } else {
11412:         return;
11413:     }
11414:     if (document.getElementById(context+'_recaptchaprivtxt')) {
11415:         privtext = document.getElementById(context+'_recaptchaprivtxt');
11416:     } else {
11417:         return;
11418:     }
11419:     if (caller.checked) {
11420:         if (caller.value == 'recaptcha') {
11421:             pubitem.type = 'text';
11422:             privitem.type = 'text';
11423:             pubitem.size = '40';
11424:             privitem.size = '40';
11425:             pubtext.innerHTML = "$lt{'pub'}";
11426:             privtext.innerHTML = "$lt{'priv'}";
11427:         } else {
11428:             pubitem.type = 'hidden';
11429:             privitem.type = 'hidden';
11430:             pubtext.innerHTML = '';
11431:             privtext.innerHTML = '';
11432:         }
11433:     }
11434:     return;
11435: }
11436: 
11437: // ]]>
11438: </script>
11439: 
11440: END
11441: 
11442: }
11443: 
11444: sub toggle_display_js {
11445:     return <<"END";
11446: 
11447: <script type="text/javascript">
11448: // <![CDATA[
11449: 
11450: function toggleDisplay(domForm,caller) {
11451:     if (document.getElementById(caller)) {
11452:         var divitem = document.getElementById(caller);
11453:         var optionsElement = domForm.coursecredits;
11454:         if (caller == 'emailoptions') {
11455:             optionsElement = domForm.cancreate_email; 
11456:         }
11457:         if (optionsElement.length) {
11458:             var currval;
11459:             for (var i=0; i<optionsElement.length; i++) {
11460:                 if (optionsElement[i].checked) {
11461:                    currval = optionsElement[i].value;
11462:                 }
11463:             }
11464:             if (currval == 1) {
11465:                 divitem.style.display = 'block';
11466:             } else {
11467:                 divitem.style.display = 'none';
11468:             }
11469:         }
11470:     }
11471:     return;
11472: }
11473: 
11474: // ]]>
11475: </script>
11476: 
11477: END
11478: 
11479: }
11480: 
11481: sub captcha_phrases {
11482:     return &Apache::lonlocal::texthash (
11483:                  priv => 'Private key',
11484:                  pub  => 'Public key',
11485:                  original  => 'original (CAPTCHA)',
11486:                  recaptcha => 'successor (ReCAPTCHA)',
11487:                  notused   => 'unused',
11488:     );
11489: }
11490: 
11491: sub devalidate_remote_domconfs {
11492:     my ($dom,$cachekeys) = @_;
11493:     return unless (ref($cachekeys) eq 'HASH');
11494:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11495:     my %thismachine;
11496:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
11497:     my @posscached = ('domainconfig','domdefaults');
11498:     if (keys(%servers) > 1) {
11499:         foreach my $server (keys(%servers)) {
11500:             next if ($thismachine{$server});
11501:             my @cached;
11502:             foreach my $name (@posscached) {
11503:                 if ($cachekeys->{$name}) {
11504:                     push(@cached,&escape($name).':'.&escape($dom));
11505:                 }
11506:             }
11507:             if (@cached) {
11508:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
11509:             }
11510:         }
11511:     }
11512:     return;
11513: }
11514: 
11515: 1;

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