File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.247: download - view: text, annotated - select for diffs
Sat May 31 04:00:02 2014 UTC (10 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Alternating background colors for rows in domain configuration tables.
- Include a row for cases where Domain Coordinator is selected to receive
  messages, in cases where domain has only one DC.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.247 2014/05/31 04:00:02 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 => 'Available templates',
  373:                               col2 => ''},
  374:                              {col1 => 'Validation (not official courses)',
  375:                               col2 => 'Value'},],
  376:                   print => \&print_quotas,
  377:                   modify => \&modify_quotas,
  378:                  },
  379:         'requestauthor' =>
  380:                  {text => 'Request Authoring Space',
  381:                   help => 'Domain_Configuration_Request_Author',
  382:                   header => [{col1 => 'User affiliation',
  383:                               col2 => 'Availability/Processing of requests',},
  384:                              {col1 => 'Setting',
  385:                               col2 => 'Value'}],
  386:                   print => \&print_quotas,
  387:                   modify => \&modify_quotas,
  388:                  },
  389:         'coursecategories' =>
  390:                   { text => 'Cataloging of courses/communities',
  391:                     help => 'Domain_Configuration_Cataloging_Courses',
  392:                     header => [{col1 => 'Catalog type/availability',
  393:                                 col2 => '',},
  394:                                {col1 => 'Category settings for standard catalog',
  395:                                 col2 => '',},
  396:                                {col1 => 'Categories',
  397:                                 col2 => '',
  398:                                }],
  399:                     print => \&print_coursecategories,
  400:                     modify => \&modify_coursecategories,
  401:                   },
  402:         'serverstatuses' =>
  403:                  {text   => 'Access to server status pages',
  404:                   help   => 'Domain_Configuration_Server_Status',
  405:                   header => [{col1 => 'Status Page',
  406:                               col2 => 'Other named users',
  407:                               col3 => 'Specific IPs',
  408:                             }],
  409:                   print => \&print_serverstatuses,
  410:                   modify => \&modify_serverstatuses,
  411:                  },
  412:         'helpsettings' =>
  413:                  {text   => 'Help page settings',
  414:                   help   => 'Domain_Configuration_Help_Settings',
  415:                   header => [{col1 => 'Help Settings (logged-in users)',
  416:                               col2 => 'Value'}],
  417:                   print  => \&print_helpsettings,
  418:                   modify => \&modify_helpsettings,
  419:                  },
  420:         'coursedefaults' => 
  421:                  {text => 'Course/Community defaults',
  422:                   help => 'Domain_Configuration_Course_Defaults',
  423:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  424:                               col2 => 'Value',},
  425:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  426:                               col2 => 'Value',},],
  427:                   print => \&print_coursedefaults,
  428:                   modify => \&modify_coursedefaults,
  429:                  },
  430:         'selfenrollment' => 
  431:                  {text   => 'Self-enrollment in Course/Community',
  432:                   help   => 'Domain_Configuration_Selfenrollment',
  433:                   header => [{col1 => 'Configuration Rights',
  434:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  435:                              {col1 => 'Defaults',
  436:                               col2 => 'Value'},
  437:                              {col1 => 'Self-enrollment validation (optional)',
  438:                               col2 => 'Value'},],
  439:                   print => \&print_selfenrollment,
  440:                   modify => \&modify_selfenrollment,
  441:                  },
  442:         'privacy' => 
  443:                  {text   => 'User Privacy',
  444:                   help   => 'Domain_Configuration_User_Privacy',
  445:                   header => [{col1 => 'Setting',
  446:                               col2 => 'Value',}],
  447:                   print => \&print_privacy,
  448:                   modify => \&modify_privacy,
  449:                  },
  450:         'usersessions' =>
  451:                  {text  => 'User session hosting/offloading',
  452:                   help  => 'Domain_Configuration_User_Sessions',
  453:                   header => [{col1 => 'Domain server',
  454:                               col2 => 'Servers to offload sessions to when busy'},
  455:                              {col1 => 'Hosting of users from other domains',
  456:                               col2 => 'Rules'},
  457:                              {col1 => "Hosting domain's own users elsewhere",
  458:                               col2 => 'Rules'}],
  459:                   print => \&print_usersessions,
  460:                   modify => \&modify_usersessions,
  461:                  },
  462:          'loadbalancing' =>
  463:                  {text  => 'Dedicated Load Balancer(s)',
  464:                   help  => 'Domain_Configuration_Load_Balancing',
  465:                   header => [{col1 => 'Balancers',
  466:                               col2 => 'Default destinations',
  467:                               col3 => 'User affiliation',
  468:                               col4 => 'Overrides'},
  469:                             ],
  470:                   print => \&print_loadbalancing,
  471:                   modify => \&modify_loadbalancing,
  472:                  },
  473:     );
  474:     if (keys(%servers) > 1) {
  475:         $prefs{'login'}  = { text   => 'Log-in page options',
  476:                              help   => 'Domain_Configuration_Login_Page',
  477:                             header => [{col1 => 'Log-in Service',
  478:                                         col2 => 'Server Setting',},
  479:                                        {col1 => 'Log-in Page Items',
  480:                                         col2 => ''},
  481:                                        {col1 => 'Log-in Help',
  482:                                         col2 => 'Value'}],
  483:                             print => \&print_login,
  484:                             modify => \&modify_login,
  485:                            };
  486:     }
  487: 
  488:     my @roles = ('student','coordinator','author','admin');
  489:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  490:     &Apache::lonhtmlcommon::add_breadcrumb
  491:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  492:       text=>"Settings to display/modify"});
  493:     my $confname = $dom.'-domainconfig';
  494: 
  495:     if ($phase eq 'process') {
  496:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  497:                                                               \%prefs,\%domconfig,$confname,\@roles);
  498:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  499:             $r->rflush();
  500:             &devalidate_remote_domconfs($dom,$result);
  501:         }
  502:     } elsif ($phase eq 'display') {
  503:         my $js = &recaptcha_js().
  504:                  &toggle_display_js();
  505:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  506:             my ($othertitle,$usertypes,$types) =
  507:                 &Apache::loncommon::sorted_inst_types($dom);
  508:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  509:                                           $domconfig{'loadbalancing'}).
  510:                    &new_spares_js().
  511:                    &common_domprefs_js().
  512:                    &Apache::loncommon::javascript_array_indexof();
  513:         }
  514:         if (grep(/^requestcourses$/,@actions)) {
  515:             my $javascript_validations;
  516:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  517:             $js .= <<END;
  518: <script type="text/javascript">
  519: $javascript_validations
  520: </script>
  521: $coursebrowserjs
  522: END
  523:         }
  524:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  525:     } else {
  526: # check if domconfig user exists for the domain.
  527:         my $servadm = $r->dir_config('lonAdmEMail');
  528:         my ($configuserok,$author_ok,$switchserver) =
  529:             &config_check($dom,$confname,$servadm);
  530:         unless ($configuserok eq 'ok') {
  531:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  532:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  533:                           $confname).
  534:                       '<br />'
  535:             );
  536:             if ($switchserver) {
  537:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  538:                           '<br />'.
  539:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  540:                           '<br />'.
  541:                           &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).
  542:                           '<br />'.
  543:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  544:                 );
  545:             } else {
  546:                 $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.').
  547:                           '<br />'.
  548:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  549:                 );
  550:             }
  551:             $r->print(&Apache::loncommon::end_page());
  552:             return OK;
  553:         }
  554:         if (keys(%domconfig) == 0) {
  555:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  556:             my @ids=&Apache::lonnet::current_machine_ids();
  557:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  558:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  559:                 my @loginimages = ('img','logo','domlogo','login');
  560:                 my $custom_img_count = 0;
  561:                 foreach my $img (@loginimages) {
  562:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  563:                         $custom_img_count ++;
  564:                     }
  565:                 }
  566:                 foreach my $role (@roles) {
  567:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  568:                         $custom_img_count ++;
  569:                     }
  570:                 }
  571:                 if ($custom_img_count > 0) {
  572:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  573:                     my $switch_server = &check_switchserver($dom,$confname);
  574:                     $r->print(
  575:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  576:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  577:     &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 />'.
  578:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  579:                     if ($switch_server) {
  580:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  581:                     }
  582:                     $r->print(&Apache::loncommon::end_page());
  583:                     return OK;
  584:                 }
  585:             }
  586:         }
  587:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  588:     }
  589:     return OK;
  590: }
  591: 
  592: sub process_changes {
  593:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  594:     my %domconfig;
  595:     if (ref($values) eq 'HASH') {
  596:         %domconfig = %{$values};
  597:     }
  598:     my $output;
  599:     if ($action eq 'login') {
  600:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  601:     } elsif ($action eq 'rolecolors') {
  602:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  603:                                      $lastactref,%domconfig);
  604:     } elsif ($action eq 'quotas') {
  605:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  606:     } elsif ($action eq 'autoenroll') {
  607:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  608:     } elsif ($action eq 'autoupdate') {
  609:         $output = &modify_autoupdate($dom,%domconfig);
  610:     } elsif ($action eq 'autocreate') {
  611:         $output = &modify_autocreate($dom,%domconfig);
  612:     } elsif ($action eq 'directorysrch') {
  613:         $output = &modify_directorysrch($dom,%domconfig);
  614:     } elsif ($action eq 'usercreation') {
  615:         $output = &modify_usercreation($dom,%domconfig);
  616:     } elsif ($action eq 'selfcreation') {
  617:         $output = &modify_selfcreation($dom,%domconfig);
  618:     } elsif ($action eq 'usermodification') {
  619:         $output = &modify_usermodification($dom,%domconfig);
  620:     } elsif ($action eq 'contacts') {
  621:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  622:     } elsif ($action eq 'defaults') {
  623:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  624:     } elsif ($action eq 'scantron') {
  625:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  626:     } elsif ($action eq 'coursecategories') {
  627:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  628:     } elsif ($action eq 'serverstatuses') {
  629:         $output = &modify_serverstatuses($dom,%domconfig);
  630:     } elsif ($action eq 'requestcourses') {
  631:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  632:     } elsif ($action eq 'requestauthor') {
  633:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  634:     } elsif ($action eq 'helpsettings') {
  635:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  636:     } elsif ($action eq 'coursedefaults') {
  637:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  638:     } elsif ($action eq 'selfenrollment') {
  639:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  640:     } elsif ($action eq 'usersessions') {
  641:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  642:     } elsif ($action eq 'loadbalancing') {
  643:         $output = &modify_loadbalancing($dom,%domconfig);
  644:     }
  645:     return $output;
  646: }
  647: 
  648: sub print_config_box {
  649:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  650:     my $rowtotal = 0;
  651:     my $output;
  652:     if ($action eq 'coursecategories') {
  653:         $output = &coursecategories_javascript($settings);
  654:     } elsif ($action eq 'defaults') {
  655:         $output = &defaults_javascript($settings); 
  656:     }
  657:     $output .=
  658:          '<table class="LC_nested_outer">
  659:           <tr>
  660:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  661:            &mt($item->{text}).'&nbsp;'.
  662:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  663:           '</tr>';
  664:     $rowtotal ++;
  665:     my $numheaders = 1;
  666:     if (ref($item->{'header'}) eq 'ARRAY') {
  667:         $numheaders = scalar(@{$item->{'header'}});
  668:     }
  669:     if ($numheaders > 1) {
  670:         my $colspan = '';
  671:         my $rightcolspan = '';
  672:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  673:             (($action eq 'login') && ($numheaders < 3))) {
  674:             $colspan = ' colspan="2"';
  675:         }
  676:         if ($action eq 'usersessions') {
  677:             $rightcolspan = ' colspan="3"'; 
  678:         }
  679:         $output .= '
  680:           <tr>
  681:            <td>
  682:             <table class="LC_nested">
  683:              <tr class="LC_info_row">
  684:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  685:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  686:              </tr>';
  687:         $rowtotal ++;
  688:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  689:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  690:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
  691:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  692:         } elsif ($action eq 'coursecategories') {
  693:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  694:         } elsif ($action eq 'login') {
  695:             if ($numheaders == 3) {
  696:                 $colspan = ' colspan="2"';
  697:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  698:             } else {
  699:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  700:             }
  701:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  702:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  703:         } elsif ($action eq 'rolecolors') {
  704:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  705:         }
  706:         $output .= '
  707:            </table>
  708:           </td>
  709:          </tr>
  710:          <tr>
  711:            <td>
  712:             <table class="LC_nested">
  713:              <tr class="LC_info_row">
  714:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  715:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  716:              </tr>';
  717:             $rowtotal ++;
  718:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  719:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  720:             ($action eq 'usersessions') || ($action eq 'coursecategories')) {
  721:             if ($action eq 'coursecategories') {
  722:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  723:                 $colspan = ' colspan="2"';
  724:             } else {
  725:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  726:             }
  727:             $output .= '
  728:            </table>
  729:           </td>
  730:          </tr>
  731:          <tr>
  732:            <td>
  733:             <table class="LC_nested">
  734:              <tr class="LC_info_row">
  735:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  736:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  737:              </tr>'."\n";
  738:             if ($action eq 'coursecategories') {
  739:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  740:             } else {
  741:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  742:             }
  743:             $rowtotal ++;
  744:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  745:                   ($action eq 'defaults')) {
  746:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  747:         } elsif ($action eq 'login') {
  748:             if ($numheaders == 3) {
  749:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  750:            </table>
  751:           </td>
  752:          </tr>
  753:          <tr>
  754:            <td>
  755:             <table class="LC_nested">
  756:              <tr class="LC_info_row">
  757:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  758:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  759:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  760:                 $rowtotal ++;
  761:             } else {
  762:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  763:             }
  764:         } elsif ($action eq 'requestcourses') {
  765:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  766:             $rowtotal ++;
  767:             $output .= &print_studentcode($settings,\$rowtotal).'
  768:            </table>
  769:           </td>
  770:          </tr>
  771:          <tr>
  772:            <td>
  773:             <table class="LC_nested">
  774:              <tr class="LC_info_row">
  775:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  776:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  777:                        &textbookcourses_javascript($settings).
  778:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  779:             </table>
  780:            </td>
  781:           </tr>
  782:          <tr>
  783:            <td>
  784:             <table class="LC_nested">
  785:              <tr class="LC_info_row">
  786:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  787:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  788:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  789:             </table>
  790:            </td>
  791:           </tr>
  792:           <tr>
  793:            <td>
  794:             <table class="LC_nested">
  795:              <tr class="LC_info_row">
  796:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  797:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  798:              </tr>'.
  799:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  800:         } elsif ($action eq 'requestauthor') {
  801:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  802:             $rowtotal ++;
  803:         } elsif ($action eq 'rolecolors') {
  804:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  805:            </table>
  806:           </td>
  807:          </tr>
  808:          <tr>
  809:            <td>
  810:             <table class="LC_nested">
  811:              <tr class="LC_info_row">
  812:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  813:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  814:               <td class="LC_right_item" valign="top">'.
  815:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  816:              </tr>'.
  817:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  818:            </table>
  819:           </td>
  820:          </tr>
  821:          <tr>
  822:            <td>
  823:             <table class="LC_nested">
  824:              <tr class="LC_info_row">
  825:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  826:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  827:              </tr>'.
  828:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  829:             $rowtotal += 2;
  830:         }
  831:     } else {
  832:         $output .= '
  833:           <tr>
  834:            <td>
  835:             <table class="LC_nested">
  836:              <tr class="LC_info_row">';
  837:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  838:             $output .= '  
  839:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  840:         } elsif ($action eq 'serverstatuses') {
  841:             $output .= '
  842:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  843:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  844: 
  845:         } else {
  846:             $output .= '
  847:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  848:         }
  849:         if (defined($item->{'header'}->[0]->{'col3'})) {
  850:             $output .= '<td class="LC_left_item" valign="top">'.
  851:                        &mt($item->{'header'}->[0]->{'col2'});
  852:             if ($action eq 'serverstatuses') {
  853:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  854:             } 
  855:         } else {
  856:             $output .= '<td class="LC_right_item" valign="top">'.
  857:                        &mt($item->{'header'}->[0]->{'col2'});
  858:         }
  859:         $output .= '</td>';
  860:         if ($item->{'header'}->[0]->{'col3'}) {
  861:             if (defined($item->{'header'}->[0]->{'col4'})) {
  862:                 $output .= '<td class="LC_left_item" valign="top">'.
  863:                             &mt($item->{'header'}->[0]->{'col3'});
  864:             } else {
  865:                 $output .= '<td class="LC_right_item" valign="top">'.
  866:                            &mt($item->{'header'}->[0]->{'col3'});
  867:             }
  868:             if ($action eq 'serverstatuses') {
  869:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  870:             }
  871:             $output .= '</td>';
  872:         }
  873:         if ($item->{'header'}->[0]->{'col4'}) {
  874:             $output .= '<td class="LC_right_item" valign="top">'.
  875:                        &mt($item->{'header'}->[0]->{'col4'});
  876:         }
  877:         $output .= '</tr>';
  878:         $rowtotal ++;
  879:         if ($action eq 'quotas') {
  880:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  881:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  882:                  ($action eq 'contacts') || ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  883:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  884:         } elsif ($action eq 'scantron') {
  885:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  886:         } elsif ($action eq 'helpsettings') {
  887:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  888:         }
  889:     }
  890:     $output .= '
  891:    </table>
  892:   </td>
  893:  </tr>
  894: </table><br />';
  895:     return ($output,$rowtotal);
  896: }
  897: 
  898: sub print_login {
  899:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  900:     my ($css_class,$datatable);
  901:     my %choices = &login_choices();
  902: 
  903:     if ($caller eq 'service') {
  904:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  905:         my $choice = $choices{'disallowlogin'};
  906:         $css_class = ' class="LC_odd_row"';
  907:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  908:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  909:                       '<th>'.$choices{'server'}.'</th>'.
  910:                       '<th>'.$choices{'serverpath'}.'</th>'.
  911:                       '<th>'.$choices{'custompath'}.'</th>'.
  912:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  913:         my %disallowed;
  914:         if (ref($settings) eq 'HASH') {
  915:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  916:                %disallowed = %{$settings->{'loginvia'}};
  917:             }
  918:         }
  919:         foreach my $lonhost (sort(keys(%servers))) {
  920:             my $direct = 'selected="selected"';
  921:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  922:                 if ($disallowed{$lonhost}{'server'} ne '') {
  923:                     $direct = '';
  924:                 }
  925:             }
  926:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  927:                           '<td><select name="'.$lonhost.'_server">'.
  928:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  929:                           '</option>';
  930:             foreach my $hostid (sort(keys(%servers))) {
  931:                 next if ($servers{$hostid} eq $servers{$lonhost});
  932:                 my $selected = '';
  933:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  934:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  935:                         $selected = 'selected="selected"';
  936:                     }
  937:                 }
  938:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  939:                               $servers{$hostid}.'</option>';
  940:             }
  941:             $datatable .= '</select></td>'.
  942:                           '<td><select name="'.$lonhost.'_serverpath">';
  943:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  944:                 my $pathname = $path;
  945:                 if ($path eq 'custom') {
  946:                     $pathname = &mt('Custom Path').' ->';
  947:                 }
  948:                 my $selected = '';
  949:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  950:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  951:                         $selected = 'selected="selected"';
  952:                     }
  953:                 } elsif ($path eq '') {
  954:                     $selected = 'selected="selected"';
  955:                 }
  956:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  957:             }
  958:             $datatable .= '</select></td>';
  959:             my ($custom,$exempt);
  960:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  961:                 $custom = $disallowed{$lonhost}{'custompath'};
  962:                 $exempt = $disallowed{$lonhost}{'exempt'};
  963:             }
  964:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  965:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  966:                           '</tr>';
  967:         }
  968:         $datatable .= '</table></td></tr>';
  969:         return $datatable;
  970:     } elsif ($caller eq 'page') {
  971:         my %defaultchecked = ( 
  972:                                'coursecatalog' => 'on',
  973:                                'helpdesk'      => 'on',
  974:                                'adminmail'     => 'off',
  975:                                'newuser'       => 'off',
  976:                              );
  977:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  978:         my (%checkedon,%checkedoff);
  979:         foreach my $item (@toggles) {
  980:             if ($defaultchecked{$item} eq 'on') { 
  981:                 $checkedon{$item} = ' checked="checked" ';
  982:                 $checkedoff{$item} = ' ';
  983:             } elsif ($defaultchecked{$item} eq 'off') {
  984:                 $checkedoff{$item} = ' checked="checked" ';
  985:                 $checkedon{$item} = ' ';
  986:             }
  987:         }
  988:         my @images = ('img','logo','domlogo','login');
  989:         my @logintext = ('textcol','bgcol');
  990:         my @bgs = ('pgbg','mainbg','sidebg');
  991:         my @links = ('link','alink','vlink');
  992:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  993:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  994:         my (%is_custom,%designs);
  995:         my %defaults = (
  996:                        font => $defaultdesign{'login.font'},
  997:                        );
  998:         foreach my $item (@images) {
  999:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1000:             $defaults{'showlogo'}{$item} = 1;
 1001:         }
 1002:         foreach my $item (@bgs) {
 1003:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1004:         }
 1005:         foreach my $item (@logintext) {
 1006:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1007:         }
 1008:         foreach my $item (@links) {
 1009:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1010:         }
 1011:         if (ref($settings) eq 'HASH') {
 1012:             foreach my $item (@toggles) {
 1013:                 if ($settings->{$item} eq '1') {
 1014:                     $checkedon{$item} =  ' checked="checked" ';
 1015:                     $checkedoff{$item} = ' ';
 1016:                 } elsif ($settings->{$item} eq '0') {
 1017:                     $checkedoff{$item} =  ' checked="checked" ';
 1018:                     $checkedon{$item} = ' ';
 1019:                 }
 1020:             }
 1021:             foreach my $item (@images) {
 1022:                 if (defined($settings->{$item})) {
 1023:                     $designs{$item} = $settings->{$item};
 1024:                     $is_custom{$item} = 1;
 1025:                 }
 1026:                 if (defined($settings->{'showlogo'}{$item})) {
 1027:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1028:                 }
 1029:             }
 1030:             foreach my $item (@logintext) {
 1031:                 if ($settings->{$item} ne '') {
 1032:                     $designs{'logintext'}{$item} = $settings->{$item};
 1033:                     $is_custom{$item} = 1;
 1034:                 }
 1035:             }
 1036:             if ($settings->{'font'} ne '') {
 1037:                 $designs{'font'} = $settings->{'font'};
 1038:                 $is_custom{'font'} = 1;
 1039:             }
 1040:             foreach my $item (@bgs) {
 1041:                 if ($settings->{$item} ne '') {
 1042:                     $designs{'bgs'}{$item} = $settings->{$item};
 1043:                     $is_custom{$item} = 1;
 1044:                 }
 1045:             }
 1046:             foreach my $item (@links) {
 1047:                 if ($settings->{$item} ne '') {
 1048:                     $designs{'links'}{$item} = $settings->{$item};
 1049:                     $is_custom{$item} = 1;
 1050:                 }
 1051:             }
 1052:         } else {
 1053:             if ($designhash{$dom.'.login.font'} ne '') {
 1054:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1055:                 $is_custom{'font'} = 1;
 1056:             }
 1057:             foreach my $item (@images) {
 1058:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1059:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1060:                     $is_custom{$item} = 1;
 1061:                 }
 1062:             }
 1063:             foreach my $item (@bgs) {
 1064:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1065:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1066:                     $is_custom{$item} = 1;
 1067:                 }
 1068:             }
 1069:             foreach my $item (@links) {
 1070:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1071:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1072:                     $is_custom{$item} = 1;
 1073:                 }
 1074:             }
 1075:         }
 1076:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1077:                                                       logo => 'Institution Logo',
 1078:                                                       domlogo => 'Domain Logo',
 1079:                                                       login => 'Login box');
 1080:         my $itemcount = 1;
 1081:         foreach my $item (@toggles) {
 1082:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1083:             $datatable .=  
 1084:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1085:                 '</td><td>'.
 1086:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1087:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1088:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1089:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1090:                 '</tr>';
 1091:             $itemcount ++;
 1092:         }
 1093:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1094:         $datatable .= '</tr></table></td></tr>';
 1095:     } elsif ($caller eq 'help') {
 1096:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1097:         my $switchserver = &check_switchserver($dom,$confname);
 1098:         my $itemcount = 1;
 1099:         $defaulturl = '/adm/loginproblems.html';
 1100:         $defaulttype = 'default';
 1101:         %lt = &Apache::lonlocal::texthash (
 1102:                      del     => 'Delete?',
 1103:                      rep     => 'Replace:',
 1104:                      upl     => 'Upload:',
 1105:                      default => 'Default',
 1106:                      custom  => 'Custom',
 1107:                                              );
 1108:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1109:         my @currlangs;
 1110:         if (ref($settings) eq 'HASH') {
 1111:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1112:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1113:                     next if ($settings->{'helpurl'}{$key} eq '');
 1114:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1115:                     $type{$key} = 'custom';
 1116:                     unless ($key eq 'nolang') {
 1117:                         push(@currlangs,$key);
 1118:                     }
 1119:                 }
 1120:             } elsif ($settings->{'helpurl'} ne '') {
 1121:                 $type{'nolang'} = 'custom';
 1122:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1123:             }
 1124:         }
 1125:         foreach my $lang ('nolang',sort(@currlangs)) {
 1126:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1127:             $datatable .= '<tr'.$css_class.'>';
 1128:             if ($url{$lang} eq '') {
 1129:                 $url{$lang} = $defaulturl;
 1130:             }
 1131:             if ($type{$lang} eq '') {
 1132:                 $type{$lang} = $defaulttype;
 1133:             }
 1134:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1135:             if ($lang eq 'nolang') {
 1136:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1137:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1138:             } else {
 1139:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1140:                                   $langchoices{$lang},
 1141:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1142:             }
 1143:             $datatable .= '</span></td>'."\n".
 1144:                           '<td class="LC_left_item">';
 1145:             if ($type{$lang} eq 'custom') {
 1146:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1147:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1148:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1149:             } else {
 1150:                 $datatable .= $lt{'upl'};
 1151:             }
 1152:             $datatable .='<br />';
 1153:             if ($switchserver) {
 1154:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1155:             } else {
 1156:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1157:             }
 1158:             $datatable .= '</td></tr>';
 1159:             $itemcount ++;
 1160:         }
 1161:         my @addlangs;
 1162:         foreach my $lang (sort(keys(%langchoices))) {
 1163:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1164:             push(@addlangs,$lang);
 1165:         }
 1166:         if (@addlangs > 0) {
 1167:             my %toadd;
 1168:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1169:             $toadd{''} = &mt('Select');
 1170:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1171:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1172:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1173:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1174:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1175:             if ($switchserver) {
 1176:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1177:             } else {
 1178:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1179:             }
 1180:             $datatable .= '</td></tr>';
 1181:             $itemcount ++;
 1182:         }
 1183:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1184:     }
 1185:     return $datatable;
 1186: }
 1187: 
 1188: sub login_choices {
 1189:     my %choices =
 1190:         &Apache::lonlocal::texthash (
 1191:             coursecatalog => 'Display Course/Community Catalog link?',
 1192:             adminmail     => "Display Administrator's E-mail Address?",
 1193:             helpdesk      => 'Display "Contact Helpdesk" link',
 1194:             disallowlogin => "Login page requests redirected",
 1195:             hostid        => "Server",
 1196:             server        => "Redirect to:",
 1197:             serverpath    => "Path",
 1198:             custompath    => "Custom", 
 1199:             exempt        => "Exempt IP(s)",
 1200:             directlogin   => "No redirect",
 1201:             newuser       => "Link to create a user account",
 1202:             img           => "Header",
 1203:             logo          => "Main Logo",
 1204:             domlogo       => "Domain Logo",
 1205:             login         => "Log-in Header", 
 1206:             textcol       => "Text color",
 1207:             bgcol         => "Box color",
 1208:             bgs           => "Background colors",
 1209:             links         => "Link colors",
 1210:             font          => "Font color",
 1211:             pgbg          => "Header",
 1212:             mainbg        => "Page",
 1213:             sidebg        => "Login box",
 1214:             link          => "Link",
 1215:             alink         => "Active link",
 1216:             vlink         => "Visited link",
 1217:         );
 1218:     return %choices;
 1219: }
 1220: 
 1221: sub print_rolecolors {
 1222:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1223:     my %choices = &color_font_choices();
 1224:     my @bgs = ('pgbg','tabbg','sidebg');
 1225:     my @links = ('link','alink','vlink');
 1226:     my @images = ('img');
 1227:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1228:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1229:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1230:     my (%is_custom,%designs);
 1231:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1232:     if (ref($settings) eq 'HASH') {
 1233:         if (ref($settings->{$role}) eq 'HASH') {
 1234:             if ($settings->{$role}->{'img'} ne '') {
 1235:                 $designs{'img'} = $settings->{$role}->{'img'};
 1236:                 $is_custom{'img'} = 1;
 1237:             }
 1238:             if ($settings->{$role}->{'font'} ne '') {
 1239:                 $designs{'font'} = $settings->{$role}->{'font'};
 1240:                 $is_custom{'font'} = 1;
 1241:             }
 1242:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1243:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1244:                 $is_custom{'fontmenu'} = 1;
 1245:             }
 1246:             foreach my $item (@bgs) {
 1247:                 if ($settings->{$role}->{$item} ne '') {
 1248:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1249:                     $is_custom{$item} = 1;
 1250:                 }
 1251:             }
 1252:             foreach my $item (@links) {
 1253:                 if ($settings->{$role}->{$item} ne '') {
 1254:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1255:                     $is_custom{$item} = 1;
 1256:                 }
 1257:             }
 1258:         }
 1259:     } else {
 1260:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1261:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1262:             $is_custom{'img'} = 1;
 1263:         }
 1264:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1265:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1266:             $is_custom{'fontmenu'} = 1; 
 1267:         }
 1268:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1269:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1270:             $is_custom{'font'} = 1;
 1271:         }
 1272:         foreach my $item (@bgs) {
 1273:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1274:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1275:                 $is_custom{$item} = 1;
 1276:             
 1277:             }
 1278:         }
 1279:         foreach my $item (@links) {
 1280:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1281:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1282:                 $is_custom{$item} = 1;
 1283:             }
 1284:         }
 1285:     }
 1286:     my $itemcount = 1;
 1287:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1288:     $datatable .= '</tr></table></td></tr>';
 1289:     return $datatable;
 1290: }
 1291: 
 1292: sub role_defaults {
 1293:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1294:     my %defaults;
 1295:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1296:         return %defaults;
 1297:     }
 1298:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1299:     if ($role eq 'login') {
 1300:         %defaults = (
 1301:                        font => $defaultdesign{$role.'.font'},
 1302:                     );
 1303:         if (ref($logintext) eq 'ARRAY') {
 1304:             foreach my $item (@{$logintext}) {
 1305:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1306:             }
 1307:         }
 1308:         foreach my $item (@{$images}) {
 1309:             $defaults{'showlogo'}{$item} = 1;
 1310:         }
 1311:     } else {
 1312:         %defaults = (
 1313:                        img => $defaultdesign{$role.'.img'},
 1314:                        font => $defaultdesign{$role.'.font'},
 1315:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1316:                     );
 1317:     }
 1318:     foreach my $item (@{$bgs}) {
 1319:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1320:     }
 1321:     foreach my $item (@{$links}) {
 1322:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1323:     }
 1324:     foreach my $item (@{$images}) {
 1325:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1326:     }
 1327:     return %defaults;
 1328: }
 1329: 
 1330: sub display_color_options {
 1331:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1332:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1333:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1334:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1335:     my $datatable = '<tr'.$css_class.'>'.
 1336:         '<td>'.$choices->{'font'}.'</td>';
 1337:     if (!$is_custom->{'font'}) {
 1338:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1339:     } else {
 1340:         $datatable .= '<td>&nbsp;</td>';
 1341:     }
 1342:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1343: 
 1344:     $datatable .= '<td><span class="LC_nobreak">'.
 1345:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1346:                   ' value="'.$current_color.'" />&nbsp;'.
 1347:                   '&nbsp;</td></tr>';
 1348:     unless ($role eq 'login') { 
 1349:         $datatable .= '<tr'.$css_class.'>'.
 1350:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1351:         if (!$is_custom->{'fontmenu'}) {
 1352:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1353:         } else {
 1354:             $datatable .= '<td>&nbsp;</td>';
 1355:         }
 1356: 	$current_color = $designs->{'fontmenu'} ?
 1357: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1358:         $datatable .= '<td><span class="LC_nobreak">'.
 1359:                       '<input class="colorchooser" type="text" size="10" name="'
 1360: 		      .$role.'_fontmenu"'.
 1361:                       ' value="'.$current_color.'" />&nbsp;'.
 1362:                       '&nbsp;</td></tr>';
 1363:     }
 1364:     my $switchserver = &check_switchserver($dom,$confname);
 1365:     foreach my $img (@{$images}) {
 1366: 	$itemcount ++;
 1367:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1368:         $datatable .= '<tr'.$css_class.'>'.
 1369:                       '<td>'.$choices->{$img};
 1370:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1371:         if ($role eq 'login') {
 1372:             if ($img eq 'login') {
 1373:                 $login_hdr_pick =
 1374:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1375:                 $logincolors =
 1376:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1377:                                        $designs,$defaults);
 1378:             } elsif ($img ne 'domlogo') {
 1379:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1380:             }
 1381:         }
 1382:         $datatable .= '</td>';
 1383:         if ($designs->{$img} ne '') {
 1384:             $imgfile = $designs->{$img};
 1385: 	    $img_import = ($imgfile =~ m{^/adm/});
 1386:         } else {
 1387:             $imgfile = $defaults->{$img};
 1388:         }
 1389:         if ($imgfile) {
 1390:             my ($showfile,$fullsize);
 1391:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1392:                 my $urldir = $1;
 1393:                 my $filename = $2;
 1394:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1395:                 if (@info) {
 1396:                     my $thumbfile = 'tn-'.$filename;
 1397:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1398:                     if (@thumb) {
 1399:                         $showfile = $urldir.'/'.$thumbfile;
 1400:                     } else {
 1401:                         $showfile = $imgfile;
 1402:                     }
 1403:                 } else {
 1404:                     $showfile = '';
 1405:                 }
 1406:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1407:                 $showfile = $imgfile;
 1408:                 my $imgdir = $1;
 1409:                 my $filename = $2;
 1410:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1411:                     $showfile = "/$imgdir/tn-".$filename;
 1412:                 } else {
 1413:                     my $input = $londocroot.$imgfile;
 1414:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1415:                     if (!-e $output) {
 1416:                         my ($width,$height) = &thumb_dimensions();
 1417:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1418:                         if ($fullwidth ne '' && $fullheight ne '') {
 1419:                             if ($fullwidth > $width && $fullheight > $height) { 
 1420:                                 my $size = $width.'x'.$height;
 1421:                                 system("convert -sample $size $input $output");
 1422:                                 $showfile = "/$imgdir/tn-".$filename;
 1423:                             }
 1424:                         }
 1425:                     }
 1426:                 }
 1427:             }
 1428:             if ($showfile) {
 1429:                 if ($showfile =~ m{^/(adm|res)/}) {
 1430:                     if ($showfile =~ m{^/res/}) {
 1431:                         my $local_showfile =
 1432:                             &Apache::lonnet::filelocation('',$showfile);
 1433:                         &Apache::lonnet::repcopy($local_showfile);
 1434:                     }
 1435:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1436:                 }
 1437:                 if ($imgfile) {
 1438:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1439:                         if ($imgfile =~ m{^/res/}) {
 1440:                             my $local_imgfile =
 1441:                                 &Apache::lonnet::filelocation('',$imgfile);
 1442:                             &Apache::lonnet::repcopy($local_imgfile);
 1443:                         }
 1444:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1445:                     } else {
 1446:                         $fullsize = $imgfile;
 1447:                     }
 1448:                 }
 1449:                 $datatable .= '<td>';
 1450:                 if ($img eq 'login') {
 1451:                     $datatable .= $login_hdr_pick;
 1452:                 } 
 1453:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1454:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1455:             } else {
 1456:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1457:                               &mt('Upload:').'<br />';
 1458:             }
 1459:         } else {
 1460:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1461:                           &mt('Upload:').'<br />';
 1462:         }
 1463:         if ($switchserver) {
 1464:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1465:         } else {
 1466:             if ($img ne 'login') { # suppress file selection for Log-in header
 1467:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1468:             }
 1469:         }
 1470:         $datatable .= '</td></tr>';
 1471:     }
 1472:     $itemcount ++;
 1473:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1474:     $datatable .= '<tr'.$css_class.'>'.
 1475:                   '<td>'.$choices->{'bgs'}.'</td>';
 1476:     my $bgs_def;
 1477:     foreach my $item (@{$bgs}) {
 1478:         if (!$is_custom->{$item}) {
 1479:             $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>';
 1480:         }
 1481:     }
 1482:     if ($bgs_def) {
 1483:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1484:     } else {
 1485:         $datatable .= '<td>&nbsp;</td>';
 1486:     }
 1487:     $datatable .= '<td class="LC_right_item">'.
 1488:                   '<table border="0"><tr>';
 1489: 
 1490:     foreach my $item (@{$bgs}) {
 1491:         $datatable .= '<td align="center">'.$choices->{$item};
 1492: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1493:         if ($designs->{'bgs'}{$item}) {
 1494:             $datatable .= '&nbsp;';
 1495:         }
 1496:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1497:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1498:     }
 1499:     $datatable .= '</tr></table></td></tr>';
 1500:     $itemcount ++;
 1501:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1502:     $datatable .= '<tr'.$css_class.'>'.
 1503:                   '<td>'.$choices->{'links'}.'</td>';
 1504:     my $links_def;
 1505:     foreach my $item (@{$links}) {
 1506:         if (!$is_custom->{$item}) {
 1507:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1508:         }
 1509:     }
 1510:     if ($links_def) {
 1511:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1512:     } else {
 1513:         $datatable .= '<td>&nbsp;</td>';
 1514:     }
 1515:     $datatable .= '<td class="LC_right_item">'.
 1516:                   '<table border="0"><tr>';
 1517:     foreach my $item (@{$links}) {
 1518: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1519:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1520:         if ($designs->{'links'}{$item}) {
 1521:             $datatable.='&nbsp;';
 1522:         }
 1523:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1524:                       '" /></td>';
 1525:     }
 1526:     $$rowtotal += $itemcount;
 1527:     return $datatable;
 1528: }
 1529: 
 1530: sub logo_display_options {
 1531:     my ($img,$defaults,$designs) = @_;
 1532:     my $checkedon;
 1533:     if (ref($defaults) eq 'HASH') {
 1534:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1535:             if ($defaults->{'showlogo'}{$img}) {
 1536:                 $checkedon = 'checked="checked" ';     
 1537:             }
 1538:         } 
 1539:     }
 1540:     if (ref($designs) eq 'HASH') {
 1541:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1542:             if (defined($designs->{'showlogo'}{$img})) {
 1543:                 if ($designs->{'showlogo'}{$img} == 0) {
 1544:                     $checkedon = '';
 1545:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1546:                     $checkedon = 'checked="checked" ';
 1547:                 }
 1548:             }
 1549:         }
 1550:     }
 1551:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1552:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1553:            &mt('show').'</label>'."\n";
 1554: }
 1555: 
 1556: sub login_header_options  {
 1557:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1558:     my $output = '';
 1559:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1560:         $output .= &mt('Text default(s):').'<br />';
 1561:         if (!$is_custom->{'textcol'}) {
 1562:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1563:                        '&nbsp;&nbsp;&nbsp;';
 1564:         }
 1565:         if (!$is_custom->{'bgcol'}) {
 1566:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1567:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1568:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1569:         }
 1570:         $output .= '<br />';
 1571:     }
 1572:     $output .='<br />';
 1573:     return $output;
 1574: }
 1575: 
 1576: sub login_text_colors {
 1577:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1578:     my $color_menu = '<table border="0"><tr>';
 1579:     foreach my $item (@{$logintext}) {
 1580:         $color_menu .= '<td align="center">'.$choices->{$item};
 1581:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1582:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1583:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1584:     }
 1585:     $color_menu .= '</tr></table><br />';
 1586:     return $color_menu;
 1587: }
 1588: 
 1589: sub image_changes {
 1590:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1591:     my $output;
 1592:     if ($img eq 'login') {
 1593:             # suppress image for Log-in header
 1594:     } elsif (!$is_custom) {
 1595:         if ($img ne 'domlogo') {
 1596:             $output .= &mt('Default image:').'<br />';
 1597:         } else {
 1598:             $output .= &mt('Default in use:').'<br />';
 1599:         }
 1600:     }
 1601:     if ($img eq 'login') { # suppress image for Log-in header
 1602:         $output .= '<td>'.$logincolors;
 1603:     } else {
 1604:         if ($img_import) {
 1605:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1606:         }
 1607:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1608:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1609:         if ($is_custom) {
 1610:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1611:                        '<input type="checkbox" name="'.
 1612:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1613:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1614:         } else {
 1615:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1616:         }
 1617:     }
 1618:     return $output;
 1619: }
 1620: 
 1621: sub print_quotas {
 1622:     my ($dom,$settings,$rowtotal,$action) = @_;
 1623:     my $context;
 1624:     if ($action eq 'quotas') {
 1625:         $context = 'tools';
 1626:     } else {
 1627:         $context = $action;
 1628:     }
 1629:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1630:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1631:     my $typecount = 0;
 1632:     my ($css_class,%titles);
 1633:     if ($context eq 'requestcourses') {
 1634:         @usertools = ('official','unofficial','community','textbook');
 1635:         @options =('norequest','approval','validate','autolimit');
 1636:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1637:         %titles = &courserequest_titles();
 1638:     } elsif ($context eq 'requestauthor') {
 1639:         @usertools = ('author');
 1640:         @options = ('norequest','approval','automatic');
 1641:         %titles = &authorrequest_titles();
 1642:     } else {
 1643:         @usertools = ('aboutme','blog','webdav','portfolio');
 1644:         %titles = &tool_titles();
 1645:     }
 1646:     if (ref($types) eq 'ARRAY') {
 1647:         foreach my $type (@{$types}) {
 1648:             my ($currdefquota,$currauthorquota);
 1649:             unless (($context eq 'requestcourses') ||
 1650:                     ($context eq 'requestauthor')) {
 1651:                 if (ref($settings) eq 'HASH') {
 1652:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1653:                         $currdefquota = $settings->{defaultquota}->{$type};
 1654:                     } else {
 1655:                         $currdefquota = $settings->{$type};
 1656:                     }
 1657:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1658:                         $currauthorquota = $settings->{authorquota}->{$type};
 1659:                     }
 1660:                 }
 1661:             }
 1662:             if (defined($usertypes->{$type})) {
 1663:                 $typecount ++;
 1664:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1665:                 $datatable .= '<tr'.$css_class.'>'.
 1666:                               '<td>'.$usertypes->{$type}.'</td>'.
 1667:                               '<td class="LC_left_item">';
 1668:                 if ($context eq 'requestcourses') {
 1669:                     $datatable .= '<table><tr>';
 1670:                 }
 1671:                 my %cell;  
 1672:                 foreach my $item (@usertools) {
 1673:                     if ($context eq 'requestcourses') {
 1674:                         my ($curroption,$currlimit);
 1675:                         if (ref($settings) eq 'HASH') {
 1676:                             if (ref($settings->{$item}) eq 'HASH') {
 1677:                                 $curroption = $settings->{$item}->{$type};
 1678:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1679:                                     $currlimit = $1; 
 1680:                                 }
 1681:                             }
 1682:                         }
 1683:                         if (!$curroption) {
 1684:                             $curroption = 'norequest';
 1685:                         }
 1686:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1687:                         foreach my $option (@options) {
 1688:                             my $val = $option;
 1689:                             if ($option eq 'norequest') {
 1690:                                 $val = 0;  
 1691:                             }
 1692:                             if ($option eq 'validate') {
 1693:                                 my $canvalidate = 0;
 1694:                                 if (ref($validations{$item}) eq 'HASH') { 
 1695:                                     if ($validations{$item}{$type}) {
 1696:                                         $canvalidate = 1;
 1697:                                     }
 1698:                                 }
 1699:                                 next if (!$canvalidate);
 1700:                             }
 1701:                             my $checked = '';
 1702:                             if ($option eq $curroption) {
 1703:                                 $checked = ' checked="checked"';
 1704:                             } elsif ($option eq 'autolimit') {
 1705:                                 if ($curroption =~ /^autolimit/) {
 1706:                                     $checked = ' checked="checked"';
 1707:                                 }                       
 1708:                             } 
 1709:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1710:                                   '<input type="radio" name="crsreq_'.$item.
 1711:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1712:                                   $titles{$option}.'</label>';
 1713:                             if ($option eq 'autolimit') {
 1714:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1715:                                                 $item.'_limit_'.$type.'" size="1" '.
 1716:                                                 'value="'.$currlimit.'" />';
 1717:                             }
 1718:                             $cell{$item} .= '</span> ';
 1719:                             if ($option eq 'autolimit') {
 1720:                                 $cell{$item} .= $titles{'unlimited'};
 1721:                             }
 1722:                         }
 1723:                     } elsif ($context eq 'requestauthor') {
 1724:                         my $curroption;
 1725:                         if (ref($settings) eq 'HASH') {
 1726:                             $curroption = $settings->{$type};
 1727:                         }
 1728:                         if (!$curroption) {
 1729:                             $curroption = 'norequest';
 1730:                         }
 1731:                         foreach my $option (@options) {
 1732:                             my $val = $option;
 1733:                             if ($option eq 'norequest') {
 1734:                                 $val = 0;
 1735:                             }
 1736:                             my $checked = '';
 1737:                             if ($option eq $curroption) {
 1738:                                 $checked = ' checked="checked"';
 1739:                             }
 1740:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1741:                                   '<input type="radio" name="authorreq_'.$type.
 1742:                                   '" value="'.$val.'"'.$checked.' />'.
 1743:                                   $titles{$option}.'</label></span>&nbsp; ';
 1744:                         }
 1745:                     } else {
 1746:                         my $checked = 'checked="checked" ';
 1747:                         if (ref($settings) eq 'HASH') {
 1748:                             if (ref($settings->{$item}) eq 'HASH') {
 1749:                                 if ($settings->{$item}->{$type} == 0) {
 1750:                                     $checked = '';
 1751:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1752:                                     $checked =  'checked="checked" ';
 1753:                                 }
 1754:                             }
 1755:                         }
 1756:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1757:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1758:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1759:                                       '</label></span>&nbsp; ';
 1760:                     }
 1761:                 }
 1762:                 if ($context eq 'requestcourses') {
 1763:                     $datatable .= '</tr><tr>';
 1764:                     foreach my $item (@usertools) {
 1765:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1766:                     }
 1767:                     $datatable .= '</tr></table>';
 1768:                 }
 1769:                 $datatable .= '</td>';
 1770:                 unless (($context eq 'requestcourses') ||
 1771:                         ($context eq 'requestauthor')) {
 1772:                     $datatable .= 
 1773:                               '<td class="LC_right_item">'.
 1774:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1775:                               '<input type="text" name="quota_'.$type.
 1776:                               '" value="'.$currdefquota.
 1777:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1778:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1779:                               '<input type="text" name="authorquota_'.$type.
 1780:                               '" value="'.$currauthorquota.
 1781:                               '" size="5" /></span></td>';
 1782:                 }
 1783:                 $datatable .= '</tr>';
 1784:             }
 1785:         }
 1786:     }
 1787:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1788:         $defaultquota = '20';
 1789:         $authorquota = '500';
 1790:         if (ref($settings) eq 'HASH') {
 1791:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1792:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1793:             } elsif (defined($settings->{'default'})) {
 1794:                 $defaultquota = $settings->{'default'};
 1795:             }
 1796:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1797:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1798:             }
 1799:         }
 1800:     }
 1801:     $typecount ++;
 1802:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1803:     $datatable .= '<tr'.$css_class.'>'.
 1804:                   '<td>'.$othertitle.'</td>'.
 1805:                   '<td class="LC_left_item">';
 1806:     if ($context eq 'requestcourses') {
 1807:         $datatable .= '<table><tr>';
 1808:     }
 1809:     my %defcell;
 1810:     foreach my $item (@usertools) {
 1811:         if ($context eq 'requestcourses') {
 1812:             my ($curroption,$currlimit);
 1813:             if (ref($settings) eq 'HASH') {
 1814:                 if (ref($settings->{$item}) eq 'HASH') {
 1815:                     $curroption = $settings->{$item}->{'default'};
 1816:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1817:                         $currlimit = $1;
 1818:                     }
 1819:                 }
 1820:             }
 1821:             if (!$curroption) {
 1822:                 $curroption = 'norequest';
 1823:             }
 1824:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1825:             foreach my $option (@options) {
 1826:                 my $val = $option;
 1827:                 if ($option eq 'norequest') {
 1828:                     $val = 0;
 1829:                 }
 1830:                 if ($option eq 'validate') {
 1831:                     my $canvalidate = 0;
 1832:                     if (ref($validations{$item}) eq 'HASH') {
 1833:                         if ($validations{$item}{'default'}) {
 1834:                             $canvalidate = 1;
 1835:                         }
 1836:                     }
 1837:                     next if (!$canvalidate);
 1838:                 }
 1839:                 my $checked = '';
 1840:                 if ($option eq $curroption) {
 1841:                     $checked = ' checked="checked"';
 1842:                 } elsif ($option eq 'autolimit') {
 1843:                     if ($curroption =~ /^autolimit/) {
 1844:                         $checked = ' checked="checked"';
 1845:                     }
 1846:                 }
 1847:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1848:                                   '<input type="radio" name="crsreq_'.$item.
 1849:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1850:                                   $titles{$option}.'</label>';
 1851:                 if ($option eq 'autolimit') {
 1852:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1853:                                        $item.'_limit_default" size="1" '.
 1854:                                        'value="'.$currlimit.'" />';
 1855:                 }
 1856:                 $defcell{$item} .= '</span> ';
 1857:                 if ($option eq 'autolimit') {
 1858:                     $defcell{$item} .= $titles{'unlimited'};
 1859:                 }
 1860:             }
 1861:         } elsif ($context eq 'requestauthor') {
 1862:             my $curroption;
 1863:             if (ref($settings) eq 'HASH') {
 1864:                 $curroption = $settings->{'default'};
 1865:             }
 1866:             if (!$curroption) {
 1867:                 $curroption = 'norequest';
 1868:             }
 1869:             foreach my $option (@options) {
 1870:                 my $val = $option;
 1871:                 if ($option eq 'norequest') {
 1872:                     $val = 0;
 1873:                 }
 1874:                 my $checked = '';
 1875:                 if ($option eq $curroption) {
 1876:                     $checked = ' checked="checked"';
 1877:                 }
 1878:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1879:                               '<input type="radio" name="authorreq_default"'.
 1880:                               ' value="'.$val.'"'.$checked.' />'.
 1881:                               $titles{$option}.'</label></span>&nbsp; ';
 1882:             }
 1883:         } else {
 1884:             my $checked = 'checked="checked" ';
 1885:             if (ref($settings) eq 'HASH') {
 1886:                 if (ref($settings->{$item}) eq 'HASH') {
 1887:                     if ($settings->{$item}->{'default'} == 0) {
 1888:                         $checked = '';
 1889:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1890:                         $checked = 'checked="checked" ';
 1891:                     }
 1892:                 }
 1893:             }
 1894:             $datatable .= '<span class="LC_nobreak"><label>'.
 1895:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1896:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1897:                           '</label></span>&nbsp; ';
 1898:         }
 1899:     }
 1900:     if ($context eq 'requestcourses') {
 1901:         $datatable .= '</tr><tr>';
 1902:         foreach my $item (@usertools) {
 1903:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1904:         }
 1905:         $datatable .= '</tr></table>';
 1906:     }
 1907:     $datatable .= '</td>';
 1908:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1909:         $datatable .= '<td class="LC_right_item">'.
 1910:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1911:                       '<input type="text" name="defaultquota" value="'.
 1912:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1913:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1914:                       '<input type="text" name="authorquota" value="'.
 1915:                       $authorquota.'" size="5" /></span></td>';
 1916:     }
 1917:     $datatable .= '</tr>';
 1918:     $typecount ++;
 1919:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1920:     $datatable .= '<tr'.$css_class.'>'.
 1921:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1922:     if ($context eq 'requestcourses') {
 1923:         $datatable .= &mt('(overrides affiliation, if set)').
 1924:                       '</td>'.
 1925:                       '<td class="LC_left_item">'.
 1926:                       '<table><tr>';
 1927:     } else {
 1928:         $datatable .= &mt('(overrides affiliation, if checked)').
 1929:                       '</td>'.
 1930:                       '<td class="LC_left_item" colspan="2">'.
 1931:                       '<br />';
 1932:     }
 1933:     my %advcell;
 1934:     foreach my $item (@usertools) {
 1935:         if ($context eq 'requestcourses') {
 1936:             my ($curroption,$currlimit);
 1937:             if (ref($settings) eq 'HASH') {
 1938:                 if (ref($settings->{$item}) eq 'HASH') {
 1939:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1940:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1941:                         $currlimit = $1;
 1942:                     }
 1943:                 }
 1944:             }
 1945:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1946:             my $checked = '';
 1947:             if ($curroption eq '') {
 1948:                 $checked = ' checked="checked"';
 1949:             }
 1950:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1951:                                '<input type="radio" name="crsreq_'.$item.
 1952:                                '__LC_adv" value=""'.$checked.' />'.
 1953:                                &mt('No override set').'</label></span>&nbsp; ';
 1954:             foreach my $option (@options) {
 1955:                 my $val = $option;
 1956:                 if ($option eq 'norequest') {
 1957:                     $val = 0;
 1958:                 }
 1959:                 if ($option eq 'validate') {
 1960:                     my $canvalidate = 0;
 1961:                     if (ref($validations{$item}) eq 'HASH') {
 1962:                         if ($validations{$item}{'_LC_adv'}) {
 1963:                             $canvalidate = 1;
 1964:                         }
 1965:                     }
 1966:                     next if (!$canvalidate);
 1967:                 }
 1968:                 my $checked = '';
 1969:                 if ($val eq $curroption) {
 1970:                     $checked = ' checked="checked"';
 1971:                 } elsif ($option eq 'autolimit') {
 1972:                     if ($curroption =~ /^autolimit/) {
 1973:                         $checked = ' checked="checked"';
 1974:                     }
 1975:                 }
 1976:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1977:                                   '<input type="radio" name="crsreq_'.$item.
 1978:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1979:                                   $titles{$option}.'</label>';
 1980:                 if ($option eq 'autolimit') {
 1981:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1982:                                        $item.'_limit__LC_adv" size="1" '.
 1983:                                        'value="'.$currlimit.'" />';
 1984:                 }
 1985:                 $advcell{$item} .= '</span> ';
 1986:                 if ($option eq 'autolimit') {
 1987:                     $advcell{$item} .= $titles{'unlimited'};
 1988:                 }
 1989:             }
 1990:         } elsif ($context eq 'requestauthor') {
 1991:             my $curroption;
 1992:             if (ref($settings) eq 'HASH') {
 1993:                 $curroption = $settings->{'_LC_adv'};
 1994:             }
 1995:             my $checked = '';
 1996:             if ($curroption eq '') {
 1997:                 $checked = ' checked="checked"';
 1998:             }
 1999:             $datatable .= '<span class="LC_nobreak"><label>'.
 2000:                           '<input type="radio" name="authorreq__LC_adv"'.
 2001:                           ' value=""'.$checked.' />'.
 2002:                           &mt('No override set').'</label></span>&nbsp; ';
 2003:             foreach my $option (@options) {
 2004:                 my $val = $option;
 2005:                 if ($option eq 'norequest') {
 2006:                     $val = 0;
 2007:                 }
 2008:                 my $checked = '';
 2009:                 if ($val eq $curroption) {
 2010:                     $checked = ' checked="checked"';
 2011:                 }
 2012:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2013:                               '<input type="radio" name="authorreq__LC_adv"'.
 2014:                               ' value="'.$val.'"'.$checked.' />'.
 2015:                               $titles{$option}.'</label></span>&nbsp; ';
 2016:             }
 2017:         } else {
 2018:             my $checked = 'checked="checked" ';
 2019:             if (ref($settings) eq 'HASH') {
 2020:                 if (ref($settings->{$item}) eq 'HASH') {
 2021:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2022:                         $checked = '';
 2023:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2024:                         $checked = 'checked="checked" ';
 2025:                     }
 2026:                 }
 2027:             }
 2028:             $datatable .= '<span class="LC_nobreak"><label>'.
 2029:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2030:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2031:                           '</label></span>&nbsp; ';
 2032:         }
 2033:     }
 2034:     if ($context eq 'requestcourses') {
 2035:         $datatable .= '</tr><tr>';
 2036:         foreach my $item (@usertools) {
 2037:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2038:         }
 2039:         $datatable .= '</tr></table>';
 2040:     }
 2041:     $datatable .= '</td></tr>';
 2042:     $$rowtotal += $typecount;
 2043:     return $datatable;
 2044: }
 2045: 
 2046: sub print_requestmail {
 2047:     my ($dom,$action,$settings,$rowtotal) = @_;
 2048:     my ($now,$datatable,%currapp);
 2049:     $now = time;
 2050:     if (ref($settings) eq 'HASH') {
 2051:         if (ref($settings->{'notify'}) eq 'HASH') {
 2052:             if ($settings->{'notify'}{'approval'} ne '') {
 2053:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2054:             }
 2055:         }
 2056:     }
 2057:     my $numinrow = 2;
 2058:     my $css_class;
 2059:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2060:     my $text;
 2061:     if ($action eq 'requestcourses') {
 2062:         $text = &mt('Receive notification of course requests requiring approval');
 2063:     } elsif ($action eq 'requestauthor') {
 2064:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2065:     } else {
 2066:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2067:     }
 2068:     $datatable = '<tr'.$css_class.'>'.
 2069:                  ' <td>'.$text.'</td>'.
 2070:                  ' <td class="LC_left_item">';
 2071:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2072:                                                  $action.'notifyapproval',%currapp);
 2073:     if ($numdc > 0) {
 2074:         $datatable .= $table;
 2075:     } else {
 2076:         $datatable .= &mt('There are no active Domain Coordinators');
 2077:     }
 2078:     $datatable .='</td></tr>';
 2079:     return $datatable;
 2080: }
 2081: 
 2082: sub print_studentcode {
 2083:     my ($settings,$rowtotal) = @_;
 2084:     my $rownum = 0; 
 2085:     my ($output,%current);
 2086:     my @crstypes = ('official','unofficial','community','textbook');
 2087:     if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2088:         foreach my $type (@crstypes) {
 2089:             $current{$type} = $settings->{'uniquecode'}{$type};
 2090:         }
 2091:     }
 2092:     $output .= '<tr>'.
 2093:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2094:                '<td class="LC_left_item">';
 2095:     foreach my $type (@crstypes) {
 2096:         my $check = ' ';
 2097:         if ($current{$type}) {
 2098:             $check = ' checked="checked" ';
 2099:         }
 2100:         $output .= '<span class="LC_nobreak"><label>'.
 2101:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2102:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2103:     }
 2104:     $output .= '</td></tr>';
 2105:     $$rowtotal ++;
 2106:     return $output;
 2107: }
 2108: 
 2109: sub print_textbookcourses {
 2110:     my ($dom,$type,$settings,$rowtotal) = @_;
 2111:     my $rownum = 0;
 2112:     my $css_class;
 2113:     my $itemcount = 1;
 2114:     my $maxnum = 0;
 2115:     my $bookshash;
 2116:     if (ref($settings) eq 'HASH') {
 2117:         $bookshash = $settings->{$type};
 2118:     }
 2119:     my %ordered;
 2120:     if (ref($bookshash) eq 'HASH') {
 2121:         foreach my $item (keys(%{$bookshash})) {
 2122:             if (ref($bookshash->{$item}) eq 'HASH') {
 2123:                 my $num = $bookshash->{$item}{'order'};
 2124:                 $ordered{$num} = $item;
 2125:             }
 2126:         }
 2127:     }
 2128:     my $confname = $dom.'-domainconfig';
 2129:     my $switchserver = &check_switchserver($dom,$confname);
 2130:     my $maxnum = scalar(keys(%ordered));
 2131:     my $datatable;
 2132:     if (keys(%ordered)) {
 2133:         my @items = sort { $a <=> $b } keys(%ordered);
 2134:         for (my $i=0; $i<@items; $i++) {
 2135:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2136:             my $key = $ordered{$items[$i]};
 2137:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2138:             my $coursetitle = $coursehash{'description'};
 2139:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2140:             if (ref($bookshash->{$key}) eq 'HASH') {
 2141:                 $subject = $bookshash->{$key}->{'subject'};
 2142:                 $title = $bookshash->{$key}->{'title'};
 2143:                 if ($type eq 'textbooks') {
 2144:                     $publisher = $bookshash->{$key}->{'publisher'};
 2145:                     $author = $bookshash->{$key}->{'author'};
 2146:                     $image = $bookshash->{$key}->{'image'};
 2147:                     if ($image ne '') {
 2148:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2149:                         my $imagethumb = "$path/tn-".$imagefile;
 2150:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2151:                     }
 2152:                 }
 2153:             }
 2154:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2155:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2156:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2157:             for (my $k=0; $k<=$maxnum; $k++) {
 2158:                 my $vpos = $k+1;
 2159:                 my $selstr;
 2160:                 if ($k == $i) {
 2161:                     $selstr = ' selected="selected" ';
 2162:                 }
 2163:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2164:             }
 2165:             $datatable .= '</select>'.('&nbsp;'x2).
 2166:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2167:                 &mt('Delete?').'</label></span></td>'.
 2168:                 '<td colspan="2">'.
 2169:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2170:                 ('&nbsp;'x2).
 2171:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2172:             if ($type eq 'textbooks') {
 2173:                 $datatable .= ('&nbsp;'x2).
 2174:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2175:                               ('&nbsp;'x2).
 2176:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2177:                               ('&nbsp;'x2).
 2178:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2179:                 if ($image) {
 2180:                     $datatable .= '<span class="LC_nobreak">'.
 2181:                                   $imgsrc.
 2182:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2183:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2184:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2185:                 }
 2186:                 if ($switchserver) {
 2187:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2188:                 } else {
 2189:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2190:                 }
 2191:             }
 2192:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2193:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2194:                           $coursetitle.'</span></td></tr>'."\n";
 2195:             $itemcount ++;
 2196:         }
 2197:     }
 2198:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2199:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2200:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2201:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2202:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2203:     for (my $k=0; $k<$maxnum+1; $k++) {
 2204:         my $vpos = $k+1;
 2205:         my $selstr;
 2206:         if ($k == $maxnum) {
 2207:             $selstr = ' selected="selected" ';
 2208:         }
 2209:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2210:     }
 2211:     $datatable .= '</select>&nbsp;'."\n".
 2212:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2213:                   '<td colspan="2">'.
 2214:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2215:                   ('&nbsp;'x2).
 2216:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2217:                   ('&nbsp;'x2);
 2218:     if ($type eq 'textbooks') {
 2219:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2220:                       ('&nbsp;'x2).
 2221:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2222:                       ('&nbsp;'x2).
 2223:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2224:         if ($switchserver) {
 2225:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2226:         } else {
 2227:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2228:         }
 2229:     }
 2230:     $datatable .= '</span>'."\n".
 2231:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2232:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2233:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2234:                   &Apache::loncommon::selectcourse_link
 2235:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2236:                   '</span></td>'."\n".
 2237:                   '</tr>'."\n";
 2238:     $itemcount ++;
 2239:     return $datatable;
 2240: }
 2241: 
 2242: sub textbookcourses_javascript {
 2243:     my ($settings) = @_;
 2244:     return unless(ref($settings) eq 'HASH');
 2245:     my (%ordered,%total,%jstext);
 2246:     foreach my $type ('textbooks','templates') {
 2247:         $total{$type} = 0;
 2248:         if (ref($settings->{$type}) eq 'HASH') {
 2249:             foreach my $item (keys(%{$settings->{$type}})) {
 2250:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2251:                     my $num = $settings->{$type}->{$item}{'order'};
 2252:                     $ordered{$type}{$num} = $item;
 2253:                 }
 2254:             }
 2255:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2256:         }
 2257:         my @jsarray = ();
 2258:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2259:             push(@jsarray,$ordered{$type}{$item});
 2260:         }
 2261:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2262:     }
 2263:     return <<"ENDSCRIPT";
 2264: <script type="text/javascript">
 2265: // <![CDATA[
 2266: function reorderBooks(form,item,caller) {
 2267:     var changedVal;
 2268: $jstext{'textbooks'};
 2269: $jstext{'templates'};
 2270:     var newpos;
 2271:     var maxh;
 2272:     if (caller == 'textbooks') {  
 2273:         newpos = 'textbooks_addbook_pos';
 2274:         maxh = 1 + $total{'textbooks'};
 2275:     } else {
 2276:         newpos = 'templates_addbook_pos';
 2277:         maxh = 1 + $total{'templates'};
 2278:     }
 2279:     var current = new Array;
 2280:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2281:     if (item == newpos) {
 2282:         changedVal = newitemVal;
 2283:     } else {
 2284:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2285:         current[newitemVal] = newpos;
 2286:     }
 2287:     if (caller == 'textbooks') {
 2288:         for (var i=0; i<textbooks.length; i++) {
 2289:             var elementName = 'textbooks_'+textbooks[i];
 2290:             if (elementName != item) {
 2291:                 if (form.elements[elementName]) {
 2292:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2293:                     current[currVal] = elementName;
 2294:                 }
 2295:             }
 2296:         }
 2297:     }
 2298:     if (caller == 'templates') {
 2299:         for (var i=0; i<templates.length; i++) {
 2300:             var elementName = 'templates_'+templates[i];
 2301:             if (elementName != item) {
 2302:                 if (form.elements[elementName]) {
 2303:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2304:                     current[currVal] = elementName;
 2305:                 }
 2306:             }
 2307:         }
 2308:     }
 2309:     var oldVal;
 2310:     for (var j=0; j<maxh; j++) {
 2311:         if (current[j] == undefined) {
 2312:             oldVal = j;
 2313:         }
 2314:     }
 2315:     if (oldVal < changedVal) {
 2316:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2317:            var elementName = current[k];
 2318:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2319:         }
 2320:     } else {
 2321:         for (var k=changedVal; k<oldVal; k++) {
 2322:             var elementName = current[k];
 2323:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2324:         }
 2325:     }
 2326:     return;
 2327: }
 2328: 
 2329: // ]]>
 2330: </script>
 2331: 
 2332: ENDSCRIPT
 2333: }
 2334: 
 2335: sub print_autoenroll {
 2336:     my ($dom,$settings,$rowtotal) = @_;
 2337:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2338:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2339:     if (ref($settings) eq 'HASH') {
 2340:         if (exists($settings->{'run'})) {
 2341:             if ($settings->{'run'} eq '0') {
 2342:                 $runoff = ' checked="checked" ';
 2343:                 $runon = ' ';
 2344:             } else {
 2345:                 $runon = ' checked="checked" ';
 2346:                 $runoff = ' ';
 2347:             }
 2348:         } else {
 2349:             if ($autorun) {
 2350:                 $runon = ' checked="checked" ';
 2351:                 $runoff = ' ';
 2352:             } else {
 2353:                 $runoff = ' checked="checked" ';
 2354:                 $runon = ' ';
 2355:             }
 2356:         }
 2357:         if (exists($settings->{'co-owners'})) {
 2358:             if ($settings->{'co-owners'} eq '0') {
 2359:                 $coownersoff = ' checked="checked" ';
 2360:                 $coownerson = ' ';
 2361:             } else {
 2362:                 $coownerson = ' checked="checked" ';
 2363:                 $coownersoff = ' ';
 2364:             }
 2365:         } else {
 2366:             $coownersoff = ' checked="checked" ';
 2367:             $coownerson = ' ';
 2368:         }
 2369:         if (exists($settings->{'sender_domain'})) {
 2370:             $defdom = $settings->{'sender_domain'};
 2371:         }
 2372:     } else {
 2373:         if ($autorun) {
 2374:             $runon = ' checked="checked" ';
 2375:             $runoff = ' ';
 2376:         } else {
 2377:             $runoff = ' checked="checked" ';
 2378:             $runon = ' ';
 2379:         }
 2380:     }
 2381:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2382:     my $notif_sender;
 2383:     if (ref($settings) eq 'HASH') {
 2384:         $notif_sender = $settings->{'sender_uname'};
 2385:     }
 2386:     my $datatable='<tr class="LC_odd_row">'.
 2387:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2388:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2389:                   '<input type="radio" name="autoenroll_run"'.
 2390:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2391:                   '<label><input type="radio" name="autoenroll_run"'.
 2392:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2393:                   '</tr><tr>'.
 2394:                   '<td>'.&mt('Notification messages - sender').
 2395:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2396:                   &mt('username').':&nbsp;'.
 2397:                   '<input type="text" name="sender_uname" value="'.
 2398:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2399:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2400:                   '<tr class="LC_odd_row">'.
 2401:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2402:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2403:                   '<input type="radio" name="autoassign_coowners"'.
 2404:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2405:                   '<label><input type="radio" name="autoassign_coowners"'.
 2406:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2407:                   '</tr>';
 2408:     $$rowtotal += 3;
 2409:     return $datatable;
 2410: }
 2411: 
 2412: sub print_autoupdate {
 2413:     my ($position,$dom,$settings,$rowtotal) = @_;
 2414:     my $datatable;
 2415:     if ($position eq 'top') {
 2416:         my $updateon = ' ';
 2417:         my $updateoff = ' checked="checked" ';
 2418:         my $classlistson = ' ';
 2419:         my $classlistsoff = ' checked="checked" ';
 2420:         if (ref($settings) eq 'HASH') {
 2421:             if ($settings->{'run'} eq '1') {
 2422:                 $updateon = $updateoff;
 2423:                 $updateoff = ' ';
 2424:             }
 2425:             if ($settings->{'classlists'} eq '1') {
 2426:                 $classlistson = $classlistsoff;
 2427:                 $classlistsoff = ' ';
 2428:             }
 2429:         }
 2430:         my %title = (
 2431:                    run => 'Auto-update active?',
 2432:                    classlists => 'Update information in classlists?',
 2433:                     );
 2434:         $datatable = '<tr class="LC_odd_row">'. 
 2435:                   '<td>'.&mt($title{'run'}).'</td>'.
 2436:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2437:                   '<input type="radio" name="autoupdate_run"'.
 2438:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2439:                   '<label><input type="radio" name="autoupdate_run"'.
 2440:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2441:                   '</tr><tr>'.
 2442:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2443:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2444:                   '<label><input type="radio" name="classlists"'.
 2445:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2446:                   '<label><input type="radio" name="classlists"'.
 2447:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2448:                   '</tr>';
 2449:         $$rowtotal += 2;
 2450:     } elsif ($position eq 'middle') {
 2451:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2452:         my $numinrow = 3;
 2453:         my $locknamesettings;
 2454:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2455:                                      $dom,$numinrow,$othertitle,
 2456:                                     'lockablenames');
 2457:         $$rowtotal ++;
 2458:     } else {
 2459:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2460:         my @fields = ('lastname','firstname','middlename','generation',
 2461:                       'permanentemail','id');
 2462:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2463:         my $numrows = 0;
 2464:         if (ref($types) eq 'ARRAY') {
 2465:             if (@{$types} > 0) {
 2466:                 $datatable = 
 2467:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2468:                                          \@fields,$types,\$numrows);
 2469:                     $$rowtotal += @{$types}; 
 2470:             }
 2471:         }
 2472:         $datatable .= 
 2473:             &usertype_update_row($settings,{'default' => $othertitle},
 2474:                                  \%fieldtitles,\@fields,['default'],
 2475:                                  \$numrows);
 2476:         $$rowtotal ++;     
 2477:     }
 2478:     return $datatable;
 2479: }
 2480: 
 2481: sub print_autocreate {
 2482:     my ($dom,$settings,$rowtotal) = @_;
 2483:     my (%createon,%createoff,%currhash);
 2484:     my @types = ('xml','req');
 2485:     if (ref($settings) eq 'HASH') {
 2486:         foreach my $item (@types) {
 2487:             $createoff{$item} = ' checked="checked" ';
 2488:             $createon{$item} = ' ';
 2489:             if (exists($settings->{$item})) {
 2490:                 if ($settings->{$item}) {
 2491:                     $createon{$item} = ' checked="checked" ';
 2492:                     $createoff{$item} = ' ';
 2493:                 }
 2494:             }
 2495:         }
 2496:         if ($settings->{'xmldc'} ne '') {
 2497:             $currhash{$settings->{'xmldc'}} = 1;
 2498:         }
 2499:     } else {
 2500:         foreach my $item (@types) {
 2501:             $createoff{$item} = ' checked="checked" ';
 2502:             $createon{$item} = ' ';
 2503:         }
 2504:     }
 2505:     $$rowtotal += 2;
 2506:     my $numinrow = 2;
 2507:     my $datatable='<tr class="LC_odd_row">'.
 2508:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2509:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2510:                   '<input type="radio" name="autocreate_xml"'.
 2511:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2512:                   '<label><input type="radio" name="autocreate_xml"'.
 2513:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2514:                   '</td></tr><tr>'.
 2515:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2516:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2517:                   '<input type="radio" name="autocreate_req"'.
 2518:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2519:                   '<label><input type="radio" name="autocreate_req"'.
 2520:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2521:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2522:                                                    'autocreate_xmldc',%currhash);
 2523:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2524:     if ($numdc > 1) {
 2525:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2526:                       '</td><td class="LC_left_item">';
 2527:     } else {
 2528:         $datatable .= &mt('Course creation processed as:').
 2529:                       '</td><td class="LC_right_item">';
 2530:     }
 2531:     $datatable .= $dctable.'</td></tr>';
 2532:     $$rowtotal += $rows;
 2533:     return $datatable;
 2534: }
 2535: 
 2536: sub print_directorysrch {
 2537:     my ($dom,$settings,$rowtotal) = @_;
 2538:     my $srchon = ' ';
 2539:     my $srchoff = ' checked="checked" ';
 2540:     my ($exacton,$containson,$beginson);
 2541:     my $localon = ' ';
 2542:     my $localoff = ' checked="checked" ';
 2543:     if (ref($settings) eq 'HASH') {
 2544:         if ($settings->{'available'} eq '1') {
 2545:             $srchon = $srchoff;
 2546:             $srchoff = ' ';
 2547:         }
 2548:         if ($settings->{'localonly'} eq '1') {
 2549:             $localon = $localoff;
 2550:             $localoff = ' ';
 2551:         }
 2552:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2553:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2554:                 if ($type eq 'exact') {
 2555:                     $exacton = ' checked="checked" ';
 2556:                 } elsif ($type eq 'contains') {
 2557:                     $containson = ' checked="checked" ';
 2558:                 } elsif ($type eq 'begins') {
 2559:                     $beginson = ' checked="checked" ';
 2560:                 }
 2561:             }
 2562:         } else {
 2563:             if ($settings->{'searchtypes'} eq 'exact') {
 2564:                 $exacton = ' checked="checked" ';
 2565:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2566:                 $containson = ' checked="checked" ';
 2567:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2568:                 $exacton = ' checked="checked" ';
 2569:                 $containson = ' checked="checked" ';
 2570:             }
 2571:         }
 2572:     }
 2573:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2574:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2575: 
 2576:     my $numinrow = 4;
 2577:     my $cansrchrow = 0;
 2578:     my $datatable='<tr class="LC_odd_row">'.
 2579:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2580:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2581:                   '<input type="radio" name="dirsrch_available"'.
 2582:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2583:                   '<label><input type="radio" name="dirsrch_available"'.
 2584:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2585:                   '</tr><tr>'.
 2586:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2587:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2588:                   '<input type="radio" name="dirsrch_localonly"'.
 2589:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2590:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2591:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2592:                   '</tr>';
 2593:     $$rowtotal += 2;
 2594:     if (ref($usertypes) eq 'HASH') {
 2595:         if (keys(%{$usertypes}) > 0) {
 2596:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2597:                                          $numinrow,$othertitle,'cansearch');
 2598:             $cansrchrow = 1;
 2599:         }
 2600:     }
 2601:     if ($cansrchrow) {
 2602:         $$rowtotal ++;
 2603:         $datatable .= '<tr>';
 2604:     } else {
 2605:         $datatable .= '<tr class="LC_odd_row">';
 2606:     }
 2607:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2608:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2609:     foreach my $title (@{$titleorder}) {
 2610:         if (defined($searchtitles->{$title})) {
 2611:             my $check = ' ';
 2612:             if (ref($settings) eq 'HASH') {
 2613:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2614:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2615:                         $check = ' checked="checked" ';
 2616:                     }
 2617:                 }
 2618:             }
 2619:             $datatable .= '<td class="LC_left_item">'.
 2620:                           '<span class="LC_nobreak"><label>'.
 2621:                           '<input type="checkbox" name="searchby" '.
 2622:                           'value="'.$title.'"'.$check.'/>'.
 2623:                           $searchtitles->{$title}.'</label></span></td>';
 2624:         }
 2625:     }
 2626:     $datatable .= '</tr></table></td></tr>';
 2627:     $$rowtotal ++;
 2628:     if ($cansrchrow) {
 2629:         $datatable .= '<tr class="LC_odd_row">';
 2630:     } else {
 2631:         $datatable .= '<tr>';
 2632:     }
 2633:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2634:                   '<td class="LC_left_item" colspan="2">'.
 2635:                   '<span class="LC_nobreak"><label>'.
 2636:                   '<input type="checkbox" name="searchtypes" '.
 2637:                   $exacton.' value="exact" />'.&mt('Exact match').
 2638:                   '</label>&nbsp;'.
 2639:                   '<label><input type="checkbox" name="searchtypes" '.
 2640:                   $beginson.' value="begins" />'.&mt('Begins with').
 2641:                   '</label>&nbsp;'.
 2642:                   '<label><input type="checkbox" name="searchtypes" '.
 2643:                   $containson.' value="contains" />'.&mt('Contains').
 2644:                   '</label></span></td></tr>';
 2645:     $$rowtotal ++;
 2646:     return $datatable;
 2647: }
 2648: 
 2649: sub print_contacts {
 2650:     my ($dom,$settings,$rowtotal) = @_;
 2651:     my $datatable;
 2652:     my @contacts = ('adminemail','supportemail');
 2653:     my (%checked,%to,%otheremails,%bccemails);
 2654:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2655:                     'requestsmail','updatesmail','idconflictsmail');
 2656:     foreach my $type (@mailings) {
 2657:         $otheremails{$type} = '';
 2658:     }
 2659:     $bccemails{'helpdeskmail'} = '';
 2660:     if (ref($settings) eq 'HASH') {
 2661:         foreach my $item (@contacts) {
 2662:             if (exists($settings->{$item})) {
 2663:                 $to{$item} = $settings->{$item};
 2664:             }
 2665:         }
 2666:         foreach my $type (@mailings) {
 2667:             if (exists($settings->{$type})) {
 2668:                 if (ref($settings->{$type}) eq 'HASH') {
 2669:                     foreach my $item (@contacts) {
 2670:                         if ($settings->{$type}{$item}) {
 2671:                             $checked{$type}{$item} = ' checked="checked" ';
 2672:                         }
 2673:                     }
 2674:                     $otheremails{$type} = $settings->{$type}{'others'};
 2675:                     if ($type eq 'helpdeskmail') {
 2676:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2677:                     }
 2678:                 }
 2679:             } elsif ($type eq 'lonstatusmail') {
 2680:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2681:             }
 2682:         }
 2683:     } else {
 2684:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2685:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2686:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2687:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2688:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2689:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2690:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2691:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2692:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2693:     }
 2694:     my ($titles,$short_titles) = &contact_titles();
 2695:     my $rownum = 0;
 2696:     my $css_class;
 2697:     foreach my $item (@contacts) {
 2698:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2699:         $datatable .= '<tr'.$css_class.'>'. 
 2700:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2701:                   '</span></td><td class="LC_right_item">'.
 2702:                   '<input type="text" name="'.$item.'" value="'.
 2703:                   $to{$item}.'" /></td></tr>';
 2704:         $rownum ++;
 2705:     }
 2706:     foreach my $type (@mailings) {
 2707:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2708:         $datatable .= '<tr'.$css_class.'>'.
 2709:                       '<td><span class="LC_nobreak">'.
 2710:                       $titles->{$type}.': </span></td>'.
 2711:                       '<td class="LC_left_item">'.
 2712:                       '<span class="LC_nobreak">';
 2713:         foreach my $item (@contacts) {
 2714:             $datatable .= '<label>'.
 2715:                           '<input type="checkbox" name="'.$type.'"'.
 2716:                           $checked{$type}{$item}.
 2717:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2718:                           '</label>&nbsp;';
 2719:         }
 2720:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2721:                       '<input type="text" name="'.$type.'_others" '.
 2722:                       'value="'.$otheremails{$type}.'"  />';
 2723:         if ($type eq 'helpdeskmail') {
 2724:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2725:                           '<input type="text" name="'.$type.'_bcc" '.
 2726:                           'value="'.$bccemails{$type}.'"  />';
 2727:         }
 2728:         $datatable .= '</td></tr>'."\n";
 2729:         $rownum ++;
 2730:     }
 2731:     my %choices;
 2732:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2733:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2734:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2735:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2736:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2737:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2738:     my @toggles = ('reporterrors','reportupdates');
 2739:     my %defaultchecked = ('reporterrors'  => 'on',
 2740:                           'reportupdates' => 'on');
 2741:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2742:                                                \%choices,$rownum);
 2743:     $datatable .= $reports;
 2744:     $$rowtotal += $rownum;
 2745:     return $datatable;
 2746: }
 2747: 
 2748: sub print_helpsettings {
 2749:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2750:     my ($datatable,$itemcount);
 2751:     $itemcount = 1;
 2752:     my (%choices,%defaultchecked,@toggles);
 2753:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2754:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2755:                                  &mt('LON-CAPA bug tracker'),600,500));
 2756:     %defaultchecked = ('submitbugs' => 'on');
 2757:     @toggles = ('submitbugs',);
 2758: 
 2759:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2760:                                                  \%choices,$itemcount);
 2761:     return $datatable;
 2762: }
 2763: 
 2764: sub radiobutton_prefs {
 2765:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2766:         $additional) = @_;
 2767:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2768:                    (ref($choices) eq 'HASH'));
 2769: 
 2770:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2771: 
 2772:     foreach my $item (@{$toggles}) {
 2773:         if ($defaultchecked->{$item} eq 'on') {
 2774:             $checkedon{$item} = ' checked="checked" ';
 2775:             $checkedoff{$item} = ' ';
 2776:         } elsif ($defaultchecked->{$item} eq 'off') {
 2777:             $checkedoff{$item} = ' checked="checked" ';
 2778:             $checkedon{$item} = ' ';
 2779:         }
 2780:     }
 2781:     if (ref($settings) eq 'HASH') {
 2782:         foreach my $item (@{$toggles}) {
 2783:             if ($settings->{$item} eq '1') {
 2784:                 $checkedon{$item} =  ' checked="checked" ';
 2785:                 $checkedoff{$item} = ' ';
 2786:             } elsif ($settings->{$item} eq '0') {
 2787:                 $checkedoff{$item} =  ' checked="checked" ';
 2788:                 $checkedon{$item} = ' ';
 2789:             }
 2790:         }
 2791:     }
 2792:     if ($onclick) {
 2793:         $onclick = ' onclick="'.$onclick.'"';
 2794:     }
 2795:     foreach my $item (@{$toggles}) {
 2796:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2797:         $datatable .=
 2798:             '<tr'.$css_class.'><td valign="top">'.
 2799:             '<span class="LC_nobreak">'.$choices->{$item}.
 2800:             '</span></td>'.
 2801:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2802:             '<label><input type="radio" name="'.
 2803:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2804:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2805:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2806:             '</span>'.$additional.
 2807:             '</td>'.
 2808:             '</tr>';
 2809:         $itemcount ++;
 2810:     }
 2811:     return ($datatable,$itemcount);
 2812: }
 2813: 
 2814: sub print_coursedefaults {
 2815:     my ($position,$dom,$settings,$rowtotal) = @_;
 2816:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2817:     my $itemcount = 1;
 2818:     my %choices =  &Apache::lonlocal::texthash (
 2819:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2820:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2821:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2822:         coursecredits        => 'Credits can be specified for courses',
 2823:     );
 2824:     my %staticdefaults = (
 2825:                            anonsurvey_threshold => 10,
 2826:                            uploadquota          => 500,
 2827:                          );
 2828:     if ($position eq 'top') {
 2829:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2830:         @toggles = ('canuse_pdfforms');
 2831:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2832:                                                  \%choices,$itemcount);
 2833:     } else {
 2834:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2835:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2836:             %curruploadquota);
 2837:         my $currusecredits = 0;
 2838:         my @types = ('official','unofficial','community','textbook');
 2839:         if (ref($settings) eq 'HASH') {
 2840:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2841:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2842:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2843:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2844:                 }
 2845:             }
 2846:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2847:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2848:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2849:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2850:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2851:                     ($def_textbook_credits ne '')) {
 2852:                     $currusecredits = 1;
 2853:                 }
 2854:             }
 2855:         }
 2856:         if (!$currdefresponder) {
 2857:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2858:         } elsif ($currdefresponder < 1) {
 2859:             $currdefresponder = 1;
 2860:         }
 2861:         foreach my $type (@types) {
 2862:             if ($curruploadquota{$type} eq '') {
 2863:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2864:             }
 2865:         }
 2866:         $datatable .=
 2867:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2868:                 $choices{'anonsurvey_threshold'}.
 2869:                 '</span></td>'.
 2870:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2871:                 '<input type="text" name="anonsurvey_threshold"'.
 2872:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2873:                 '</td></tr>'."\n";
 2874:         $itemcount ++;
 2875:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2876:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2877:                       $choices{'uploadquota'}.
 2878:                       '</span></td>'.
 2879:                       '<td align="right" class="LC_right_item">'.
 2880:                       '<table><tr>';
 2881:         foreach my $type (@types) {
 2882:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2883:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2884:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2885:         }
 2886:         $datatable .= '</tr></table></td></tr>'."\n";
 2887:         $itemcount ++;
 2888:         my $onclick = "toggleDisplay(this.form,'credits');";
 2889:         my $display = 'none';
 2890:         if ($currusecredits) {
 2891:             $display = 'block';
 2892:         }
 2893:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2894:                          '<span class="LC_nobreak">'.
 2895:                          &mt('Default credits for official courses [_1]',
 2896:                          '<input type="text" name="official_credits" value="'.
 2897:                          $def_official_credits.'" size="3" />').
 2898:                          '</span><br />'.
 2899:                          '<span class="LC_nobreak">'.
 2900:                          &mt('Default credits for unofficial courses [_1]',
 2901:                          '<input type="text" name="unofficial_credits" value="'.
 2902:                          $def_unofficial_credits.'" size="3" />').
 2903:                          '</span><br />'.
 2904:                          '<span class="LC_nobreak">'.
 2905:                          &mt('Default credits for textbook courses [_1]',
 2906:                          '<input type="text" name="textbook_credits" value="'.
 2907:                          $def_textbook_credits.'" size="3" />').
 2908:                          '</span></div>'."\n";
 2909:         %defaultchecked = ('coursecredits' => 'off');
 2910:         @toggles = ('coursecredits');
 2911:         my $current = {
 2912:                         'coursecredits' => $currusecredits,
 2913:                       };
 2914:         (my $table,$itemcount) =
 2915:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2916:                                \%choices,$itemcount,$onclick,$additional);
 2917:         $datatable .= $table;
 2918:         $itemcount ++;
 2919:     }
 2920:     $$rowtotal += $itemcount;
 2921:     return $datatable;
 2922: }
 2923: 
 2924: sub print_selfenrollment {
 2925:     my ($position,$dom,$settings,$rowtotal) = @_;
 2926:     my ($css_class,$datatable);
 2927:     my $itemcount = 1;
 2928:     my @types = ('official','unofficial','community','textbook');
 2929:     if (($position eq 'top') || ($position eq 'middle')) {
 2930:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 2931:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 2932:         my @rows;
 2933:         my $key;
 2934:         if ($position eq 'top') {
 2935:             $key = 'admin'; 
 2936:             if (ref($rowsref) eq 'ARRAY') {
 2937:                 @rows = @{$rowsref};
 2938:             }
 2939:         } elsif ($position eq 'middle') {
 2940:             $key = 'default';
 2941:             @rows = ('types','registered','approval','limit');
 2942:         }
 2943:         foreach my $row (@rows) {
 2944:             if (defined($titlesref->{$row})) {
 2945:                 $itemcount ++;
 2946:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2947:                 $datatable .= '<tr'.$css_class.'>'.
 2948:                               '<td>'.$titlesref->{$row}.'</td>'.
 2949:                               '<td class="LC_left_item">'.
 2950:                               '<table><tr>';
 2951:                 my (%current,%currentcap);
 2952:                 if (ref($settings) eq 'HASH') {
 2953:                     if (ref($settings->{$key}) eq 'HASH') {
 2954:                         foreach my $type (@types) {
 2955:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2956:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 2957:                             }
 2958:                             if (($row eq 'limit') && ($key eq 'default')) {
 2959:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2960:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 2961:                                 }
 2962:                             }
 2963:                         }
 2964:                     }
 2965:                 }
 2966:                 my %roles = (
 2967:                              '0' => &Apache::lonnet::plaintext('dc'),
 2968:                             ); 
 2969:             
 2970:                 foreach my $type (@types) {
 2971:                     unless (($row eq 'registered') && ($key eq 'default')) {
 2972:                         $datatable .= '<th>'.&mt($type).'</th>';
 2973:                     }
 2974:                 }
 2975:                 unless (($row eq 'registered') && ($key eq 'default')) {
 2976:                     $datatable .= '</tr><tr>';
 2977:                 }
 2978:                 foreach my $type (@types) {
 2979:                     if ($type eq 'community') {
 2980:                         $roles{'1'} = &mt('Community personnel');
 2981:                     } else {
 2982:                         $roles{'1'} = &mt('Course personnel');
 2983:                     }
 2984:                     $datatable .= '<td style="vertical-align: top">';
 2985:                     if ($position eq 'top') {
 2986:                         my %checked;
 2987:                         if ($current{$type} eq '0') {
 2988:                             $checked{'0'} = ' checked="checked"';
 2989:                         } else {
 2990:                             $checked{'1'} = ' checked="checked"';
 2991:                         }
 2992:                         foreach my $role ('1','0') {
 2993:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2994:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 2995:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 2996:                                           $roles{$role}.'</label></span> ';
 2997:                         }
 2998:                     } else {
 2999:                         if ($row eq 'types') {
 3000:                             my %checked;
 3001:                             if ($current{$type} =~ /^(all|dom)$/) {
 3002:                                 $checked{$1} = ' checked="checked"';
 3003:                             } else {
 3004:                                 $checked{''} = ' checked="checked"';
 3005:                             }
 3006:                             foreach my $val ('','dom','all') {
 3007:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3008:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3009:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3010:                             }
 3011:                         } elsif ($row eq 'registered') {
 3012:                             my %checked;
 3013:                             if ($current{$type} eq '1') {
 3014:                                 $checked{'1'} = ' checked="checked"';
 3015:                             } else {
 3016:                                 $checked{'0'} = ' checked="checked"';
 3017:                             }
 3018:                             foreach my $val ('0','1') {
 3019:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3020:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3021:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3022:                             }
 3023:                         } elsif ($row eq 'approval') {
 3024:                             my %checked;
 3025:                             if ($current{$type} =~ /^([12])$/) {
 3026:                                 $checked{$1} = ' checked="checked"';
 3027:                             } else {
 3028:                                 $checked{'0'} = ' checked="checked"';
 3029:                             }
 3030:                             for my $val (0..2) {
 3031:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3032:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3033:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3034:                             }
 3035:                         } elsif ($row eq 'limit') {
 3036:                             my %checked;
 3037:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3038:                                 $checked{$1} = ' checked="checked"';
 3039:                             } else {
 3040:                                 $checked{'none'} = ' checked="checked"';
 3041:                             }
 3042:                             my $cap;
 3043:                             if ($currentcap{$type} =~ /^\d+$/) {
 3044:                                 $cap = $currentcap{$type};
 3045:                             }
 3046:                             foreach my $val ('none','allstudents','selfenrolled') {
 3047:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3048:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3049:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3050:                             }
 3051:                             $datatable .= '<br />'.
 3052:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3053:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3054:                                           '</span>'; 
 3055:                         }
 3056:                     }
 3057:                     $datatable .= '</td>';
 3058:                 }
 3059:                 $datatable .= '</tr>';
 3060:             }
 3061:             $datatable .= '</table></td></tr>';
 3062:         }
 3063:     } elsif ($position eq 'bottom') {
 3064:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3065:     }
 3066:     $$rowtotal += $itemcount;
 3067:     return $datatable;
 3068: }
 3069: 
 3070: sub print_validation_rows {
 3071:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3072:     my ($itemsref,$namesref,$fieldsref);
 3073:     if ($caller eq 'selfenroll') { 
 3074:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3075:     } elsif ($caller eq 'requestcourses') {
 3076:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3077:     }
 3078:     my %currvalidation;
 3079:     if (ref($settings) eq 'HASH') {
 3080:         if (ref($settings->{'validation'}) eq 'HASH') {
 3081:             %currvalidation = %{$settings->{'validation'}};
 3082:         }
 3083:     }
 3084:     my $datatable;
 3085:     my $itemcount = 0;
 3086:     foreach my $item (@{$itemsref}) {
 3087:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3088:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3089:                       $namesref->{$item}.
 3090:                       '</span></td>'.
 3091:                       '<td class="LC_left_item">';
 3092:         if (($item eq 'url') || ($item eq 'button')) {
 3093:             $datatable .= '<span class="LC_nobreak">'.
 3094:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3095:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3096:         } elsif ($item eq 'fields') {
 3097:             my @currfields;
 3098:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3099:                 @currfields = @{$currvalidation{$item}};
 3100:             }
 3101:             foreach my $field (@{$fieldsref}) {
 3102:                 my $check = '';
 3103:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3104:                     $check = ' checked="checked"';
 3105:                 }
 3106:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3107:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3108:                               ' value="'.$field.'"'.$check.' />'.$field.
 3109:                               '</label></span> ';
 3110:             }
 3111:         } elsif ($item eq 'markup') {
 3112:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3113:                            $currvalidation{$item}.
 3114:                               '</textarea>';
 3115:         }
 3116:         $datatable .= '</td></tr>'."\n";
 3117:         if (ref($rowtotal)) {
 3118:             $itemcount ++;
 3119:         }
 3120:     }
 3121:     if ($caller eq 'requestcourses') {
 3122:         my %currhash;
 3123:         if (ref($settings->{'validation'}) eq 'HASH') {
 3124:             if ($settings->{'validation'}{'dc'} ne '') {
 3125:                 $currhash{$settings->{'validation'}{'dc'}} = 1;
 3126:             }
 3127:         }
 3128:         my $numinrow = 2;
 3129:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3130:                                                        'validationdc',%currhash);
 3131:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3132:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 3133:         if ($numdc > 1) {
 3134:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 3135:         } else {
 3136:             $datatable .=  &mt('Course creation processed as: ');
 3137:         }
 3138:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3139:         $itemcount ++;
 3140:     }
 3141:     if (ref($rowtotal)) {
 3142:         $$rowtotal += $itemcount;
 3143:     }
 3144:     return $datatable;
 3145: }
 3146: 
 3147: sub print_usersessions {
 3148:     my ($position,$dom,$settings,$rowtotal) = @_;
 3149:     my ($css_class,$datatable,%checked,%choices);
 3150:     my (%by_ip,%by_location,@intdoms);
 3151:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3152: 
 3153:     my @alldoms = &Apache::lonnet::all_domains();
 3154:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3155:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3156:     my %altids = &id_for_thisdom(%servers);
 3157:     my $itemcount = 1;
 3158:     if ($position eq 'top') {
 3159:         if (keys(%serverhomes) > 1) {
 3160:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3161:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 3162:         } else {
 3163:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3164:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3165:         }
 3166:     } else {
 3167:         if (keys(%by_location) == 0) {
 3168:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3169:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3170:         } else {
 3171:             my %lt = &usersession_titles();
 3172:             my $numinrow = 5;
 3173:             my $prefix;
 3174:             my @types;
 3175:             if ($position eq 'bottom') {
 3176:                 $prefix = 'remote';
 3177:                 @types = ('version','excludedomain','includedomain');
 3178:             } else {
 3179:                 $prefix = 'hosted';
 3180:                 @types = ('excludedomain','includedomain');
 3181:             }
 3182:             my (%current,%checkedon,%checkedoff);
 3183:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3184:             my @locations = sort(keys(%by_location));
 3185:             foreach my $type (@types) {
 3186:                 $checkedon{$type} = '';
 3187:                 $checkedoff{$type} = ' checked="checked"';
 3188:             }
 3189:             if (ref($settings) eq 'HASH') {
 3190:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3191:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3192:                         $current{$key} = $settings->{$prefix}{$key};
 3193:                         if ($key eq 'version') {
 3194:                             if ($current{$key} ne '') {
 3195:                                 $checkedon{$key} = ' checked="checked"';
 3196:                                 $checkedoff{$key} = '';
 3197:                             }
 3198:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3199:                             $checkedon{$key} = ' checked="checked"';
 3200:                             $checkedoff{$key} = '';
 3201:                         }
 3202:                     }
 3203:                 }
 3204:             }
 3205:             foreach my $type (@types) {
 3206:                 next if ($type ne 'version' && !@locations);
 3207:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3208:                 $datatable .= '<tr'.$css_class.'>
 3209:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3210:                                <span class="LC_nobreak">&nbsp;
 3211:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3212:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3213:                 if ($type eq 'version') {
 3214:                     my $selector = '<select name="'.$prefix.'_version">';
 3215:                     foreach my $version (@lcversions) {
 3216:                         my $selected = '';
 3217:                         if ($current{'version'} eq $version) {
 3218:                             $selected = ' selected="selected"';
 3219:                         }
 3220:                         $selector .= ' <option value="'.$version.'"'.
 3221:                                      $selected.'>'.$version.'</option>';
 3222:                     }
 3223:                     $selector .= '</select> ';
 3224:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3225:                 } else {
 3226:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3227:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3228:                                  ' />'.('&nbsp;'x2).
 3229:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3230:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3231:                                  "\n".
 3232:                                  '</div><div><table>';
 3233:                     my $rem;
 3234:                     for (my $i=0; $i<@locations; $i++) {
 3235:                         my ($showloc,$value,$checkedtype);
 3236:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3237:                             my $ip = $by_location{$locations[$i]}->[0];
 3238:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3239:                                  $value = join(':',@{$by_ip{$ip}});
 3240:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3241:                                 if (ref($current{$type}) eq 'ARRAY') {
 3242:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3243:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3244:                                             $checkedtype = ' checked="checked"';
 3245:                                             last;
 3246:                                         }
 3247:                                     }
 3248:                                 }
 3249:                             }
 3250:                         }
 3251:                         $rem = $i%($numinrow);
 3252:                         if ($rem == 0) {
 3253:                             if ($i > 0) {
 3254:                                 $datatable .= '</tr>';
 3255:                             }
 3256:                             $datatable .= '<tr>';
 3257:                         }
 3258:                         $datatable .= '<td class="LC_left_item">'.
 3259:                                       '<span class="LC_nobreak"><label>'.
 3260:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3261:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3262:                                       '</label></span></td>';
 3263:                     }
 3264:                     $rem = @locations%($numinrow);
 3265:                     my $colsleft = $numinrow - $rem;
 3266:                     if ($colsleft > 1 ) {
 3267:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3268:                                       '&nbsp;</td>';
 3269:                     } elsif ($colsleft == 1) {
 3270:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3271:                     }
 3272:                     $datatable .= '</tr></table>';
 3273:                 }
 3274:                 $datatable .= '</td></tr>';
 3275:                 $itemcount ++;
 3276:             }
 3277:         }
 3278:     }
 3279:     $$rowtotal += $itemcount;
 3280:     return $datatable;
 3281: }
 3282: 
 3283: sub build_location_hashes {
 3284:     my ($intdoms,$by_ip,$by_location) = @_;
 3285:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3286:                   (ref($by_location) eq 'HASH')); 
 3287:     my %iphost = &Apache::lonnet::get_iphost();
 3288:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3289:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3290:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3291:         foreach my $id (@{$iphost{$primary_ip}}) {
 3292:             my $intdom = &Apache::lonnet::internet_dom($id);
 3293:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3294:                 push(@{$intdoms},$intdom);
 3295:             }
 3296:         }
 3297:     }
 3298:     foreach my $ip (keys(%iphost)) {
 3299:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3300:             foreach my $id (@{$iphost{$ip}}) {
 3301:                 my $location = &Apache::lonnet::internet_dom($id);
 3302:                 if ($location) {
 3303:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3304:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3305:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3306:                             push(@{$by_ip->{$ip}},$location);
 3307:                         }
 3308:                     } else {
 3309:                         $by_ip->{$ip} = [$location];
 3310:                     }
 3311:                 }
 3312:             }
 3313:         }
 3314:     }
 3315:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3316:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3317:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3318:             my $first = $by_ip->{$ip}->[0];
 3319:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3320:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3321:                     push(@{$by_location->{$first}},$ip);
 3322:                 }
 3323:             } else {
 3324:                 $by_location->{$first} = [$ip];
 3325:             }
 3326:         }
 3327:     }
 3328:     return;
 3329: }
 3330: 
 3331: sub current_offloads_to {
 3332:     my ($dom,$settings,$servers) = @_;
 3333:     my (%spareid,%otherdomconfigs);
 3334:     if (ref($servers) eq 'HASH') {
 3335:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3336:             my $gotspares;
 3337:             if (ref($settings) eq 'HASH') {
 3338:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3339:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3340:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3341:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3342:                         $gotspares = 1;
 3343:                     }
 3344:                 }
 3345:             }
 3346:             unless ($gotspares) {
 3347:                 my $gotspares;
 3348:                 my $serverhomeID =
 3349:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3350:                 my $serverhomedom =
 3351:                     &Apache::lonnet::host_domain($serverhomeID);
 3352:                 if ($serverhomedom ne $dom) {
 3353:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3354:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3355:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3356:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3357:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3358:                                 $gotspares = 1;
 3359:                             }
 3360:                         }
 3361:                     } else {
 3362:                         $otherdomconfigs{$serverhomedom} =
 3363:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3364:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3365:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3366:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3367:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3368:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3369:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3370:                                         $gotspares = 1;
 3371:                                     }
 3372:                                 }
 3373:                             }
 3374:                         }
 3375:                     }
 3376:                 }
 3377:             }
 3378:             unless ($gotspares) {
 3379:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3380:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3381:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3382:                } else {
 3383:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3384:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3385:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3386:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3387:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3388:                     } else {
 3389:                         my %what = (
 3390:                              spareid => 1,
 3391:                         );
 3392:                         my ($result,$returnhash) = 
 3393:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3394:                         if ($result eq 'ok') { 
 3395:                             if (ref($returnhash) eq 'HASH') {
 3396:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3397:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3398:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3399:                                 }
 3400:                             }
 3401:                         }
 3402:                     }
 3403:                 }
 3404:             }
 3405:         }
 3406:     }
 3407:     return %spareid;
 3408: }
 3409: 
 3410: sub spares_row {
 3411:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3412:     my $css_class;
 3413:     my $numinrow = 4;
 3414:     my $itemcount = 1;
 3415:     my $datatable;
 3416:     my %typetitles = &sparestype_titles();
 3417:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3418:         foreach my $server (sort(keys(%{$servers}))) {
 3419:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3420:             my ($othercontrol,$serverdom);
 3421:             if ($serverhome ne $server) {
 3422:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3423:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3424:             } else {
 3425:                 $serverdom = &Apache::lonnet::host_domain($server);
 3426:                 if ($serverdom ne $dom) {
 3427:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3428:                 }
 3429:             }
 3430:             next unless (ref($spareid->{$server}) eq 'HASH');
 3431:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3432:             $datatable .= '<tr'.$css_class.'>
 3433:                            <td rowspan="2">
 3434:                             <span class="LC_nobreak">'.
 3435:                           &mt('[_1] when busy, offloads to:'
 3436:                               ,'<b>'.$server.'</b>').
 3437:                           "\n";
 3438:             my (%current,%canselect);
 3439:             my @choices = 
 3440:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3441:             foreach my $type ('primary','default') {
 3442:                 if (ref($spareid->{$server}) eq 'HASH') {
 3443:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3444:                         my @spares = @{$spareid->{$server}{$type}};
 3445:                         if (@spares > 0) {
 3446:                             if ($othercontrol) {
 3447:                                 $current{$type} = join(', ',@spares);
 3448:                             } else {
 3449:                                 $current{$type} .= '<table>';
 3450:                                 my $numspares = scalar(@spares);
 3451:                                 for (my $i=0;  $i<@spares; $i++) {
 3452:                                     my $rem = $i%($numinrow);
 3453:                                     if ($rem == 0) {
 3454:                                         if ($i > 0) {
 3455:                                             $current{$type} .= '</tr>';
 3456:                                         }
 3457:                                         $current{$type} .= '<tr>';
 3458:                                     }
 3459:                                     $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;'.
 3460:                                                        $spareid->{$server}{$type}[$i].
 3461:                                                        '</label></td>'."\n";
 3462:                                 }
 3463:                                 my $rem = @spares%($numinrow);
 3464:                                 my $colsleft = $numinrow - $rem;
 3465:                                 if ($colsleft > 1 ) {
 3466:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3467:                                                        '" class="LC_left_item">'.
 3468:                                                        '&nbsp;</td>';
 3469:                                 } elsif ($colsleft == 1) {
 3470:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3471:                                 }
 3472:                                 $current{$type} .= '</tr></table>';
 3473:                             }
 3474:                         }
 3475:                     }
 3476:                     if ($current{$type} eq '') {
 3477:                         $current{$type} = &mt('None specified');
 3478:                     }
 3479:                     if ($othercontrol) {
 3480:                         if ($type eq 'primary') {
 3481:                             $canselect{$type} = $othercontrol;
 3482:                         }
 3483:                     } else {
 3484:                         $canselect{$type} = 
 3485:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3486:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3487:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3488:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3489:                         if (@choices > 0) {
 3490:                             foreach my $lonhost (@choices) {
 3491:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3492:                             }
 3493:                         }
 3494:                         $canselect{$type} .= '</select>'."\n";
 3495:                     }
 3496:                 } else {
 3497:                     $current{$type} = &mt('Could not be determined');
 3498:                     if ($type eq 'primary') {
 3499:                         $canselect{$type} =  $othercontrol;
 3500:                     }
 3501:                 }
 3502:                 if ($type eq 'default') {
 3503:                     $datatable .= '<tr'.$css_class.'>';
 3504:                 }
 3505:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3506:                               '<td>'.$current{$type}.'</td>'."\n".
 3507:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3508:             }
 3509:             $itemcount ++;
 3510:         }
 3511:     }
 3512:     $$rowtotal += $itemcount;
 3513:     return $datatable;
 3514: }
 3515: 
 3516: sub possible_newspares {
 3517:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3518:     my $serverhostname = &Apache::lonnet::hostname($server);
 3519:     my %excluded;
 3520:     if ($serverhostname ne '') {
 3521:         %excluded = (
 3522:                        $serverhostname => 1,
 3523:                     );
 3524:     }
 3525:     if (ref($currspares) eq 'HASH') {
 3526:         foreach my $type (keys(%{$currspares})) {
 3527:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3528:                 if (@{$currspares->{$type}} > 0) {
 3529:                     foreach my $curr (@{$currspares->{$type}}) {
 3530:                         my $hostname = &Apache::lonnet::hostname($curr);
 3531:                         $excluded{$hostname} = 1;
 3532:                     }
 3533:                 }
 3534:             }
 3535:         }
 3536:     }
 3537:     my @choices;
 3538:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3539:         if (keys(%{$serverhomes}) > 1) {
 3540:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3541:                 unless ($excluded{$name}) {
 3542:                     if (exists($altids->{$serverhomes->{$name}})) {
 3543:                         push(@choices,$altids->{$serverhomes->{$name}});
 3544:                     } else {
 3545:                         push(@choices,$serverhomes->{$name});
 3546:                     }
 3547:                 }
 3548:             }
 3549:         }
 3550:     }
 3551:     return sort(@choices);
 3552: }
 3553: 
 3554: sub print_loadbalancing {
 3555:     my ($dom,$settings,$rowtotal) = @_;
 3556:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3557:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3558:     my $numinrow = 1;
 3559:     my $datatable;
 3560:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3561:     my (%currbalancer,%currtargets,%currrules,%existing);
 3562:     if (ref($settings) eq 'HASH') {
 3563:         %existing = %{$settings};
 3564:     }
 3565:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3566:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3567:                                   \%currtargets,\%currrules);
 3568:     } else {
 3569:         return;
 3570:     }
 3571:     my ($othertitle,$usertypes,$types) =
 3572:         &Apache::loncommon::sorted_inst_types($dom);
 3573:     my $rownum = 8;
 3574:     if (ref($types) eq 'ARRAY') {
 3575:         $rownum += scalar(@{$types});
 3576:     }
 3577:     my @css_class = ('LC_odd_row','LC_even_row');
 3578:     my $balnum = 0;
 3579:     my $islast;
 3580:     my (@toshow,$disabledtext);
 3581:     if (keys(%currbalancer) > 0) {
 3582:         @toshow = sort(keys(%currbalancer));
 3583:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3584:             push(@toshow,'');
 3585:         }
 3586:     } else {
 3587:         @toshow = ('');
 3588:         $disabledtext = &mt('No existing load balancer');
 3589:     }
 3590:     foreach my $lonhost (@toshow) {
 3591:         if ($balnum == scalar(@toshow)-1) {
 3592:             $islast = 1;
 3593:         } else {
 3594:             $islast = 0;
 3595:         }
 3596:         my $cssidx = $balnum%2;
 3597:         my $targets_div_style = 'display: none';
 3598:         my $disabled_div_style = 'display: block';
 3599:         my $homedom_div_style = 'display: none';
 3600:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3601:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3602:                       '<p>';
 3603:         if ($lonhost eq '') {
 3604:             $datatable .= '<span class="LC_nobreak">';
 3605:             if (keys(%currbalancer) > 0) {
 3606:                 $datatable .= &mt('Add balancer:');
 3607:             } else {
 3608:                 $datatable .= &mt('Enable balancer:');
 3609:             }
 3610:             $datatable .= '&nbsp;'.
 3611:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3612:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3613:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3614:                           '<option value="" selected="selected">'.&mt('None').
 3615:                           '</option>'."\n";
 3616:             foreach my $server (sort(keys(%servers))) {
 3617:                 next if ($currbalancer{$server});
 3618:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3619:             }
 3620:             $datatable .=
 3621:                 '</select>'."\n".
 3622:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3623:         } else {
 3624:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3625:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3626:                            &mt('Stop balancing').'</label>'.
 3627:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3628:             $targets_div_style = 'display: block';
 3629:             $disabled_div_style = 'display: none';
 3630:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3631:                 $homedom_div_style = 'display: block';
 3632:             }
 3633:         }
 3634:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3635:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3636:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3637:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3638:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3639:         my @sparestypes = ('primary','default');
 3640:         my %typetitles = &sparestype_titles();
 3641:         foreach my $sparetype (@sparestypes) {
 3642:             my $targettable;
 3643:             for (my $i=0; $i<$numspares; $i++) {
 3644:                 my $checked;
 3645:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3646:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3647:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3648:                             $checked = ' checked="checked"';
 3649:                         }
 3650:                     }
 3651:                 }
 3652:                 my ($chkboxval,$disabled);
 3653:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3654:                     $chkboxval = $spares[$i];
 3655:                 }
 3656:                 if (exists($currbalancer{$spares[$i]})) {
 3657:                     $disabled = ' disabled="disabled"';
 3658:                 }
 3659:                 $targettable .=
 3660:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3661:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3662:                     '</span></label></td>';
 3663:                 my $rem = $i%($numinrow);
 3664:                 if ($rem == 0) {
 3665:                     if (($i > 0) && ($i < $numspares-1)) {
 3666:                         $targettable .= '</tr>';
 3667:                     }
 3668:                     if ($i < $numspares-1) {
 3669:                         $targettable .= '<tr>';
 3670:                     }
 3671:                 }
 3672:             }
 3673:             if ($targettable ne '') {
 3674:                 my $rem = $numspares%($numinrow);
 3675:                 my $colsleft = $numinrow - $rem;
 3676:                 if ($colsleft > 1 ) {
 3677:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3678:                                     '&nbsp;</td>';
 3679:                 } elsif ($colsleft == 1) {
 3680:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3681:                 }
 3682:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3683:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3684:             }
 3685:         }
 3686:         $datatable .= '</div></td></tr>'.
 3687:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3688:                                            $othertitle,$usertypes,$types,\%servers,
 3689:                                            \%currbalancer,$lonhost,
 3690:                                            $targets_div_style,$homedom_div_style,
 3691:                                            $css_class[$cssidx],$balnum,$islast);
 3692:         $$rowtotal += $rownum;
 3693:         $balnum ++;
 3694:     }
 3695:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3696:     return $datatable;
 3697: }
 3698: 
 3699: sub get_loadbalancers_config {
 3700:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3701:     return unless ((ref($servers) eq 'HASH') &&
 3702:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3703:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3704:     if (keys(%{$existing}) > 0) {
 3705:         my $oldlonhost;
 3706:         foreach my $key (sort(keys(%{$existing}))) {
 3707:             if ($key eq 'lonhost') {
 3708:                 $oldlonhost = $existing->{'lonhost'};
 3709:                 $currbalancer->{$oldlonhost} = 1;
 3710:             } elsif ($key eq 'targets') {
 3711:                 if ($oldlonhost) {
 3712:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3713:                 }
 3714:             } elsif ($key eq 'rules') {
 3715:                 if ($oldlonhost) {
 3716:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3717:                 }
 3718:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3719:                 $currbalancer->{$key} = 1;
 3720:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3721:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3722:             }
 3723:         }
 3724:     } else {
 3725:         my ($balancerref,$targetsref) =
 3726:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3727:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3728:             foreach my $server (sort(keys(%{$balancerref}))) {
 3729:                 $currbalancer->{$server} = 1;
 3730:                 $currtargets->{$server} = $targetsref->{$server};
 3731:             }
 3732:         }
 3733:     }
 3734:     return;
 3735: }
 3736: 
 3737: sub loadbalancing_rules {
 3738:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3739:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3740:         $css_class,$balnum,$islast) = @_;
 3741:     my $output;
 3742:     my $num = 0;
 3743:     my ($alltypes,$othertypes,$titles) =
 3744:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3745:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3746:         foreach my $type (@{$alltypes}) {
 3747:             $num ++;
 3748:             my $current;
 3749:             if (ref($currrules) eq 'HASH') {
 3750:                 $current = $currrules->{$type};
 3751:             }
 3752:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3753:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3754:                     $current = '';
 3755:                 }
 3756:             }
 3757:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3758:                                              $servers,$currbalancer,$lonhost,$dom,
 3759:                                              $targets_div_style,$homedom_div_style,
 3760:                                              $css_class,$balnum,$num,$islast);
 3761:         }
 3762:     }
 3763:     return $output;
 3764: }
 3765: 
 3766: sub loadbalancing_titles {
 3767:     my ($dom,$intdom,$usertypes,$types) = @_;
 3768:     my %othertypes = (
 3769:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3770:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3771:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3772:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3773:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3774:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3775:                      );
 3776:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3777:     if (ref($types) eq 'ARRAY') {
 3778:         unshift(@alltypes,@{$types},'default');
 3779:     }
 3780:     my %titles;
 3781:     foreach my $type (@alltypes) {
 3782:         if ($type =~ /^_LC_/) {
 3783:             $titles{$type} = $othertypes{$type};
 3784:         } elsif ($type eq 'default') {
 3785:             $titles{$type} = &mt('All users from [_1]',$dom);
 3786:             if (ref($types) eq 'ARRAY') {
 3787:                 if (@{$types} > 0) {
 3788:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3789:                 }
 3790:             }
 3791:         } elsif (ref($usertypes) eq 'HASH') {
 3792:             $titles{$type} = $usertypes->{$type};
 3793:         }
 3794:     }
 3795:     return (\@alltypes,\%othertypes,\%titles);
 3796: }
 3797: 
 3798: sub loadbalance_rule_row {
 3799:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3800:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3801:     my @rulenames;
 3802:     my %ruletitles = &offloadtype_text();
 3803:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3804:         @rulenames = ('balancer','offloadedto');
 3805:     } else {
 3806:         @rulenames = ('default','homeserver');
 3807:         if ($type eq '_LC_external') {
 3808:             push(@rulenames,'externalbalancer');
 3809:         } else {
 3810:             push(@rulenames,'specific');
 3811:         }
 3812:         push(@rulenames,'none');
 3813:     }
 3814:     my $style = $targets_div_style;
 3815:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3816:         $style = $homedom_div_style;
 3817:     }
 3818:     my $space;
 3819:     if ($islast && $num == 1) {
 3820:         $space = '<div display="inline-block">&nbsp;</div>';
 3821:     }
 3822:     my $output =
 3823:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3824:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3825:         '<td valaign="top">'.$space.
 3826:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3827:     for (my $i=0; $i<@rulenames; $i++) {
 3828:         my $rule = $rulenames[$i];
 3829:         my ($checked,$extra);
 3830:         if ($rulenames[$i] eq 'default') {
 3831:             $rule = '';
 3832:         }
 3833:         if ($rulenames[$i] eq 'specific') {
 3834:             if (ref($servers) eq 'HASH') {
 3835:                 my $default;
 3836:                 if (($current ne '') && (exists($servers->{$current}))) {
 3837:                     $checked = ' checked="checked"';
 3838:                 }
 3839:                 unless ($checked) {
 3840:                     $default = ' selected="selected"';
 3841:                 }
 3842:                 $extra =
 3843:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3844:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3845:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3846:                     '<option value=""'.$default.'></option>'."\n";
 3847:                 foreach my $server (sort(keys(%{$servers}))) {
 3848:                     if (ref($currbalancer) eq 'HASH') {
 3849:                         next if (exists($currbalancer->{$server}));
 3850:                     }
 3851:                     my $selected;
 3852:                     if ($server eq $current) {
 3853:                         $selected = ' selected="selected"';
 3854:                     }
 3855:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3856:                 }
 3857:                 $extra .= '</select>';
 3858:             }
 3859:         } elsif ($rule eq $current) {
 3860:             $checked = ' checked="checked"';
 3861:         }
 3862:         $output .= '<span class="LC_nobreak"><label>'.
 3863:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3864:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3865:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3866:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3867:                    '</label>'.$extra.'</span><br />'."\n";
 3868:     }
 3869:     $output .= '</div></td></tr>'."\n";
 3870:     return $output;
 3871: }
 3872: 
 3873: sub offloadtype_text {
 3874:     my %ruletitles = &Apache::lonlocal::texthash (
 3875:            'default'          => 'Offloads to default destinations',
 3876:            'homeserver'       => "Offloads to user's home server",
 3877:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3878:            'specific'         => 'Offloads to specific server',
 3879:            'none'             => 'No offload',
 3880:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3881:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3882:     );
 3883:     return %ruletitles;
 3884: }
 3885: 
 3886: sub sparestype_titles {
 3887:     my %typestitles = &Apache::lonlocal::texthash (
 3888:                           'primary' => 'primary',
 3889:                           'default' => 'default',
 3890:                       );
 3891:     return %typestitles;
 3892: }
 3893: 
 3894: sub contact_titles {
 3895:     my %titles = &Apache::lonlocal::texthash (
 3896:                    'supportemail' => 'Support E-mail address',
 3897:                    'adminemail'   => 'Default Server Admin E-mail address',
 3898:                    'errormail'    => 'Error reports to be e-mailed to',
 3899:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3900:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3901:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3902:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3903:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3904:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3905:                  );
 3906:     my %short_titles = &Apache::lonlocal::texthash (
 3907:                            adminemail   => 'Admin E-mail address',
 3908:                            supportemail => 'Support E-mail',
 3909:                        );   
 3910:     return (\%titles,\%short_titles);
 3911: }
 3912: 
 3913: sub tool_titles {
 3914:     my %titles = &Apache::lonlocal::texthash (
 3915:                      aboutme    => 'Personal web page',
 3916:                      blog       => 'Blog',
 3917:                      webdav     => 'WebDAV',
 3918:                      portfolio  => 'Portfolio',
 3919:                      official   => 'Official courses (with institutional codes)',
 3920:                      unofficial => 'Unofficial courses',
 3921:                      community  => 'Communities',
 3922:                      textbook   => 'Textbook courses',
 3923:                  );
 3924:     return %titles;
 3925: }
 3926: 
 3927: sub courserequest_titles {
 3928:     my %titles = &Apache::lonlocal::texthash (
 3929:                                    official   => 'Official',
 3930:                                    unofficial => 'Unofficial',
 3931:                                    community  => 'Communities',
 3932:                                    textbook   => 'Textbook',
 3933:                                    norequest  => 'Not allowed',
 3934:                                    approval   => 'Approval by Dom. Coord.',
 3935:                                    validate   => 'With validation',
 3936:                                    autolimit  => 'Numerical limit',
 3937:                                    unlimited  => '(blank for unlimited)',
 3938:                  );
 3939:     return %titles;
 3940: }
 3941: 
 3942: sub authorrequest_titles {
 3943:     my %titles = &Apache::lonlocal::texthash (
 3944:                                    norequest  => 'Not allowed',
 3945:                                    approval   => 'Approval by Dom. Coord.',
 3946:                                    automatic  => 'Automatic approval',
 3947:                  );
 3948:     return %titles;
 3949: }
 3950: 
 3951: sub courserequest_conditions {
 3952:     my %conditions = &Apache::lonlocal::texthash (
 3953:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3954:        validate   => '(Processing of request subject to institutional validation).',
 3955:                  );
 3956:     return %conditions;
 3957: }
 3958: 
 3959: 
 3960: sub print_usercreation {
 3961:     my ($position,$dom,$settings,$rowtotal) = @_;
 3962:     my $numinrow = 4;
 3963:     my $datatable;
 3964:     if ($position eq 'top') {
 3965:         $$rowtotal ++;
 3966:         my $rowcount = 0;
 3967:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3968:         if (ref($rules) eq 'HASH') {
 3969:             if (keys(%{$rules}) > 0) {
 3970:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3971:                                                 $ruleorder,$numinrow,$rowcount);
 3972:                 $$rowtotal ++;
 3973:                 $rowcount ++;
 3974:             }
 3975:         }
 3976:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3977:         if (ref($idrules) eq 'HASH') {
 3978:             if (keys(%{$idrules}) > 0) {
 3979:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3980:                                                 $idruleorder,$numinrow,$rowcount);
 3981:                 $$rowtotal ++;
 3982:                 $rowcount ++;
 3983:             }
 3984:         }
 3985:         if ($rowcount == 0) {
 3986:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3987:             $$rowtotal ++;
 3988:             $rowcount ++;
 3989:         }
 3990:     } elsif ($position eq 'middle') {
 3991:         my @creators = ('author','course','requestcrs');
 3992:         my ($rules,$ruleorder) =
 3993:             &Apache::lonnet::inst_userrules($dom,'username');
 3994:         my %lt = &usercreation_types();
 3995:         my %checked;
 3996:         if (ref($settings) eq 'HASH') {
 3997:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3998:                 foreach my $item (@creators) {
 3999:                     $checked{$item} = $settings->{'cancreate'}{$item};
 4000:                 }
 4001:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 4002:                 foreach my $item (@creators) {
 4003:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 4004:                         $checked{$item} = 'none';
 4005:                     }
 4006:                 }
 4007:             }
 4008:         }
 4009:         my $rownum = 0;
 4010:         foreach my $item (@creators) {
 4011:             $rownum ++;
 4012:             if ($checked{$item} eq '') {
 4013:                 $checked{$item} = 'any';
 4014:             }
 4015:             my $css_class;
 4016:             if ($rownum%2) {
 4017:                 $css_class = '';
 4018:             } else {
 4019:                 $css_class = ' class="LC_odd_row" ';
 4020:             }
 4021:             $datatable .= '<tr'.$css_class.'>'.
 4022:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 4023:                          '</span></td><td align="right">';
 4024:             my @options = ('any');
 4025:             if (ref($rules) eq 'HASH') {
 4026:                 if (keys(%{$rules}) > 0) {
 4027:                     push(@options,('official','unofficial'));
 4028:                 }
 4029:             }
 4030:             push(@options,'none');
 4031:             foreach my $option (@options) {
 4032:                 my $type = 'radio';
 4033:                 my $check = ' ';
 4034:                 if ($checked{$item} eq $option) {
 4035:                     $check = ' checked="checked" ';
 4036:                 } 
 4037:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4038:                               '<input type="'.$type.'" name="can_createuser_'.
 4039:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 4040:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 4041:             }
 4042:             $datatable .= '</td></tr>';
 4043:         }
 4044:     } else {
 4045:         my @contexts = ('author','course','domain');
 4046:         my @authtypes = ('int','krb4','krb5','loc');
 4047:         my %checked;
 4048:         if (ref($settings) eq 'HASH') {
 4049:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 4050:                 foreach my $item (@contexts) {
 4051:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 4052:                         foreach my $auth (@authtypes) {
 4053:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 4054:                                 $checked{$item}{$auth} = ' checked="checked" ';
 4055:                             }
 4056:                         }
 4057:                     }
 4058:                 }
 4059:             }
 4060:         } else {
 4061:             foreach my $item (@contexts) {
 4062:                 foreach my $auth (@authtypes) {
 4063:                     $checked{$item}{$auth} = ' checked="checked" ';
 4064:                 }
 4065:             }
 4066:         }
 4067:         my %title = &context_names();
 4068:         my %authname = &authtype_names();
 4069:         my $rownum = 0;
 4070:         my $css_class; 
 4071:         foreach my $item (@contexts) {
 4072:             if ($rownum%2) {
 4073:                 $css_class = '';
 4074:             } else {
 4075:                 $css_class = ' class="LC_odd_row" ';
 4076:             }
 4077:             $datatable .=   '<tr'.$css_class.'>'.
 4078:                             '<td>'.$title{$item}.
 4079:                             '</td><td class="LC_left_item">'.
 4080:                             '<span class="LC_nobreak">';
 4081:             foreach my $auth (@authtypes) {
 4082:                 $datatable .= '<label>'. 
 4083:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4084:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4085:                               $authname{$auth}.'</label>&nbsp;';
 4086:             }
 4087:             $datatable .= '</span></td></tr>';
 4088:             $rownum ++;
 4089:         }
 4090:         $$rowtotal += $rownum;
 4091:     }
 4092:     return $datatable;
 4093: }
 4094: 
 4095: sub print_selfcreation {
 4096:     my ($position,$dom,$settings,$rowtotal) = @_;
 4097:     my (@selfcreate,$createsettings,$processing,$datatable);
 4098:     if (ref($settings) eq 'HASH') {
 4099:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4100:             $createsettings = $settings->{'cancreate'};
 4101:             if (ref($createsettings) eq 'HASH') {
 4102:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4103:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4104:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4105:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4106:                         @selfcreate = ('email','login','sso');
 4107:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4108:                         @selfcreate = ($createsettings->{'selfcreate'});
 4109:                     }
 4110:                 }
 4111:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4112:                     $processing = $createsettings->{'selfcreateprocessing'};
 4113:                 }
 4114:             }
 4115:         }
 4116:     }
 4117:     my %radiohash;
 4118:     my $numinrow = 4;
 4119:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4120:     if ($position eq 'top') {
 4121:         my %choices = &Apache::lonlocal::texthash (
 4122:                                                       cancreate_login      => 'Institutional Login',
 4123:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4124:                                                   );
 4125:         my @toggles = sort(keys(%choices));
 4126:         my %defaultchecked = (
 4127:                                'cancreate_login' => 'off',
 4128:                                'cancreate_sso'   => 'off',
 4129:                              );
 4130:         my ($onclick,$itemcount);
 4131:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4132:                                                      \%choices,$itemcount,$onclick);
 4133:         $$rowtotal += $itemcount;
 4134:         
 4135:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4136: 
 4137:         if (ref($usertypes) eq 'HASH') {
 4138:             if (keys(%{$usertypes}) > 0) {
 4139:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4140:                                              $dom,$numinrow,$othertitle,
 4141:                                              'statustocreate',$$rowtotal);
 4142:                 $$rowtotal ++;
 4143:             }
 4144:         }
 4145:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 4146:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4147:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 4148:         my $rem;
 4149:         my $numperrow = 2;
 4150:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4151:         $datatable .= '<tr'.$css_class.'>'.
 4152:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 4153:                      '<td class="LC_left_item">'."\n".
 4154:                      '<table><tr><td>'."\n";
 4155:         for (my $i=0; $i<@fields; $i++) {
 4156:             $rem = $i%($numperrow);
 4157:             if ($rem == 0) {
 4158:                 if ($i > 0) {
 4159:                     $datatable .= '</tr>';
 4160:                 }
 4161:                 $datatable .= '<tr>';
 4162:             }
 4163:             my $currval;
 4164:             if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 4165:                 $currval = $createsettings->{'shibenv'}{$fields[$i]};
 4166:             }
 4167:             $datatable .= '<td class="LC_left_item">'.
 4168:                           '<span class="LC_nobreak">'.
 4169:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 4170:                           'value="'.$currval.'" size="10" />&nbsp;'.
 4171:                           $fieldtitles{$fields[$i]}.'</span></td>';
 4172:         }
 4173:         my $colsleft = $numperrow - $rem;
 4174:         if ($colsleft > 1 ) {
 4175:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4176:                          '&nbsp;</td>';
 4177:         } elsif ($colsleft == 1) {
 4178:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4179:         }
 4180:         $datatable .= '</tr></table></td></tr>';
 4181:         $$rowtotal ++;
 4182:     } elsif ($position eq 'middle') {
 4183:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4184:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4185:         $usertypes->{'default'} = $othertitle;
 4186:         if (ref($types) eq 'ARRAY') {
 4187:             push(@{$types},'default');
 4188:             $usertypes->{'default'} = $othertitle;
 4189:             foreach my $status (@{$types}) {
 4190:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4191:                                                        $numinrow,$$rowtotal,$usertypes);
 4192:                 $$rowtotal ++;
 4193:             }
 4194:         }
 4195:     } else {
 4196:         my %choices = &Apache::lonlocal::texthash (
 4197:                                                       cancreate_email => 'E-mail address as username',
 4198:                                                   );
 4199:         my @toggles = sort(keys(%choices));
 4200:         my %defaultchecked = (
 4201:                                'cancreate_email' => 'off',
 4202:                              );
 4203:         my $itemcount = 0;
 4204:         my $display = 'none';
 4205:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 4206:             $display = 'block';
 4207:         }
 4208:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 4209:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 4210:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4211:         my $usertypes = {};
 4212:         my $order = [];
 4213:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 4214:             $usertypes = $domdefaults{'inststatustypes'};
 4215:             $order = $domdefaults{'inststatusguest'};
 4216:         }
 4217:         if (ref($order) eq 'ARRAY') {
 4218:             push(@{$order},'default');
 4219:             if (@{$order} > 1) {
 4220:                 $usertypes->{'default'} = &mt('Other users');
 4221:                 $additional .= '<table><tr>';
 4222:                 foreach my $status (@{$order}) {
 4223:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 4224:                 }
 4225:                 $additional .= '</tr><tr>';
 4226:                 foreach my $status (@{$order}) {
 4227:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 4228:                 }
 4229:                 $additional .= '</tr></table>';
 4230:             } else {
 4231:                 $usertypes->{'default'} = &mt('All users');
 4232:                 $additional .= &email_as_username($rowtotal,$processing);
 4233:             }
 4234:         }
 4235:         $additional .= '</div>'."\n";
 4236: 
 4237:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4238:                                                      \%choices,$$rowtotal,$onclick,$additional);
 4239:         $$rowtotal ++;
 4240:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4241:         $$rowtotal ++;
 4242:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4243:         $numinrow = 1;
 4244:         if (ref($order) eq 'ARRAY') {
 4245:             foreach my $status (@{$order}) {
 4246:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4247:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4248:                 $$rowtotal ++;
 4249:             }
 4250:         }
 4251:         my ($emailrules,$emailruleorder) =
 4252:             &Apache::lonnet::inst_userrules($dom,'email');
 4253:         if (ref($emailrules) eq 'HASH') {
 4254:             if (keys(%{$emailrules}) > 0) {
 4255:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4256:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4257:                 $$rowtotal ++;
 4258:             }
 4259:         }
 4260:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4261:     }
 4262:     return $datatable;
 4263: }
 4264: 
 4265: sub email_as_username {
 4266:     my ($rowtotal,$processing,$type) = @_;
 4267:     my %choices =
 4268:         &Apache::lonlocal::texthash (
 4269:                                       automatic => 'Automatic approval',
 4270:                                       approval  => 'Queued for approval',
 4271:                                     );
 4272:     my $output;
 4273:     foreach my $option ('automatic','approval') {
 4274:         my $checked;
 4275:         if (ref($processing) eq 'HASH') {
 4276:             if ($type eq '') {   
 4277:                 if (!exists($processing->{'default'})) {
 4278:                     if ($option eq 'automatic') {
 4279:                         $checked = ' checked="checked"';
 4280:                     }
 4281:                 } else {
 4282:                     if ($processing->{'default'} eq $option) {
 4283:                         $checked = ' checked="checked"';
 4284:                     }
 4285:                 }
 4286:             } else {
 4287:                 if (!exists($processing->{$type})) {
 4288:                     if ($option eq 'automatic') {
 4289:                         $checked = ' checked="checked"';
 4290:                     }
 4291:                 } else {
 4292:                     if ($processing->{$type} eq $option) {
 4293:                         $checked = ' checked="checked"';
 4294:                     }
 4295:                 }
 4296:             }
 4297:         } elsif ($option eq 'automatic') {
 4298:             $checked = ' checked="checked"'; 
 4299:         }
 4300:         my $name = 'cancreate_emailprocess';
 4301:         if (($type ne '') && ($type ne 'default')) {
 4302:             $name .= '_'.$type;
 4303:         }
 4304:         $output .= '<span class="LC_nobreak"><label>'.
 4305:                    '<input type="radio" name="'.$name.'"'.
 4306:                    $checked.' value="'.$option.'" />'.
 4307:                    $choices{$option}.'</label></span>';
 4308:         if ($type eq '') {
 4309:             $output .= '&nbsp;';
 4310:         } else {
 4311:             $output .= '<br />';
 4312:         }
 4313:     }
 4314:     $$rowtotal ++;
 4315:     return $output;
 4316: }
 4317: 
 4318: sub captcha_choice {
 4319:     my ($context,$settings,$itemcount) = @_;
 4320:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4321:     my %lt = &captcha_phrases();
 4322:     $keyentry = 'hidden';
 4323:     if ($context eq 'cancreate') {
 4324:         $rowname = &mt('CAPTCHA validation');
 4325:     } elsif ($context eq 'login') {
 4326:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4327:     }
 4328:     if (ref($settings) eq 'HASH') {
 4329:         if ($settings->{'captcha'}) {
 4330:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4331:         } else {
 4332:             $checked{'original'} = ' checked="checked"';
 4333:         }
 4334:         if ($settings->{'captcha'} eq 'recaptcha') {
 4335:             $pubtext = $lt{'pub'};
 4336:             $privtext = $lt{'priv'};
 4337:             $keyentry = 'text';
 4338:         }
 4339:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4340:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4341:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4342:         }
 4343:     } else {
 4344:         $checked{'original'} = ' checked="checked"';
 4345:     }
 4346:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4347:     my $output = '<tr'.$css_class.'>'.
 4348:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4349:                  '<table><tr><td>'."\n";
 4350:     foreach my $option ('original','recaptcha','notused') {
 4351:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4352:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4353:                    $lt{$option}.'</label></span>';
 4354:         unless ($option eq 'notused') {
 4355:             $output .= ('&nbsp;'x2)."\n";
 4356:         }
 4357:     }
 4358: #
 4359: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4360: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4361: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4362: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4363: #
 4364:     $output .= '</td></tr>'."\n".
 4365:                '<tr><td>'."\n".
 4366:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4367:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4368:                $currpub.'" size="40" /></span><br />'."\n".
 4369:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4370:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4371:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4372:                '</td></tr>';
 4373:     return $output;
 4374: }
 4375: 
 4376: sub user_formats_row {
 4377:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4378:     my $output;
 4379:     my %text = (
 4380:                    'username' => 'new usernames',
 4381:                    'id'       => 'IDs',
 4382:                    'email'    => 'self-created accounts (e-mail)',
 4383:                );
 4384:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4385:     $output = '<tr '.$css_class.'>'.
 4386:               '<td><span class="LC_nobreak">';
 4387:     if ($type eq 'email') {
 4388:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4389:     } else {
 4390:         $output .= &mt("Format rules to check for $text{$type}: ");
 4391:     }
 4392:     $output .= '</span></td>'.
 4393:                '<td class="LC_left_item" colspan="2"><table>';
 4394:     my $rem;
 4395:     if (ref($ruleorder) eq 'ARRAY') {
 4396:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4397:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4398:                 my $rem = $i%($numinrow);
 4399:                 if ($rem == 0) {
 4400:                     if ($i > 0) {
 4401:                         $output .= '</tr>';
 4402:                     }
 4403:                     $output .= '<tr>';
 4404:                 }
 4405:                 my $check = ' ';
 4406:                 if (ref($settings) eq 'HASH') {
 4407:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4408:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4409:                             $check = ' checked="checked" ';
 4410:                         }
 4411:                     }
 4412:                 }
 4413:                 $output .= '<td class="LC_left_item">'.
 4414:                            '<span class="LC_nobreak"><label>'.
 4415:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4416:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4417:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4418:             }
 4419:         }
 4420:         $rem = @{$ruleorder}%($numinrow);
 4421:     }
 4422:     my $colsleft = $numinrow - $rem;
 4423:     if ($colsleft > 1 ) {
 4424:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4425:                    '&nbsp;</td>';
 4426:     } elsif ($colsleft == 1) {
 4427:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4428:     }
 4429:     $output .= '</tr></table></td></tr>';
 4430:     return $output;
 4431: }
 4432: 
 4433: sub usercreation_types {
 4434:     my %lt = &Apache::lonlocal::texthash (
 4435:                     author     => 'When adding a co-author',
 4436:                     course     => 'When adding a user to a course',
 4437:                     requestcrs => 'When requesting a course',
 4438:                     any        => 'Any',
 4439:                     official   => 'Institutional only ',
 4440:                     unofficial => 'Non-institutional only',
 4441:                     none       => 'None',
 4442:     );
 4443:     return %lt;
 4444: }
 4445: 
 4446: sub selfcreation_types {
 4447:     my %lt = &Apache::lonlocal::texthash (
 4448:                     selfcreate => 'User creates own account',
 4449:                     any        => 'Any',
 4450:                     official   => 'Institutional only ',
 4451:                     unofficial => 'Non-institutional only',
 4452:                     email      => 'E-mail address',
 4453:                     login      => 'Institutional Login',
 4454:                     sso        => 'SSO',
 4455:              );
 4456: }
 4457: 
 4458: sub authtype_names {
 4459:     my %lt = &Apache::lonlocal::texthash(
 4460:                       int    => 'Internal',
 4461:                       krb4   => 'Kerberos 4',
 4462:                       krb5   => 'Kerberos 5',
 4463:                       loc    => 'Local',
 4464:                   );
 4465:     return %lt;
 4466: }
 4467: 
 4468: sub context_names {
 4469:     my %context_title = &Apache::lonlocal::texthash(
 4470:        author => 'Creating users when an Author',
 4471:        course => 'Creating users when in a course',
 4472:        domain => 'Creating users when a Domain Coordinator',
 4473:     );
 4474:     return %context_title;
 4475: }
 4476: 
 4477: sub print_usermodification {
 4478:     my ($position,$dom,$settings,$rowtotal) = @_;
 4479:     my $numinrow = 4;
 4480:     my ($context,$datatable,$rowcount);
 4481:     if ($position eq 'top') {
 4482:         $rowcount = 0;
 4483:         $context = 'author'; 
 4484:         foreach my $role ('ca','aa') {
 4485:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4486:                                                    $numinrow,$rowcount);
 4487:             $$rowtotal ++;
 4488:             $rowcount ++;
 4489:         }
 4490:     } elsif ($position eq 'bottom') {
 4491:         $context = 'course';
 4492:         $rowcount = 0;
 4493:         foreach my $role ('st','ep','ta','in','cr') {
 4494:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4495:                                                    $numinrow,$rowcount);
 4496:             $$rowtotal ++;
 4497:             $rowcount ++;
 4498:         }
 4499:     }
 4500:     return $datatable;
 4501: }
 4502: 
 4503: sub print_defaults {
 4504:     my ($position,$dom,$settings,$rowtotal) = @_;
 4505:     my $rownum = 0;
 4506:     my ($datatable,$css_class);
 4507:     if ($position eq 'top') {
 4508:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4509:                      'datelocale_def','portal_def');
 4510:         my %defaults;
 4511:         if (ref($settings) eq 'HASH') {
 4512:             %defaults = %{$settings};
 4513:         } else {
 4514:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4515:             foreach my $item (@items) {
 4516:                 $defaults{$item} = $domdefaults{$item};
 4517:             }
 4518:         }
 4519:         my $titles = &defaults_titles($dom);
 4520:         foreach my $item (@items) {
 4521:             if ($rownum%2) {
 4522:                 $css_class = '';
 4523:             } else {
 4524:                 $css_class = ' class="LC_odd_row" ';
 4525:             }
 4526:             $datatable .= '<tr'.$css_class.'>'.
 4527:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4528:                           '</span></td><td class="LC_right_item" colspan="3">';
 4529:             if ($item eq 'auth_def') {
 4530:                 my @authtypes = ('internal','krb4','krb5','localauth');
 4531:                 my %shortauth = (
 4532:                                  internal => 'int',
 4533:                                  krb4 => 'krb4',
 4534:                                  krb5 => 'krb5',
 4535:                                  localauth  => 'loc'
 4536:                                 );
 4537:                 my %authnames = &authtype_names();
 4538:                 foreach my $auth (@authtypes) {
 4539:                     my $checked = ' ';
 4540:                     if ($defaults{$item} eq $auth) {
 4541:                         $checked = ' checked="checked" ';
 4542:                     }
 4543:                     $datatable .= '<label><input type="radio" name="'.$item.
 4544:                                   '" value="'.$auth.'"'.$checked.'/>'.
 4545:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4546:                 }
 4547:             } elsif ($item eq 'timezone_def') {
 4548:                 my $includeempty = 1;
 4549:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4550:             } elsif ($item eq 'datelocale_def') {
 4551:                 my $includeempty = 1;
 4552:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4553:             } elsif ($item eq 'lang_def') {
 4554:                 my %langchoices = &get_languages_hash();
 4555:                 $langchoices{''} = 'No language preference';
 4556:                 %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4557:                 $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4558:                                                               \%langchoices);
 4559:             } else {
 4560:                 my $size;
 4561:                 if ($item eq 'portal_def') {
 4562:                     $size = ' size="25"';
 4563:                 }
 4564:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4565:                               $defaults{$item}.'"'.$size.' />';
 4566:             }
 4567:             $datatable .= '</td></tr>';
 4568:             $rownum ++;
 4569:         }
 4570:     } else {
 4571:         my (%defaults);
 4572:         if (ref($settings) eq 'HASH') {
 4573:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 4574:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 4575:                 my $maxnum = @{$settings->{'inststatusorder'}};
 4576:                 for (my $i=0; $i<$maxnum; $i++) {
 4577:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 4578:                     my $item = $settings->{'inststatusorder'}->[$i];
 4579:                     my $title = $settings->{'inststatustypes'}->{$item};
 4580:                     my $guestok;
 4581:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 4582:                         $guestok = 1;
 4583:                     }
 4584:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 4585:                     $datatable .= '<tr'.$css_class.'>'.
 4586:                                   '<td><span class="LC_nobreak">'.
 4587:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 4588:                     for (my $k=0; $k<=$maxnum; $k++) {
 4589:                         my $vpos = $k+1;
 4590:                         my $selstr;
 4591:                         if ($k == $i) {
 4592:                             $selstr = ' selected="selected" ';
 4593:                         }
 4594:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4595:                     }
 4596:                     my ($checkedon,$checkedoff);
 4597:                     $checkedoff = ' checked="checked"';
 4598:                     if ($guestok) {
 4599:                         $checkedon = $checkedoff;
 4600:                         $checkedoff = ''; 
 4601:                     }
 4602:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 4603:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 4604:                                   &mt('delete').'</span></td>'.
 4605:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 4606:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 4607:                                   '</span></td>'.
 4608:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4609:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 4610:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 4611:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 4612:                                   &mt('No').'</label></span></td></tr>';
 4613:                 }
 4614:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 4615:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 4616:                 $datatable .= '<tr '.$css_class.'>'.
 4617:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 4618:                 for (my $k=0; $k<=$maxnum; $k++) {
 4619:                     my $vpos = $k+1;
 4620:                     my $selstr;
 4621:                     if ($k == $maxnum) {
 4622:                         $selstr = ' selected="selected" ';
 4623:                     }
 4624:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4625:                 }
 4626:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 4627:                               '<input type="text" size="10" name="addinststatus" value="" /></span>'.
 4628:                               '&nbsp;'.&mt('(new)').
 4629:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 4630:                               &mt('Name displayed:').
 4631:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 4632:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 4633:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 4634:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 4635:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 4636:                               &mt('No').'</label></span></td></tr>';
 4637:                               '</tr>'."\n";
 4638:                 $rownum ++;
 4639:             }
 4640:         }
 4641:     }
 4642:     $$rowtotal += $rownum;
 4643:     return $datatable;
 4644: }
 4645: 
 4646: sub get_languages_hash {
 4647:     my %langchoices;
 4648:     foreach my $id (&Apache::loncommon::languageids()) {
 4649:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4650:         if ($code ne '') {
 4651:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4652:         }
 4653:     }
 4654:     return %langchoices;
 4655: }
 4656: 
 4657: sub defaults_titles {
 4658:     my ($dom) = @_;
 4659:     my %titles = &Apache::lonlocal::texthash (
 4660:                    'auth_def'      => 'Default authentication type',
 4661:                    'auth_arg_def'  => 'Default authentication argument',
 4662:                    'lang_def'      => 'Default language',
 4663:                    'timezone_def'  => 'Default timezone',
 4664:                    'datelocale_def' => 'Default locale for dates',
 4665:                    'portal_def'     => 'Portal/Default URL',
 4666:                  );
 4667:     if ($dom) {
 4668:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4669:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4670:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4671:         $protocol = 'http' if ($protocol ne 'https');
 4672:         if ($uint_dom) {
 4673:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4674:                                          $uint_dom);
 4675:         }
 4676:     }
 4677:     return (\%titles);
 4678: }
 4679: 
 4680: sub print_scantronformat {
 4681:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4682:     my $itemcount = 1;
 4683:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4684:         %confhash);
 4685:     my $switchserver = &check_switchserver($dom,$confname);
 4686:     my %lt = &Apache::lonlocal::texthash (
 4687:                 default => 'Default bubblesheet format file error',
 4688:                 custom  => 'Custom bubblesheet format file error',
 4689:              );
 4690:     my %scantronfiles = (
 4691:         default => 'default.tab',
 4692:         custom => 'custom.tab',
 4693:     );
 4694:     foreach my $key (keys(%scantronfiles)) {
 4695:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4696:                               .$scantronfiles{$key};
 4697:     }
 4698:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4699:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4700:         if (!$switchserver) {
 4701:             my $servadm = $r->dir_config('lonAdmEMail');
 4702:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4703:             if ($configuserok eq 'ok') {
 4704:                 if ($author_ok eq 'ok') {
 4705:                     my %legacyfile = (
 4706:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4707:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4708:                     );
 4709:                     my %md5chk;
 4710:                     foreach my $type (keys(%legacyfile)) {
 4711:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4712:                         chomp($md5chk{$type});
 4713:                     }
 4714:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4715:                         foreach my $type (keys(%legacyfile)) {
 4716:                             ($scantronurls{$type},my $error) = 
 4717:                                 &legacy_scantronformat($r,$dom,$confname,
 4718:                                                  $type,$legacyfile{$type},
 4719:                                                  $scantronurls{$type},
 4720:                                                  $scantronfiles{$type});
 4721:                             if ($error ne '') {
 4722:                                 $error{$type} = $error;
 4723:                             }
 4724:                         }
 4725:                         if (keys(%error) == 0) {
 4726:                             $is_custom = 1;
 4727:                             $confhash{'scantron'}{'scantronformat'} = 
 4728:                                 $scantronurls{'custom'};
 4729:                             my $putresult = 
 4730:                                 &Apache::lonnet::put_dom('configuration',
 4731:                                                          \%confhash,$dom);
 4732:                             if ($putresult ne 'ok') {
 4733:                                 $error{'custom'} = 
 4734:                                     '<span class="LC_error">'.
 4735:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4736:                             }
 4737:                         }
 4738:                     } else {
 4739:                         ($scantronurls{'default'},my $error) =
 4740:                             &legacy_scantronformat($r,$dom,$confname,
 4741:                                           'default',$legacyfile{'default'},
 4742:                                           $scantronurls{'default'},
 4743:                                           $scantronfiles{'default'});
 4744:                         if ($error eq '') {
 4745:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4746:                             my $putresult =
 4747:                                 &Apache::lonnet::put_dom('configuration',
 4748:                                                          \%confhash,$dom);
 4749:                             if ($putresult ne 'ok') {
 4750:                                 $error{'default'} =
 4751:                                     '<span class="LC_error">'.
 4752:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4753:                             }
 4754:                         } else {
 4755:                             $error{'default'} = $error;
 4756:                         }
 4757:                     }
 4758:                 }
 4759:             }
 4760:         } else {
 4761:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4762:         }
 4763:     }
 4764:     if (ref($settings) eq 'HASH') {
 4765:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4766:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4767:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4768:                 $scantronurl = '';
 4769:             } else {
 4770:                 $scantronurl = $settings->{'scantronformat'};
 4771:             }
 4772:             $is_custom = 1;
 4773:         } else {
 4774:             $scantronurl = $scantronurls{'default'};
 4775:         }
 4776:     } else {
 4777:         if ($is_custom) {
 4778:             $scantronurl = $scantronurls{'custom'};
 4779:         } else {
 4780:             $scantronurl = $scantronurls{'default'};
 4781:         }
 4782:     }
 4783:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4784:     $datatable .= '<tr'.$css_class.'>';
 4785:     if (!$is_custom) {
 4786:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4787:                       '<span class="LC_nobreak">';
 4788:         if ($scantronurl) {
 4789:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4790:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4791:         } else {
 4792:             $datatable = &mt('File unavailable for display');
 4793:         }
 4794:         $datatable .= '</span></td>';
 4795:         if (keys(%error) == 0) { 
 4796:             $datatable .= '<td valign="bottom">';
 4797:             if (!$switchserver) {
 4798:                 $datatable .= &mt('Upload:').'<br />';
 4799:             }
 4800:         } else {
 4801:             my $errorstr;
 4802:             foreach my $key (sort(keys(%error))) {
 4803:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4804:             }
 4805:             $datatable .= '<td>'.$errorstr;
 4806:         }
 4807:     } else {
 4808:         if (keys(%error) > 0) {
 4809:             my $errorstr;
 4810:             foreach my $key (sort(keys(%error))) {
 4811:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4812:             } 
 4813:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4814:         } elsif ($scantronurl) {
 4815:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4816:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4817:             $datatable .= '<td><span class="LC_nobreak">'.
 4818:                           $link.
 4819:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4820:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4821:                           '<td><span class="LC_nobreak">&nbsp;'.
 4822:                           &mt('Replace:').'</span><br />';
 4823:         }
 4824:     }
 4825:     if (keys(%error) == 0) {
 4826:         if ($switchserver) {
 4827:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4828:         } else {
 4829:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4830:                          '<input type="file" name="scantronformat" /></span>';
 4831:         }
 4832:     }
 4833:     $datatable .= '</td></tr>';
 4834:     $$rowtotal ++;
 4835:     return $datatable;
 4836: }
 4837: 
 4838: sub legacy_scantronformat {
 4839:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4840:     my ($url,$error);
 4841:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4842:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4843:         (my $result,$url) =
 4844:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4845:                          '','',$newfile);
 4846:         if ($result ne 'ok') {
 4847:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4848:         }
 4849:     }
 4850:     return ($url,$error);
 4851: }
 4852: 
 4853: sub print_coursecategories {
 4854:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4855:     my $datatable;
 4856:     if ($position eq 'top') {
 4857:         my (%checked);
 4858:         my @catitems = ('unauth','auth');
 4859:         my @cattypes = ('std','domonly','codesrch','none');
 4860:         $checked{'unauth'} = 'std';
 4861:         $checked{'auth'} = 'std';
 4862:         if (ref($settings) eq 'HASH') {
 4863:             foreach my $type (@cattypes) {
 4864:                 if ($type eq $settings->{'unauth'}) {
 4865:                     $checked{'unauth'} = $type;
 4866:                 }
 4867:                 if ($type eq $settings->{'auth'}) {
 4868:                     $checked{'auth'} = $type;
 4869:                 }
 4870:             }
 4871:         }
 4872:         my %lt = &Apache::lonlocal::texthash (
 4873:                                                unauth   => 'Catalog type for unauthenticated users',
 4874:                                                auth     => 'Catalog type for authenticated users',
 4875:                                                none     => 'No catalog',
 4876:                                                std      => 'Standard catalog',
 4877:                                                domonly  => 'Domain-only catalog',
 4878:                                                codesrch => "Code search form",
 4879:                                              );
 4880:        my $itemcount = 0;
 4881:        foreach my $item (@catitems) {
 4882:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 4883:            $datatable .= '<tr '.$css_class.'>'.
 4884:                          '<td>'.$lt{$item}.'</td>'.
 4885:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 4886:            foreach my $type (@cattypes) {
 4887:                my $ischecked;
 4888:                if ($checked{$item} eq $type) {
 4889:                    $ischecked=' checked="checked"';
 4890:                }
 4891:                $datatable .= '<label>'.
 4892:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 4893:                              ' />'.$lt{$type}.'</label>&nbsp;';
 4894:            }
 4895:            $datatable .= '</td></tr>';
 4896:            $itemcount ++;
 4897:         }
 4898:         $$rowtotal += $itemcount;
 4899:     } elsif ($position eq 'middle') {
 4900:         my $toggle_cats_crs = ' ';
 4901:         my $toggle_cats_dom = ' checked="checked" ';
 4902:         my $can_cat_crs = ' ';
 4903:         my $can_cat_dom = ' checked="checked" ';
 4904:         my $toggle_catscomm_comm = ' ';
 4905:         my $toggle_catscomm_dom = ' checked="checked" ';
 4906:         my $can_catcomm_comm = ' ';
 4907:         my $can_catcomm_dom = ' checked="checked" ';
 4908: 
 4909:         if (ref($settings) eq 'HASH') {
 4910:             if ($settings->{'togglecats'} eq 'crs') {
 4911:                 $toggle_cats_crs = $toggle_cats_dom;
 4912:                 $toggle_cats_dom = ' ';
 4913:             }
 4914:             if ($settings->{'categorize'} eq 'crs') {
 4915:                 $can_cat_crs = $can_cat_dom;
 4916:                 $can_cat_dom = ' ';
 4917:             }
 4918:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4919:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4920:                 $toggle_catscomm_dom = ' ';
 4921:             }
 4922:             if ($settings->{'categorizecomm'} eq 'comm') {
 4923:                 $can_catcomm_comm = $can_catcomm_dom;
 4924:                 $can_catcomm_dom = ' ';
 4925:             }
 4926:         }
 4927:         my %title = &Apache::lonlocal::texthash (
 4928:                      togglecats     => 'Show/Hide a course in catalog',
 4929:                      togglecatscomm => 'Show/Hide a community in catalog',
 4930:                      categorize     => 'Assign a category to a course',
 4931:                      categorizecomm => 'Assign a category to a community',
 4932:                     );
 4933:         my %level = &Apache::lonlocal::texthash (
 4934:                      dom  => 'Set in Domain',
 4935:                      crs  => 'Set in Course',
 4936:                      comm => 'Set in Community',
 4937:                     );
 4938:         $datatable = '<tr class="LC_odd_row">'.
 4939:                   '<td>'.$title{'togglecats'}.'</td>'.
 4940:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4941:                   '<input type="radio" name="togglecats"'.
 4942:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4943:                   '<label><input type="radio" name="togglecats"'.
 4944:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4945:                   '</tr><tr>'.
 4946:                   '<td>'.$title{'categorize'}.'</td>'.
 4947:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4948:                   '<label><input type="radio" name="categorize"'.
 4949:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4950:                   '<label><input type="radio" name="categorize"'.
 4951:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4952:                   '</tr><tr class="LC_odd_row">'.
 4953:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4954:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4955:                   '<input type="radio" name="togglecatscomm"'.
 4956:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4957:                   '<label><input type="radio" name="togglecatscomm"'.
 4958:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4959:                   '</tr><tr>'.
 4960:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4961:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4962:                   '<label><input type="radio" name="categorizecomm"'.
 4963:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4964:                   '<label><input type="radio" name="categorizecomm"'.
 4965:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4966:                   '</tr>';
 4967:         $$rowtotal += 4;
 4968:     } else {
 4969:         my $css_class;
 4970:         my $itemcount = 1;
 4971:         my $cathash; 
 4972:         if (ref($settings) eq 'HASH') {
 4973:             $cathash = $settings->{'cats'};
 4974:         }
 4975:         if (ref($cathash) eq 'HASH') {
 4976:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4977:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4978:                                                    \%allitems,\%idx,\@jsarray);
 4979:             my $maxdepth = scalar(@cats);
 4980:             my $colattrib = '';
 4981:             if ($maxdepth > 2) {
 4982:                 $colattrib = ' colspan="2" ';
 4983:             }
 4984:             my @path;
 4985:             if (@cats > 0) {
 4986:                 if (ref($cats[0]) eq 'ARRAY') {
 4987:                     my $numtop = @{$cats[0]};
 4988:                     my $maxnum = $numtop;
 4989:                     my %default_names = (
 4990:                           instcode    => &mt('Official courses'),
 4991:                           communities => &mt('Communities'),
 4992:                     );
 4993: 
 4994:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4995:                         ($cathash->{'instcode::0'} eq '') ||
 4996:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4997:                         ($cathash->{'communities::0'} eq '')) {
 4998:                         $maxnum ++;
 4999:                     }
 5000:                     my $lastidx;
 5001:                     for (my $i=0; $i<$numtop; $i++) {
 5002:                         my $parent = $cats[0][$i];
 5003:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5004:                         my $item = &escape($parent).'::0';
 5005:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 5006:                         $lastidx = $idx{$item};
 5007:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5008:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 5009:                         for (my $k=0; $k<=$maxnum; $k++) {
 5010:                             my $vpos = $k+1;
 5011:                             my $selstr;
 5012:                             if ($k == $i) {
 5013:                                 $selstr = ' selected="selected" ';
 5014:                             }
 5015:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5016:                         }
 5017:                         $datatable .= '</select></span></td><td>';
 5018:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 5019:                             $datatable .=  '<span class="LC_nobreak">'
 5020:                                            .$default_names{$parent}.'</span>';
 5021:                             if ($parent eq 'instcode') {
 5022:                                 $datatable .= '<br /><span class="LC_nobreak">('
 5023:                                               .&mt('with institutional codes')
 5024:                                               .')</span></td><td'.$colattrib.'>';
 5025:                             } else {
 5026:                                 $datatable .= '<table><tr><td>';
 5027:                             }
 5028:                             $datatable .= '<span class="LC_nobreak">'
 5029:                                           .'<label><input type="radio" name="'
 5030:                                           .$parent.'" value="1" checked="checked" />'
 5031:                                           .&mt('Display').'</label>';
 5032:                             if ($parent eq 'instcode') {
 5033:                                 $datatable .= '&nbsp;';
 5034:                             } else {
 5035:                                 $datatable .= '</span></td></tr><tr><td>'
 5036:                                               .'<span class="LC_nobreak">';
 5037:                             }
 5038:                             $datatable .= '<label><input type="radio" name="'
 5039:                                           .$parent.'" value="0" />'
 5040:                                           .&mt('Do not display').'</label></span>';
 5041:                             if ($parent eq 'communities') {
 5042:                                 $datatable .= '</td></tr></table>';
 5043:                             }
 5044:                             $datatable .= '</td>';
 5045:                         } else {
 5046:                             $datatable .= $parent
 5047:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 5048:                                           .'<input type="checkbox" name="deletecategory" '
 5049:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 5050:                         }
 5051:                         my $depth = 1;
 5052:                         push(@path,$parent);
 5053:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 5054:                         pop(@path);
 5055:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 5056:                         $itemcount ++;
 5057:                     }
 5058:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5059:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 5060:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 5061:                     for (my $k=0; $k<=$maxnum; $k++) {
 5062:                         my $vpos = $k+1;
 5063:                         my $selstr;
 5064:                         if ($k == $numtop) {
 5065:                             $selstr = ' selected="selected" ';
 5066:                         }
 5067:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5068:                     }
 5069:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 5070:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 5071:                                   .'</tr>'."\n";
 5072:                     $itemcount ++;
 5073:                     foreach my $default ('instcode','communities') {
 5074:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 5075:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5076:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 5077:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 5078:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5079:                             for (my $k=0; $k<=$maxnum; $k++) {
 5080:                                 my $vpos = $k+1;
 5081:                                 my $selstr;
 5082:                                 if ($k == $maxnum) {
 5083:                                     $selstr = ' selected="selected" ';
 5084:                                 }
 5085:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5086:                             }
 5087:                             $datatable .= '</select></span></td>'.
 5088:                                           '<td><span class="LC_nobreak">'.
 5089:                                           $default_names{$default}.'</span>';
 5090:                             if ($default eq 'instcode') {
 5091:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5092:                                               .&mt('with institutional codes').')</span>';
 5093:                             }
 5094:                             $datatable .= '</td>'
 5095:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5096:                                           .&mt('Display').'</label>&nbsp;'
 5097:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5098:                                           .&mt('Do not display').'</label></span></td></tr>';
 5099:                         }
 5100:                     }
 5101:                 }
 5102:             } else {
 5103:                 $datatable .= &initialize_categories($itemcount);
 5104:             }
 5105:         } else {
 5106:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 5107:                           .&initialize_categories($itemcount);
 5108:         }
 5109:         $$rowtotal += $itemcount;
 5110:     }
 5111:     return $datatable;
 5112: }
 5113: 
 5114: sub print_serverstatuses {
 5115:     my ($dom,$settings,$rowtotal) = @_;
 5116:     my $datatable;
 5117:     my @pages = &serverstatus_pages();
 5118:     my (%namedaccess,%machineaccess);
 5119:     foreach my $type (@pages) {
 5120:         $namedaccess{$type} = '';
 5121:         $machineaccess{$type}= '';
 5122:     }
 5123:     if (ref($settings) eq 'HASH') {
 5124:         foreach my $type (@pages) {
 5125:             if (exists($settings->{$type})) {
 5126:                 if (ref($settings->{$type}) eq 'HASH') {
 5127:                     foreach my $key (keys(%{$settings->{$type}})) {
 5128:                         if ($key eq 'namedusers') {
 5129:                             $namedaccess{$type} = $settings->{$type}->{$key};
 5130:                         } elsif ($key eq 'machines') {
 5131:                             $machineaccess{$type} = $settings->{$type}->{$key};
 5132:                         }
 5133:                     }
 5134:                 }
 5135:             }
 5136:         }
 5137:     }
 5138:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 5139:     my $rownum = 0;
 5140:     my $css_class;
 5141:     foreach my $type (@pages) {
 5142:         $rownum ++;
 5143:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 5144:         $datatable .= '<tr'.$css_class.'>'.
 5145:                       '<td><span class="LC_nobreak">'.
 5146:                       $titles->{$type}.'</span></td>'.
 5147:                       '<td class="LC_left_item">'.
 5148:                       '<input type="text" name="'.$type.'_namedusers" '.
 5149:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 5150:                       '<td class="LC_right_item">'.
 5151:                       '<span class="LC_nobreak">'.
 5152:                       '<input type="text" name="'.$type.'_machines" '.
 5153:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 5154:                       '</td></tr>'."\n";
 5155:     }
 5156:     $$rowtotal += $rownum;
 5157:     return $datatable;
 5158: }
 5159: 
 5160: sub serverstatus_pages {
 5161:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 5162:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 5163:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 5164:             'uniquecodes','diskusage');
 5165: }
 5166: 
 5167: sub defaults_javascript {
 5168:     my ($settings) = @_;
 5169:     my ($output,$jstext); 
 5170:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5171:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 5172:         if ($maxnum eq '') {
 5173:             $maxnum = 0;
 5174:         }
 5175:         $maxnum ++;
 5176:         $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 5177:         return <<"ENDSCRIPT";
 5178: <script type="text/javascript">
 5179: // <![CDATA[
 5180: function reorderTypes(form,caller) {
 5181:     var changedVal;
 5182: $jstext 
 5183:     var newpos = 'addinststatus_pos';
 5184:     var current = new Array;
 5185:     var maxh = $maxnum;
 5186:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5187:     var oldVal;
 5188:     if (caller == newpos) {
 5189:         changedVal = newitemVal;
 5190:     } else {
 5191:         var curritem = 'inststatus_pos_'+caller;
 5192:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 5193:         current[newitemVal] = newpos;
 5194:     }
 5195:     for (var i=0; i<inststatuses.length; i++) {
 5196:         if (inststatuses[i] != caller) {
 5197:             var elementName = 'inststatus_pos_'+inststatuses[i];
 5198:             if (form.elements[elementName]) {
 5199:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5200:                 current[currVal] = elementName;
 5201:             }
 5202:         }
 5203:     }
 5204:     for (var j=0; j<maxh; j++) {
 5205:         if (current[j] == undefined) {
 5206:             oldVal = j;
 5207:         }
 5208:     }
 5209:     if (oldVal < changedVal) {
 5210:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5211:            var elementName = current[k];
 5212:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5213:         }
 5214:     } else {
 5215:         for (var k=changedVal; k<oldVal; k++) {
 5216:             var elementName = current[k];
 5217:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5218:         }
 5219:     }
 5220:     return;
 5221: }
 5222: 
 5223: // ]]>
 5224: </script>
 5225: 
 5226: ENDSCRIPT
 5227:     }
 5228: }
 5229: 
 5230: sub coursecategories_javascript {
 5231:     my ($settings) = @_;
 5232:     my ($output,$jstext,$cathash);
 5233:     if (ref($settings) eq 'HASH') {
 5234:         $cathash = $settings->{'cats'};
 5235:     }
 5236:     if (ref($cathash) eq 'HASH') {
 5237:         my (@cats,@jsarray,%idx);
 5238:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 5239:         if (@jsarray > 0) {
 5240:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 5241:             for (my $i=0; $i<@jsarray; $i++) {
 5242:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 5243:                     my $catstr = join('","',@{$jsarray[$i]});
 5244:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 5245:                 }
 5246:             }
 5247:         }
 5248:     } else {
 5249:         $jstext  = '    var categories = Array(1);'."\n".
 5250:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 5251:     }
 5252:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 5253:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 5254:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category.'); 
 5255:     $output = <<"ENDSCRIPT";
 5256: <script type="text/javascript">
 5257: // <![CDATA[
 5258: function reorderCats(form,parent,item,idx) {
 5259:     var changedVal;
 5260: $jstext
 5261:     var newpos = 'addcategory_pos';
 5262:     if (parent == '') {
 5263:         var has_instcode = 0;
 5264:         var maxtop = categories[idx].length;
 5265:         for (var j=0; j<maxtop; j++) {
 5266:             if (categories[idx][j] == 'instcode::0') {
 5267:                 has_instcode == 1;
 5268:             }
 5269:         }
 5270:         if (has_instcode == 0) {
 5271:             categories[idx][maxtop] = 'instcode_pos';
 5272:         }
 5273:     } else {
 5274:         newpos += '_'+parent;
 5275:     }
 5276:     var maxh = 1 + categories[idx].length;
 5277:     var current = new Array;
 5278:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5279:     if (item == newpos) {
 5280:         changedVal = newitemVal;
 5281:     } else {
 5282:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5283:         current[newitemVal] = newpos;
 5284:     }
 5285:     for (var i=0; i<categories[idx].length; i++) {
 5286:         var elementName = categories[idx][i];
 5287:         if (elementName != item) {
 5288:             if (form.elements[elementName]) {
 5289:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5290:                 current[currVal] = elementName;
 5291:             }
 5292:         }
 5293:     }
 5294:     var oldVal;
 5295:     for (var j=0; j<maxh; j++) {
 5296:         if (current[j] == undefined) {
 5297:             oldVal = j;
 5298:         }
 5299:     }
 5300:     if (oldVal < changedVal) {
 5301:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5302:            var elementName = current[k];
 5303:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5304:         }
 5305:     } else {
 5306:         for (var k=changedVal; k<oldVal; k++) {
 5307:             var elementName = current[k];
 5308:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5309:         }
 5310:     }
 5311:     return;
 5312: }
 5313: 
 5314: function categoryCheck(form) {
 5315:     if (form.elements['addcategory_name'].value == 'instcode') {
 5316:         alert('$instcode_reserved\\n$choose_again');
 5317:         return false;
 5318:     }
 5319:     if (form.elements['addcategory_name'].value == 'communities') {
 5320:         alert('$communities_reserved\\n$choose_again');
 5321:         return false;
 5322:     }
 5323:     return true;
 5324: }
 5325: 
 5326: // ]]>
 5327: </script>
 5328: 
 5329: ENDSCRIPT
 5330:     return $output;
 5331: }
 5332: 
 5333: sub initialize_categories {
 5334:     my ($itemcount) = @_;
 5335:     my ($datatable,$css_class,$chgstr);
 5336:     my %default_names = (
 5337:                       instcode    => 'Official courses (with institutional codes)',
 5338:                       communities => 'Communities',
 5339:                         );
 5340:     my $select0 = ' selected="selected"';
 5341:     my $select1 = '';
 5342:     foreach my $default ('instcode','communities') {
 5343:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5344:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 5345:         if ($default eq 'communities') {
 5346:             $select1 = $select0;
 5347:             $select0 = '';
 5348:         }
 5349:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5350:                      .'<select name="'.$default.'_pos">'
 5351:                      .'<option value="0"'.$select0.'>1</option>'
 5352:                      .'<option value="1"'.$select1.'>2</option>'
 5353:                      .'<option value="2">3</option></select>&nbsp;'
 5354:                      .$default_names{$default}
 5355:                      .'</span></td><td><span class="LC_nobreak">'
 5356:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5357:                      .&mt('Display').'</label>&nbsp;<label>'
 5358:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5359:                  .'</label></span></td></tr>';
 5360:         $itemcount ++;
 5361:     }
 5362:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5363:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5364:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5365:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5366:                   .'<option value="0">1</option>'
 5367:                   .'<option value="1">2</option>'
 5368:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5369:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5370:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5371:     return $datatable;
 5372: }
 5373: 
 5374: sub build_category_rows {
 5375:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5376:     my ($text,$name,$item,$chgstr);
 5377:     if (ref($cats) eq 'ARRAY') {
 5378:         my $maxdepth = scalar(@{$cats});
 5379:         if (ref($cats->[$depth]) eq 'HASH') {
 5380:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5381:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5382:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5383:                 $text .= '<td><table class="LC_data_table">';
 5384:                 my ($idxnum,$parent_name,$parent_item);
 5385:                 my $higher = $depth - 1;
 5386:                 if ($higher == 0) {
 5387:                     $parent_name = &escape($parent).'::'.$higher;
 5388:                 } else {
 5389:                     if (ref($path) eq 'ARRAY') {
 5390:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5391:                     }
 5392:                 }
 5393:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5394:                 for (my $j=0; $j<=$numchildren; $j++) {
 5395:                     if ($j < $numchildren) {
 5396:                         $name = $cats->[$depth]{$parent}[$j];
 5397:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5398:                         $idxnum = $idx->{$item};
 5399:                     } else {
 5400:                         $name = $parent_name;
 5401:                         $item = $parent_item;
 5402:                     }
 5403:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5404:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5405:                     for (my $i=0; $i<=$numchildren; $i++) {
 5406:                         my $vpos = $i+1;
 5407:                         my $selstr;
 5408:                         if ($j == $i) {
 5409:                             $selstr = ' selected="selected" ';
 5410:                         }
 5411:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5412:                     }
 5413:                     $text .= '</select>&nbsp;';
 5414:                     if ($j < $numchildren) {
 5415:                         my $deeper = $depth+1;
 5416:                         $text .= $name.'&nbsp;'
 5417:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5418:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5419:                         if(ref($path) eq 'ARRAY') {
 5420:                             push(@{$path},$name);
 5421:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5422:                             pop(@{$path});
 5423:                         }
 5424:                     } else {
 5425:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5426:                         if ($j == $numchildren) {
 5427:                             $text .= $name;
 5428:                         } else {
 5429:                             $text .= $item;
 5430:                         }
 5431:                         $text .= '" value="" />';
 5432:                     }
 5433:                     $text .= '</td></tr>';
 5434:                 }
 5435:                 $text .= '</table></td>';
 5436:             } else {
 5437:                 my $higher = $depth-1;
 5438:                 if ($higher == 0) {
 5439:                     $name = &escape($parent).'::'.$higher;
 5440:                 } else {
 5441:                     if (ref($path) eq 'ARRAY') {
 5442:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5443:                     }
 5444:                 }
 5445:                 my $colspan;
 5446:                 if ($parent ne 'instcode') {
 5447:                     $colspan = $maxdepth - $depth - 1;
 5448:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5449:                 }
 5450:             }
 5451:         }
 5452:     }
 5453:     return $text;
 5454: }
 5455: 
 5456: sub modifiable_userdata_row {
 5457:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5458:     my ($role,$rolename,$statustype);
 5459:     $role = $item;
 5460:     if ($context eq 'cancreate') {
 5461:         if ($item =~ /^emailusername_(.+)$/) {
 5462:             $statustype = $1;
 5463:             $role = 'emailusername';
 5464:             if (ref($usertypes) eq 'HASH') {
 5465:                 if ($usertypes->{$statustype}) {
 5466:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5467:                 } else {
 5468:                     $rolename = &mt('Data provided by user');
 5469:                 }
 5470:             }
 5471:         }
 5472:     } elsif ($context eq 'selfcreate') {
 5473:         if (ref($usertypes) eq 'HASH') {
 5474:             $rolename = $usertypes->{$role};
 5475:         } else {
 5476:             $rolename = $role;
 5477:         }
 5478:     } else {
 5479:         if ($role eq 'cr') {
 5480:             $rolename = &mt('Custom role');
 5481:         } else {
 5482:             $rolename = &Apache::lonnet::plaintext($role);
 5483:         }
 5484:     }
 5485:     my (@fields,%fieldtitles);
 5486:     if (ref($fieldsref) eq 'ARRAY') {
 5487:         @fields = @{$fieldsref};
 5488:     } else {
 5489:         @fields = ('lastname','firstname','middlename','generation',
 5490:                    'permanentemail','id');
 5491:     }
 5492:     if ((ref($titlesref) eq 'HASH')) {
 5493:         %fieldtitles = %{$titlesref};
 5494:     } else {
 5495:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5496:     }
 5497:     my $output;
 5498:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5499:     $output = '<tr '.$css_class.'>'.
 5500:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5501:               '<td class="LC_left_item" colspan="2"><table>';
 5502:     my $rem;
 5503:     my %checks;
 5504:     if (ref($settings) eq 'HASH') {
 5505:         if (ref($settings->{$context}) eq 'HASH') {
 5506:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5507:                 my $hashref = $settings->{$context}->{$role};
 5508:                 if ($role eq 'emailusername') {
 5509:                     if ($statustype) {
 5510:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5511:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5512:                             if (ref($hashref) eq 'HASH') { 
 5513:                                 foreach my $field (@fields) {
 5514:                                     if ($hashref->{$field}) {
 5515:                                         $checks{$field} = $hashref->{$field};
 5516:                                     }
 5517:                                 }
 5518:                             }
 5519:                         }
 5520:                     }
 5521:                 } else {
 5522:                     if (ref($hashref) eq 'HASH') {
 5523:                         foreach my $field (@fields) {
 5524:                             if ($hashref->{$field}) {
 5525:                                 $checks{$field} = ' checked="checked" ';
 5526:                             }
 5527:                         }
 5528:                     }
 5529:                 }
 5530:             }
 5531:         }
 5532:     }
 5533:      
 5534:     for (my $i=0; $i<@fields; $i++) {
 5535:         my $rem = $i%($numinrow);
 5536:         if ($rem == 0) {
 5537:             if ($i > 0) {
 5538:                 $output .= '</tr>';
 5539:             }
 5540:             $output .= '<tr>';
 5541:         }
 5542:         my $check = ' ';
 5543:         unless ($role eq 'emailusername') {
 5544:             if (exists($checks{$fields[$i]})) {
 5545:                 $check = $checks{$fields[$i]}
 5546:             } else {
 5547:                 if ($role eq 'st') {
 5548:                     if (ref($settings) ne 'HASH') {
 5549:                         $check = ' checked="checked" '; 
 5550:                     }
 5551:                 }
 5552:             }
 5553:         }
 5554:         $output .= '<td class="LC_left_item">'.
 5555:                    '<span class="LC_nobreak">';
 5556:         if ($role eq 'emailusername') {
 5557:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5558:                 $checks{$fields[$i]} = 'omit';
 5559:             }
 5560:             foreach my $option ('required','optional','omit') {
 5561:                 my $checked='';
 5562:                 if ($checks{$fields[$i]} eq $option) {
 5563:                     $checked='checked="checked" ';
 5564:                 }
 5565:                 $output .= '<label>'.
 5566:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5567:                            &mt($option).'</label>'.('&nbsp;' x2);
 5568:             }
 5569:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5570:         } else {
 5571:             $output .= '<label>'.
 5572:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5573:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5574:                        '</label>';
 5575:         }
 5576:         $output .= '</span></td>';
 5577:         $rem = @fields%($numinrow);
 5578:     }
 5579:     my $colsleft = $numinrow - $rem;
 5580:     if ($colsleft > 1 ) {
 5581:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5582:                    '&nbsp;</td>';
 5583:     } elsif ($colsleft == 1) {
 5584:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5585:     }
 5586:     $output .= '</tr></table></td></tr>';
 5587:     return $output;
 5588: }
 5589: 
 5590: sub insttypes_row {
 5591:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5592:     my %lt = &Apache::lonlocal::texthash (
 5593:                       cansearch => 'Users allowed to search',
 5594:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5595:                       lockablenames => 'User preference to lock name',
 5596:              );
 5597:     my $showdom;
 5598:     if ($context eq 'cansearch') {
 5599:         $showdom = ' ('.$dom.')';
 5600:     }
 5601:     my $class = 'LC_left_item';
 5602:     if ($context eq 'statustocreate') {
 5603:         $class = 'LC_right_item';
 5604:     }
 5605:     my $css_class = ' class="LC_odd_row"';
 5606:     if ($rownum ne '') { 
 5607:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5608:     }
 5609:     my $output = '<tr'.$css_class.'>'.
 5610:                  '<td>'.$lt{$context}.$showdom.
 5611:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5612:     my $rem;
 5613:     if (ref($types) eq 'ARRAY') {
 5614:         for (my $i=0; $i<@{$types}; $i++) {
 5615:             if (defined($usertypes->{$types->[$i]})) {
 5616:                 my $rem = $i%($numinrow);
 5617:                 if ($rem == 0) {
 5618:                     if ($i > 0) {
 5619:                         $output .= '</tr>';
 5620:                     }
 5621:                     $output .= '<tr>';
 5622:                 }
 5623:                 my $check = ' ';
 5624:                 if (ref($settings) eq 'HASH') {
 5625:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5626:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5627:                             $check = ' checked="checked" ';
 5628:                         }
 5629:                     } elsif ($context eq 'statustocreate') {
 5630:                         $check = ' checked="checked" ';
 5631:                     }
 5632:                 }
 5633:                 $output .= '<td class="LC_left_item">'.
 5634:                            '<span class="LC_nobreak"><label>'.
 5635:                            '<input type="checkbox" name="'.$context.'" '.
 5636:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5637:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5638:             }
 5639:         }
 5640:         $rem = @{$types}%($numinrow);
 5641:     }
 5642:     my $colsleft = $numinrow - $rem;
 5643:     if (($rem == 0) && (@{$types} > 0)) {
 5644:         $output .= '<tr>';
 5645:     }
 5646:     if ($colsleft > 1) {
 5647:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5648:     } else {
 5649:         $output .= '<td class="LC_left_item">';
 5650:     }
 5651:     my $defcheck = ' ';
 5652:     if (ref($settings) eq 'HASH') {  
 5653:         if (ref($settings->{$context}) eq 'ARRAY') {
 5654:             if (grep(/^default$/,@{$settings->{$context}})) {
 5655:                 $defcheck = ' checked="checked" ';
 5656:             }
 5657:         } elsif ($context eq 'statustocreate') {
 5658:             $defcheck = ' checked="checked" ';
 5659:         }
 5660:     }
 5661:     $output .= '<span class="LC_nobreak"><label>'.
 5662:                '<input type="checkbox" name="'.$context.'" '.
 5663:                'value="default"'.$defcheck.'/>'.
 5664:                $othertitle.'</label></span></td>'.
 5665:                '</tr></table></td></tr>';
 5666:     return $output;
 5667: }
 5668: 
 5669: sub sorted_searchtitles {
 5670:     my %searchtitles = &Apache::lonlocal::texthash(
 5671:                          'uname' => 'username',
 5672:                          'lastname' => 'last name',
 5673:                          'lastfirst' => 'last name, first name',
 5674:                      );
 5675:     my @titleorder = ('uname','lastname','lastfirst');
 5676:     return (\%searchtitles,\@titleorder);
 5677: }
 5678: 
 5679: sub sorted_searchtypes {
 5680:     my %srchtypes_desc = (
 5681:                            exact    => 'is exact match',
 5682:                            contains => 'contains ..',
 5683:                            begins   => 'begins with ..',
 5684:                          );
 5685:     my @srchtypeorder = ('exact','begins','contains');
 5686:     return (\%srchtypes_desc,\@srchtypeorder);
 5687: }
 5688: 
 5689: sub usertype_update_row {
 5690:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5691:     my $datatable;
 5692:     my $numinrow = 4;
 5693:     foreach my $type (@{$types}) {
 5694:         if (defined($usertypes->{$type})) {
 5695:             $$rownums ++;
 5696:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5697:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5698:                           '</td><td class="LC_left_item"><table>';
 5699:             for (my $i=0; $i<@{$fields}; $i++) {
 5700:                 my $rem = $i%($numinrow);
 5701:                 if ($rem == 0) {
 5702:                     if ($i > 0) {
 5703:                         $datatable .= '</tr>';
 5704:                     }
 5705:                     $datatable .= '<tr>';
 5706:                 }
 5707:                 my $check = ' ';
 5708:                 if (ref($settings) eq 'HASH') {
 5709:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5710:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5711:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5712:                                 $check = ' checked="checked" ';
 5713:                             }
 5714:                         }
 5715:                     }
 5716:                 }
 5717: 
 5718:                 if ($i == @{$fields}-1) {
 5719:                     my $colsleft = $numinrow - $rem;
 5720:                     if ($colsleft > 1) {
 5721:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5722:                     } else {
 5723:                         $datatable .= '<td>';
 5724:                     }
 5725:                 } else {
 5726:                     $datatable .= '<td>';
 5727:                 }
 5728:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5729:                               '<input type="checkbox" name="updateable_'.$type.
 5730:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5731:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5732:             }
 5733:             $datatable .= '</tr></table></td></tr>';
 5734:         }
 5735:     }
 5736:     return $datatable;
 5737: }
 5738: 
 5739: sub modify_login {
 5740:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5741:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5742:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5743:     %title = ( coursecatalog => 'Display course catalog',
 5744:                adminmail => 'Display administrator E-mail address',
 5745:                helpdesk  => 'Display "Contact Helpdesk" link',
 5746:                newuser => 'Link for visitors to create a user account',
 5747:                loginheader => 'Log-in box header');
 5748:     @offon = ('off','on');
 5749:     if (ref($domconfig{login}) eq 'HASH') {
 5750:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5751:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5752:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5753:             }
 5754:         }
 5755:     }
 5756:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5757:                                            \%domconfig,\%loginhash);
 5758:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5759:     foreach my $item (@toggles) {
 5760:         $loginhash{login}{$item} = $env{'form.'.$item};
 5761:     }
 5762:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5763:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5764:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5765:                                          \%loginhash);
 5766:     }
 5767: 
 5768:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5769:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5770:     if (keys(%servers) > 1) {
 5771:         foreach my $lonhost (keys(%servers)) {
 5772:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5773:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5774:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5775:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5776:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5777:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5778:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5779:                         $changes{'loginvia'}{$lonhost} = 1;
 5780:                     } else {
 5781:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5782:                         $changes{'loginvia'}{$lonhost} = 1;
 5783:                     }
 5784:                 } else {
 5785:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5786:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5787:                         $changes{'loginvia'}{$lonhost} = 1;
 5788:                     }
 5789:                 }
 5790:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5791:                     foreach my $item (@loginvia_attribs) {
 5792:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5793:                     }
 5794:                 } else {
 5795:                     foreach my $item (@loginvia_attribs) {
 5796:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5797:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5798:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5799:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5800:                                 $new = '/';
 5801:                             }
 5802:                         }
 5803:                         if (($item eq 'custompath') && 
 5804:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5805:                             $new = '';
 5806:                         }
 5807:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5808:                             $changes{'loginvia'}{$lonhost} = 1;
 5809:                         }
 5810:                         if ($item eq 'exempt') {
 5811:                             $new =~ s/^\s+//;
 5812:                             $new =~ s/\s+$//;
 5813:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5814:                             my @okips;
 5815:                             foreach my $ip (@poss_ips) {
 5816:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5817:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5818:                                         push(@okips,$ip); 
 5819:                                     }
 5820:                                 }
 5821:                             }
 5822:                             if (@okips > 0) {
 5823:                                 $new = join(',',@okips); 
 5824:                             } else {
 5825:                                 $new = ''; 
 5826:                             }
 5827:                         }
 5828:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5829:                     }
 5830:                 }
 5831:             } else {
 5832:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5833:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5834:                     $changes{'loginvia'}{$lonhost} = 1;
 5835:                     foreach my $item (@loginvia_attribs) {
 5836:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5837:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5838:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5839:                                 $new = '/';
 5840:                             }
 5841:                         }
 5842:                         if (($item eq 'custompath') && 
 5843:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5844:                             $new = '';
 5845:                         }
 5846:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5847:                     }
 5848:                 }
 5849:             }
 5850:         }
 5851:     }
 5852: 
 5853:     my $servadm = $r->dir_config('lonAdmEMail');
 5854:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5855:     if (ref($domconfig{'login'}) eq 'HASH') {
 5856:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5857:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5858:                 if ($lang eq 'nolang') {
 5859:                     push(@currlangs,$lang);
 5860:                 } elsif (defined($langchoices{$lang})) {
 5861:                     push(@currlangs,$lang);
 5862:                 } else {
 5863:                     next;
 5864:                 }
 5865:             }
 5866:         }
 5867:     }
 5868:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5869:     if (@currlangs > 0) {
 5870:         foreach my $lang (@currlangs) {
 5871:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5872:                 $changes{'helpurl'}{$lang} = 1;
 5873:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5874:                 $changes{'helpurl'}{$lang} = 1;
 5875:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5876:                 push(@newlangs,$lang);
 5877:             } else {
 5878:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5879:             }
 5880:         }
 5881:     }
 5882:     unless (grep(/^nolang$/,@currlangs)) {
 5883:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5884:             $changes{'helpurl'}{'nolang'} = 1;
 5885:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5886:             push(@newlangs,'nolang');
 5887:         }
 5888:     }
 5889:     if ($env{'form.loginhelpurl_add_lang'}) {
 5890:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5891:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5892:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5893:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5894:         }
 5895:     }
 5896:     if ((@newlangs > 0) || ($addedfile)) {
 5897:         my $error;
 5898:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5899:         if ($configuserok eq 'ok') {
 5900:             if ($switchserver) {
 5901:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5902:             } elsif ($author_ok eq 'ok') {
 5903:                 my @allnew = @newlangs;
 5904:                 if ($addedfile ne '') {
 5905:                     push(@allnew,$addedfile);
 5906:                 }
 5907:                 foreach my $lang (@allnew) {
 5908:                     my $formelem = 'loginhelpurl_'.$lang;
 5909:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5910:                         $formelem = 'loginhelpurl_add_file';
 5911:                     }
 5912:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5913:                                                                "help/$lang",'','',$newfile{$lang});
 5914:                     if ($result eq 'ok') {
 5915:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5916:                         $changes{'helpurl'}{$lang} = 1;
 5917:                     } else {
 5918:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5919:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5920:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5921:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5922: 
 5923:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5924:                         }
 5925:                     }
 5926:                 }
 5927:             } else {
 5928:                 $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);
 5929:             }
 5930:         } else {
 5931:             $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);
 5932:         }
 5933:         if ($error) {
 5934:             &Apache::lonnet::logthis($error);
 5935:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5936:         }
 5937:     }
 5938:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5939: 
 5940:     my $defaulthelpfile = '/adm/loginproblems.html';
 5941:     my $defaulttext = &mt('Default in use');
 5942: 
 5943:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5944:                                              $dom);
 5945:     if ($putresult eq 'ok') {
 5946:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5947:         my %defaultchecked = (
 5948:                     'coursecatalog' => 'on',
 5949:                     'helpdesk'      => 'on',
 5950:                     'adminmail'     => 'off',
 5951:                     'newuser'       => 'off',
 5952:         );
 5953:         if (ref($domconfig{'login'}) eq 'HASH') {
 5954:             foreach my $item (@toggles) {
 5955:                 if ($defaultchecked{$item} eq 'on') { 
 5956:                     if (($domconfig{'login'}{$item} eq '0') &&
 5957:                         ($env{'form.'.$item} eq '1')) {
 5958:                         $changes{$item} = 1;
 5959:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5960:                               $domconfig{'login'}{$item} eq '1') &&
 5961:                              ($env{'form.'.$item} eq '0')) {
 5962:                         $changes{$item} = 1;
 5963:                     }
 5964:                 } elsif ($defaultchecked{$item} eq 'off') {
 5965:                     if (($domconfig{'login'}{$item} eq '1') &&
 5966:                         ($env{'form.'.$item} eq '0')) {
 5967:                         $changes{$item} = 1;
 5968:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5969:                               $domconfig{'login'}{$item} eq '0') &&
 5970:                              ($env{'form.'.$item} eq '1')) {
 5971:                         $changes{$item} = 1;
 5972:                     }
 5973:                 }
 5974:             }
 5975:         }
 5976:         if (keys(%changes) > 0 || $colchgtext) {
 5977:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5978:             if (ref($lastactref) eq 'HASH') {
 5979:                 $lastactref->{'domainconfig'} = 1;
 5980:             }
 5981:             $resulttext = &mt('Changes made:').'<ul>';
 5982:             foreach my $item (sort(keys(%changes))) {
 5983:                 if ($item eq 'loginvia') {
 5984:                     if (ref($changes{$item}) eq 'HASH') {
 5985:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5986:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5987:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5988:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5989:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5990:                                     $protocol = 'http' if ($protocol ne 'https');
 5991:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5992: 
 5993:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5994:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5995:                                     } else {
 5996:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5997:                                     }
 5998:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5999:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 6000:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 6001:                                     }
 6002:                                     $resulttext .= '</li>';
 6003:                                 } else {
 6004:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 6005:                                 }
 6006:                             } else {
 6007:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 6008:                             }
 6009:                         }
 6010:                         $resulttext .= '</ul></li>';
 6011:                     }
 6012:                 } elsif ($item eq 'helpurl') {
 6013:                     if (ref($changes{$item}) eq 'HASH') {
 6014:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 6015:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 6016:                                 my ($chg,$link);
 6017:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 6018:                                 if ($lang eq 'nolang') {
 6019:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 6020:                                 } else {
 6021:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 6022:                                 }
 6023:                                 $resulttext .= '<li>'.$chg.'</li>';
 6024:                             } else {
 6025:                                 my $chg;
 6026:                                 if ($lang eq 'nolang') {
 6027:                                     $chg = &mt('custom log-in help file for no preferred language');
 6028:                                 } else {
 6029:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 6030:                                 }
 6031:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 6032:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 6033:                                                       '?inhibitmenu=yes',$chg,600,500).
 6034:                                                '</li>';
 6035:                             }
 6036:                         }
 6037:                     }
 6038:                 } elsif ($item eq 'captcha') {
 6039:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6040:                         my $chgtxt;
 6041:                         if ($loginhash{'login'}{$item} eq 'notused') {
 6042:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 6043:                         } else {
 6044:                             my %captchas = &captcha_phrases();
 6045:                             if ($captchas{$loginhash{'login'}{$item}}) {
 6046:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 6047:                             } else {
 6048:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 6049:                             }
 6050:                         }
 6051:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6052:                     }
 6053:                 } elsif ($item eq 'recaptchakeys') {
 6054:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6055:                         my ($privkey,$pubkey);
 6056:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 6057:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 6058:                             $privkey = $loginhash{'login'}{$item}{'private'};
 6059:                         }
 6060:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 6061:                         if (!$pubkey) {
 6062:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 6063:                         } else {
 6064:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6065:                         }
 6066:                         if (!$privkey) {
 6067:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 6068:                         } else {
 6069:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6070:                         }
 6071:                         $chgtxt .= '</ul>';
 6072:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6073:                     }
 6074:                 } else {
 6075:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 6076:                 }
 6077:             }
 6078:             $resulttext .= $colchgtext.'</ul>';
 6079:         } else {
 6080:             $resulttext = &mt('No changes made to log-in page settings');
 6081:         }
 6082:     } else {
 6083:         $resulttext = '<span class="LC_error">'.
 6084: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6085:     }
 6086:     if ($errors) {
 6087:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 6088:                        $errors.'</ul>';
 6089:     }
 6090:     return $resulttext;
 6091: }
 6092: 
 6093: sub color_font_choices {
 6094:     my %choices =
 6095:         &Apache::lonlocal::texthash (
 6096:             img => "Header",
 6097:             bgs => "Background colors",
 6098:             links => "Link colors",
 6099:             images => "Images",
 6100:             font => "Font color",
 6101:             fontmenu => "Font menu",
 6102:             pgbg => "Page",
 6103:             tabbg => "Header",
 6104:             sidebg => "Border",
 6105:             link => "Link",
 6106:             alink => "Active link",
 6107:             vlink => "Visited link",
 6108:         );
 6109:     return %choices;
 6110: }
 6111: 
 6112: sub modify_rolecolors {
 6113:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 6114:     my ($resulttext,%rolehash);
 6115:     $rolehash{'rolecolors'} = {};
 6116:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 6117:         if ($domconfig{'rolecolors'} eq '') {
 6118:             $domconfig{'rolecolors'} = {};
 6119:         }
 6120:     }
 6121:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 6122:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 6123:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 6124:                                              $dom);
 6125:     if ($putresult eq 'ok') {
 6126:         if (keys(%changes) > 0) {
 6127:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6128:             if (ref($lastactref) eq 'HASH') {
 6129:                 $lastactref->{'domainconfig'} = 1;
 6130:             }
 6131:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 6132:                                              $rolehash{'rolecolors'});
 6133:         } else {
 6134:             $resulttext = &mt('No changes made to default color schemes');
 6135:         }
 6136:     } else {
 6137:         $resulttext = '<span class="LC_error">'.
 6138: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6139:     }
 6140:     if ($errors) {
 6141:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6142:                        $errors.'</ul>';
 6143:     }
 6144:     return $resulttext;
 6145: }
 6146: 
 6147: sub modify_colors {
 6148:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 6149:     my (%changes,%choices);
 6150:     my @bgs;
 6151:     my @links = ('link','alink','vlink');
 6152:     my @logintext;
 6153:     my @images;
 6154:     my $servadm = $r->dir_config('lonAdmEMail');
 6155:     my $errors;
 6156:     my %defaults;
 6157:     foreach my $role (@{$roles}) {
 6158:         if ($role eq 'login') {
 6159:             %choices = &login_choices();
 6160:             @logintext = ('textcol','bgcol');
 6161:         } else {
 6162:             %choices = &color_font_choices();
 6163:         }
 6164:         if ($role eq 'login') {
 6165:             @images = ('img','logo','domlogo','login');
 6166:             @bgs = ('pgbg','mainbg','sidebg');
 6167:         } else {
 6168:             @images = ('img');
 6169:             @bgs = ('pgbg','tabbg','sidebg');
 6170:         }
 6171:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 6172:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 6173:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 6174:         }
 6175:         if ($role eq 'login') {
 6176:             foreach my $item (@logintext) {
 6177:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6178:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6179:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6180:                 }
 6181:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 6182:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6183:                 }
 6184:             }
 6185:         } else {
 6186:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 6187:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 6188:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 6189:             }
 6190:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 6191:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 6192:             }
 6193:         }
 6194:         foreach my $item (@bgs) {
 6195:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6196:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6197:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6198:             }
 6199:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 6200:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6201:             }
 6202:         }
 6203:         foreach my $item (@links) {
 6204:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6205:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6206:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6207:             }
 6208:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 6209:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6210:             }
 6211:         }
 6212:         my ($configuserok,$author_ok,$switchserver) = 
 6213:             &config_check($dom,$confname,$servadm);
 6214:         my ($width,$height) = &thumb_dimensions();
 6215:         if (ref($domconfig->{$role}) ne 'HASH') {
 6216:             $domconfig->{$role} = {};
 6217:         }
 6218:         foreach my $img (@images) {
 6219:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 6220:                 if (defined($env{'form.login_showlogo_'.$img})) {
 6221:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 6222:                 } else { 
 6223:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 6224:                 }
 6225:             } 
 6226: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 6227: 		 && !defined($domconfig->{$role}{$img})
 6228: 		 && !$env{'form.'.$role.'_del_'.$img}
 6229: 		 && $env{'form.'.$role.'_import_'.$img}) {
 6230: 		# import the old configured image from the .tab setting
 6231: 		# if they haven't provided a new one 
 6232: 		$domconfig->{$role}{$img} = 
 6233: 		    $env{'form.'.$role.'_import_'.$img};
 6234: 	    }
 6235:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 6236:                 my $error;
 6237:                 if ($configuserok eq 'ok') {
 6238:                     if ($switchserver) {
 6239:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 6240:                     } else {
 6241:                         if ($author_ok eq 'ok') {
 6242:                             my ($result,$logourl) = 
 6243:                                 &publishlogo($r,'upload',$role.'_'.$img,
 6244:                                            $dom,$confname,$img,$width,$height);
 6245:                             if ($result eq 'ok') {
 6246:                                 $confhash->{$role}{$img} = $logourl;
 6247:                                 $changes{$role}{'images'}{$img} = 1;
 6248:                             } else {
 6249:                                 $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);
 6250:                             }
 6251:                         } else {
 6252:                             $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);
 6253:                         }
 6254:                     }
 6255:                 } else {
 6256:                     $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);
 6257:                 }
 6258:                 if ($error) {
 6259:                     &Apache::lonnet::logthis($error);
 6260:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6261:                 }
 6262:             } elsif ($domconfig->{$role}{$img} ne '') {
 6263:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 6264:                     my $error;
 6265:                     if ($configuserok eq 'ok') {
 6266: # is confname an author?
 6267:                         if ($switchserver eq '') {
 6268:                             if ($author_ok eq 'ok') {
 6269:                                 my ($result,$logourl) = 
 6270:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 6271:                                             $dom,$confname,$img,$width,$height);
 6272:                                 if ($result eq 'ok') {
 6273:                                     $confhash->{$role}{$img} = $logourl;
 6274: 				    $changes{$role}{'images'}{$img} = 1;
 6275:                                 }
 6276:                             }
 6277:                         }
 6278:                     }
 6279:                 }
 6280:             }
 6281:         }
 6282:         if (ref($domconfig) eq 'HASH') {
 6283:             if (ref($domconfig->{$role}) eq 'HASH') {
 6284:                 foreach my $img (@images) {
 6285:                     if ($domconfig->{$role}{$img} ne '') {
 6286:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6287:                             $confhash->{$role}{$img} = '';
 6288:                             $changes{$role}{'images'}{$img} = 1;
 6289:                         } else {
 6290:                             if ($confhash->{$role}{$img} eq '') {
 6291:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 6292:                             }
 6293:                         }
 6294:                     } else {
 6295:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6296:                             $confhash->{$role}{$img} = '';
 6297:                             $changes{$role}{'images'}{$img} = 1;
 6298:                         } 
 6299:                     }
 6300:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 6301:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 6302:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 6303:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 6304:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 6305:                             }
 6306:                         } else {
 6307:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6308:                                 $changes{$role}{'showlogo'}{$img} = 1;
 6309:                             }
 6310:                         }
 6311:                     }
 6312:                 }
 6313:                 if ($domconfig->{$role}{'font'} ne '') {
 6314:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 6315:                         $changes{$role}{'font'} = 1;
 6316:                     }
 6317:                 } else {
 6318:                     if ($confhash->{$role}{'font'}) {
 6319:                         $changes{$role}{'font'} = 1;
 6320:                     }
 6321:                 }
 6322:                 if ($role ne 'login') {
 6323:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 6324:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 6325:                             $changes{$role}{'fontmenu'} = 1;
 6326:                         }
 6327:                     } else {
 6328:                         if ($confhash->{$role}{'fontmenu'}) {
 6329:                             $changes{$role}{'fontmenu'} = 1;
 6330:                         }
 6331:                     }
 6332:                 }
 6333:                 foreach my $item (@bgs) {
 6334:                     if ($domconfig->{$role}{$item} ne '') {
 6335:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6336:                             $changes{$role}{'bgs'}{$item} = 1;
 6337:                         } 
 6338:                     } else {
 6339:                         if ($confhash->{$role}{$item}) {
 6340:                             $changes{$role}{'bgs'}{$item} = 1;
 6341:                         }
 6342:                     }
 6343:                 }
 6344:                 foreach my $item (@links) {
 6345:                     if ($domconfig->{$role}{$item} ne '') {
 6346:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6347:                             $changes{$role}{'links'}{$item} = 1;
 6348:                         }
 6349:                     } else {
 6350:                         if ($confhash->{$role}{$item}) {
 6351:                             $changes{$role}{'links'}{$item} = 1;
 6352:                         }
 6353:                     }
 6354:                 }
 6355:                 foreach my $item (@logintext) {
 6356:                     if ($domconfig->{$role}{$item} ne '') {
 6357:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6358:                             $changes{$role}{'logintext'}{$item} = 1;
 6359:                         }
 6360:                     } else {
 6361:                         if ($confhash->{$role}{$item}) {
 6362:                             $changes{$role}{'logintext'}{$item} = 1;
 6363:                         }
 6364:                     }
 6365:                 }
 6366:             } else {
 6367:                 &default_change_checker($role,\@images,\@links,\@bgs,
 6368:                                         \@logintext,$confhash,\%changes); 
 6369:             }
 6370:         } else {
 6371:             &default_change_checker($role,\@images,\@links,\@bgs,
 6372:                                     \@logintext,$confhash,\%changes); 
 6373:         }
 6374:     }
 6375:     return ($errors,%changes);
 6376: }
 6377: 
 6378: sub config_check {
 6379:     my ($dom,$confname,$servadm) = @_;
 6380:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6381:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6382:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6383:                                                    $confname,$servadm);
 6384:     if ($configuserok eq 'ok') {
 6385:         $switchserver = &check_switchserver($dom,$confname);
 6386:         if ($switchserver eq '') {
 6387:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6388:         }
 6389:     }
 6390:     return ($configuserok,$author_ok,$switchserver);
 6391: }
 6392: 
 6393: sub default_change_checker {
 6394:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6395:     foreach my $item (@{$links}) {
 6396:         if ($confhash->{$role}{$item}) {
 6397:             $changes->{$role}{'links'}{$item} = 1;
 6398:         }
 6399:     }
 6400:     foreach my $item (@{$bgs}) {
 6401:         if ($confhash->{$role}{$item}) {
 6402:             $changes->{$role}{'bgs'}{$item} = 1;
 6403:         }
 6404:     }
 6405:     foreach my $item (@{$logintext}) {
 6406:         if ($confhash->{$role}{$item}) {
 6407:             $changes->{$role}{'logintext'}{$item} = 1;
 6408:         }
 6409:     }
 6410:     foreach my $img (@{$images}) {
 6411:         if ($env{'form.'.$role.'_del_'.$img}) {
 6412:             $confhash->{$role}{$img} = '';
 6413:             $changes->{$role}{'images'}{$img} = 1;
 6414:         }
 6415:         if ($role eq 'login') {
 6416:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6417:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6418:             }
 6419:         }
 6420:     }
 6421:     if ($confhash->{$role}{'font'}) {
 6422:         $changes->{$role}{'font'} = 1;
 6423:     }
 6424: }
 6425: 
 6426: sub display_colorchgs {
 6427:     my ($dom,$changes,$roles,$confhash) = @_;
 6428:     my (%choices,$resulttext);
 6429:     if (!grep(/^login$/,@{$roles})) {
 6430:         $resulttext = &mt('Changes made:').'<br />';
 6431:     }
 6432:     foreach my $role (@{$roles}) {
 6433:         if ($role eq 'login') {
 6434:             %choices = &login_choices();
 6435:         } else {
 6436:             %choices = &color_font_choices();
 6437:         }
 6438:         if (ref($changes->{$role}) eq 'HASH') {
 6439:             if ($role ne 'login') {
 6440:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6441:             }
 6442:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6443:                 if ($role ne 'login') {
 6444:                     $resulttext .= '<ul>';
 6445:                 }
 6446:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6447:                     if ($role ne 'login') {
 6448:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6449:                     }
 6450:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6451:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6452:                             if ($confhash->{$role}{$key}{$item}) {
 6453:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6454:                             } else {
 6455:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6456:                             }
 6457:                         } elsif ($confhash->{$role}{$item} eq '') {
 6458:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6459:                         } else {
 6460:                             my $newitem = $confhash->{$role}{$item};
 6461:                             if ($key eq 'images') {
 6462:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6463:                             }
 6464:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6465:                         }
 6466:                     }
 6467:                     if ($role ne 'login') {
 6468:                         $resulttext .= '</ul></li>';
 6469:                     }
 6470:                 } else {
 6471:                     if ($confhash->{$role}{$key} eq '') {
 6472:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6473:                     } else {
 6474:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6475:                     }
 6476:                 }
 6477:                 if ($role ne 'login') {
 6478:                     $resulttext .= '</ul>';
 6479:                 }
 6480:             }
 6481:         }
 6482:     }
 6483:     return $resulttext;
 6484: }
 6485: 
 6486: sub thumb_dimensions {
 6487:     return ('200','50');
 6488: }
 6489: 
 6490: sub check_dimensions {
 6491:     my ($inputfile) = @_;
 6492:     my ($fullwidth,$fullheight);
 6493:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6494:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6495:             my $imageinfo = <PIPE>;
 6496:             if (!close(PIPE)) {
 6497:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6498:             }
 6499:             chomp($imageinfo);
 6500:             my ($fullsize) = 
 6501:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6502:             if ($fullsize) {
 6503:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6504:             }
 6505:         }
 6506:     }
 6507:     return ($fullwidth,$fullheight);
 6508: }
 6509: 
 6510: sub check_configuser {
 6511:     my ($uhome,$dom,$confname,$servadm) = @_;
 6512:     my ($configuserok,%currroles);
 6513:     if ($uhome eq 'no_host') {
 6514:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6515:         my $configpass = &LONCAPA::Enrollment::create_password();
 6516:         $configuserok = 
 6517:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6518:                              $configpass,'','','','','',undef,$servadm);
 6519:     } else {
 6520:         $configuserok = 'ok';
 6521:         %currroles = 
 6522:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6523:     }
 6524:     return ($configuserok,%currroles);
 6525: }
 6526: 
 6527: sub check_authorstatus {
 6528:     my ($dom,$confname,%currroles) = @_;
 6529:     my $author_ok;
 6530:     if (!$currroles{':'.$dom.':au'}) {
 6531:         my $start = time;
 6532:         my $end = 0;
 6533:         $author_ok = 
 6534:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6535:                                         'au',$end,$start,'','','domconfig');
 6536:     } else {
 6537:         $author_ok = 'ok';
 6538:     }
 6539:     return $author_ok;
 6540: }
 6541: 
 6542: sub publishlogo {
 6543:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6544:     my ($output,$fname,$logourl);
 6545:     if ($action eq 'upload') {
 6546:         $fname=$env{'form.'.$formname.'.filename'};
 6547:         chop($env{'form.'.$formname});
 6548:     } else {
 6549:         ($fname) = ($formname =~ /([^\/]+)$/);
 6550:     }
 6551:     if ($savefileas ne '') {
 6552:         $fname = $savefileas;
 6553:     }
 6554:     $fname=&Apache::lonnet::clean_filename($fname);
 6555: # See if there is anything left
 6556:     unless ($fname) { return ('error: no uploaded file'); }
 6557:     $fname="$subdir/$fname";
 6558:     my $docroot=$r->dir_config('lonDocRoot');
 6559:     my $filepath="$docroot/priv";
 6560:     my $relpath = "$dom/$confname";
 6561:     my ($fnamepath,$file,$fetchthumb);
 6562:     $file=$fname;
 6563:     if ($fname=~m|/|) {
 6564:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6565:     }
 6566:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6567:     my $count;
 6568:     for ($count=5;$count<=$#parts;$count++) {
 6569:         $filepath.="/$parts[$count]";
 6570:         if ((-e $filepath)!=1) {
 6571:             mkdir($filepath,02770);
 6572:         }
 6573:     }
 6574:     # Check for bad extension and disallow upload
 6575:     if ($file=~/\.(\w+)$/ &&
 6576:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6577:         $output = 
 6578:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6579:     } elsif ($file=~/\.(\w+)$/ &&
 6580:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6581:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6582:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6583:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6584:     } elsif (-d "$filepath/$file") {
 6585:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6586:     } else {
 6587:         my $source = $filepath.'/'.$file;
 6588:         my $logfile;
 6589:         if (!open($logfile,">>$source".'.log')) {
 6590:             return (&mt('No write permission to Authoring Space'));
 6591:         }
 6592:         print $logfile
 6593: "\n================= Publish ".localtime()." ================\n".
 6594: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6595: # Save the file
 6596:         if (!open(FH,'>'.$source)) {
 6597:             &Apache::lonnet::logthis('Failed to create '.$source);
 6598:             return (&mt('Failed to create file'));
 6599:         }
 6600:         if ($action eq 'upload') {
 6601:             if (!print FH ($env{'form.'.$formname})) {
 6602:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6603:                 return (&mt('Failed to write file'));
 6604:             }
 6605:         } else {
 6606:             my $original = &Apache::lonnet::filelocation('',$formname);
 6607:             if(!copy($original,$source)) {
 6608:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6609:                 return (&mt('Failed to write file'));
 6610:             }
 6611:         }
 6612:         close(FH);
 6613:         chmod(0660, $source); # Permissions to rw-rw---.
 6614: 
 6615:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6616:         my $copyfile=$targetdir.'/'.$file;
 6617: 
 6618:         my @parts=split(/\//,$targetdir);
 6619:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6620:         for (my $count=5;$count<=$#parts;$count++) {
 6621:             $path.="/$parts[$count]";
 6622:             if (!-e $path) {
 6623:                 print $logfile "\nCreating directory ".$path;
 6624:                 mkdir($path,02770);
 6625:             }
 6626:         }
 6627:         my $versionresult;
 6628:         if (-e $copyfile) {
 6629:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6630:         } else {
 6631:             $versionresult = 'ok';
 6632:         }
 6633:         if ($versionresult eq 'ok') {
 6634:             if (copy($source,$copyfile)) {
 6635:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6636:                 $output = 'ok';
 6637:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6638:                 push(@{$modified_urls},[$copyfile,$source]);
 6639:                 my $metaoutput = 
 6640:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6641:                 unless ($registered_cleanup) {
 6642:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6643:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6644:                     $registered_cleanup=1;
 6645:                 }
 6646:             } else {
 6647:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6648:                 $output = &mt('Failed to copy file to RES space').", $!";
 6649:             }
 6650:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6651:                 my $inputfile = $filepath.'/'.$file;
 6652:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6653:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6654:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6655:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6656:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6657:                         system("convert -sample $thumbsize $inputfile $outfile");
 6658:                         chmod(0660, $filepath.'/tn-'.$file);
 6659:                         if (-e $outfile) {
 6660:                             my $copyfile=$targetdir.'/tn-'.$file;
 6661:                             if (copy($outfile,$copyfile)) {
 6662:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6663:                                 my $thumb_metaoutput = 
 6664:                                     &write_metadata($dom,$confname,$formname,
 6665:                                                     $targetdir,'tn-'.$file,$logfile);
 6666:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6667:                                 unless ($registered_cleanup) {
 6668:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6669:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6670:                                     $registered_cleanup=1;
 6671:                                 }
 6672:                             } else {
 6673:                                 print $logfile "\nUnable to write ".$copyfile.
 6674:                                                ':'.$!."\n";
 6675:                             }
 6676:                         }
 6677:                     }
 6678:                 }
 6679:             }
 6680:         } else {
 6681:             $output = $versionresult;
 6682:         }
 6683:     }
 6684:     return ($output,$logourl);
 6685: }
 6686: 
 6687: sub logo_versioning {
 6688:     my ($targetdir,$file,$logfile) = @_;
 6689:     my $target = $targetdir.'/'.$file;
 6690:     my ($maxversion,$fn,$extn,$output);
 6691:     $maxversion = 0;
 6692:     if ($file =~ /^(.+)\.(\w+)$/) {
 6693:         $fn=$1;
 6694:         $extn=$2;
 6695:     }
 6696:     opendir(DIR,$targetdir);
 6697:     while (my $filename=readdir(DIR)) {
 6698:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6699:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6700:         }
 6701:     }
 6702:     $maxversion++;
 6703:     print $logfile "\nCreating old version ".$maxversion."\n";
 6704:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6705:     if (copy($target,$copyfile)) {
 6706:         print $logfile "Copied old target to ".$copyfile."\n";
 6707:         $copyfile=$copyfile.'.meta';
 6708:         if (copy($target.'.meta',$copyfile)) {
 6709:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6710:             $output = 'ok';
 6711:         } else {
 6712:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6713:             $output = &mt('Failed to copy old meta').", $!, ";
 6714:         }
 6715:     } else {
 6716:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6717:         $output = &mt('Failed to copy old target').", $!, ";
 6718:     }
 6719:     return $output;
 6720: }
 6721: 
 6722: sub write_metadata {
 6723:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6724:     my (%metadatafields,%metadatakeys,$output);
 6725:     $metadatafields{'title'}=$formname;
 6726:     $metadatafields{'creationdate'}=time;
 6727:     $metadatafields{'lastrevisiondate'}=time;
 6728:     $metadatafields{'copyright'}='public';
 6729:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6730:                                          $env{'user.domain'};
 6731:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6732:     $metadatafields{'domain'}=$dom;
 6733:     {
 6734:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6735:         my $mfh;
 6736:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6737:             foreach (sort(keys(%metadatafields))) {
 6738:                 unless ($_=~/\./) {
 6739:                     my $unikey=$_;
 6740:                     $unikey=~/^([A-Za-z]+)/;
 6741:                     my $tag=$1;
 6742:                     $tag=~tr/A-Z/a-z/;
 6743:                     print $mfh "\n\<$tag";
 6744:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6745:                         my $value=$metadatafields{$unikey.'.'.$_};
 6746:                         $value=~s/\"/\'\'/g;
 6747:                         print $mfh ' '.$_.'="'.$value.'"';
 6748:                     }
 6749:                     print $mfh '>'.
 6750:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6751:                             .'</'.$tag.'>';
 6752:                 }
 6753:             }
 6754:             $output = 'ok';
 6755:             print $logfile "\nWrote metadata";
 6756:             close($mfh);
 6757:         } else {
 6758:             print $logfile "\nFailed to open metadata file";
 6759:             $output = &mt('Could not write metadata');
 6760:         }
 6761:     }
 6762:     return $output;
 6763: }
 6764: 
 6765: sub notifysubscribed {
 6766:     foreach my $targetsource (@{$modified_urls}){
 6767:         next unless (ref($targetsource) eq 'ARRAY');
 6768:         my ($target,$source)=@{$targetsource};
 6769:         if ($source ne '') {
 6770:             if (open(my $logfh,'>>'.$source.'.log')) {
 6771:                 print $logfh "\nCleanup phase: Notifications\n";
 6772:                 my @subscribed=&subscribed_hosts($target);
 6773:                 foreach my $subhost (@subscribed) {
 6774:                     print $logfh "\nNotifying host ".$subhost.':';
 6775:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6776:                     print $logfh $reply;
 6777:                 }
 6778:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6779:                 foreach my $subhost (@subscribedmeta) {
 6780:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6781:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6782:                                                         $subhost);
 6783:                     print $logfh $reply;
 6784:                 }
 6785:                 print $logfh "\n============ Done ============\n";
 6786:                 close($logfh);
 6787:             }
 6788:         }
 6789:     }
 6790:     return OK;
 6791: }
 6792: 
 6793: sub subscribed_hosts {
 6794:     my ($target) = @_;
 6795:     my @subscribed;
 6796:     if (open(my $fh,"<$target.subscription")) {
 6797:         while (my $subline=<$fh>) {
 6798:             if ($subline =~ /^($match_lonid):/) {
 6799:                 my $host = $1;
 6800:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6801:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6802:                         push(@subscribed,$host);
 6803:                     }
 6804:                 }
 6805:             }
 6806:         }
 6807:     }
 6808:     return @subscribed;
 6809: }
 6810: 
 6811: sub check_switchserver {
 6812:     my ($dom,$confname) = @_;
 6813:     my ($allowed,$switchserver);
 6814:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6815:     if ($home eq 'no_host') {
 6816:         $home = &Apache::lonnet::domain($dom,'primary');
 6817:     }
 6818:     my @ids=&Apache::lonnet::current_machine_ids();
 6819:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6820:     if (!$allowed) {
 6821: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6822:     }
 6823:     return $switchserver;
 6824: }
 6825: 
 6826: sub modify_quotas {
 6827:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6828:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6829:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6830:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 6831:         $validationfieldsref);
 6832:     if ($action eq 'quotas') {
 6833:         $context = 'tools'; 
 6834:     } else {
 6835:         $context = $action;
 6836:     }
 6837:     if ($context eq 'requestcourses') {
 6838:         @usertools = ('official','unofficial','community','textbook');
 6839:         @options =('norequest','approval','validate','autolimit');
 6840:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6841:         %titles = &courserequest_titles();
 6842:         $toolregexp = join('|',@usertools);
 6843:         %conditions = &courserequest_conditions();
 6844:         $confname = $dom.'-domainconfig';
 6845:         my $servadm = $r->dir_config('lonAdmEMail');
 6846:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6847:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 6848:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6849:     } elsif ($context eq 'requestauthor') {
 6850:         @usertools = ('author');
 6851:         %titles = &authorrequest_titles();
 6852:     } else {
 6853:         @usertools = ('aboutme','blog','webdav','portfolio');
 6854:         %titles = &tool_titles();
 6855:     }
 6856:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6857:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6858:     foreach my $key (keys(%env)) {
 6859:         if ($context eq 'requestcourses') {
 6860:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6861:                 my $item = $1;
 6862:                 my $type = $2;
 6863:                 if ($type =~ /^limit_(.+)/) {
 6864:                     $limithash{$item}{$1} = $env{$key};
 6865:                 } else {
 6866:                     $confhash{$item}{$type} = $env{$key};
 6867:                 }
 6868:             }
 6869:         } elsif ($context eq 'requestauthor') {
 6870:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6871:                 $confhash{$1} = $env{$key};
 6872:             }
 6873:         } else {
 6874:             if ($key =~ /^form\.quota_(.+)$/) {
 6875:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6876:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6877:                 $confhash{'authorquota'}{$1} = $env{$key};
 6878:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6879:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6880:             }
 6881:         }
 6882:     }
 6883:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6884:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6885:         @approvalnotify = sort(@approvalnotify);
 6886:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6887:         my @crstypes = ('official','unofficial','community','textbook');
 6888:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6889:         foreach my $type (@hasuniquecode) {
 6890:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6891:                 $confhash{'uniquecode'}{$type} = 1;
 6892:             }
 6893:         }
 6894:         my (%newbook,%allpos);
 6895:         if ($context eq 'requestcourses') {
 6896:             foreach my $type ('textbooks','templates') {
 6897:                 @{$allpos{$type}} = (); 
 6898:                 my $invalid;
 6899:                 if ($type eq 'textbooks') {
 6900:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 6901:                 } else {
 6902:                     $invalid = &mt('Invalid LON-CAPA course for template');
 6903:                 }
 6904:                 if ($env{'form.'.$type.'_addbook'}) {
 6905:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 6906:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 6907:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 6908:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 6909:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 6910:                         } else {
 6911:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 6912:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 6913:                             $position =~ s/\D+//g;
 6914:                             if ($position ne '') {
 6915:                                 $allpos{$type}[$position] = $newbook{$type};
 6916:                             }
 6917:                         }
 6918:                     } else {
 6919:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 6920:                     }
 6921:                 }
 6922:             } 
 6923:         }
 6924:         if (ref($domconfig{$action}) eq 'HASH') {
 6925:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6926:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6927:                     $changes{'notify'}{'approval'} = 1;
 6928:                 }
 6929:             } else {
 6930:                 if ($confhash{'notify'}{'approval'}) {
 6931:                     $changes{'notify'}{'approval'} = 1;
 6932:                 }
 6933:             }
 6934:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6935:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6936:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6937:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6938:                             $changes{'uniquecode'} = 1;
 6939:                         }
 6940:                     }
 6941:                     unless ($changes{'uniquecode'}) {
 6942:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6943:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6944:                                 $changes{'uniquecode'} = 1;
 6945:                             }
 6946:                         }
 6947:                     }
 6948:                } else {
 6949:                    $changes{'uniquecode'} = 1;
 6950:                }
 6951:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6952:                 $changes{'uniquecode'} = 1;
 6953:             }
 6954:             if ($context eq 'requestcourses') {
 6955:                 foreach my $type ('textbooks','templates') {
 6956:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 6957:                         my %deletions;
 6958:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 6959:                         if (@todelete) {
 6960:                             map { $deletions{$_} = 1; } @todelete;
 6961:                         }
 6962:                         my %imgdeletions;
 6963:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 6964:                         if (@todeleteimages) {
 6965:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 6966:                         }
 6967:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 6968:                         for (my $i=0; $i<=$maxnum; $i++) {
 6969:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 6970:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 6971:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 6972:                                 if ($deletions{$key}) {
 6973:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 6974:                                         #FIXME need to obsolete item in RES space
 6975:                                     }
 6976:                                     next;
 6977:                                 } else {
 6978:                                     my $newpos = $env{'form.'.$itemid};
 6979:                                     $newpos =~ s/\D+//g;
 6980:                                     foreach my $item ('subject','title','publisher','author') {
 6981:                                         next if ((($item eq 'author') || ($item eq 'publisher')) && 
 6982:                                                  ($type eq 'templates'));
 6983:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 6984:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 6985:                                             $changes{$type}{$key} = 1;
 6986:                                         }
 6987:                                     }
 6988:                                     $allpos{$type}[$newpos] = $key;
 6989:                                 }
 6990:                                 if ($imgdeletions{$key}) {
 6991:                                     $changes{$type}{$key} = 1;
 6992:                                     #FIXME need to obsolete item in RES space
 6993:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 6994:                                     my ($cdom,$cnum) = split(/_/,$key);
 6995:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 6996:                                                                                   $cdom,$cnum,$type,$configuserok,
 6997:                                                                                   $switchserver,$author_ok);
 6998:                                     if ($imgurl) {
 6999:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 7000:                                         $changes{$type}{$key} = 1; 
 7001:                                     }
 7002:                                     if ($error) {
 7003:                                         &Apache::lonnet::logthis($error);
 7004:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7005:                                     } 
 7006:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 7007:                                     $confhash{$type}{$key}{'image'} = 
 7008:                                         $domconfig{$action}{$type}{$key}{'image'};
 7009:                                 }
 7010:                             }
 7011:                         }
 7012:                     }
 7013:                 }
 7014:             }
 7015:         } else {
 7016:             if ($confhash{'notify'}{'approval'}) {
 7017:                 $changes{'notify'}{'approval'} = 1;
 7018:             }
 7019:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 7020:                 $changes{'uniquecode'} = 1;
 7021:             }
 7022:         }
 7023:         if ($context eq 'requestcourses') {
 7024:             foreach my $type ('textbooks','templates') {
 7025:                 if ($newbook{$type}) {
 7026:                     $changes{$type}{$newbook{$type}} = 1;
 7027:                     foreach my $item ('subject','title','publisher','author') {
 7028:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7029:                                  ($type eq 'template'));
 7030:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 7031:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 7032:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 7033:                         }
 7034:                     }
 7035:                     if ($type eq 'textbooks') {
 7036:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 7037:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 7038:                             my ($imageurl,$error) =
 7039:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 7040:                                                         $configuserok,$switchserver,$author_ok);
 7041:                             if ($imageurl) {
 7042:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 7043:                             }
 7044:                             if ($error) {
 7045:                                 &Apache::lonnet::logthis($error);
 7046:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7047:                             }
 7048:                         }
 7049:                     }
 7050:                 }
 7051:                 if (@{$allpos{$type}} > 0) {
 7052:                     my $idx = 0;
 7053:                     foreach my $item (@{$allpos{$type}}) {
 7054:                         if ($item ne '') {
 7055:                             $confhash{$type}{$item}{'order'} = $idx;
 7056:                             if (ref($domconfig{$action}) eq 'HASH') {
 7057:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7058:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 7059:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 7060:                                             $changes{$type}{$item} = 1;
 7061:                                         }
 7062:                                     }
 7063:                                 }
 7064:                             }
 7065:                             $idx ++;
 7066:                         }
 7067:                     }
 7068:                 }
 7069:             }
 7070:             if (ref($validationitemsref) eq 'ARRAY') {
 7071:                 foreach my $item (@{$validationitemsref}) {
 7072:                     if ($item eq 'fields') {
 7073:                         my @changed;
 7074:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 7075:                         if (@{$confhash{'validation'}{$item}} > 0) {
 7076:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 7077:                         }
 7078:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7079:                             if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 7080:                                 @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 7081:                                                                               $domconfig{'requestcourses'}{'validation'}{$item});
 7082:                             } else {
 7083:                                 @changed = @{$confhash{'validation'}{$item}};
 7084:                             }
 7085:                         } else {
 7086:                             @changed = @{$confhash{'validation'}{$item}};
 7087:                         }
 7088:                         if (@changed) {
 7089:                             if ($confhash{'validation'}{$item}) {
 7090:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 7091:                             } else {
 7092:                                 $changes{'validation'}{$item} = &mt('None');
 7093:                             }
 7094:                         }
 7095:                     } else {
 7096:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 7097:                         if ($item eq 'markup') {
 7098:                             if ($env{'form.requestcourses_validation_'.$item}) {
 7099:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 7100:                             }
 7101:                         }
 7102:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7103:                             if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 7104:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7105:                             }
 7106:                         } else {
 7107:                             if ($confhash{'validation'}{$item} ne '') {
 7108:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7109:                             }
 7110:                         }
 7111:                     }
 7112:                 }
 7113:             }
 7114:             if ($env{'form.validationdc'}) {
 7115:                 my $newval = $env{'form.validationdc'};
 7116:                 my %domcoords = &get_active_dcs($dom);
 7117:                 if (exists($domcoords{$newval})) {
 7118:                     $confhash{'validation'}{'dc'} = $newval;
 7119:                 }
 7120:             }
 7121:             if (ref($confhash{'validation'}) eq 'HASH') {
 7122:                 if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7123:                     if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7124:                         unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7125:                             if ($confhash{'validation'}{'dc'} eq '') {
 7126:                                 $changes{'validation'}{'dc'} = &mt('None');
 7127:                             } else {
 7128:                                 $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7129:                             }
 7130:                         }
 7131:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 7132:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7133:                     }
 7134:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 7135:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7136:                 }
 7137:             } elsif (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7138:                 if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7139:                     $changes{'validation'}{'dc'} = &mt('None');
 7140:                 }
 7141:             }
 7142:         }
 7143:     } else {
 7144:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 7145:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 7146:     }
 7147:     foreach my $item (@usertools) {
 7148:         foreach my $type (@{$types},'default','_LC_adv') {
 7149:             my $unset; 
 7150:             if ($context eq 'requestcourses') {
 7151:                 $unset = '0';
 7152:                 if ($type eq '_LC_adv') {
 7153:                     $unset = '';
 7154:                 }
 7155:                 if ($confhash{$item}{$type} eq 'autolimit') {
 7156:                     $confhash{$item}{$type} .= '=';
 7157:                     unless ($limithash{$item}{$type} =~ /\D/) {
 7158:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 7159:                     }
 7160:                 }
 7161:             } elsif ($context eq 'requestauthor') {
 7162:                 $unset = '0';
 7163:                 if ($type eq '_LC_adv') {
 7164:                     $unset = '';
 7165:                 }
 7166:             } else {
 7167:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 7168:                     $confhash{$item}{$type} = 1;
 7169:                 } else {
 7170:                     $confhash{$item}{$type} = 0;
 7171:                 }
 7172:             }
 7173:             if (ref($domconfig{$action}) eq 'HASH') {
 7174:                 if ($action eq 'requestauthor') {
 7175:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 7176:                         $changes{$type} = 1;
 7177:                     }
 7178:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 7179:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 7180:                         $changes{$item}{$type} = 1;
 7181:                     }
 7182:                 } else {
 7183:                     if ($context eq 'requestcourses') {
 7184:                         if ($confhash{$item}{$type} ne $unset) {
 7185:                             $changes{$item}{$type} = 1;
 7186:                         }
 7187:                     } else {
 7188:                         if (!$confhash{$item}{$type}) {
 7189:                             $changes{$item}{$type} = 1;
 7190:                         }
 7191:                     }
 7192:                 }
 7193:             } else {
 7194:                 if ($context eq 'requestcourses') {
 7195:                     if ($confhash{$item}{$type} ne $unset) {
 7196:                         $changes{$item}{$type} = 1;
 7197:                     }
 7198:                 } elsif ($context eq 'requestauthor') {
 7199:                     if ($confhash{$type} ne $unset) {
 7200:                         $changes{$type} = 1;
 7201:                     }
 7202:                 } else {
 7203:                     if (!$confhash{$item}{$type}) {
 7204:                         $changes{$item}{$type} = 1;
 7205:                     }
 7206:                 }
 7207:             }
 7208:         }
 7209:     }
 7210:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7211:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 7212:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7213:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 7214:                     if (exists($confhash{'defaultquota'}{$key})) {
 7215:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 7216:                             $changes{'defaultquota'}{$key} = 1;
 7217:                         }
 7218:                     } else {
 7219:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 7220:                     }
 7221:                 }
 7222:             } else {
 7223:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 7224:                     if (exists($confhash{'defaultquota'}{$key})) {
 7225:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 7226:                             $changes{'defaultquota'}{$key} = 1;
 7227:                         }
 7228:                     } else {
 7229:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 7230:                     }
 7231:                 }
 7232:             }
 7233:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7234:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 7235:                     if (exists($confhash{'authorquota'}{$key})) {
 7236:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 7237:                             $changes{'authorquota'}{$key} = 1;
 7238:                         }
 7239:                     } else {
 7240:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 7241:                     }
 7242:                 }
 7243:             }
 7244:         }
 7245:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 7246:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 7247:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7248:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7249:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 7250:                             $changes{'defaultquota'}{$key} = 1;
 7251:                         }
 7252:                     } else {
 7253:                         if (!exists($domconfig{'quotas'}{$key})) {
 7254:                             $changes{'defaultquota'}{$key} = 1;
 7255:                         }
 7256:                     }
 7257:                 } else {
 7258:                     $changes{'defaultquota'}{$key} = 1;
 7259:                 }
 7260:             }
 7261:         }
 7262:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 7263:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 7264:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7265:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7266:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 7267:                             $changes{'authorquota'}{$key} = 1;
 7268:                         }
 7269:                     } else {
 7270:                         $changes{'authorquota'}{$key} = 1;
 7271:                     }
 7272:                 } else {
 7273:                     $changes{'authorquota'}{$key} = 1;
 7274:                 }
 7275:             }
 7276:         }
 7277:     }
 7278: 
 7279:     if ($context eq 'requestauthor') {
 7280:         $domdefaults{'requestauthor'} = \%confhash;
 7281:     } else {
 7282:         foreach my $key (keys(%confhash)) {
 7283:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 7284:                 $domdefaults{$key} = $confhash{$key};
 7285:             }
 7286:         }
 7287:     }
 7288: 
 7289:     my %quotahash = (
 7290:                       $action => { %confhash }
 7291:                     );
 7292:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 7293:                                              $dom);
 7294:     if ($putresult eq 'ok') {
 7295:         if (keys(%changes) > 0) {
 7296:             my $cachetime = 24*60*60;
 7297:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7298:             if (ref($lastactref) eq 'HASH') {
 7299:                 $lastactref->{'domdefaults'} = 1;
 7300:             }
 7301:             $resulttext = &mt('Changes made:').'<ul>';
 7302:             unless (($context eq 'requestcourses') ||
 7303:                     ($context eq 'requestauthor')) {
 7304:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 7305:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 7306:                     foreach my $type (@{$types},'default') {
 7307:                         if (defined($changes{'defaultquota'}{$type})) {
 7308:                             my $typetitle = $usertypes->{$type};
 7309:                             if ($type eq 'default') {
 7310:                                 $typetitle = $othertitle;
 7311:                             }
 7312:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 7313:                         }
 7314:                     }
 7315:                     $resulttext .= '</ul></li>';
 7316:                 }
 7317:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 7318:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 7319:                     foreach my $type (@{$types},'default') {
 7320:                         if (defined($changes{'authorquota'}{$type})) {
 7321:                             my $typetitle = $usertypes->{$type};
 7322:                             if ($type eq 'default') {
 7323:                                 $typetitle = $othertitle;
 7324:                             }
 7325:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 7326:                         }
 7327:                     }
 7328:                     $resulttext .= '</ul></li>';
 7329:                 }
 7330:             }
 7331:             my %newenv;
 7332:             foreach my $item (@usertools) {
 7333:                 my (%haschgs,%inconf);
 7334:                 if ($context eq 'requestauthor') {
 7335:                     %haschgs = %changes;
 7336:                     %inconf = %confhash;
 7337:                 } else {
 7338:                     if (ref($changes{$item}) eq 'HASH') {
 7339:                         %haschgs = %{$changes{$item}};
 7340:                     }
 7341:                     if (ref($confhash{$item}) eq 'HASH') {
 7342:                         %inconf = %{$confhash{$item}};
 7343:                     }
 7344:                 }
 7345:                 if (keys(%haschgs) > 0) {
 7346:                     my $newacc = 
 7347:                         &Apache::lonnet::usertools_access($env{'user.name'},
 7348:                                                           $env{'user.domain'},
 7349:                                                           $item,'reload',$context);
 7350:                     if (($context eq 'requestcourses') ||
 7351:                         ($context eq 'requestauthor')) {
 7352:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 7353:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 7354:                         }
 7355:                     } else {
 7356:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 7357:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 7358:                         }
 7359:                     }
 7360:                     unless ($context eq 'requestauthor') {
 7361:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 7362:                     }
 7363:                     foreach my $type (@{$types},'default','_LC_adv') {
 7364:                         if ($haschgs{$type}) {
 7365:                             my $typetitle = $usertypes->{$type};
 7366:                             if ($type eq 'default') {
 7367:                                 $typetitle = $othertitle;
 7368:                             } elsif ($type eq '_LC_adv') {
 7369:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 7370:                             }
 7371:                             if ($inconf{$type}) {
 7372:                                 if ($context eq 'requestcourses') {
 7373:                                     my $cond;
 7374:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 7375:                                         if ($1 eq '') {
 7376:                                             $cond = &mt('(Automatic processing of any request).');
 7377:                                         } else {
 7378:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 7379:                                         }
 7380:                                     } else { 
 7381:                                         $cond = $conditions{$inconf{$type}};
 7382:                                     }
 7383:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 7384:                                 } elsif ($context eq 'requestauthor') {
 7385:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 7386:                                                              $titles{$inconf{$type}},$typetitle);
 7387: 
 7388:                                 } else {
 7389:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7390:                                 }
 7391:                             } else {
 7392:                                 if ($type eq '_LC_adv') {
 7393:                                     if ($inconf{$type} eq '0') {
 7394:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7395:                                     } else { 
 7396:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7397:                                     }
 7398:                                 } else {
 7399:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7400:                                 }
 7401:                             }
 7402:                         }
 7403:                     }
 7404:                     unless ($context eq 'requestauthor') {
 7405:                         $resulttext .= '</ul></li>';
 7406:                     }
 7407:                 }
 7408:             }
 7409:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7410:                 if (ref($changes{'notify'}) eq 'HASH') {
 7411:                     if ($changes{'notify'}{'approval'}) {
 7412:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7413:                             if ($confhash{'notify'}{'approval'}) {
 7414:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7415:                             } else {
 7416:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7417:                             }
 7418:                         }
 7419:                     }
 7420:                 }
 7421:             }
 7422:             if ($action eq 'requestcourses') {
 7423:                 my @offon = ('off','on');
 7424:                 if ($changes{'uniquecode'}) {
 7425:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7426:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7427:                         $resulttext .= '<li>'.
 7428:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7429:                                        '</li>';
 7430:                     } else {
 7431:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7432:                                        '</li>';
 7433:                     }
 7434:                 }
 7435:                 foreach my $type ('textbooks','templates') {
 7436:                     if (ref($changes{$type}) eq 'HASH') {
 7437:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 7438:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 7439:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 7440:                             my $coursetitle = $coursehash{'description'};
 7441:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 7442:                             $resulttext .= '<li>';
 7443:                             foreach my $item ('subject','title','publisher','author') {
 7444:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7445:                                          ($type eq 'templates'));
 7446:                                 my $name = $item.':';
 7447:                                 $name =~ s/^(\w)/\U$1/;
 7448:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 7449:                             }
 7450:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7451:                             if ($type eq 'textbooks') {
 7452:                                 if ($confhash{$type}{$key}{'image'}) {
 7453:                                     $resulttext .= ' '.&mt('Image: [_1]',
 7454:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 7455:                                                    ' alt="Textbook cover" />').'<br />';
 7456:                                 }
 7457:                             }
 7458:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7459:                         }
 7460:                         $resulttext .= '</ul></li>';
 7461:                     }
 7462:                 }
 7463:                 if (ref($changes{'validation'}) eq 'HASH') {
 7464:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7465:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7466:                         foreach my $item (@{$validationitemsref}) {
 7467:                             if (exists($changes{'validation'}{$item})) {
 7468:                                 if ($item eq 'markup') {
 7469:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7470:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7471:                                 } else {
 7472:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7473:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7474:                                 }
 7475:                             }
 7476:                         }
 7477:                         if (exists($changes{'validation'}{'dc'})) {
 7478:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7479:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7480:                         }
 7481:                     }
 7482:                 }
 7483:             }
 7484:             $resulttext .= '</ul>';
 7485:             if (keys(%newenv)) {
 7486:                 &Apache::lonnet::appenv(\%newenv);
 7487:             }
 7488:         } else {
 7489:             if ($context eq 'requestcourses') {
 7490:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7491:             } elsif ($context eq 'requestauthor') {
 7492:                 $resulttext = &mt('No changes made to rights to request author space.');
 7493:             } else {
 7494:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7495:             }
 7496:         }
 7497:     } else {
 7498:         $resulttext = '<span class="LC_error">'.
 7499: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7500:     }
 7501:     if ($errors) {
 7502:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7503:                        '<ul>'.$errors.'</ul></p>';
 7504:     }
 7505:     return $resulttext;
 7506: }
 7507: 
 7508: sub process_textbook_image {
 7509:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 7510:     my $filename = $env{'form.'.$caller.'.filename'};
 7511:     my ($error,$url);
 7512:     my ($width,$height) = (50,50);
 7513:     if ($configuserok eq 'ok') {
 7514:         if ($switchserver) {
 7515:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7516:                          $switchserver);
 7517:         } elsif ($author_ok eq 'ok') {
 7518:             my ($result,$imageurl) =
 7519:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7520:                              "$type/$dom/$cnum/cover",$width,$height);
 7521:             if ($result eq 'ok') {
 7522:                 $url = $imageurl;
 7523:             } else {
 7524:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7525:             }
 7526:         } else {
 7527:             $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);
 7528:         }
 7529:     } else {
 7530:         $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);
 7531:     }
 7532:     return ($url,$error);
 7533: }
 7534: 
 7535: sub modify_autoenroll {
 7536:     my ($dom,$lastactref,%domconfig) = @_;
 7537:     my ($resulttext,%changes);
 7538:     my %currautoenroll;
 7539:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7540:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7541:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7542:         }
 7543:     }
 7544:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7545:     my %title = ( run => 'Auto-enrollment active',
 7546:                   sender => 'Sender for notification messages',
 7547:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7548:     my @offon = ('off','on');
 7549:     my $sender_uname = $env{'form.sender_uname'};
 7550:     my $sender_domain = $env{'form.sender_domain'};
 7551:     if ($sender_domain eq '') {
 7552:         $sender_uname = '';
 7553:     } elsif ($sender_uname eq '') {
 7554:         $sender_domain = '';
 7555:     }
 7556:     my $coowners = $env{'form.autoassign_coowners'};
 7557:     my %autoenrollhash =  (
 7558:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7559:                                        'sender_uname' => $sender_uname,
 7560:                                        'sender_domain' => $sender_domain,
 7561:                                        'co-owners' => $coowners,
 7562:                                 }
 7563:                      );
 7564:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7565:                                              $dom);
 7566:     if ($putresult eq 'ok') {
 7567:         if (exists($currautoenroll{'run'})) {
 7568:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7569:                  $changes{'run'} = 1;
 7570:              }
 7571:         } elsif ($autorun) {
 7572:             if ($env{'form.autoenroll_run'} ne '1') {
 7573:                  $changes{'run'} = 1;
 7574:             }
 7575:         }
 7576:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7577:             $changes{'sender'} = 1;
 7578:         }
 7579:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7580:             $changes{'sender'} = 1;
 7581:         }
 7582:         if ($currautoenroll{'co-owners'} ne '') {
 7583:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7584:                 $changes{'coowners'} = 1;
 7585:             }
 7586:         } elsif ($coowners) {
 7587:             $changes{'coowners'} = 1;
 7588:         }      
 7589:         if (keys(%changes) > 0) {
 7590:             $resulttext = &mt('Changes made:').'<ul>';
 7591:             if ($changes{'run'}) {
 7592:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7593:             }
 7594:             if ($changes{'sender'}) {
 7595:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7596:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7597:                 } else {
 7598:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7599:                 }
 7600:             }
 7601:             if ($changes{'coowners'}) {
 7602:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7603:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7604:                 if (ref($lastactref) eq 'HASH') {
 7605:                     $lastactref->{'domainconfig'} = 1;
 7606:                 }
 7607:             }
 7608:             $resulttext .= '</ul>';
 7609:         } else {
 7610:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7611:         }
 7612:     } else {
 7613:         $resulttext = '<span class="LC_error">'.
 7614: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7615:     }
 7616:     return $resulttext;
 7617: }
 7618: 
 7619: sub modify_autoupdate {
 7620:     my ($dom,%domconfig) = @_;
 7621:     my ($resulttext,%currautoupdate,%fields,%changes);
 7622:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7623:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7624:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7625:         }
 7626:     }
 7627:     my @offon = ('off','on');
 7628:     my %title = &Apache::lonlocal::texthash (
 7629:                    run => 'Auto-update:',
 7630:                    classlists => 'Updates to user information in classlists?'
 7631:                 );
 7632:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7633:     my %fieldtitles = &Apache::lonlocal::texthash (
 7634:                         id => 'Student/Employee ID',
 7635:                         permanentemail => 'E-mail address',
 7636:                         lastname => 'Last Name',
 7637:                         firstname => 'First Name',
 7638:                         middlename => 'Middle Name',
 7639:                         generation => 'Generation',
 7640:                       );
 7641:     $othertitle = &mt('All users');
 7642:     if (keys(%{$usertypes}) >  0) {
 7643:         $othertitle = &mt('Other users');
 7644:     }
 7645:     foreach my $key (keys(%env)) {
 7646:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7647:             my ($usertype,$item) = ($1,$2);
 7648:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7649:                 if ($usertype eq 'default') {   
 7650:                     push(@{$fields{$1}},$2);
 7651:                 } elsif (ref($types) eq 'ARRAY') {
 7652:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7653:                         push(@{$fields{$1}},$2);
 7654:                     }
 7655:                 }
 7656:             }
 7657:         }
 7658:     }
 7659:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 7660:     @lockablenames = sort(@lockablenames);
 7661:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 7662:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7663:         if (@changed) {
 7664:             $changes{'lockablenames'} = 1;
 7665:         }
 7666:     } else {
 7667:         if (@lockablenames) {
 7668:             $changes{'lockablenames'} = 1;
 7669:         }
 7670:     }
 7671:     my %updatehash = (
 7672:                       autoupdate => { run => $env{'form.autoupdate_run'},
 7673:                                       classlists => $env{'form.classlists'},
 7674:                                       fields => {%fields},
 7675:                                       lockablenames => \@lockablenames,
 7676:                                     }
 7677:                      );
 7678:     foreach my $key (keys(%currautoupdate)) {
 7679:         if (($key eq 'run') || ($key eq 'classlists')) {
 7680:             if (exists($updatehash{autoupdate}{$key})) {
 7681:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 7682:                     $changes{$key} = 1;
 7683:                 }
 7684:             }
 7685:         } elsif ($key eq 'fields') {
 7686:             if (ref($currautoupdate{$key}) eq 'HASH') {
 7687:                 foreach my $item (@{$types},'default') {
 7688:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 7689:                         my $change = 0;
 7690:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 7691:                             if (!exists($fields{$item})) {
 7692:                                 $change = 1;
 7693:                                 last;
 7694:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 7695:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 7696:                                     $change = 1;
 7697:                                     last;
 7698:                                 }
 7699:                             }
 7700:                         }
 7701:                         if ($change) {
 7702:                             push(@{$changes{$key}},$item);
 7703:                         }
 7704:                     } 
 7705:                 }
 7706:             }
 7707:         } elsif ($key eq 'lockablenames') {
 7708:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 7709:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7710:                 if (@changed) {
 7711:                     $changes{'lockablenames'} = 1;
 7712:                 }
 7713:             } else {
 7714:                 if (@lockablenames) {
 7715:                     $changes{'lockablenames'} = 1;
 7716:                 }
 7717:             }
 7718:         }
 7719:     }
 7720:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 7721:         if (@lockablenames) {
 7722:             $changes{'lockablenames'} = 1;
 7723:         }
 7724:     }
 7725:     foreach my $item (@{$types},'default') {
 7726:         if (defined($fields{$item})) {
 7727:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 7728:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 7729:                     my $change = 0;
 7730:                     if (ref($fields{$item}) eq 'ARRAY') {
 7731:                         foreach my $type (@{$fields{$item}}) {
 7732:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 7733:                                 $change = 1;
 7734:                                 last;
 7735:                             }
 7736:                         }
 7737:                     }
 7738:                     if ($change) {
 7739:                         push(@{$changes{'fields'}},$item);
 7740:                     }
 7741:                 } else {
 7742:                     push(@{$changes{'fields'}},$item);
 7743:                 }
 7744:             } else {
 7745:                 push(@{$changes{'fields'}},$item);
 7746:             }
 7747:         }
 7748:     }
 7749:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7750:                                              $dom);
 7751:     if ($putresult eq 'ok') {
 7752:         if (keys(%changes) > 0) {
 7753:             $resulttext = &mt('Changes made:').'<ul>';
 7754:             foreach my $key (sort(keys(%changes))) {
 7755:                 if ($key eq 'lockablenames') {
 7756:                     $resulttext .= '<li>';
 7757:                     if (@lockablenames) {
 7758:                         $usertypes->{'default'} = $othertitle;
 7759:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7760:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7761:                     } else {
 7762:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7763:                     }
 7764:                     $resulttext .= '</li>';
 7765:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7766:                     foreach my $item (@{$changes{$key}}) {
 7767:                         my @newvalues;
 7768:                         foreach my $type (@{$fields{$item}}) {
 7769:                             push(@newvalues,$fieldtitles{$type});
 7770:                         }
 7771:                         my $newvaluestr;
 7772:                         if (@newvalues > 0) {
 7773:                             $newvaluestr = join(', ',@newvalues);
 7774:                         } else {
 7775:                             $newvaluestr = &mt('none');
 7776:                         }
 7777:                         if ($item eq 'default') {
 7778:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7779:                         } else {
 7780:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7781:                         }
 7782:                     }
 7783:                 } else {
 7784:                     my $newvalue;
 7785:                     if ($key eq 'run') {
 7786:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7787:                     } else {
 7788:                         $newvalue = $offon[$env{'form.'.$key}];
 7789:                     }
 7790:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7791:                 }
 7792:             }
 7793:             $resulttext .= '</ul>';
 7794:         } else {
 7795:             $resulttext = &mt('No changes made to autoupdates');
 7796:         }
 7797:     } else {
 7798:         $resulttext = '<span class="LC_error">'.
 7799: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7800:     }
 7801:     return $resulttext;
 7802: }
 7803: 
 7804: sub modify_autocreate {
 7805:     my ($dom,%domconfig) = @_;
 7806:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7807:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7808:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7809:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7810:         }
 7811:     }
 7812:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7813:                  req => 'Auto-creation of validated requests for official courses',
 7814:                  xmldc => 'Identity of course creator of courses from XML files',
 7815:                );
 7816:     my @types = ('xml','req');
 7817:     foreach my $item (@types) {
 7818:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7819:         $newvals{$item} =~ s/\D//g;
 7820:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7821:     }
 7822:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7823:     my %domcoords = &get_active_dcs($dom);
 7824:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7825:         $newvals{'xmldc'} = '';
 7826:     } 
 7827:     %autocreatehash =  (
 7828:                         autocreate => { xml => $newvals{'xml'},
 7829:                                         req => $newvals{'req'},
 7830:                                       }
 7831:                        );
 7832:     if ($newvals{'xmldc'} ne '') {
 7833:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7834:     }
 7835:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7836:                                              $dom);
 7837:     if ($putresult eq 'ok') {
 7838:         my @items = @types;
 7839:         if ($newvals{'xml'}) {
 7840:             push(@items,'xmldc');
 7841:         }
 7842:         foreach my $item (@items) {
 7843:             if (exists($currautocreate{$item})) {
 7844:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7845:                     $changes{$item} = 1;
 7846:                 }
 7847:             } elsif ($newvals{$item}) {
 7848:                 $changes{$item} = 1;
 7849:             }
 7850:         }
 7851:         if (keys(%changes) > 0) {
 7852:             my @offon = ('off','on'); 
 7853:             $resulttext = &mt('Changes made:').'<ul>';
 7854:             foreach my $item (@types) {
 7855:                 if ($changes{$item}) {
 7856:                     my $newtxt = $offon[$newvals{$item}];
 7857:                     $resulttext .= '<li>'.
 7858:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7859:                                        '<b>','</b>').
 7860:                                    '</li>';
 7861:                 }
 7862:             }
 7863:             if ($changes{'xmldc'}) {
 7864:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7865:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7866:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7867:             }
 7868:             $resulttext .= '</ul>';
 7869:         } else {
 7870:             $resulttext = &mt('No changes made to auto-creation settings');
 7871:         }
 7872:     } else {
 7873:         $resulttext = '<span class="LC_error">'.
 7874:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7875:     }
 7876:     return $resulttext;
 7877: }
 7878: 
 7879: sub modify_directorysrch {
 7880:     my ($dom,%domconfig) = @_;
 7881:     my ($resulttext,%changes);
 7882:     my %currdirsrch;
 7883:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7884:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7885:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7886:         }
 7887:     }
 7888:     my %title = ( available => 'Directory search available',
 7889:                   localonly => 'Other domains can search',
 7890:                   searchby => 'Search types',
 7891:                   searchtypes => 'Search latitude');
 7892:     my @offon = ('off','on');
 7893:     my @otherdoms = ('Yes','No');
 7894: 
 7895:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7896:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7897:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7898: 
 7899:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7900:     if (keys(%{$usertypes}) == 0) {
 7901:         @cansearch = ('default');
 7902:     } else {
 7903:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7904:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7905:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7906:                     push(@{$changes{'cansearch'}},$type);
 7907:                 }
 7908:             }
 7909:             foreach my $type (@cansearch) {
 7910:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7911:                     push(@{$changes{'cansearch'}},$type);
 7912:                 }
 7913:             }
 7914:         } else {
 7915:             push(@{$changes{'cansearch'}},@cansearch);
 7916:         }
 7917:     }
 7918: 
 7919:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7920:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7921:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7922:                 push(@{$changes{'searchby'}},$by);
 7923:             }
 7924:         }
 7925:         foreach my $by (@searchby) {
 7926:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7927:                 push(@{$changes{'searchby'}},$by);
 7928:             }
 7929:         }
 7930:     } else {
 7931:         push(@{$changes{'searchby'}},@searchby);
 7932:     }
 7933: 
 7934:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7935:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7936:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7937:                 push(@{$changes{'searchtypes'}},$type);
 7938:             }
 7939:         }
 7940:         foreach my $type (@searchtypes) {
 7941:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7942:                 push(@{$changes{'searchtypes'}},$type);
 7943:             }
 7944:         }
 7945:     } else {
 7946:         if (exists($currdirsrch{'searchtypes'})) {
 7947:             foreach my $type (@searchtypes) {  
 7948:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7949:                     push(@{$changes{'searchtypes'}},$type);
 7950:                 }
 7951:             }
 7952:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7953:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7954:             }   
 7955:         } else {
 7956:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7957:         }
 7958:     }
 7959: 
 7960:     my %dirsrch_hash =  (
 7961:             directorysrch => { available => $env{'form.dirsrch_available'},
 7962:                                cansearch => \@cansearch,
 7963:                                localonly => $env{'form.dirsrch_localonly'},
 7964:                                searchby => \@searchby,
 7965:                                searchtypes => \@searchtypes,
 7966:                              }
 7967:             );
 7968:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7969:                                              $dom);
 7970:     if ($putresult eq 'ok') {
 7971:         if (exists($currdirsrch{'available'})) {
 7972:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7973:                  $changes{'available'} = 1;
 7974:              }
 7975:         } else {
 7976:             if ($env{'form.dirsrch_available'} eq '1') {
 7977:                 $changes{'available'} = 1;
 7978:             }
 7979:         }
 7980:         if (exists($currdirsrch{'localonly'})) {
 7981:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7982:                  $changes{'localonly'} = 1;
 7983:              }
 7984:         } else {
 7985:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7986:                 $changes{'localonly'} = 1;
 7987:             }
 7988:         }
 7989:         if (keys(%changes) > 0) {
 7990:             $resulttext = &mt('Changes made:').'<ul>';
 7991:             if ($changes{'available'}) {
 7992:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7993:             }
 7994:             if ($changes{'localonly'}) {
 7995:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7996:             }
 7997: 
 7998:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7999:                 my $chgtext;
 8000:                 if (ref($usertypes) eq 'HASH') {
 8001:                     if (keys(%{$usertypes}) > 0) {
 8002:                         foreach my $type (@{$types}) {
 8003:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 8004:                                 $chgtext .= $usertypes->{$type}.'; ';
 8005:                             }
 8006:                         }
 8007:                         if (grep(/^default$/,@cansearch)) {
 8008:                             $chgtext .= $othertitle;
 8009:                         } else {
 8010:                             $chgtext =~ s/\; $//;
 8011:                         }
 8012:                         $resulttext .=
 8013:                             '<li>'.
 8014:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 8015:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 8016:                             '</li>';
 8017:                     }
 8018:                 }
 8019:             }
 8020:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 8021:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 8022:                 my $chgtext;
 8023:                 foreach my $type (@{$titleorder}) {
 8024:                     if (grep(/^\Q$type\E$/,@searchby)) {
 8025:                         if (defined($searchtitles->{$type})) {
 8026:                             $chgtext .= $searchtitles->{$type}.'; ';
 8027:                         }
 8028:                     }
 8029:                 }
 8030:                 $chgtext =~ s/\; $//;
 8031:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 8032:             }
 8033:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 8034:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 8035:                 my $chgtext;
 8036:                 foreach my $type (@{$srchtypeorder}) {
 8037:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 8038:                         if (defined($srchtypes_desc->{$type})) {
 8039:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 8040:                         }
 8041:                     }
 8042:                 }
 8043:                 $chgtext =~ s/\; $//;
 8044:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 8045:             }
 8046:             $resulttext .= '</ul>';
 8047:         } else {
 8048:             $resulttext = &mt('No changes made to institution directory search settings');
 8049:         }
 8050:     } else {
 8051:         $resulttext = '<span class="LC_error">'.
 8052:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 8053:     }
 8054:     return $resulttext;
 8055: }
 8056: 
 8057: sub modify_contacts {
 8058:     my ($dom,$lastactref,%domconfig) = @_;
 8059:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 8060:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 8061:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 8062:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 8063:         }
 8064:     }
 8065:     my (%others,%to,%bcc);
 8066:     my @contacts = ('supportemail','adminemail');
 8067:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 8068:                     'requestsmail','updatesmail','idconflictsmail');
 8069:     my @toggles = ('reporterrors','reportupdates');
 8070:     foreach my $type (@mailings) {
 8071:         @{$newsetting{$type}} = 
 8072:             &Apache::loncommon::get_env_multiple('form.'.$type);
 8073:         foreach my $item (@contacts) {
 8074:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 8075:                 $contacts_hash{contacts}{$type}{$item} = 1;
 8076:             } else {
 8077:                 $contacts_hash{contacts}{$type}{$item} = 0;
 8078:             }
 8079:         }  
 8080:         $others{$type} = $env{'form.'.$type.'_others'};
 8081:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 8082:         if ($type eq 'helpdeskmail') {
 8083:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 8084:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 8085:         }
 8086:     }
 8087:     foreach my $item (@contacts) {
 8088:         $to{$item} = $env{'form.'.$item};
 8089:         $contacts_hash{'contacts'}{$item} = $to{$item};
 8090:     }
 8091:     foreach my $item (@toggles) {
 8092:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 8093:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 8094:         }
 8095:     }
 8096:     if (keys(%currsetting) > 0) {
 8097:         foreach my $item (@contacts) {
 8098:             if ($to{$item} ne $currsetting{$item}) {
 8099:                 $changes{$item} = 1;
 8100:             }
 8101:         }
 8102:         foreach my $type (@mailings) {
 8103:             foreach my $item (@contacts) {
 8104:                 if (ref($currsetting{$type}) eq 'HASH') {
 8105:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 8106:                         push(@{$changes{$type}},$item);
 8107:                     }
 8108:                 } else {
 8109:                     push(@{$changes{$type}},@{$newsetting{$type}});
 8110:                 }
 8111:             }
 8112:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 8113:                 push(@{$changes{$type}},'others');
 8114:             }
 8115:             if ($type eq 'helpdeskmail') {   
 8116:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 8117:                     push(@{$changes{$type}},'bcc'); 
 8118:                 }
 8119:             }
 8120:         }
 8121:     } else {
 8122:         my %default;
 8123:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 8124:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 8125:         $default{'errormail'} = 'adminemail';
 8126:         $default{'packagesmail'} = 'adminemail';
 8127:         $default{'helpdeskmail'} = 'supportemail';
 8128:         $default{'lonstatusmail'} = 'adminemail';
 8129:         $default{'requestsmail'} = 'adminemail';
 8130:         $default{'updatesmail'} = 'adminemail';
 8131:         foreach my $item (@contacts) {
 8132:            if ($to{$item} ne $default{$item}) {
 8133:               $changes{$item} = 1;
 8134:            }
 8135:         }
 8136:         foreach my $type (@mailings) {
 8137:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 8138:                
 8139:                 push(@{$changes{$type}},@{$newsetting{$type}});
 8140:             }
 8141:             if ($others{$type} ne '') {
 8142:                 push(@{$changes{$type}},'others');
 8143:             }
 8144:             if ($type eq 'helpdeskmail') {
 8145:                 if ($bcc{$type} ne '') {
 8146:                     push(@{$changes{$type}},'bcc');
 8147:                 }
 8148:             }
 8149:         }
 8150:     }
 8151:     foreach my $item (@toggles) {
 8152:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 8153:             $changes{$item} = 1;
 8154:         } elsif ((!$env{'form.'.$item}) &&
 8155:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 8156:             $changes{$item} = 1;
 8157:         }
 8158:     }
 8159:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 8160:                                              $dom);
 8161:     if ($putresult eq 'ok') {
 8162:         if (keys(%changes) > 0) {
 8163:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8164:             if (ref($lastactref) eq 'HASH') {
 8165:                 $lastactref->{'domainconfig'} = 1;
 8166:             }
 8167:             my ($titles,$short_titles)  = &contact_titles();
 8168:             $resulttext = &mt('Changes made:').'<ul>';
 8169:             foreach my $item (@contacts) {
 8170:                 if ($changes{$item}) {
 8171:                     $resulttext .= '<li>'.$titles->{$item}.
 8172:                                     &mt(' set to: ').
 8173:                                     '<span class="LC_cusr_emph">'.
 8174:                                     $to{$item}.'</span></li>';
 8175:                 }
 8176:             }
 8177:             foreach my $type (@mailings) {
 8178:                 if (ref($changes{$type}) eq 'ARRAY') {
 8179:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 8180:                     my @text;
 8181:                     foreach my $item (@{$newsetting{$type}}) {
 8182:                         push(@text,$short_titles->{$item});
 8183:                     }
 8184:                     if ($others{$type} ne '') {
 8185:                         push(@text,$others{$type});
 8186:                     }
 8187:                     $resulttext .= '<span class="LC_cusr_emph">'.
 8188:                                    join(', ',@text).'</span>';
 8189:                     if ($type eq 'helpdeskmail') {
 8190:                         if ($bcc{$type} ne '') {
 8191:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 8192:                         }
 8193:                     }
 8194:                     $resulttext .= '</li>';
 8195:                 }
 8196:             }
 8197:             my @offon = ('off','on');
 8198:             if ($changes{'reporterrors'}) {
 8199:                 $resulttext .= '<li>'.
 8200:                                &mt('E-mail error reports to [_1] set to "'.
 8201:                                    $offon[$env{'form.reporterrors'}].'".',
 8202:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8203:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 8204:                                '</li>';
 8205:             }
 8206:             if ($changes{'reportupdates'}) {
 8207:                 $resulttext .= '<li>'.
 8208:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 8209:                                     $offon[$env{'form.reportupdates'}].'".',
 8210:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8211:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 8212:                                 '</li>';
 8213:             }
 8214:             $resulttext .= '</ul>';
 8215:         } else {
 8216:             $resulttext = &mt('No changes made to contact information');
 8217:         }
 8218:     } else {
 8219:         $resulttext = '<span class="LC_error">'.
 8220:             &mt('An error occurred: [_1].',$putresult).'</span>';
 8221:     }
 8222:     return $resulttext;
 8223: }
 8224: 
 8225: sub modify_usercreation {
 8226:     my ($dom,%domconfig) = @_;
 8227:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 8228:     my $warningmsg;
 8229:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8230:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8231:             if ($key eq 'cancreate') {
 8232:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8233:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8234:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8235:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 8236:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8237:                         } else {
 8238:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8239:                         }
 8240:                     }
 8241:                 }
 8242:             } elsif ($key eq 'email_rule') {
 8243:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8244:             } else {
 8245:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8246:             }
 8247:         }
 8248:     }
 8249:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 8250:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 8251:     my @contexts = ('author','course','requestcrs');
 8252:     foreach my $item(@contexts) {
 8253:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 8254:     }
 8255:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8256:         foreach my $item (@contexts) {
 8257:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 8258:                 push(@{$changes{'cancreate'}},$item);
 8259:             }
 8260:         }
 8261:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 8262:         foreach my $item (@contexts) {
 8263:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 8264:                 if ($cancreate{$item} ne 'any') {
 8265:                     push(@{$changes{'cancreate'}},$item);
 8266:                 }
 8267:             } else {
 8268:                 if ($cancreate{$item} ne 'none') {
 8269:                     push(@{$changes{'cancreate'}},$item);
 8270:                 }
 8271:             }
 8272:         }
 8273:     } else {
 8274:         foreach my $item (@contexts)  {
 8275:             push(@{$changes{'cancreate'}},$item);
 8276:         }
 8277:     }
 8278: 
 8279:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 8280:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 8281:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 8282:                 push(@{$changes{'username_rule'}},$type);
 8283:             }
 8284:         }
 8285:         foreach my $type (@username_rule) {
 8286:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 8287:                 push(@{$changes{'username_rule'}},$type);
 8288:             }
 8289:         }
 8290:     } else {
 8291:         push(@{$changes{'username_rule'}},@username_rule);
 8292:     }
 8293: 
 8294:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 8295:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 8296:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 8297:                 push(@{$changes{'id_rule'}},$type);
 8298:             }
 8299:         }
 8300:         foreach my $type (@id_rule) {
 8301:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 8302:                 push(@{$changes{'id_rule'}},$type);
 8303:             }
 8304:         }
 8305:     } else {
 8306:         push(@{$changes{'id_rule'}},@id_rule);
 8307:     }
 8308: 
 8309:     my @authen_contexts = ('author','course','domain');
 8310:     my @authtypes = ('int','krb4','krb5','loc');
 8311:     my %authhash;
 8312:     foreach my $item (@authen_contexts) {
 8313:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 8314:         foreach my $auth (@authtypes) {
 8315:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 8316:                 $authhash{$item}{$auth} = 1;
 8317:             } else {
 8318:                 $authhash{$item}{$auth} = 0;
 8319:             }
 8320:         }
 8321:     }
 8322:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 8323:         foreach my $item (@authen_contexts) {
 8324:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 8325:                 foreach my $auth (@authtypes) {
 8326:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 8327:                         push(@{$changes{'authtypes'}},$item);
 8328:                         last;
 8329:                     }
 8330:                 }
 8331:             }
 8332:         }
 8333:     } else {
 8334:         foreach my $item (@authen_contexts) {
 8335:             push(@{$changes{'authtypes'}},$item);
 8336:         }
 8337:     }
 8338: 
 8339:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 8340:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 8341:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 8342:     $save_usercreate{'id_rule'} = \@id_rule;
 8343:     $save_usercreate{'username_rule'} = \@username_rule,
 8344:     $save_usercreate{'authtypes'} = \%authhash;
 8345: 
 8346:     my %usercreation_hash =  (
 8347:         usercreation     => \%save_usercreate,
 8348:     );
 8349: 
 8350:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 8351:                                              $dom);
 8352: 
 8353:     if ($putresult eq 'ok') {
 8354:         if (keys(%changes) > 0) {
 8355:             $resulttext = &mt('Changes made:').'<ul>';
 8356:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8357:                 my %lt = &usercreation_types();
 8358:                 foreach my $type (@{$changes{'cancreate'}}) {
 8359:                     my $chgtext = $lt{$type}.', ';
 8360:                     if ($cancreate{$type} eq 'none') {
 8361:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 8362:                     } elsif ($cancreate{$type} eq 'any') {
 8363:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 8364:                     } elsif ($cancreate{$type} eq 'official') {
 8365:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 8366:                     } elsif ($cancreate{$type} eq 'unofficial') {
 8367:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 8368:                     }
 8369:                     $resulttext .= '<li>'.$chgtext.'</li>';
 8370:                 }
 8371:             }
 8372:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 8373:                 my ($rules,$ruleorder) = 
 8374:                     &Apache::lonnet::inst_userrules($dom,'username');
 8375:                 my $chgtext = '<ul>';
 8376:                 foreach my $type (@username_rule) {
 8377:                     if (ref($rules->{$type}) eq 'HASH') {
 8378:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 8379:                     }
 8380:                 }
 8381:                 $chgtext .= '</ul>';
 8382:                 if (@username_rule > 0) {
 8383:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 8384:                 } else {
 8385:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 8386:                 }
 8387:             }
 8388:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 8389:                 my ($idrules,$idruleorder) = 
 8390:                     &Apache::lonnet::inst_userrules($dom,'id');
 8391:                 my $chgtext = '<ul>';
 8392:                 foreach my $type (@id_rule) {
 8393:                     if (ref($idrules->{$type}) eq 'HASH') {
 8394:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8395:                     }
 8396:                 }
 8397:                 $chgtext .= '</ul>';
 8398:                 if (@id_rule > 0) {
 8399:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8400:                 } else {
 8401:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8402:                 }
 8403:             }
 8404:             my %authname = &authtype_names();
 8405:             my %context_title = &context_names();
 8406:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8407:                 my $chgtext = '<ul>';
 8408:                 foreach my $type (@{$changes{'authtypes'}}) {
 8409:                     my @allowed;
 8410:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8411:                     foreach my $auth (@authtypes) {
 8412:                         if ($authhash{$type}{$auth}) {
 8413:                             push(@allowed,$authname{$auth});
 8414:                         }
 8415:                     }
 8416:                     if (@allowed > 0) {
 8417:                         $chgtext .= join(', ',@allowed).'</li>';
 8418:                     } else {
 8419:                         $chgtext .= &mt('none').'</li>';
 8420:                     }
 8421:                 }
 8422:                 $chgtext .= '</ul>';
 8423:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8424:                 $resulttext .= '</li>';
 8425:             }
 8426:             $resulttext .= '</ul>';
 8427:         } else {
 8428:             $resulttext = &mt('No changes made to user creation settings');
 8429:         }
 8430:     } else {
 8431:         $resulttext = '<span class="LC_error">'.
 8432:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8433:     }
 8434:     if ($warningmsg ne '') {
 8435:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8436:     }
 8437:     return $resulttext;
 8438: }
 8439: 
 8440: sub modify_selfcreation {
 8441:     my ($dom,%domconfig) = @_;
 8442:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8443:     my (%save_usercreate,%save_usermodify);
 8444:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8445:     if (ref($types) eq 'ARRAY') {
 8446:         $usertypes->{'default'} = $othertitle;
 8447:         push(@{$types},'default');
 8448:     }
 8449: #
 8450: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8451: #
 8452:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8453:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8454:             if ($key eq 'cancreate') {
 8455:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8456:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8457:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8458:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8459:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 8460:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 8461:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8462:                         } else {
 8463:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8464:                         }
 8465:                     }
 8466:                 }
 8467:             } elsif ($key eq 'email_rule') {
 8468:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8469:             } else {
 8470:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8471:             }
 8472:         }
 8473:     }
 8474: #
 8475: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8476: #
 8477:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8478:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8479:             if ($key eq 'selfcreate') {
 8480:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8481:             } else {
 8482:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8483:             }
 8484:         }
 8485:     }
 8486: 
 8487:     my @contexts = ('selfcreate');
 8488:     @{$cancreate{'selfcreate'}} = ();
 8489:     %{$cancreate{'emailusername'}} = ();
 8490:     @{$cancreate{'statustocreate'}} = ();
 8491:     %{$cancreate{'selfcreateprocessing'}} = ();
 8492:     %{$cancreate{'shibenv'}} = ();
 8493:     my %selfcreatetypes = (
 8494:                              sso   => 'users authenticated by institutional single sign on',
 8495:                              login => 'users authenticated by institutional log-in',
 8496:                              email => 'users who provide a valid e-mail address for use as username',
 8497:                           );
 8498: #
 8499: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8500: # is permitted.
 8501: #
 8502: 
 8503:     my @statuses;
 8504:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 8505:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 8506:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 8507:         }
 8508:     }
 8509:     push(@statuses,'default');
 8510: 
 8511:     foreach my $item ('login','sso','email') {
 8512:         if ($item eq 'email') {
 8513:             if ($env{'form.cancreate_email'}) {
 8514:                 push(@{$cancreate{'selfcreate'}},'email');
 8515:                 push(@contexts,'selfcreateprocessing');
 8516:                 foreach my $type (@statuses) {
 8517:                     if ($type eq 'default') {
 8518:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 8519:                     } else { 
 8520:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 8521:                     }
 8522:                 }
 8523:             }
 8524:         } else {
 8525:             if ($env{'form.cancreate_'.$item}) {
 8526:                 push(@{$cancreate{'selfcreate'}},$item);
 8527:             }
 8528:         }
 8529:     }
 8530:     my (@email_rule,%userinfo,%savecaptcha);
 8531:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8532: #
 8533: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8534: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8535: #
 8536: 
 8537:     if ($env{'form.cancreate_email'}) {
 8538:         push(@contexts,'emailusername');
 8539:         if (ref($types) eq 'ARRAY') {
 8540:             foreach my $type (@{$types}) {
 8541:                 if (ref($infofields) eq 'ARRAY') {
 8542:                     foreach my $field (@{$infofields}) {
 8543:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8544:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8545:                         }
 8546:                     }
 8547:                 }
 8548:             }
 8549:         }
 8550: #
 8551: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8552: # queued requests for self-creation of account using e-mail address as username
 8553: #
 8554: 
 8555:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8556:         @approvalnotify = sort(@approvalnotify);
 8557:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8558:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8559:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8560:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8561:                     push(@{$changes{'cancreate'}},'notify');
 8562:                 }
 8563:             } else {
 8564:                 if ($cancreate{'notify'}{'approval'}) {
 8565:                     push(@{$changes{'cancreate'}},'notify');
 8566:                 }
 8567:             }
 8568:         } elsif ($cancreate{'notify'}{'approval'}) {
 8569:             push(@{$changes{'cancreate'}},'notify');
 8570:         }
 8571: 
 8572: #
 8573: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8574: #
 8575:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8576:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8577:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8578:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8579:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8580:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8581:                         push(@{$changes{'email_rule'}},$type);
 8582:                     }
 8583:                 }
 8584:             }
 8585:             if (@email_rule > 0) {
 8586:                 foreach my $type (@email_rule) {
 8587:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8588:                         push(@{$changes{'email_rule'}},$type);
 8589:                     }
 8590:                 }
 8591:             }
 8592:         } elsif (@email_rule > 0) {
 8593:             push(@{$changes{'email_rule'}},@email_rule);
 8594:         }
 8595:     }
 8596: #  
 8597: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 8598: # institutional log-in.
 8599: #
 8600:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8601:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8602:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8603:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8604:             $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.').' '.
 8605:                           &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.');
 8606:         }
 8607:     }
 8608:     my @fields = ('lastname','firstname','middlename','generation',
 8609:                   'permanentemail','id');
 8610:     my @shibfields = (@fields,'inststatus');
 8611:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8612: #
 8613: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8614: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8615: # may self-create accounts 
 8616: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8617: # which the user may supply, if institutional data is unavailable.
 8618: #
 8619:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8620:         if (ref($types) eq 'ARRAY') {
 8621:             if (@{$types} > 1) {
 8622:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8623:                 push(@contexts,'statustocreate');
 8624:             } else {
 8625:                 undef($cancreate{'statustocreate'});
 8626:             } 
 8627:             foreach my $type (@{$types}) {
 8628:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8629:                 foreach my $field (@fields) {
 8630:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8631:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8632:                     } else {
 8633:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8634:                     }
 8635:                 }
 8636:             }
 8637:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8638:                 foreach my $type (@{$types}) {
 8639:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8640:                         foreach my $field (@fields) {
 8641:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8642:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8643:                                 push(@{$changes{'selfcreate'}},$type);
 8644:                                 last;
 8645:                             }
 8646:                         }
 8647:                     }
 8648:                 }
 8649:             } else {
 8650:                 foreach my $type (@{$types}) {
 8651:                     push(@{$changes{'selfcreate'}},$type);
 8652:                 }
 8653:             }
 8654:         }
 8655:         foreach my $field (@shibfields) {
 8656:             if ($env{'form.shibenv_'.$field} ne '') {
 8657:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 8658:             }
 8659:         }
 8660:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8661:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 8662:                 foreach my $field (@shibfields) {
 8663:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 8664:                         push(@{$changes{'cancreate'}},'shibenv');
 8665:                     }
 8666:                 }
 8667:             } else {
 8668:                 foreach my $field (@shibfields) {
 8669:                     if ($env{'form.shibenv_'.$field}) {
 8670:                         push(@{$changes{'cancreate'}},'shibenv');
 8671:                         last;
 8672:                     }
 8673:                 }
 8674:             }
 8675:         }
 8676:     }
 8677:     foreach my $item (@contexts) {
 8678:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 8679:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 8680:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 8681:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 8682:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8683:                             push(@{$changes{'cancreate'}},$item);
 8684:                         }
 8685:                     }
 8686:                 }
 8687:             }
 8688:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8689:                 foreach my $type (@{$cancreate{$item}}) {
 8690:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 8691:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8692:                             push(@{$changes{'cancreate'}},$item);
 8693:                         }
 8694:                     }
 8695:                 }
 8696:             }
 8697:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 8698:             if (ref($cancreate{$item}) eq 'HASH') {
 8699:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 8700:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 8701:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 8702:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 8703:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8704:                                     push(@{$changes{'cancreate'}},$item);
 8705:                                 }
 8706:                             }
 8707:                         }
 8708:                     } elsif ($item eq 'selfcreateprocessing') {
 8709:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 8710:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8711:                                 push(@{$changes{'cancreate'}},$item);
 8712:                             }
 8713:                         }
 8714:                     } else {
 8715:                         if (!$cancreate{$item}{$curr}) {
 8716:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8717:                                 push(@{$changes{'cancreate'}},$item);
 8718:                             }
 8719:                         }
 8720:                     }
 8721:                 }
 8722:                 foreach my $field (keys(%{$cancreate{$item}})) {
 8723:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 8724:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 8725:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 8726:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 8727:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8728:                                         push(@{$changes{'cancreate'}},$item);
 8729:                                     }
 8730:                                 }
 8731:                             } else {
 8732:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8733:                                     push(@{$changes{'cancreate'}},$item);
 8734:                                 }
 8735:                             }
 8736:                         }
 8737:                     } elsif ($item eq 'selfcreateprocessing') {
 8738:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 8739:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8740:                                 push(@{$changes{'cancreate'}},$item);
 8741:                             }
 8742:                         }
 8743:                     } else {
 8744:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 8745:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8746:                                 push(@{$changes{'cancreate'}},$item);
 8747:                             }
 8748:                         }
 8749:                     }
 8750:                 }
 8751:             }
 8752:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 8753:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8754:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 8755:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8756:                         push(@{$changes{'cancreate'}},$item);
 8757:                     }
 8758:                 }
 8759:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 8760:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 8761:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8762:                         push(@{$changes{'cancreate'}},$item);
 8763:                     }
 8764:                 }
 8765:             }
 8766:         } elsif ($item eq 'emailusername') {
 8767:             if (ref($cancreate{$item}) eq 'HASH') {
 8768:                 foreach my $type (keys(%{$cancreate{$item}})) {
 8769:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 8770:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 8771:                             if ($cancreate{$item}{$type}{$field}) {
 8772:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8773:                                     push(@{$changes{'cancreate'}},$item);
 8774:                                 }
 8775:                                 last;
 8776:                             }
 8777:                         }
 8778:                     }
 8779:                 }
 8780:             }
 8781:         }
 8782:     }
 8783: #
 8784: # Populate %save_usercreate hash with updates to self-creation configuration.
 8785: #
 8786:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 8787:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 8788:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 8789:     if (ref($cancreate{'notify'}) eq 'HASH') {
 8790:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 8791:     }
 8792:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
 8793:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
 8794:     }
 8795:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8796:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 8797:     }
 8798:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
 8799:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
 8800:     }
 8801:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 8802:     $save_usercreate{'emailrule'} = \@email_rule;
 8803: 
 8804:     my %userconfig_hash = (
 8805:             usercreation     => \%save_usercreate,
 8806:             usermodification => \%save_usermodify,
 8807:     );
 8808:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8809:                                              $dom);
 8810: #
 8811: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8812: #
 8813:     if ($putresult eq 'ok') {
 8814:         if (keys(%changes) > 0) {
 8815:             $resulttext = &mt('Changes made:').'<ul>';
 8816:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8817:                 my %lt = &selfcreation_types();
 8818:                 foreach my $type (@{$changes{'cancreate'}}) {
 8819:                     my $chgtext;
 8820:                     if ($type eq 'selfcreate') {
 8821:                         if (@{$cancreate{$type}} == 0) {
 8822:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 8823:                         } else {
 8824:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 8825:                                         '<ul>';
 8826:                             foreach my $case (@{$cancreate{$type}}) {
 8827:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 8828:                             }
 8829:                             $chgtext .= '</ul>';
 8830:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 8831:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 8832:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8833:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 8834:                                             $chgtext .= '<br />'.
 8835:                                                         '<span class="LC_warning">'.
 8836:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8837:                                                         '</span>';
 8838:                                         }
 8839:                                     }
 8840:                                 }
 8841:                             }
 8842:                         }
 8843:                     } elsif ($type eq 'shibenv') {
 8844:                         if (keys(%{$cancreate{$type}}) == 0) {
 8845:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
 8846:                         } else {
 8847:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
 8848:                                         '<ul>';
 8849:                             foreach my $field (@shibfields) {
 8850:                                 next if ($cancreate{$type}{$field} eq '');
 8851:                                 if ($field eq 'inststatus') {
 8852:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
 8853:                                 } else {
 8854:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
 8855:                                 }
 8856:                             }
 8857:                             $chgtext .= '</ul>';
 8858:                         }  
 8859:                     } elsif ($type eq 'statustocreate') {
 8860:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 8861:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 8862:                             if (@{$cancreate{'selfcreate'}} > 0) {
 8863:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 8864:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 8865:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 8866:                                         $chgtext .= '<br />'.
 8867:                                                     '<span class="LC_warning">'.
 8868:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8869:                                                     '</span>';
 8870:                                     }
 8871:                                 } elsif (ref($usertypes) eq 'HASH') {
 8872:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8873:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 8874:                                     } else {
 8875:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 8876:                                     }
 8877:                                     $chgtext .= '<ul>';
 8878:                                     foreach my $case (@{$cancreate{$type}}) {
 8879:                                         if ($case eq 'default') {
 8880:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 8881:                                         } else {
 8882:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 8883:                                         }
 8884:                                     }
 8885:                                     $chgtext .= '</ul>';
 8886:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8887:                                         $chgtext .= '<br /><span class="LC_warning">'.
 8888:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 8889:                                                     '</span>';
 8890:                                     }
 8891:                                 }
 8892:                             } else {
 8893:                                 if (@{$cancreate{$type}} == 0) {
 8894:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8895:                                 } else {
 8896:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8897:                                 }
 8898:                             }
 8899:                         }
 8900:                     } elsif ($type eq 'selfcreateprocessing') {
 8901:                         my %choices = &Apache::lonlocal::texthash (
 8902:                                                                     automatic => 'Automatic approval',
 8903:                                                                     approval  => 'Queued for approval',
 8904:                                                                   );
 8905:                         if (@statuses > 1) {
 8906:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
 8907:                                         '<ul>';
 8908:                            foreach my $type (@statuses) {
 8909:                                if ($type eq 'default') {
 8910:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8911:                                } else {
 8912:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8913:                                }
 8914:                            }
 8915:                            $chgtext .= '</ul>';
 8916:                         } else {
 8917:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
 8918:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
 8919:                         }
 8920:                     } elsif ($type eq 'captcha') {
 8921:                         if ($savecaptcha{$type} eq 'notused') {
 8922:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8923:                         } else {
 8924:                             my %captchas = &captcha_phrases();
 8925:                             if ($captchas{$savecaptcha{$type}}) {
 8926:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8927:                             } else {
 8928:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8929:                             }
 8930:                         }
 8931:                     } elsif ($type eq 'recaptchakeys') {
 8932:                         my ($privkey,$pubkey);
 8933:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8934:                             $pubkey = $savecaptcha{$type}{'public'};
 8935:                             $privkey = $savecaptcha{$type}{'private'};
 8936:                         }
 8937:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8938:                         if (!$pubkey) {
 8939:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8940:                         } else {
 8941:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8942:                         }
 8943:                         if (!$privkey) {
 8944:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8945:                         } else {
 8946:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8947:                         }
 8948:                         $chgtext .= '</ul>';
 8949:                     } elsif ($type eq 'emailusername') {
 8950:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8951:                             if (ref($types) eq 'ARRAY') {
 8952:                                 foreach my $type (@{$types}) {
 8953:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 8954:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 8955:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
 8956:                                                     '<ul>';
 8957:                                             foreach my $field (@{$infofields}) {
 8958:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 8959:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8960:                                                 }
 8961:                                             }
 8962:                                             $chgtext .= '</ul>';
 8963:                                         } else {
 8964:                                             $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 />';
 8965:                                         }
 8966:                                     } else {
 8967:                                         $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 />';
 8968:                                     }
 8969:                                 }
 8970:                             }
 8971:                         }
 8972:                     } elsif ($type eq 'notify') {
 8973:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8974:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8975:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8976:                                 if ($cancreate{'notify'}{'approval'}) {
 8977:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8978:                                 }
 8979:                             }
 8980:                         }
 8981:                     }
 8982:                     if ($chgtext) {
 8983:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8984:                     }
 8985:                 }
 8986:             }
 8987:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8988:                 my ($emailrules,$emailruleorder) =
 8989:                     &Apache::lonnet::inst_userrules($dom,'email');
 8990:                 my $chgtext = '<ul>';
 8991:                 foreach my $type (@email_rule) {
 8992:                     if (ref($emailrules->{$type}) eq 'HASH') {
 8993:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 8994:                     }
 8995:                 }
 8996:                 $chgtext .= '</ul>';
 8997:                 if (@email_rule > 0) {
 8998:                     $resulttext .= '<li>'.
 8999:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 9000:                                        $chgtext.
 9001:                                    '</li>';
 9002:                 } else {
 9003:                     $resulttext .= '<li>'.
 9004:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 9005:                                    '</li>';
 9006:                 }
 9007:             }
 9008:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 9009:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 9010:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9011:                 foreach my $type (@{$changes{'selfcreate'}}) {
 9012:                     my $typename = $type;
 9013:                     if (ref($usertypes) eq 'HASH') {
 9014:                         if ($usertypes->{$type} ne '') {
 9015:                             $typename = $usertypes->{$type};
 9016:                         }
 9017:                     }
 9018:                     my @modifiable;
 9019:                     $resulttext .= '<li>'.
 9020:                                     &mt('Self-creation of account by users with status: [_1]',
 9021:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 9022:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 9023:                     foreach my $field (@fields) {
 9024:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 9025:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 9026:                         }
 9027:                     }
 9028:                     if (@modifiable > 0) {
 9029:                         $resulttext .= join(', ',@modifiable);
 9030:                     } else {
 9031:                         $resulttext .= &mt('none');
 9032:                     }
 9033:                     $resulttext .= '</li>';
 9034:                 }
 9035:                 $resulttext .= '</ul></li>';
 9036:             }
 9037:             $resulttext .= '</ul>';
 9038:         } else {
 9039:             $resulttext = &mt('No changes made to self-creation settings');
 9040:         }
 9041:     } else {
 9042:         $resulttext = '<span class="LC_error">'.
 9043:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9044:     }
 9045:     if ($warningmsg ne '') {
 9046:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9047:     }
 9048:     return $resulttext;
 9049: }
 9050: 
 9051: sub process_captcha {
 9052:     my ($container,$changes,$newsettings,$current) = @_;
 9053:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 9054:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 9055:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 9056:         $newsettings->{'captcha'} = 'original';
 9057:     }
 9058:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 9059:         if ($container eq 'cancreate') {
 9060:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9061:                 push(@{$changes->{'cancreate'}},'captcha');
 9062:             } elsif (!defined($changes->{'cancreate'})) {
 9063:                 $changes->{'cancreate'} = ['captcha'];
 9064:             }
 9065:         } else {
 9066:             $changes->{'captcha'} = 1;
 9067:         }
 9068:     }
 9069:     my ($newpub,$newpriv,$currpub,$currpriv);
 9070:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 9071:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 9072:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 9073:         $newpub =~ s/\W//g;
 9074:         $newpriv =~ s/\W//g;
 9075:         $newsettings->{'recaptchakeys'} = {
 9076:                                              public  => $newpub,
 9077:                                              private => $newpriv,
 9078:                                           };
 9079:     }
 9080:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 9081:         $currpub = $current->{'recaptchakeys'}{'public'};
 9082:         $currpriv = $current->{'recaptchakeys'}{'private'};
 9083:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 9084:             $newsettings->{'recaptchakeys'} = {
 9085:                                                  public  => '',
 9086:                                                  private => '',
 9087:                                               }
 9088:         }
 9089:     }
 9090:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 9091:         if ($container eq 'cancreate') {
 9092:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9093:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 9094:             } elsif (!defined($changes->{'cancreate'})) {
 9095:                 $changes->{'cancreate'} = ['recaptchakeys'];
 9096:             }
 9097:         } else {
 9098:             $changes->{'recaptchakeys'} = 1;
 9099:         }
 9100:     }
 9101:     return;
 9102: }
 9103: 
 9104: sub modify_usermodification {
 9105:     my ($dom,%domconfig) = @_;
 9106:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 9107:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9108:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9109:             if ($key eq 'selfcreate') {
 9110:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 9111:             } else {  
 9112:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 9113:             }
 9114:         }
 9115:     }
 9116:     my @contexts = ('author','course');
 9117:     my %context_title = (
 9118:                            author => 'In author context',
 9119:                            course => 'In course context',
 9120:                         );
 9121:     my @fields = ('lastname','firstname','middlename','generation',
 9122:                   'permanentemail','id');
 9123:     my %roles = (
 9124:                   author => ['ca','aa'],
 9125:                   course => ['st','ep','ta','in','cr'],
 9126:                 );
 9127:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9128:     foreach my $context (@contexts) {
 9129:         foreach my $role (@{$roles{$context}}) {
 9130:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 9131:             foreach my $item (@fields) {
 9132:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 9133:                     $modifyhash{$context}{$role}{$item} = 1;
 9134:                 } else {
 9135:                     $modifyhash{$context}{$role}{$item} = 0;
 9136:                 }
 9137:             }
 9138:         }
 9139:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 9140:             foreach my $role (@{$roles{$context}}) {
 9141:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 9142:                     foreach my $field (@fields) {
 9143:                         if ($modifyhash{$context}{$role}{$field} ne 
 9144:                                 $curr_usermodification{$context}{$role}{$field}) {
 9145:                             push(@{$changes{$context}},$role);
 9146:                             last;
 9147:                         }
 9148:                     }
 9149:                 }
 9150:             }
 9151:         } else {
 9152:             foreach my $context (@contexts) {
 9153:                 foreach my $role (@{$roles{$context}}) {
 9154:                     push(@{$changes{$context}},$role);
 9155:                 }
 9156:             }
 9157:         }
 9158:     }
 9159:     my %usermodification_hash =  (
 9160:                                    usermodification => \%modifyhash,
 9161:                                  );
 9162:     my $putresult = &Apache::lonnet::put_dom('configuration',
 9163:                                              \%usermodification_hash,$dom);
 9164:     if ($putresult eq 'ok') {
 9165:         if (keys(%changes) > 0) {
 9166:             $resulttext = &mt('Changes made: ').'<ul>';
 9167:             foreach my $context (@contexts) {
 9168:                 if (ref($changes{$context}) eq 'ARRAY') {
 9169:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 9170:                     if (ref($changes{$context}) eq 'ARRAY') {
 9171:                         foreach my $role (@{$changes{$context}}) {
 9172:                             my $rolename;
 9173:                             if ($role eq 'cr') {
 9174:                                 $rolename = &mt('Custom');
 9175:                             } else {
 9176:                                 $rolename = &Apache::lonnet::plaintext($role);
 9177:                             }
 9178:                             my @modifiable;
 9179:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 9180:                             foreach my $field (@fields) {
 9181:                                 if ($modifyhash{$context}{$role}{$field}) {
 9182:                                     push(@modifiable,$fieldtitles{$field});
 9183:                                 }
 9184:                             }
 9185:                             if (@modifiable > 0) {
 9186:                                 $resulttext .= join(', ',@modifiable);
 9187:                             } else {
 9188:                                 $resulttext .= &mt('none'); 
 9189:                             }
 9190:                             $resulttext .= '</li>';
 9191:                         }
 9192:                         $resulttext .= '</ul></li>';
 9193:                     }
 9194:                 }
 9195:             }
 9196:             $resulttext .= '</ul>';
 9197:         } else {
 9198:             $resulttext = &mt('No changes made to user modification settings');
 9199:         }
 9200:     } else {
 9201:         $resulttext = '<span class="LC_error">'.
 9202:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9203:     }
 9204:     return $resulttext;
 9205: }
 9206: 
 9207: sub modify_defaults {
 9208:     my ($dom,$lastactref,%domconfig) = @_;
 9209:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 9210:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9211:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 9212:     my @authtypes = ('internal','krb4','krb5','localauth');
 9213:     foreach my $item (@items) {
 9214:         $newvalues{$item} = $env{'form.'.$item};
 9215:         if ($item eq 'auth_def') {
 9216:             if ($newvalues{$item} ne '') {
 9217:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 9218:                     push(@errors,$item);
 9219:                 }
 9220:             }
 9221:         } elsif ($item eq 'lang_def') {
 9222:             if ($newvalues{$item} ne '') {
 9223:                 if ($newvalues{$item} =~ /^(\w+)/) {
 9224:                     my $langcode = $1;
 9225:                     if ($langcode ne 'x_chef') {
 9226:                         if (code2language($langcode) eq '') {
 9227:                             push(@errors,$item);
 9228:                         }
 9229:                     }
 9230:                 } else {
 9231:                     push(@errors,$item);
 9232:                 }
 9233:             }
 9234:         } elsif ($item eq 'timezone_def') {
 9235:             if ($newvalues{$item} ne '') {
 9236:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 9237:                     push(@errors,$item);   
 9238:                 }
 9239:             }
 9240:         } elsif ($item eq 'datelocale_def') {
 9241:             if ($newvalues{$item} ne '') {
 9242:                 my @datelocale_ids = DateTime::Locale->ids();
 9243:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 9244:                     push(@errors,$item);
 9245:                 }
 9246:             }
 9247:         } elsif ($item eq 'portal_def') {
 9248:             if ($newvalues{$item} ne '') {
 9249:                 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])\/?$/) {
 9250:                     push(@errors,$item);
 9251:                 }
 9252:             }
 9253:         }
 9254:         if (grep(/^\Q$item\E$/,@errors)) {
 9255:             $newvalues{$item} = $domdefaults{$item};
 9256:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 9257:             $changes{$item} = 1;
 9258:         }
 9259:         $domdefaults{$item} = $newvalues{$item};
 9260:     }
 9261:     my %defaults_hash = (
 9262:                          defaults => \%newvalues,
 9263:                         );
 9264:     my $title = &defaults_titles();
 9265: 
 9266:     my $currinststatus;
 9267:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9268:         $currinststatus = $domconfig{'inststatus'};
 9269:     } else {
 9270:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9271:         $currinststatus = {
 9272:                              inststatustypes => $usertypes,
 9273:                              inststatusorder => $types,
 9274:                              inststatusguest => [],
 9275:                           };
 9276:     }
 9277:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
 9278:     my @allpos;
 9279:     my %guests;
 9280:     my %alltypes;
 9281:     my ($currtitles,$currguests,$currorder);
 9282:     if (ref($currinststatus) eq 'HASH') {
 9283:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
 9284:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
 9285:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
 9286:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
 9287:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
 9288:                     }
 9289:                 }
 9290:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
 9291:                     my $position = $env{'form.inststatus_pos_'.$type};
 9292:                     $position =~ s/\D+//g;
 9293:                     $allpos[$position] = $type;
 9294:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
 9295:                     $alltypes{$type} =~ s/`//g;
 9296:                     if ($env{'form.inststatus_guest_'.$type}) {
 9297:                         $guests{$type} = 1;
 9298:                     }
 9299:                 }
 9300:             }
 9301:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
 9302:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
 9303:             }
 9304:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
 9305:             $currtitles =~ s/,$//;
 9306:         }
 9307:     }
 9308:     if ($env{'form.addinststatus'}) {
 9309:         my $newtype = $env{'form.addinststatus'};
 9310:         $newtype =~ s/\W//g;
 9311:         unless (exists($alltypes{$newtype})) {
 9312:             if ($env{'form.addinststatus_guest'}) {
 9313:                 $guests{$newtype} = 1;
 9314:             }
 9315:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
 9316:             $alltypes{$newtype} =~ s/`//g; 
 9317:             my $position = $env{'form.addinststatus_pos'};
 9318:             $position =~ s/\D+//g;
 9319:             if ($position ne '') {
 9320:                 $allpos[$position] = $newtype;
 9321:             }
 9322:         }
 9323:     }
 9324:     my (@orderedstatus,@orderedguests);
 9325:     foreach my $type (@allpos) {
 9326:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
 9327:             push(@orderedstatus,$type);
 9328:             if ($guests{$type}) {
 9329:                 push(@orderedguests,$type);
 9330:             }
 9331:         }
 9332:     }
 9333:     foreach my $type (keys(%alltypes)) {
 9334:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
 9335:             delete($alltypes{$type});
 9336:         }
 9337:     }
 9338:     $defaults_hash{'inststatus'} = {
 9339:                                      inststatustypes => \%alltypes,
 9340:                                      inststatusorder => \@orderedstatus,
 9341:                                      inststatusguest => \@orderedguests,
 9342:                                    };
 9343:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
 9344:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 9345:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
 9346:         }
 9347:     }
 9348:     if ($currorder ne join(',',@orderedstatus)) {
 9349:         $changes{'inststatus'}{'inststatusorder'} = 1;
 9350:     }
 9351:     if ($currguests ne join(',',@orderedguests)) {
 9352:         $changes{'inststatus'}{'inststatusguest'} = 1;
 9353:     }
 9354:     my $newtitles;
 9355:     foreach my $item (@orderedstatus) {
 9356:         $newtitles .= $alltypes{$item}.',';
 9357:     }
 9358:     $newtitles =~ s/,$//;
 9359:     if ($currtitles ne $newtitles) {
 9360:         $changes{'inststatus'}{'inststatustypes'} = 1;
 9361:     }
 9362:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 9363:                                              $dom);
 9364:     if ($putresult eq 'ok') {
 9365:         if (keys(%changes) > 0) {
 9366:             $resulttext = &mt('Changes made:').'<ul>';
 9367:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 9368:             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";
 9369:             foreach my $item (sort(keys(%changes))) {
 9370:                 if ($item eq 'inststatus') {
 9371:                     if (ref($changes{'inststatus'}) eq 'HASH') {
 9372:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
 9373:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
 9374:                             foreach my $type (@orderedstatus) { 
 9375:                                 $resulttext .= $alltypes{$type}.', ';
 9376:                             }
 9377:                             $resulttext =~ s/, $//;
 9378:                             $resulttext .= '</li>';
 9379:                         }
 9380:                         if ($changes{'inststatus'}{'inststatusguest'}) {
 9381:                             $resulttext .= '<li>'; 
 9382:                             if (@orderedguests) {
 9383:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
 9384:                                 foreach my $type (@orderedguests) {
 9385:                                     $resulttext .= $alltypes{$type}.', ';
 9386:                                 }
 9387:                                 $resulttext =~ s/, $//;
 9388:                             } else {
 9389:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
 9390:                             }
 9391:                             $resulttext .= '</li>';
 9392:                         }
 9393:                     }
 9394:                 } else {
 9395:                     my $value = $env{'form.'.$item};
 9396:                     if ($value eq '') {
 9397:                         $value = &mt('none');
 9398:                     } elsif ($item eq 'auth_def') {
 9399:                         my %authnames = &authtype_names();
 9400:                         my %shortauth = (
 9401:                                           internal   => 'int',
 9402:                                           krb4       => 'krb4',
 9403:                                           krb5       => 'krb5',
 9404:                                           localauth  => 'loc',
 9405:                         );
 9406:                         $value = $authnames{$shortauth{$value}};
 9407:                     }
 9408:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 9409:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
 9410:                 }
 9411:             }
 9412:             $resulttext .= '</ul>';
 9413:             $mailmsgtext .= "\n";
 9414:             my $cachetime = 24*60*60;
 9415:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9416:             if (ref($lastactref) eq 'HASH') {
 9417:                 $lastactref->{'domdefaults'} = 1;
 9418:             }
 9419:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 9420:                 my $notify = 1;
 9421:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 9422:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 9423:                         $notify = 0;
 9424:                     }
 9425:                 }
 9426:                 if ($notify) {
 9427:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 9428:                                                "LON-CAPA Domain Settings Change - $dom",
 9429:                                                $mailmsgtext);
 9430:                 }
 9431:             }
 9432:         } else {
 9433:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 9434:         }
 9435:     } else {
 9436:         $resulttext = '<span class="LC_error">'.
 9437:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9438:     }
 9439:     if (@errors > 0) {
 9440:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 9441:         foreach my $item (@errors) {
 9442:             $resulttext .= ' "'.$title->{$item}.'",';
 9443:         }
 9444:         $resulttext =~ s/,$//;
 9445:     }
 9446:     return $resulttext;
 9447: }
 9448: 
 9449: sub modify_scantron {
 9450:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9451:     my ($resulttext,%confhash,%changes,$errors);
 9452:     my $custom = 'custom.tab';
 9453:     my $default = 'default.tab';
 9454:     my $servadm = $r->dir_config('lonAdmEMail');
 9455:     my ($configuserok,$author_ok,$switchserver) = 
 9456:         &config_check($dom,$confname,$servadm);
 9457:     if ($env{'form.scantronformat.filename'} ne '') {
 9458:         my $error;
 9459:         if ($configuserok eq 'ok') {
 9460:             if ($switchserver) {
 9461:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 9462:             } else {
 9463:                 if ($author_ok eq 'ok') {
 9464:                     my ($result,$scantronurl) =
 9465:                         &publishlogo($r,'upload','scantronformat',$dom,
 9466:                                      $confname,'scantron','','',$custom);
 9467:                     if ($result eq 'ok') {
 9468:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 9469:                         $changes{'scantronformat'} = 1;
 9470:                     } else {
 9471:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 9472:                     }
 9473:                 } else {
 9474:                     $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);
 9475:                 }
 9476:             }
 9477:         } else {
 9478:             $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);
 9479:         }
 9480:         if ($error) {
 9481:             &Apache::lonnet::logthis($error);
 9482:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9483:         }
 9484:     }
 9485:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 9486:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 9487:             if ($env{'form.scantronformat_del'}) {
 9488:                 $confhash{'scantron'}{'scantronformat'} = '';
 9489:                 $changes{'scantronformat'} = 1;
 9490:             }
 9491:         }
 9492:     }
 9493:     if (keys(%confhash) > 0) {
 9494:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 9495:                                                  $dom);
 9496:         if ($putresult eq 'ok') {
 9497:             if (keys(%changes) > 0) {
 9498:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 9499:                     $resulttext = &mt('Changes made:').'<ul>';
 9500:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 9501:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 9502:                     } else {
 9503:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 9504:                     }
 9505:                     $resulttext .= '</ul>';
 9506:                 } else {
 9507:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 9508:                 }
 9509:                 $resulttext .= '</ul>';
 9510:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9511:                 if (ref($lastactref) eq 'HASH') {
 9512:                     $lastactref->{'domainconfig'} = 1;
 9513:                 }
 9514:             } else {
 9515:                 $resulttext = &mt('No changes made to bubblesheet format file');
 9516:             }
 9517:         } else {
 9518:             $resulttext = '<span class="LC_error">'.
 9519:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 9520:         }
 9521:     } else {
 9522:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 9523:     }
 9524:     if ($errors) {
 9525:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9526:                        $errors.'</ul>';
 9527:     }
 9528:     return $resulttext;
 9529: }
 9530: 
 9531: sub modify_coursecategories {
 9532:     my ($dom,$lastactref,%domconfig) = @_;
 9533:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 9534:         $cathash);
 9535:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 9536:     my @catitems = ('unauth','auth');
 9537:     my @cattypes = ('std','domonly','codesrch','none');
 9538:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9539:         $cathash = $domconfig{'coursecategories'}{'cats'};
 9540:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 9541:             $changes{'togglecats'} = 1;
 9542:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 9543:         }
 9544:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 9545:             $changes{'categorize'} = 1;
 9546:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 9547:         }
 9548:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 9549:             $changes{'togglecatscomm'} = 1;
 9550:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 9551:         }
 9552:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 9553:             $changes{'categorizecomm'} = 1;
 9554:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 9555:         }
 9556:         foreach my $item (@catitems) {
 9557:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9558:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
 9559:                     $changes{$item} = 1;
 9560:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9561:                 }
 9562:             }
 9563:         }
 9564:     } else {
 9565:         $changes{'togglecats'} = 1;
 9566:         $changes{'categorize'} = 1;
 9567:         $changes{'togglecatscomm'} = 1;
 9568:         $changes{'categorizecomm'} = 1;
 9569:         $domconfig{'coursecategories'} = {
 9570:                                              togglecats => $env{'form.togglecats'},
 9571:                                              categorize => $env{'form.categorize'},
 9572:                                              togglecatscomm => $env{'form.togglecatscomm'},
 9573:                                              categorizecomm => $env{'form.categorizecomm'},
 9574:                                          };
 9575:         foreach my $item (@catitems) {
 9576:             if ($env{'form.coursecat_'.$item} ne 'std') {
 9577:                 $changes{$item} = 1;
 9578:             }
 9579:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9580:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9581:             }
 9582:         }
 9583:     }
 9584:     if (ref($cathash) eq 'HASH') {
 9585:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 9586:             push (@deletecategory,'instcode::0');
 9587:         }
 9588:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 9589:             push(@deletecategory,'communities::0');
 9590:         }
 9591:     }
 9592:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 9593:     if (ref($cathash) eq 'HASH') {
 9594:         if (@deletecategory > 0) {
 9595:             #FIXME Need to remove category from all courses using a deleted category 
 9596:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 9597:             foreach my $item (@deletecategory) {
 9598:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 9599:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 9600:                     $deletions{$item} = 1;
 9601:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 9602:                 }
 9603:             }
 9604:         }
 9605:         foreach my $item (keys(%{$cathash})) {
 9606:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9607:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 9608:                 $reorderings{$item} = 1;
 9609:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 9610:             }
 9611:             if ($env{'form.addcategory_name_'.$item} ne '') {
 9612:                 my $newcat = $env{'form.addcategory_name_'.$item};
 9613:                 my $newdepth = $depth+1;
 9614:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9615:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 9616:                 $adds{$newitem} = 1; 
 9617:             }
 9618:             if ($env{'form.subcat_'.$item} ne '') {
 9619:                 my $newcat = $env{'form.subcat_'.$item};
 9620:                 my $newdepth = $depth+1;
 9621:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9622:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 9623:                 $adds{$newitem} = 1;
 9624:             }
 9625:         }
 9626:     }
 9627:     if ($env{'form.instcode'} eq '1') {
 9628:         if (ref($cathash) eq 'HASH') {
 9629:             my $newitem = 'instcode::0';
 9630:             if ($cathash->{$newitem} eq '') {  
 9631:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9632:                 $adds{$newitem} = 1;
 9633:             }
 9634:         } else {
 9635:             my $newitem = 'instcode::0';
 9636:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9637:             $adds{$newitem} = 1;
 9638:         }
 9639:     }
 9640:     if ($env{'form.communities'} eq '1') {
 9641:         if (ref($cathash) eq 'HASH') {
 9642:             my $newitem = 'communities::0';
 9643:             if ($cathash->{$newitem} eq '') {
 9644:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9645:                 $adds{$newitem} = 1;
 9646:             }
 9647:         } else {
 9648:             my $newitem = 'communities::0';
 9649:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9650:             $adds{$newitem} = 1;
 9651:         }
 9652:     }
 9653:     if ($env{'form.addcategory_name'} ne '') {
 9654:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 9655:             ($env{'form.addcategory_name'} ne 'communities')) {
 9656:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 9657:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 9658:             $adds{$newitem} = 1;
 9659:         }
 9660:     }
 9661:     my $putresult;
 9662:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9663:         if (keys(%deletions) > 0) {
 9664:             foreach my $key (keys(%deletions)) {
 9665:                 if ($predelallitems{$key} ne '') {
 9666:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 9667:                 }
 9668:             }
 9669:         }
 9670:         my (@chkcats,@chktrails,%chkallitems);
 9671:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 9672:         if (ref($chkcats[0]) eq 'ARRAY') {
 9673:             my $depth = 0;
 9674:             my $chg = 0;
 9675:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 9676:                 my $name = $chkcats[0][$i];
 9677:                 my $item;
 9678:                 if ($name eq '') {
 9679:                     $chg ++;
 9680:                 } else {
 9681:                     $item = &escape($name).'::0';
 9682:                     if ($chg) {
 9683:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 9684:                     }
 9685:                     $depth ++; 
 9686:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 9687:                     $depth --;
 9688:                 }
 9689:             }
 9690:         }
 9691:     }
 9692:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9693:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 9694:         if ($putresult eq 'ok') {
 9695:             my %title = (
 9696:                          togglecats     => 'Show/Hide a course in catalog',
 9697:                          categorize     => 'Assign a category to a course',
 9698:                          togglecatscomm => 'Show/Hide a community in catalog',
 9699:                          categorizecomm => 'Assign a category to a community',
 9700:                         );
 9701:             my %level = (
 9702:                          dom  => 'set in Domain ("Modify Course/Community")',
 9703:                          crs  => 'set in Course ("Course Configuration")',
 9704:                          comm => 'set in Community ("Community Configuration")',
 9705:                          none     => 'No catalog',
 9706:                          std      => 'Standard catalog',
 9707:                          domonly  => 'Domain-only catalog',
 9708:                          codesrch => 'Code search form',
 9709:                         );
 9710:             $resulttext = &mt('Changes made:').'<ul>';
 9711:             if ($changes{'togglecats'}) {
 9712:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 9713:             }
 9714:             if ($changes{'categorize'}) {
 9715:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 9716:             }
 9717:             if ($changes{'togglecatscomm'}) {
 9718:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 9719:             }
 9720:             if ($changes{'categorizecomm'}) {
 9721:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 9722:             }
 9723:             if ($changes{'unauth'}) {
 9724:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
 9725:             }
 9726:             if ($changes{'auth'}) {
 9727:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
 9728:             }
 9729:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9730:                 my $cathash;
 9731:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9732:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 9733:                 } else {
 9734:                     $cathash = {};
 9735:                 } 
 9736:                 my (@cats,@trails,%allitems);
 9737:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 9738:                 if (keys(%deletions) > 0) {
 9739:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 9740:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 9741:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 9742:                     }
 9743:                     $resulttext .= '</ul></li>';
 9744:                 }
 9745:                 if (keys(%reorderings) > 0) {
 9746:                     my %sort_by_trail;
 9747:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 9748:                     foreach my $key (keys(%reorderings)) {
 9749:                         if ($allitems{$key} ne '') {
 9750:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9751:                         }
 9752:                     }
 9753:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9754:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9755:                     }
 9756:                     $resulttext .= '</ul></li>';
 9757:                 }
 9758:                 if (keys(%adds) > 0) {
 9759:                     my %sort_by_trail;
 9760:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 9761:                     foreach my $key (keys(%adds)) {
 9762:                         if ($allitems{$key} ne '') {
 9763:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9764:                         }
 9765:                     }
 9766:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9767:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9768:                     }
 9769:                     $resulttext .= '</ul></li>';
 9770:                 }
 9771:             }
 9772:             $resulttext .= '</ul>';
 9773:             if ($changes{'unauth'} || $changes{'auth'}) {
 9774:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 9775:                 if ($changes{'auth'}) {
 9776:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 9777:                 }
 9778:                 if ($changes{'unauth'}) {
 9779:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 9780:                 }
 9781:                 my $cachetime = 24*60*60;
 9782:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9783:                 if (ref($lastactref) eq 'HASH') {
 9784:                     $lastactref->{'domdefaults'} = 1;
 9785:                 }
 9786:             }
 9787:         } else {
 9788:             $resulttext = '<span class="LC_error">'.
 9789:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9790:         }
 9791:     } else {
 9792:         $resulttext = &mt('No changes made to course and community categories');
 9793:     }
 9794:     return $resulttext;
 9795: }
 9796: 
 9797: sub modify_serverstatuses {
 9798:     my ($dom,%domconfig) = @_;
 9799:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 9800:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 9801:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 9802:     }
 9803:     my @pages = &serverstatus_pages();
 9804:     foreach my $type (@pages) {
 9805:         $newserverstatus{$type}{'namedusers'} = '';
 9806:         $newserverstatus{$type}{'machines'} = '';
 9807:         if (defined($env{'form.'.$type.'_namedusers'})) {
 9808:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 9809:             my @okusers;
 9810:             foreach my $user (@users) {
 9811:                 my ($uname,$udom) = split(/:/,$user);
 9812:                 if (($udom =~ /^$match_domain$/) &&   
 9813:                     (&Apache::lonnet::domain($udom)) &&
 9814:                     ($uname =~ /^$match_username$/)) {
 9815:                     if (!grep(/^\Q$user\E/,@okusers)) {
 9816:                         push(@okusers,$user);
 9817:                     }
 9818:                 }
 9819:             }
 9820:             if (@okusers > 0) {
 9821:                  @okusers = sort(@okusers);
 9822:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 9823:             }
 9824:         }
 9825:         if (defined($env{'form.'.$type.'_machines'})) {
 9826:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 9827:             my @okmachines;
 9828:             foreach my $ip (@machines) {
 9829:                 my @parts = split(/\./,$ip);
 9830:                 next if (@parts < 4);
 9831:                 my $badip = 0;
 9832:                 for (my $i=0; $i<4; $i++) {
 9833:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 9834:                         $badip = 1;
 9835:                         last;
 9836:                     }
 9837:                 }
 9838:                 if (!$badip) {
 9839:                     push(@okmachines,$ip);     
 9840:                 }
 9841:             }
 9842:             @okmachines = sort(@okmachines);
 9843:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 9844:         }
 9845:     }
 9846:     my %serverstatushash =  (
 9847:                                 serverstatuses => \%newserverstatus,
 9848:                             );
 9849:     foreach my $type (@pages) {
 9850:         foreach my $setting ('namedusers','machines') {
 9851:             my (@current,@new);
 9852:             if (ref($currserverstatus{$type}) eq 'HASH') {
 9853:                 if ($currserverstatus{$type}{$setting} ne '') { 
 9854:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 9855:                 }
 9856:             }
 9857:             if ($newserverstatus{$type}{$setting} ne '') {
 9858:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 9859:             }
 9860:             if (@current > 0) {
 9861:                 if (@new > 0) {
 9862:                     foreach my $item (@current) {
 9863:                         if (!grep(/^\Q$item\E$/,@new)) {
 9864:                             $changes{$type}{$setting} = 1;
 9865:                             last;
 9866:                         }
 9867:                     }
 9868:                     foreach my $item (@new) {
 9869:                         if (!grep(/^\Q$item\E$/,@current)) {
 9870:                             $changes{$type}{$setting} = 1;
 9871:                             last;
 9872:                         }
 9873:                     }
 9874:                 } else {
 9875:                     $changes{$type}{$setting} = 1;
 9876:                 }
 9877:             } elsif (@new > 0) {
 9878:                 $changes{$type}{$setting} = 1;
 9879:             }
 9880:         }
 9881:     }
 9882:     if (keys(%changes) > 0) {
 9883:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9884:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9885:                                                  \%serverstatushash,$dom);
 9886:         if ($putresult eq 'ok') {
 9887:             $resulttext .= &mt('Changes made:').'<ul>';
 9888:             foreach my $type (@pages) {
 9889:                 if (ref($changes{$type}) eq 'HASH') {
 9890:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 9891:                     if ($changes{$type}{'namedusers'}) {
 9892:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 9893:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 9894:                         } else {
 9895:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 9896:                         }
 9897:                     }
 9898:                     if ($changes{$type}{'machines'}) {
 9899:                         if ($newserverstatus{$type}{'machines'} eq '') {
 9900:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 9901:                         } else {
 9902:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 9903:                         }
 9904: 
 9905:                     }
 9906:                     $resulttext .= '</ul></li>';
 9907:                 }
 9908:             }
 9909:             $resulttext .= '</ul>';
 9910:         } else {
 9911:             $resulttext = '<span class="LC_error">'.
 9912:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 9913: 
 9914:         }
 9915:     } else {
 9916:         $resulttext = &mt('No changes made to access to server status pages');
 9917:     }
 9918:     return $resulttext;
 9919: }
 9920: 
 9921: sub modify_helpsettings {
 9922:     my ($r,$dom,$confname,%domconfig) = @_;
 9923:     my ($resulttext,$errors,%changes,%helphash);
 9924:     my %defaultchecked = ('submitbugs' => 'on');
 9925:     my @offon = ('off','on');
 9926:     my @toggles = ('submitbugs');
 9927:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 9928:         foreach my $item (@toggles) {
 9929:             if ($defaultchecked{$item} eq 'on') { 
 9930:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9931:                     if ($env{'form.'.$item} eq '0') {
 9932:                         $changes{$item} = 1;
 9933:                     }
 9934:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9935:                     $changes{$item} = 1;
 9936:                 }
 9937:             } elsif ($defaultchecked{$item} eq 'off') {
 9938:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9939:                     if ($env{'form.'.$item} eq '1') {
 9940:                         $changes{$item} = 1;
 9941:                     }
 9942:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9943:                     $changes{$item} = 1;
 9944:                 }
 9945:             }
 9946:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 9947:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 9948:             }
 9949:         }
 9950:     }
 9951:     my $putresult;
 9952:     if (keys(%changes) > 0) {
 9953:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 9954:         if ($putresult eq 'ok') {
 9955:             $resulttext = &mt('Changes made:').'<ul>';
 9956:             foreach my $item (sort(keys(%changes))) {
 9957:                 if ($item eq 'submitbugs') {
 9958:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 9959:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 9960:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 9961:                 }
 9962:             }
 9963:             $resulttext .= '</ul>';
 9964:         } else {
 9965:             $resulttext = &mt('No changes made to help settings');
 9966:             $errors .= '<li><span class="LC_error">'.
 9967:                        &mt('An error occurred storing the settings: [_1]',
 9968:                            $putresult).'</span></li>';
 9969:         }
 9970:     }
 9971:     if ($errors) {
 9972:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9973:                        $errors.'</ul>';
 9974:     }
 9975:     return $resulttext;
 9976: }
 9977: 
 9978: sub modify_coursedefaults {
 9979:     my ($dom,$lastactref,%domconfig) = @_;
 9980:     my ($resulttext,$errors,%changes,%defaultshash);
 9981:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 9982:     my @toggles = ('canuse_pdfforms');
 9983:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 9984:                    'uploadquota_community','uploadquota_textbook');
 9985:     my @types = ('official','unofficial','community','textbook');
 9986:     my %staticdefaults = (
 9987:                            anonsurvey_threshold => 10,
 9988:                            uploadquota          => 500,
 9989:                          );
 9990: 
 9991:     $defaultshash{'coursedefaults'} = {};
 9992: 
 9993:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 9994:         if ($domconfig{'coursedefaults'} eq '') {
 9995:             $domconfig{'coursedefaults'} = {};
 9996:         }
 9997:     }
 9998: 
 9999:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
10000:         foreach my $item (@toggles) {
10001:             if ($defaultchecked{$item} eq 'on') {
10002:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
10003:                     ($env{'form.'.$item} eq '0')) {
10004:                     $changes{$item} = 1;
10005:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10006:                     $changes{$item} = 1;
10007:                 }
10008:             } elsif ($defaultchecked{$item} eq 'off') {
10009:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
10010:                     ($env{'form.'.$item} eq '1')) {
10011:                     $changes{$item} = 1;
10012:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10013:                     $changes{$item} = 1;
10014:                 }
10015:             }
10016:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
10017:         }
10018:         foreach my $item (@numbers) {
10019:             my ($currdef,$newdef);
10020:             $newdef = $env{'form.'.$item};
10021:             if ($item eq 'anonsurvey_threshold') {
10022:                 $currdef = $domconfig{'coursedefaults'}{$item};
10023:                 $newdef =~ s/\D//g;
10024:                 if ($newdef eq '' || $newdef < 1) {
10025:                     $newdef = 1;
10026:                 }
10027:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
10028:             } else {
10029:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
10030:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10031:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
10032:                 }
10033:                 $newdef =~ s/[^\w.\-]//g;
10034:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
10035:             }
10036:             if ($currdef ne $newdef) {
10037:                 my $staticdef;
10038:                 if ($item eq 'anonsurvey_threshold') {
10039:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
10040:                         $changes{$item} = 1;
10041:                     }
10042:                 } else {
10043:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
10044:                         $changes{'uploadquota'} = 1;
10045:                     }
10046:                 }
10047:             }
10048:         }
10049: 
10050:         my $officialcreds = $env{'form.official_credits'};
10051:         $officialcreds =~ s/[^\d.]+//g;
10052:         my $unofficialcreds = $env{'form.unofficial_credits'};
10053:         $unofficialcreds =~ s/[^\d.]+//g;
10054:         my $textbookcreds = $env{'form.textbook_credits'};
10055:         $textbookcreds =~ s/[^\d.]+//g;
10056:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
10057:                 ($env{'form.coursecredits'} eq '1')) {
10058:                 $changes{'coursecredits'} = 1;
10059:         } else {
10060:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
10061:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
10062:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
10063:                 $changes{'coursecredits'} = 1;
10064:             }
10065:         }
10066:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
10067:             official   => $officialcreds,
10068:             unofficial => $unofficialcreds,
10069:             textbook   => $textbookcreds,
10070:         }
10071:     }
10072:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10073:                                              $dom);
10074:     if ($putresult eq 'ok') {
10075:         if (keys(%changes) > 0) {
10076:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10077:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || 
10078:                 ($changes{'uploadquota'})) { 
10079:                 if ($changes{'canuse_pdfforms'}) {
10080:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
10081:                 }
10082:                 if ($changes{'coursecredits'}) {
10083:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10084:                         $domdefaults{'officialcredits'} =
10085:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
10086:                         $domdefaults{'unofficialcredits'} =
10087:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
10088:                         $domdefaults{'textbookcredits'} =
10089:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
10090:                     }
10091:                 }
10092:                 if ($changes{'uploadquota'}) {
10093:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10094:                         foreach my $type (@types) {
10095:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
10096:                         }
10097:                     }
10098:                 }
10099:                 my $cachetime = 24*60*60;
10100:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10101:                 if (ref($lastactref) eq 'HASH') {
10102:                     $lastactref->{'domdefaults'} = 1;
10103:                 }
10104:             }
10105:             $resulttext = &mt('Changes made:').'<ul>';
10106:             foreach my $item (sort(keys(%changes))) {
10107:                 if ($item eq 'canuse_pdfforms') {
10108:                     if ($env{'form.'.$item} eq '1') {
10109:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
10110:                     } else {
10111:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
10112:                     }
10113:                 } elsif ($item eq 'anonsurvey_threshold') {
10114:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
10115:                 } elsif ($item eq 'uploadquota') {
10116:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10117:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
10118:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
10119:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
10120:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
10121: 
10122:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
10123:                                        '</ul>'.
10124:                                        '</li>';
10125:                     } else {
10126:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
10127:                     }
10128:                 } elsif ($item eq 'coursecredits') {
10129:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10130:                         if (($domdefaults{'officialcredits'} eq '') &&
10131:                             ($domdefaults{'unofficialcredits'} eq '') &&
10132:                             ($domdefaults{'textbookcredits'} eq '')) {
10133:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10134:                         } else {
10135:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
10136:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
10137:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
10138:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
10139:                                            '</ul>'.
10140:                                            '</li>';
10141:                         }
10142:                     } else {
10143:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10144:                     }
10145:                 }
10146:             }
10147:             $resulttext .= '</ul>';
10148:         } else {
10149:             $resulttext = &mt('No changes made to course defaults');
10150:         }
10151:     } else {
10152:         $resulttext = '<span class="LC_error">'.
10153:             &mt('An error occurred: [_1]',$putresult).'</span>';
10154:     }
10155:     return $resulttext;
10156: }
10157: 
10158: sub modify_selfenrollment {
10159:     my ($dom,$lastactref,%domconfig) = @_;
10160:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
10161:     my @types = ('official','unofficial','community','textbook');
10162:     my %titles = &tool_titles();
10163:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
10164:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
10165:     $ordered{'default'} = ['types','registered','approval','limit'];
10166: 
10167:     my (%roles,%shown,%toplevel);
10168:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
10169: 
10170:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
10171:         if ($domconfig{'selfenrollment'} eq '') {
10172:             $domconfig{'selfenrollment'} = {};
10173:         }
10174:     }
10175:     %toplevel = (
10176:                   admin      => 'Configuration Rights',
10177:                   default    => 'Default settings',
10178:                   validation => 'Validation of self-enrollment requests',
10179:                 );
10180:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
10181: 
10182:     if (ref($ordered{'admin'}) eq 'ARRAY') {
10183:         foreach my $item (@{$ordered{'admin'}}) {
10184:             foreach my $type (@types) {
10185:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
10186:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
10187:                 } else {
10188:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
10189:                 }
10190:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
10191:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
10192:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
10193:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
10194:                             push(@{$changes{'admin'}{$type}},$item);
10195:                         }
10196:                     } else {
10197:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
10198:                             push(@{$changes{'admin'}{$type}},$item);
10199:                         }
10200:                     }
10201:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
10202:                     push(@{$changes{'admin'}{$type}},$item);
10203:                 }
10204:             }
10205:         }
10206:     }
10207: 
10208:     foreach my $item (@{$ordered{'default'}}) {
10209:         foreach my $type (@types) {
10210:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
10211:             if ($item eq 'types') {
10212:                 unless (($value eq 'all') || ($value eq 'dom')) {
10213:                     $value = '';
10214:                 }
10215:             } elsif ($item eq 'registered') {
10216:                 unless ($value eq '1') {
10217:                     $value = 0;
10218:                 }
10219:             } elsif ($item eq 'approval') {
10220:                 unless ($value =~ /^[012]$/) {
10221:                     $value = 0;
10222:                 }
10223:             } else {
10224:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10225:                     $value = 'none';
10226:                 }
10227:             }
10228:             $selfenrollhash{'default'}{$type}{$item} = $value;
10229:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
10230:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10231:                     if ($selfenrollhash{'default'}{$type}{$item} ne
10232:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
10233:                          push(@{$changes{'default'}{$type}},$item);
10234:                     }
10235:                 } else {
10236:                     push(@{$changes{'default'}{$type}},$item);
10237:                 }
10238:             } else {
10239:                 push(@{$changes{'default'}{$type}},$item);
10240:             }
10241:             if ($item eq 'limit') {
10242:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10243:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
10244:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
10245:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
10246:                     }
10247:                 } else {
10248:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
10249:                 }
10250:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10251:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
10252:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
10253:                          push(@{$changes{'default'}{$type}},'cap');
10254:                     }
10255:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
10256:                     push(@{$changes{'default'}{$type}},'cap');
10257:                 }
10258:             }
10259:         }
10260:     }
10261: 
10262:     foreach my $item (@{$itemsref}) {
10263:         if ($item eq 'fields') {
10264:             my @changed;
10265:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
10266:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
10267:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
10268:             }
10269:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10270:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
10271:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
10272:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
10273:                 } else {
10274:                     @changed = @{$selfenrollhash{'validation'}{$item}};
10275:                 }
10276:             } else {
10277:                 @changed = @{$selfenrollhash{'validation'}{$item}};
10278:             }
10279:             if (@changed) {
10280:                 if ($selfenrollhash{'validation'}{$item}) { 
10281:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
10282:                 } else {
10283:                     $changes{'validation'}{$item} = &mt('None');
10284:                 }
10285:             }
10286:         } else {
10287:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
10288:             if ($item eq 'markup') {
10289:                if ($env{'form.selfenroll_validation_'.$item}) {
10290:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10291:                }
10292:             }
10293:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10294:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
10295:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
10296:                 }
10297:             }
10298:         }
10299:     }
10300: 
10301:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
10302:                                              $dom);
10303:     if ($putresult eq 'ok') {
10304:         if (keys(%changes) > 0) {
10305:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10306:             $resulttext = &mt('Changes made:').'<ul>';
10307:             foreach my $key ('admin','default','validation') {
10308:                 if (ref($changes{$key}) eq 'HASH') {
10309:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
10310:                     if ($key eq 'validation') {
10311:                         foreach my $item (@{$itemsref}) {
10312:                             if (exists($changes{$key}{$item})) {
10313:                                 if ($item eq 'markup') {
10314:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10315:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
10316:                                 } else {  
10317:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10318:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
10319:                                 }
10320:                             }
10321:                         }
10322:                     } else {
10323:                         foreach my $type (@types) {
10324:                             if ($type eq 'community') {
10325:                                 $roles{'1'} = &mt('Community personnel');
10326:                             } else {
10327:                                 $roles{'1'} = &mt('Course personnel');
10328:                             }
10329:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
10330:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
10331:                                     if ($key eq 'admin') {
10332:                                         my @mgrdc = ();
10333:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10334:                                             foreach my $item (@{$ordered{'admin'}}) {
10335:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
10336:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
10337:                                                         push(@mgrdc,$item);
10338:                                                     }
10339:                                                 }
10340:                                             }
10341:                                             if (@mgrdc) {
10342:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
10343:                                             } else {
10344:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
10345:                                             }
10346:                                         }
10347:                                     } else {
10348:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10349:                                             foreach my $item (@{$ordered{$key}}) {
10350:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10351:                                                     $domdefaults{$type.'selfenroll'.$item} =
10352:                                                         $selfenrollhash{$key}{$type}{$item};
10353:                                                 }
10354:                                             }
10355:                                         }
10356:                                     }
10357:                                 }
10358:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
10359:                                 foreach my $item (@{$ordered{$key}}) {
10360:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10361:                                         $resulttext .= '<li>';
10362:                                         if ($key eq 'admin') {
10363:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
10364:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
10365:                                         } else {
10366:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
10367:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
10368:                                         }
10369:                                         $resulttext .= '</li>';
10370:                                     }
10371:                                 }
10372:                                 $resulttext .= '</ul></li>';
10373:                             }
10374:                         }
10375:                         $resulttext .= '</ul></li>'; 
10376:                     }
10377:                 }
10378:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
10379:                     my $cachetime = 24*60*60;
10380:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10381:                     if (ref($lastactref) eq 'HASH') {
10382:                         $lastactref->{'domdefaults'} = 1;
10383:                     }
10384:                 }
10385:             }
10386:             $resulttext .= '</ul>';
10387:         } else {
10388:             $resulttext = &mt('No changes made to self-enrollment settings');
10389:         }
10390:     } else {
10391:         $resulttext = '<span class="LC_error">'.
10392:             &mt('An error occurred: [_1]',$putresult).'</span>';
10393:     }
10394:     return $resulttext;
10395: }
10396: 
10397: sub modify_usersessions {
10398:     my ($dom,$lastactref,%domconfig) = @_;
10399:     my @hostingtypes = ('version','excludedomain','includedomain');
10400:     my @offloadtypes = ('primary','default');
10401:     my %types = (
10402:                   remote => \@hostingtypes,
10403:                   hosted => \@hostingtypes,
10404:                   spares => \@offloadtypes,
10405:                 );
10406:     my @prefixes = ('remote','hosted','spares');
10407:     my @lcversions = &Apache::lonnet::all_loncaparevs();
10408:     my (%by_ip,%by_location,@intdoms);
10409:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
10410:     my @locations = sort(keys(%by_location));
10411:     my (%defaultshash,%changes);
10412:     foreach my $prefix (@prefixes) {
10413:         $defaultshash{'usersessions'}{$prefix} = {};
10414:     }
10415:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10416:     my $resulttext;
10417:     my %iphost = &Apache::lonnet::get_iphost();
10418:     foreach my $prefix (@prefixes) {
10419:         next if ($prefix eq 'spares');
10420:         foreach my $type (@{$types{$prefix}}) {
10421:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
10422:             if ($type eq 'version') {
10423:                 my $value = $env{'form.'.$prefix.'_'.$type};
10424:                 my $okvalue;
10425:                 if ($value ne '') {
10426:                     if (grep(/^\Q$value\E$/,@lcversions)) {
10427:                         $okvalue = $value;
10428:                     }
10429:                 }
10430:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10431:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10432:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
10433:                             if ($inuse == 0) {
10434:                                 $changes{$prefix}{$type} = 1;
10435:                             } else {
10436:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
10437:                                     $changes{$prefix}{$type} = 1;
10438:                                 }
10439:                                 if ($okvalue ne '') {
10440:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10441:                                 } 
10442:                             }
10443:                         } else {
10444:                             if (($inuse == 1) && ($okvalue ne '')) {
10445:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10446:                                 $changes{$prefix}{$type} = 1;
10447:                             }
10448:                         }
10449:                     } else {
10450:                         if (($inuse == 1) && ($okvalue ne '')) {
10451:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10452:                             $changes{$prefix}{$type} = 1;
10453:                         }
10454:                     }
10455:                 } else {
10456:                     if (($inuse == 1) && ($okvalue ne '')) {
10457:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10458:                         $changes{$prefix}{$type} = 1;
10459:                     }
10460:                 }
10461:             } else {
10462:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
10463:                 my @okvals;
10464:                 foreach my $val (@vals) {
10465:                     if ($val =~ /:/) {
10466:                         my @items = split(/:/,$val);
10467:                         foreach my $item (@items) {
10468:                             if (ref($by_location{$item}) eq 'ARRAY') {
10469:                                 push(@okvals,$item);
10470:                             }
10471:                         }
10472:                     } else {
10473:                         if (ref($by_location{$val}) eq 'ARRAY') {
10474:                             push(@okvals,$val);
10475:                         }
10476:                     }
10477:                 }
10478:                 @okvals = sort(@okvals);
10479:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10480:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10481:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10482:                             if ($inuse == 0) {
10483:                                 $changes{$prefix}{$type} = 1; 
10484:                             } else {
10485:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10486:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
10487:                                 if (@changed > 0) {
10488:                                     $changes{$prefix}{$type} = 1;
10489:                                 }
10490:                             }
10491:                         } else {
10492:                             if ($inuse == 1) {
10493:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10494:                                 $changes{$prefix}{$type} = 1;
10495:                             }
10496:                         } 
10497:                     } else {
10498:                         if ($inuse == 1) {
10499:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10500:                             $changes{$prefix}{$type} = 1;
10501:                         }
10502:                     }
10503:                 } else {
10504:                     if ($inuse == 1) {
10505:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10506:                         $changes{$prefix}{$type} = 1;
10507:                     }
10508:                 }
10509:             }
10510:         }
10511:     }
10512: 
10513:     my @alldoms = &Apache::lonnet::all_domains();
10514:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10515:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
10516:     my $savespares;
10517: 
10518:     foreach my $lonhost (sort(keys(%servers))) {
10519:         my $serverhomeID =
10520:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
10521:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
10522:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
10523:         my %spareschg;
10524:         foreach my $type (@{$types{'spares'}}) {
10525:             my @okspares;
10526:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
10527:             foreach my $server (@checked) {
10528:                 if (&Apache::lonnet::hostname($server) ne '') {
10529:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
10530:                         unless (grep(/^\Q$server\E$/,@okspares)) {
10531:                             push(@okspares,$server);
10532:                         }
10533:                     }
10534:                 }
10535:             }
10536:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
10537:             my $newspare;
10538:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
10539:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
10540:                     $newspare = $new;
10541:                 }
10542:             }
10543:             my @spares;
10544:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
10545:                 @spares = sort(@okspares,$newspare);
10546:             } else {
10547:                 @spares = sort(@okspares);
10548:             }
10549:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
10550:             if (ref($spareid{$lonhost}) eq 'HASH') {
10551:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
10552:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
10553:                     if (@diffs > 0) {
10554:                         $spareschg{$type} = 1;
10555:                     }
10556:                 }
10557:             }
10558:         }
10559:         if (keys(%spareschg) > 0) {
10560:             $changes{'spares'}{$lonhost} = \%spareschg;
10561:         }
10562:     }
10563: 
10564:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
10565:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
10566:             if (ref($changes{'spares'}) eq 'HASH') {
10567:                 if (keys(%{$changes{'spares'}}) > 0) {
10568:                     $savespares = 1;
10569:                 }
10570:             }
10571:         } else {
10572:             $savespares = 1;
10573:         }
10574:     }
10575: 
10576:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
10577:     if ((keys(%changes) > 0) || ($savespares)) {
10578:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10579:                                                  $dom);
10580:         if ($putresult eq 'ok') {
10581:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10582:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
10583:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
10584:                 }
10585:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
10586:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
10587:                 }
10588:             }
10589:             my $cachetime = 24*60*60;
10590:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10591:             if (ref($lastactref) eq 'HASH') {
10592:                 $lastactref->{'domdefaults'} = 1;
10593:             }
10594:             if (keys(%changes) > 0) {
10595:                 my %lt = &usersession_titles();
10596:                 $resulttext = &mt('Changes made:').'<ul>';
10597:                 foreach my $prefix (@prefixes) {
10598:                     if (ref($changes{$prefix}) eq 'HASH') {
10599:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
10600:                         if ($prefix eq 'spares') {
10601:                             if (ref($changes{$prefix}) eq 'HASH') {
10602:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
10603:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
10604:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
10605:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
10606:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
10607:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
10608:                                         foreach my $type (@{$types{$prefix}}) {
10609:                                             if ($changes{$prefix}{$lonhost}{$type}) {
10610:                                                 my $offloadto = &mt('None');
10611:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
10612:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
10613:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
10614:                                                     }
10615:                                                 }
10616:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
10617:                                             }
10618:                                         }
10619:                                     }
10620:                                     $resulttext .= '</li>';
10621:                                 }
10622:                             }
10623:                         } else {
10624:                             foreach my $type (@{$types{$prefix}}) {
10625:                                 if (defined($changes{$prefix}{$type})) {
10626:                                     my $newvalue;
10627:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10628:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
10629:                                             if ($type eq 'version') {
10630:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
10631:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10632:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
10633:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
10634:                                                 }
10635:                                             }
10636:                                         }
10637:                                     }
10638:                                     if ($newvalue eq '') {
10639:                                         if ($type eq 'version') {
10640:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
10641:                                         } else {
10642:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
10643:                                         }
10644:                                     } else {
10645:                                         if ($type eq 'version') {
10646:                                             $newvalue .= ' '.&mt('(or later)'); 
10647:                                         }
10648:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
10649:                                     }
10650:                                 }
10651:                             }
10652:                         }
10653:                         $resulttext .= '</ul>';
10654:                     }
10655:                 }
10656:                 $resulttext .= '</ul>';
10657:             } else {
10658:                 $resulttext = $nochgmsg;
10659:             }
10660:         } else {
10661:             $resulttext = '<span class="LC_error">'.
10662:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10663:         }
10664:     } else {
10665:         $resulttext = $nochgmsg;
10666:     }
10667:     return $resulttext;
10668: }
10669: 
10670: sub modify_loadbalancing {
10671:     my ($dom,%domconfig) = @_;
10672:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
10673:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
10674:     my ($othertitle,$usertypes,$types) =
10675:         &Apache::loncommon::sorted_inst_types($dom);
10676:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10677:     my @sparestypes = ('primary','default');
10678:     my %typetitles = &sparestype_titles();
10679:     my $resulttext;
10680:     my (%currbalancer,%currtargets,%currrules,%existing);
10681:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10682:         %existing = %{$domconfig{'loadbalancing'}};
10683:     }
10684:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
10685:                               \%currtargets,\%currrules);
10686:     my ($saveloadbalancing,%defaultshash,%changes);
10687:     my ($alltypes,$othertypes,$titles) =
10688:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
10689:     my %ruletitles = &offloadtype_text();
10690:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
10691:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
10692:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
10693:         if ($balancer eq '') {
10694:             next;
10695:         }
10696:         if (!exists($servers{$balancer})) {
10697:             if (exists($currbalancer{$balancer})) {
10698:                 push(@{$changes{'delete'}},$balancer);
10699:             }
10700:             next;
10701:         }
10702:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
10703:             push(@{$changes{'delete'}},$balancer);
10704:             next;
10705:         }
10706:         if (!exists($currbalancer{$balancer})) {
10707:             push(@{$changes{'add'}},$balancer);
10708:         }
10709:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
10710:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
10711:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
10712:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10713:             $saveloadbalancing = 1;
10714:         }
10715:         foreach my $sparetype (@sparestypes) {
10716:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
10717:             my @offloadto;
10718:             foreach my $target (@targets) {
10719:                 if (($servers{$target}) && ($target ne $balancer)) {
10720:                     if ($sparetype eq 'default') {
10721:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
10722:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
10723:                         }
10724:                     }
10725:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
10726:                         push(@offloadto,$target);
10727:                     }
10728:                 }
10729:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
10730:             }
10731:         }
10732:         if (ref($currtargets{$balancer}) eq 'HASH') {
10733:             foreach my $sparetype (@sparestypes) {
10734:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
10735:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
10736:                     if (@targetdiffs > 0) {
10737:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10738:                     }
10739:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10740:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10741:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10742:                     }
10743:                 }
10744:             }
10745:         } else {
10746:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
10747:                 foreach my $sparetype (@sparestypes) {
10748:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10749:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10750:                             $changes{'curr'}{$balancer}{'targets'} = 1;
10751:                         }
10752:                     }
10753:                 }
10754:             }
10755:         }
10756:         my $ishomedom;
10757:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
10758:             $ishomedom = 1;
10759:         }
10760:         if (ref($alltypes) eq 'ARRAY') {
10761:             foreach my $type (@{$alltypes}) {
10762:                 my $rule;
10763:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
10764:                          (!$ishomedom)) {
10765:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
10766:                 }
10767:                 if ($rule eq 'specific') {
10768:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
10769:                 }
10770:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
10771:                 if (ref($currrules{$balancer}) eq 'HASH') {
10772:                     if ($rule ne $currrules{$balancer}{$type}) {
10773:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10774:                     }
10775:                 } elsif ($rule ne '') {
10776:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10777:                 }
10778:             }
10779:         }
10780:     }
10781:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
10782:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
10783:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
10784:             $defaultshash{'loadbalancing'} = {};
10785:         }
10786:         my $putresult = &Apache::lonnet::put_dom('configuration',
10787:                                                  \%defaultshash,$dom);
10788:         if ($putresult eq 'ok') {
10789:             if (keys(%changes) > 0) {
10790:                 if (ref($changes{'delete'}) eq 'ARRAY') {
10791:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
10792:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
10793:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10794:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10795:                     }
10796:                 }
10797:                 if (ref($changes{'add'}) eq 'ARRAY') {
10798:                     foreach my $balancer (sort(@{$changes{'add'}})) {
10799:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
10800:                     }
10801:                 }
10802:                 if (ref($changes{'curr'}) eq 'HASH') {
10803:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
10804:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
10805:                             if ($changes{'curr'}{$balancer}{'targets'}) {
10806:                                 my %offloadstr;
10807:                                 foreach my $sparetype (@sparestypes) {
10808:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10809:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10810:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10811:                                         }
10812:                                     }
10813:                                 }
10814:                                 if (keys(%offloadstr) == 0) {
10815:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
10816:                                 } else {
10817:                                     my $showoffload;
10818:                                     foreach my $sparetype (@sparestypes) {
10819:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
10820:                                         if (defined($offloadstr{$sparetype})) {
10821:                                             $showoffload .= $offloadstr{$sparetype};
10822:                                         } else {
10823:                                             $showoffload .= &mt('None');
10824:                                         }
10825:                                         $showoffload .= ('&nbsp;'x3);
10826:                                     }
10827:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
10828:                                 }
10829:                             }
10830:                         }
10831:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
10832:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
10833:                                 foreach my $type (@{$alltypes}) {
10834:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
10835:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
10836:                                         my $balancetext;
10837:                                         if ($rule eq '') {
10838:                                             $balancetext =  $ruletitles{'default'};
10839:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
10840:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
10841:                                             $balancetext =  $ruletitles{$rule};
10842:                                         } else {
10843:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
10844:                                         }
10845:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
10846:                                     }
10847:                                 }
10848:                             }
10849:                         }
10850:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10851:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10852:                     }
10853:                 }
10854:                 if ($resulttext ne '') {
10855:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
10856:                 } else {
10857:                     $resulttext = $nochgmsg;
10858:                 }
10859:             } else {
10860:                 $resulttext = $nochgmsg;
10861:             }
10862:         } else {
10863:             $resulttext = '<span class="LC_error">'.
10864:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10865:         }
10866:     } else {
10867:         $resulttext = $nochgmsg;
10868:     }
10869:     return $resulttext;
10870: }
10871: 
10872: sub recurse_check {
10873:     my ($chkcats,$categories,$depth,$name) = @_;
10874:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
10875:         my $chg = 0;
10876:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
10877:             my $category = $chkcats->[$depth]{$name}[$j];
10878:             my $item;
10879:             if ($category eq '') {
10880:                 $chg ++;
10881:             } else {
10882:                 my $deeper = $depth + 1;
10883:                 $item = &escape($category).':'.&escape($name).':'.$depth;
10884:                 if ($chg) {
10885:                     $categories->{$item} -= $chg;
10886:                 }
10887:                 &recurse_check($chkcats,$categories,$deeper,$category);
10888:                 $deeper --;
10889:             }
10890:         }
10891:     }
10892:     return;
10893: }
10894: 
10895: sub recurse_cat_deletes {
10896:     my ($item,$coursecategories,$deletions) = @_;
10897:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10898:     my $subdepth = $depth + 1;
10899:     if (ref($coursecategories) eq 'HASH') {
10900:         foreach my $subitem (keys(%{$coursecategories})) {
10901:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
10902:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
10903:                 delete($coursecategories->{$subitem});
10904:                 $deletions->{$subitem} = 1;
10905:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
10906:             }
10907:         }
10908:     }
10909:     return;
10910: }
10911: 
10912: sub get_active_dcs {
10913:     my ($dom) = @_;
10914:     my $now = time;
10915:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
10916:     my %domcoords;
10917:     my $numdcs = 0;
10918:     foreach my $server (keys(%dompersonnel)) {
10919:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
10920:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
10921:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
10922:         }
10923:     }
10924:     return %domcoords;
10925: }
10926: 
10927: sub active_dc_picker {
10928:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
10929:     my %domcoords = &get_active_dcs($dom);
10930:     my @domcoord = keys(%domcoords);
10931:     if (keys(%currhash)) {
10932:         foreach my $dc (keys(%currhash)) {
10933:             unless (exists($domcoords{$dc})) {
10934:                 push(@domcoord,$dc);
10935:             }
10936:         }
10937:     }
10938:     @domcoord = sort(@domcoord);
10939:     my $numdcs = scalar(@domcoord);
10940:     my $rows = 0;
10941:     my $table;
10942:     if ($numdcs > 1) {
10943:         $table = '<table>';
10944:         for (my $i=0; $i<@domcoord; $i++) {
10945:             my $rem = $i%($numinrow);
10946:             if ($rem == 0) {
10947:                 if ($i > 0) {
10948:                     $table .= '</tr>';
10949:                 }
10950:                 $table .= '<tr>';
10951:                 $rows ++;
10952:             }
10953:             my $check = '';
10954:             if ($inputtype eq 'radio') {
10955:                 if (keys(%currhash) == 0) {
10956:                     if (!$i) {
10957:                         $check = ' checked="checked"';
10958:                     }
10959:                 } elsif (exists($currhash{$domcoord[$i]})) {
10960:                     $check = ' checked="checked"';
10961:                 }
10962:             } else {
10963:                 if (exists($currhash{$domcoord[$i]})) {
10964:                     $check = ' checked="checked"';
10965:                 }
10966:             }
10967:             if ($i == @domcoord - 1) {
10968:                 my $colsleft = $numinrow - $rem;
10969:                 if ($colsleft > 1) {
10970:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
10971:                 } else {
10972:                     $table .= '<td class="LC_left_item">';
10973:                 }
10974:             } else {
10975:                 $table .= '<td class="LC_left_item">';
10976:             }
10977:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
10978:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10979:             $table .= '<span class="LC_nobreak"><label>'.
10980:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
10981:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
10982:             if ($user ne $dcname.':'.$dcdom) {
10983:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10984:             }
10985:             $table .= '</label></span></td>';
10986:         }
10987:         $table .= '</tr></table>';
10988:     } elsif ($numdcs == 1) {
10989:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
10990:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10991:         if ($inputtype eq 'radio') {
10992:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
10993:             if ($user ne $dcname.':'.$dcdom) {
10994:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10995:             }
10996:         } else {
10997:             my $check;
10998:             if (exists($currhash{$domcoord[0]})) {
10999:                 $check = ' checked="checked"';
11000:             }
11001:             $table = '<span class="LC_nobreak"><label>'.
11002:                      '<input type="checkbox" name="'.$name.'" '.
11003:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
11004:             if ($user ne $dcname.':'.$dcdom) {
11005:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11006:             }
11007:             $table .= '</label></span>';
11008:             $rows ++;
11009:         }
11010:     }
11011:     return ($numdcs,$table,$rows);
11012: }
11013: 
11014: sub usersession_titles {
11015:     return &Apache::lonlocal::texthash(
11016:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
11017:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
11018:                spares => 'Servers offloaded to, when busy',
11019:                version => 'LON-CAPA version requirement',
11020:                excludedomain => 'Allow all, but exclude specific domains',
11021:                includedomain => 'Deny all, but include specific domains',
11022:                primary => 'Primary (checked first)',
11023:                default => 'Default',
11024:            );
11025: }
11026: 
11027: sub id_for_thisdom {
11028:     my (%servers) = @_;
11029:     my %altids;
11030:     foreach my $server (keys(%servers)) {
11031:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
11032:         if ($serverhome ne $server) {
11033:             $altids{$serverhome} = $server;
11034:         }
11035:     }
11036:     return %altids;
11037: }
11038: 
11039: sub count_servers {
11040:     my ($currbalancer,%servers) = @_;
11041:     my (@spares,$numspares);
11042:     foreach my $lonhost (sort(keys(%servers))) {
11043:         next if ($currbalancer eq $lonhost);
11044:         push(@spares,$lonhost);
11045:     }
11046:     if ($currbalancer) {
11047:         $numspares = scalar(@spares);
11048:     } else {
11049:         $numspares = scalar(@spares) - 1;
11050:     }
11051:     return ($numspares,@spares);
11052: }
11053: 
11054: sub lonbalance_targets_js {
11055:     my ($dom,$types,$servers,$settings) = @_;
11056:     my $select = &mt('Select');
11057:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
11058:     if (ref($servers) eq 'HASH') {
11059:         $alltargets = join("','",sort(keys(%{$servers})));
11060:         my @homedoms;
11061:         foreach my $server (sort(keys(%{$servers}))) {
11062:             if (&Apache::lonnet::host_domain($server) eq $dom) {
11063:                 push(@homedoms,'1');
11064:             } else {
11065:                 push(@homedoms,'0');
11066:             }
11067:         }
11068:         $allishome = join("','",@homedoms);
11069:     }
11070:     if (ref($types) eq 'ARRAY') {
11071:         if (@{$types} > 0) {
11072:             @alltypes = @{$types};
11073:         }
11074:     }
11075:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
11076:     $allinsttypes = join("','",@alltypes);
11077:     my (%currbalancer,%currtargets,%currrules,%existing);
11078:     if (ref($settings) eq 'HASH') {
11079:         %existing = %{$settings};
11080:     }
11081:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
11082:                               \%currtargets,\%currrules);
11083:     my $balancers = join("','",sort(keys(%currbalancer)));
11084:     return <<"END";
11085: 
11086: <script type="text/javascript">
11087: // <![CDATA[
11088: 
11089: currBalancers = new Array('$balancers');
11090: 
11091: function toggleTargets(balnum) {
11092:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11093:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
11094:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
11095:     var prevbalancer = prevhostitem.value;
11096:     var baltotal = document.getElementById('loadbalancing_total').value;
11097:     prevhostitem.value = balancer;
11098:     if (prevbalancer != '') {
11099:         var prevIdx = currBalancers.indexOf(prevbalancer);
11100:         if (prevIdx != -1) {
11101:             currBalancers.splice(prevIdx,1);
11102:         }
11103:     }
11104:     if (balancer == '') {
11105:         hideSpares(balnum);
11106:     } else {
11107:         var currIdx = currBalancers.indexOf(balancer);
11108:         if (currIdx == -1) {
11109:             currBalancers.push(balancer);
11110:         }
11111:         var homedoms = new Array('$allishome');
11112:         var ishomedom = homedoms[lonhostitem.selectedIndex];
11113:         showSpares(balancer,ishomedom,balnum);
11114:     }
11115:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
11116:     return;
11117: }
11118: 
11119: function showSpares(balancer,ishomedom,balnum) {
11120:     var alltargets = new Array('$alltargets');
11121:     var insttypes = new Array('$allinsttypes');
11122:     var offloadtypes = new Array('primary','default');
11123: 
11124:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
11125:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
11126:  
11127:     for (var i=0; i<offloadtypes.length; i++) {
11128:         var count = 0;
11129:         for (var j=0; j<alltargets.length; j++) {
11130:             if (alltargets[j] != balancer) {
11131:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
11132:                 item.value = alltargets[j];
11133:                 item.style.textAlign='left';
11134:                 item.style.textFace='normal';
11135:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
11136:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
11137:                     item.disabled = '';
11138:                 } else {
11139:                     item.disabled = 'disabled';
11140:                     item.checked = false;
11141:                 }
11142:                 count ++;
11143:             }
11144:         }
11145:     }
11146:     for (var k=0; k<insttypes.length; k++) {
11147:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
11148:             if (ishomedom == 1) {
11149:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11150:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11151:             } else {
11152:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11153:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11154:             }
11155:         } else {
11156:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11157:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11158:         }
11159:         if ((insttypes[k] != '_LC_external') && 
11160:             ((insttypes[k] != '_LC_internetdom') ||
11161:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
11162:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
11163:             item.options.length = 0;
11164:             item.options[0] = new Option("","",true,true);
11165:             var idx = 0;
11166:             for (var m=0; m<alltargets.length; m++) {
11167:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
11168:                     idx ++;
11169:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11170:                 }
11171:             }
11172:         }
11173:     }
11174:     return;
11175: }
11176: 
11177: function hideSpares(balnum) {
11178:     var alltargets = new Array('$alltargets');
11179:     var insttypes = new Array('$allinsttypes');
11180:     var offloadtypes = new Array('primary','default');
11181: 
11182:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
11183:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
11184: 
11185:     var total = alltargets.length - 1;
11186:     for (var i=0; i<offloadtypes; i++) {
11187:         for (var j=0; j<total; j++) {
11188:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
11189:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
11190:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
11191:         }
11192:     }
11193:     for (var k=0; k<insttypes.length; k++) {
11194:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11195:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11196:         if (insttypes[k] != '_LC_external') {
11197:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
11198:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
11199:         }
11200:     }
11201:     return;
11202: }
11203: 
11204: function checkOffloads(item,balnum,type) {
11205:     var alltargets = new Array('$alltargets');
11206:     var offloadtypes = new Array('primary','default');
11207:     if (item.checked) {
11208:         var total = alltargets.length - 1;
11209:         var other;
11210:         if (type == offloadtypes[0]) {
11211:             other = offloadtypes[1];
11212:         } else {
11213:             other = offloadtypes[0];
11214:         }
11215:         for (var i=0; i<total; i++) {
11216:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
11217:             if (server == item.value) {
11218:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
11219:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
11220:                 }
11221:             }
11222:         }
11223:     }
11224:     return;
11225: }
11226: 
11227: function singleServerToggle(balnum,type) {
11228:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
11229:     if (offloadtoSelIdx == 0) {
11230:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
11231:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11232: 
11233:     } else {
11234:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
11235:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11236:     }
11237:     return;
11238: }
11239: 
11240: function balanceruleChange(formname,balnum,type) {
11241:     if (type == '_LC_external') {
11242:         return;
11243:     }
11244:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
11245:     for (var i=0; i<typesRules.length; i++) {
11246:         if (formname.elements[typesRules[i]].checked) {
11247:             if (formname.elements[typesRules[i]].value != 'specific') {
11248:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
11249:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11250:             } else {
11251:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11252:             }
11253:         }
11254:     }
11255:     return;
11256: }
11257: 
11258: function balancerDeleteChange(balnum) {
11259:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11260:     var baltotal = document.getElementById('loadbalancing_total').value;
11261:     var addtarget;
11262:     var removetarget;
11263:     var action = 'delete';
11264:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
11265:         var lonhost = hostitem.value;
11266:         var currIdx = currBalancers.indexOf(lonhost);
11267:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
11268:             if (currIdx != -1) {
11269:                 currBalancers.splice(currIdx,1);
11270:             }
11271:             addtarget = lonhost;
11272:         } else {
11273:             if (currIdx == -1) {
11274:                 currBalancers.push(lonhost);
11275:             }
11276:             removetarget = lonhost;
11277:             action = 'undelete';
11278:         }
11279:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
11280:     }
11281:     return;
11282: }
11283: 
11284: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
11285:     if (baltotal > 1) {
11286:         var offloadtypes = new Array('primary','default');
11287:         var alltargets = new Array('$alltargets');
11288:         var insttypes = new Array('$allinsttypes');
11289:         for (var i=0; i<baltotal; i++) {
11290:             if (i != balnum) {
11291:                 for (var j=0; j<offloadtypes.length; j++) {
11292:                     var total = alltargets.length - 1;
11293:                     for (var k=0; k<total; k++) {
11294:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
11295:                         var server = serveritem.value;
11296:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
11297:                             if (server == addtarget) {
11298:                                 serveritem.disabled = '';
11299:                             }
11300:                         }
11301:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11302:                             if (server == removetarget) {
11303:                                 serveritem.disabled = 'disabled';
11304:                                 serveritem.checked = false;
11305:                             }
11306:                         }
11307:                     }
11308:                 }
11309:                 for (var j=0; j<insttypes.length; j++) {
11310:                     if (insttypes[j] != '_LC_external') {
11311:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
11312:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
11313:                             var currSel = singleserver.selectedIndex;
11314:                             var currVal = singleserver.options[currSel].value;
11315:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
11316:                                 var numoptions = singleserver.options.length;
11317:                                 var needsnew = 1;
11318:                                 for (var k=0; k<numoptions; k++) {
11319:                                     if (singleserver.options[k] == addtarget) {
11320:                                         needsnew = 0;
11321:                                         break;
11322:                                     }
11323:                                 }
11324:                                 if (needsnew == 1) {
11325:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
11326:                                 }
11327:                             }
11328:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11329:                                 singleserver.options.length = 0;
11330:                                 if ((currVal) && (currVal != removetarget)) {
11331:                                     singleserver.options[0] = new Option("","",false,false);
11332:                                 } else {
11333:                                     singleserver.options[0] = new Option("","",true,true);
11334:                                 }
11335:                                 var idx = 0;
11336:                                 for (var m=0; m<alltargets.length; m++) {
11337:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
11338:                                         idx ++;
11339:                                         if (currVal == alltargets[m]) {
11340:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
11341:                                         } else {
11342:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11343:                                         }
11344:                                     }
11345:                                 }
11346:                             }
11347:                         }
11348:                     }
11349:                 }
11350:             }
11351:         }
11352:     }
11353:     return;
11354: }
11355: 
11356: // ]]>
11357: </script>
11358: 
11359: END
11360: }
11361: 
11362: sub new_spares_js {
11363:     my @sparestypes = ('primary','default');
11364:     my $types = join("','",@sparestypes);
11365:     my $select = &mt('Select');
11366:     return <<"END";
11367: 
11368: <script type="text/javascript">
11369: // <![CDATA[
11370: 
11371: function updateNewSpares(formname,lonhost) {
11372:     var types = new Array('$types');
11373:     var include = new Array();
11374:     var exclude = new Array();
11375:     for (var i=0; i<types.length; i++) {
11376:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
11377:         for (var j=0; j<spareboxes.length; j++) {
11378:             if (formname.elements[spareboxes[j]].checked) {
11379:                 exclude.push(formname.elements[spareboxes[j]].value);
11380:             } else {
11381:                 include.push(formname.elements[spareboxes[j]].value);
11382:             }
11383:         }
11384:     }
11385:     for (var i=0; i<types.length; i++) {
11386:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
11387:         var selIdx = newSpare.selectedIndex;
11388:         var currnew = newSpare.options[selIdx].value;
11389:         var okSpares = new Array();
11390:         for (var j=0; j<newSpare.options.length; j++) {
11391:             var possible = newSpare.options[j].value;
11392:             if (possible != '') {
11393:                 if (exclude.indexOf(possible) == -1) {
11394:                     okSpares.push(possible);
11395:                 } else {
11396:                     if (currnew == possible) {
11397:                         selIdx = 0;
11398:                     }
11399:                 }
11400:             }
11401:         }
11402:         for (var k=0; k<include.length; k++) {
11403:             if (okSpares.indexOf(include[k]) == -1) {
11404:                 okSpares.push(include[k]);
11405:             }
11406:         }
11407:         okSpares.sort();
11408:         newSpare.options.length = 0;
11409:         if (selIdx == 0) {
11410:             newSpare.options[0] = new Option("$select","",true,true);
11411:         } else {
11412:             newSpare.options[0] = new Option("$select","",false,false);
11413:         }
11414:         for (var m=0; m<okSpares.length; m++) {
11415:             var idx = m+1;
11416:             var selThis = 0;
11417:             if (selIdx != 0) {
11418:                 if (okSpares[m] == currnew) {
11419:                     selThis = 1;
11420:                 }
11421:             }
11422:             if (selThis == 1) {
11423:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
11424:             } else {
11425:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
11426:             }
11427:         }
11428:     }
11429:     return;
11430: }
11431: 
11432: function checkNewSpares(lonhost,type) {
11433:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
11434:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
11435:     if (chosen != '') { 
11436:         var othertype;
11437:         var othernewSpare;
11438:         if (type == 'primary') {
11439:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
11440:         }
11441:         if (type == 'default') {
11442:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
11443:         }
11444:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
11445:             othernewSpare.selectedIndex = 0;
11446:         }
11447:     }
11448:     return;
11449: }
11450: 
11451: // ]]>
11452: </script>
11453: 
11454: END
11455: 
11456: }
11457: 
11458: sub common_domprefs_js {
11459:     return <<"END";
11460: 
11461: <script type="text/javascript">
11462: // <![CDATA[
11463: 
11464: function getIndicesByName(formname,item) {
11465:     var group = new Array();
11466:     for (var i=0;i<formname.elements.length;i++) {
11467:         if (formname.elements[i].name == item) {
11468:             group.push(formname.elements[i].id);
11469:         }
11470:     }
11471:     return group;
11472: }
11473: 
11474: // ]]>
11475: </script>
11476: 
11477: END
11478: 
11479: }
11480: 
11481: sub recaptcha_js {
11482:     my %lt = &captcha_phrases();
11483:     return <<"END";
11484: 
11485: <script type="text/javascript">
11486: // <![CDATA[
11487: 
11488: function updateCaptcha(caller,context) {
11489:     var privitem;
11490:     var pubitem;
11491:     var privtext;
11492:     var pubtext;
11493:     if (document.getElementById(context+'_recaptchapub')) {
11494:         pubitem = document.getElementById(context+'_recaptchapub');
11495:     } else {
11496:         return;
11497:     }
11498:     if (document.getElementById(context+'_recaptchapriv')) {
11499:         privitem = document.getElementById(context+'_recaptchapriv');
11500:     } else {
11501:         return;
11502:     }
11503:     if (document.getElementById(context+'_recaptchapubtxt')) {
11504:         pubtext = document.getElementById(context+'_recaptchapubtxt');
11505:     } else {
11506:         return;
11507:     }
11508:     if (document.getElementById(context+'_recaptchaprivtxt')) {
11509:         privtext = document.getElementById(context+'_recaptchaprivtxt');
11510:     } else {
11511:         return;
11512:     }
11513:     if (caller.checked) {
11514:         if (caller.value == 'recaptcha') {
11515:             pubitem.type = 'text';
11516:             privitem.type = 'text';
11517:             pubitem.size = '40';
11518:             privitem.size = '40';
11519:             pubtext.innerHTML = "$lt{'pub'}";
11520:             privtext.innerHTML = "$lt{'priv'}";
11521:         } else {
11522:             pubitem.type = 'hidden';
11523:             privitem.type = 'hidden';
11524:             pubtext.innerHTML = '';
11525:             privtext.innerHTML = '';
11526:         }
11527:     }
11528:     return;
11529: }
11530: 
11531: // ]]>
11532: </script>
11533: 
11534: END
11535: 
11536: }
11537: 
11538: sub toggle_display_js {
11539:     return <<"END";
11540: 
11541: <script type="text/javascript">
11542: // <![CDATA[
11543: 
11544: function toggleDisplay(domForm,caller) {
11545:     if (document.getElementById(caller)) {
11546:         var divitem = document.getElementById(caller);
11547:         var optionsElement = domForm.coursecredits;
11548:         if (caller == 'emailoptions') {
11549:             optionsElement = domForm.cancreate_email; 
11550:         }
11551:         if (optionsElement.length) {
11552:             var currval;
11553:             for (var i=0; i<optionsElement.length; i++) {
11554:                 if (optionsElement[i].checked) {
11555:                    currval = optionsElement[i].value;
11556:                 }
11557:             }
11558:             if (currval == 1) {
11559:                 divitem.style.display = 'block';
11560:             } else {
11561:                 divitem.style.display = 'none';
11562:             }
11563:         }
11564:     }
11565:     return;
11566: }
11567: 
11568: // ]]>
11569: </script>
11570: 
11571: END
11572: 
11573: }
11574: 
11575: sub captcha_phrases {
11576:     return &Apache::lonlocal::texthash (
11577:                  priv => 'Private key',
11578:                  pub  => 'Public key',
11579:                  original  => 'original (CAPTCHA)',
11580:                  recaptcha => 'successor (ReCAPTCHA)',
11581:                  notused   => 'unused',
11582:     );
11583: }
11584: 
11585: sub devalidate_remote_domconfs {
11586:     my ($dom,$cachekeys) = @_;
11587:     return unless (ref($cachekeys) eq 'HASH');
11588:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11589:     my %thismachine;
11590:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
11591:     my @posscached = ('domainconfig','domdefaults');
11592:     if (keys(%servers) > 1) {
11593:         foreach my $server (keys(%servers)) {
11594:             next if ($thismachine{$server});
11595:             my @cached;
11596:             foreach my $name (@posscached) {
11597:                 if ($cachekeys->{$name}) {
11598:                     push(@cached,&escape($name).':'.&escape($dom));
11599:                 }
11600:             }
11601:             if (@cached) {
11602:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
11603:             }
11604:         }
11605:     }
11606:     return;
11607: }
11608: 
11609: 1;

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