File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.266: download - view: text, annotated - select for diffs
Mon Jun 15 20:11:56 2015 UTC (9 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Need to check if the requestcourses domain config does point at a hash,
  because if has yet to be configured, then it will not (and there will be
  an ISE.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.266 2015/06/15 20:11:56 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:                                  {col1 => 'Custom HTML in document head',
  255:                                   col2 => 'Value'}],
  256:                       print => \&print_login,
  257:                       modify => \&modify_login,
  258:                     },
  259:         'defaults' => 
  260:                     { text => 'Default authentication/language/timezone/portal/types',
  261:                       help => 'Domain_Configuration_LangTZAuth',
  262:                       header => [{col1 => 'Setting',
  263:                                   col2 => 'Value'},
  264:                                  {col1 => 'Institutional user types',
  265:                                   col2 => 'Assignable to e-mail usernames'}],
  266:                       print => \&print_defaults,
  267:                       modify => \&modify_defaults,
  268:                     },
  269:         'quotas' => 
  270:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  271:                       help => 'Domain_Configuration_Quotas',
  272:                       header => [{col1 => 'User affiliation',
  273:                                   col2 => 'Available tools',
  274:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  275:                       print => \&print_quotas,
  276:                       modify => \&modify_quotas,
  277:                     },
  278:         'autoenroll' =>
  279:                    { text => 'Auto-enrollment settings',
  280:                      help => 'Domain_Configuration_Auto_Enrollment',
  281:                      header => [{col1 => 'Configuration setting',
  282:                                  col2 => 'Value(s)'}],
  283:                      print => \&print_autoenroll,
  284:                      modify => \&modify_autoenroll,
  285:                    },
  286:         'autoupdate' => 
  287:                    { text => 'Auto-update settings',
  288:                      help => 'Domain_Configuration_Auto_Updates',
  289:                      header => [{col1 => 'Setting',
  290:                                  col2 => 'Value',},
  291:                                 {col1 => 'Setting',
  292:                                  col2 => 'Affiliation'},
  293:                                 {col1 => 'User population',
  294:                                  col2 => 'Updatable user data'}],
  295:                      print => \&print_autoupdate,
  296:                      modify => \&modify_autoupdate,
  297:                   },
  298:         'autocreate' => 
  299:                   { text => 'Auto-course creation settings',
  300:                      help => 'Domain_Configuration_Auto_Creation',
  301:                      header => [{col1 => 'Configuration Setting',
  302:                                  col2 => 'Value',}],
  303:                      print => \&print_autocreate,
  304:                      modify => \&modify_autocreate,
  305:                   },
  306:         'directorysrch' => 
  307:                   { text => 'Institutional directory searches',
  308:                     help => 'Domain_Configuration_InstDirectory_Search',
  309:                     header => [{col1 => 'Setting',
  310:                                 col2 => 'Value',}],
  311:                     print => \&print_directorysrch,
  312:                     modify => \&modify_directorysrch,
  313:                   },
  314:         'contacts' =>
  315:                   { text => 'Contact Information',
  316:                     help => 'Domain_Configuration_Contact_Info',
  317:                     header => [{col1 => 'Setting',
  318:                                 col2 => 'Value',}],
  319:                     print => \&print_contacts,
  320:                     modify => \&modify_contacts,
  321:                   },
  322:         'usercreation' => 
  323:                   { text => 'User creation',
  324:                     help => 'Domain_Configuration_User_Creation',
  325:                     header => [{col1 => 'Format rule type',
  326:                                 col2 => 'Format rules in force'},
  327:                                {col1 => 'User account creation',
  328:                                 col2 => 'Usernames which may be created',},
  329:                                {col1 => 'Context',
  330:                                 col2 => 'Assignable authentication types'}],
  331:                     print => \&print_usercreation,
  332:                     modify => \&modify_usercreation,
  333:                   },
  334:         'selfcreation' => 
  335:                   { text => 'Users self-creating accounts',
  336:                     help => 'Domain_Configuration_Self_Creation', 
  337:                     header => [{col1 => 'Self-creation with institutional username',
  338:                                 col2 => 'Enabled?'},
  339:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  340:                                 col2 => 'Information user can enter'},
  341:                                {col1 => 'Self-creation with e-mail as username',
  342:                                 col2 => 'Settings'}],
  343:                     print => \&print_selfcreation,
  344:                     modify => \&modify_selfcreation,
  345:                   },
  346:         'usermodification' =>
  347:                   { text => 'User modification',
  348:                     help => 'Domain_Configuration_User_Modification',
  349:                     header => [{col1 => 'Target user has role',
  350:                                 col2 => 'User information updatable in author context'},
  351:                                {col1 => 'Target user has role',
  352:                                 col2 => 'User information updatable in course context'}],
  353:                     print => \&print_usermodification,
  354:                     modify => \&modify_usermodification,
  355:                   },
  356:         'scantron' =>
  357:                   { text => 'Bubblesheet format file',
  358:                     help => 'Domain_Configuration_Scantron_Format',
  359:                     header => [ {col1 => 'Item',
  360:                                  col2 => '',
  361:                               }],
  362:                     print => \&print_scantron,
  363:                     modify => \&modify_scantron,
  364:                   },
  365:         'requestcourses' => 
  366:                  {text => 'Request creation of courses',
  367:                   help => 'Domain_Configuration_Request_Courses',
  368:                   header => [{col1 => 'User affiliation',
  369:                               col2 => 'Availability/Processing of requests',},
  370:                              {col1 => 'Setting',
  371:                               col2 => 'Value'},
  372:                              {col1 => 'Available textbooks',
  373:                               col2 => ''},
  374:                              {col1 => 'Available templates',
  375:                               col2 => ''},
  376:                              {col1 => 'Validation (not official courses)',
  377:                               col2 => 'Value'},],
  378:                   print => \&print_quotas,
  379:                   modify => \&modify_quotas,
  380:                  },
  381:         'requestauthor' =>
  382:                  {text => 'Request Authoring Space',
  383:                   help => 'Domain_Configuration_Request_Author',
  384:                   header => [{col1 => 'User affiliation',
  385:                               col2 => 'Availability/Processing of requests',},
  386:                              {col1 => 'Setting',
  387:                               col2 => 'Value'}],
  388:                   print => \&print_quotas,
  389:                   modify => \&modify_quotas,
  390:                  },
  391:         'coursecategories' =>
  392:                   { text => 'Cataloging of courses/communities',
  393:                     help => 'Domain_Configuration_Cataloging_Courses',
  394:                     header => [{col1 => 'Catalog type/availability',
  395:                                 col2 => '',},
  396:                                {col1 => 'Category settings for standard catalog',
  397:                                 col2 => '',},
  398:                                {col1 => 'Categories',
  399:                                 col2 => '',
  400:                                }],
  401:                     print => \&print_coursecategories,
  402:                     modify => \&modify_coursecategories,
  403:                   },
  404:         'serverstatuses' =>
  405:                  {text   => 'Access to server status pages',
  406:                   help   => 'Domain_Configuration_Server_Status',
  407:                   header => [{col1 => 'Status Page',
  408:                               col2 => 'Other named users',
  409:                               col3 => 'Specific IPs',
  410:                             }],
  411:                   print => \&print_serverstatuses,
  412:                   modify => \&modify_serverstatuses,
  413:                  },
  414:         'helpsettings' =>
  415:                  {text   => 'Help page settings',
  416:                   help   => 'Domain_Configuration_Help_Settings',
  417:                   header => [{col1 => 'Help Settings (logged-in users)',
  418:                               col2 => 'Value'}],
  419:                   print  => \&print_helpsettings,
  420:                   modify => \&modify_helpsettings,
  421:                  },
  422:         'coursedefaults' => 
  423:                  {text => 'Course/Community defaults',
  424:                   help => 'Domain_Configuration_Course_Defaults',
  425:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  426:                               col2 => 'Value',},
  427:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  428:                               col2 => 'Value',},],
  429:                   print => \&print_coursedefaults,
  430:                   modify => \&modify_coursedefaults,
  431:                  },
  432:         'selfenrollment' => 
  433:                  {text   => 'Self-enrollment in Course/Community',
  434:                   help   => 'Domain_Configuration_Selfenrollment',
  435:                   header => [{col1 => 'Configuration Rights',
  436:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  437:                              {col1 => 'Defaults',
  438:                               col2 => 'Value'},
  439:                              {col1 => 'Self-enrollment validation (optional)',
  440:                               col2 => 'Value'},],
  441:                   print => \&print_selfenrollment,
  442:                   modify => \&modify_selfenrollment,
  443:                  },
  444:         'privacy' => 
  445:                  {text   => 'User Privacy',
  446:                   help   => 'Domain_Configuration_User_Privacy',
  447:                   header => [{col1 => 'Setting',
  448:                               col2 => 'Value',}],
  449:                   print => \&print_privacy,
  450:                   modify => \&modify_privacy,
  451:                  },
  452:         'usersessions' =>
  453:                  {text  => 'User session hosting/offloading',
  454:                   help  => 'Domain_Configuration_User_Sessions',
  455:                   header => [{col1 => 'Domain server',
  456:                               col2 => 'Servers to offload sessions to when busy'},
  457:                              {col1 => 'Hosting of users from other domains',
  458:                               col2 => 'Rules'},
  459:                              {col1 => "Hosting domain's own users elsewhere",
  460:                               col2 => 'Rules'}],
  461:                   print => \&print_usersessions,
  462:                   modify => \&modify_usersessions,
  463:                  },
  464:          'loadbalancing' =>
  465:                  {text  => 'Dedicated Load Balancer(s)',
  466:                   help  => 'Domain_Configuration_Load_Balancing',
  467:                   header => [{col1 => 'Balancers',
  468:                               col2 => 'Default destinations',
  469:                               col3 => 'User affiliation',
  470:                               col4 => 'Overrides'},
  471:                             ],
  472:                   print => \&print_loadbalancing,
  473:                   modify => \&modify_loadbalancing,
  474:                  },
  475:     );
  476:     if (keys(%servers) > 1) {
  477:         $prefs{'login'}  = { text   => 'Log-in page options',
  478:                              help   => 'Domain_Configuration_Login_Page',
  479:                             header => [{col1 => 'Log-in Service',
  480:                                         col2 => 'Server Setting',},
  481:                                        {col1 => 'Log-in Page Items',
  482:                                         col2 => ''},
  483:                                        {col1 => 'Log-in Help',
  484:                                         col2 => 'Value'},
  485:                                        {col1 => 'Custom HTML in document head',
  486:                                         col2 => 'Value'}],
  487:                             print => \&print_login,
  488:                             modify => \&modify_login,
  489:                            };
  490:     }
  491: 
  492:     my @roles = ('student','coordinator','author','admin');
  493:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  494:     &Apache::lonhtmlcommon::add_breadcrumb
  495:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  496:       text=>"Settings to display/modify"});
  497:     my $confname = $dom.'-domainconfig';
  498: 
  499:     if ($phase eq 'process') {
  500:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  501:                                                               \%prefs,\%domconfig,$confname,\@roles);
  502:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  503:             $r->rflush();
  504:             &devalidate_remote_domconfs($dom,$result);
  505:         }
  506:     } elsif ($phase eq 'display') {
  507:         my $js = &recaptcha_js().
  508:                  &toggle_display_js();
  509:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  510:             my ($othertitle,$usertypes,$types) =
  511:                 &Apache::loncommon::sorted_inst_types($dom);
  512:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  513:                                           $domconfig{'loadbalancing'}).
  514:                    &new_spares_js().
  515:                    &common_domprefs_js().
  516:                    &Apache::loncommon::javascript_array_indexof();
  517:         }
  518:         if (grep(/^requestcourses$/,@actions)) {
  519:             my $javascript_validations;
  520:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  521:             $js .= <<END;
  522: <script type="text/javascript">
  523: $javascript_validations
  524: </script>
  525: $coursebrowserjs
  526: END
  527:         }
  528:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  529:     } else {
  530: # check if domconfig user exists for the domain.
  531:         my $servadm = $r->dir_config('lonAdmEMail');
  532:         my ($configuserok,$author_ok,$switchserver) =
  533:             &config_check($dom,$confname,$servadm);
  534:         unless ($configuserok eq 'ok') {
  535:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  536:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  537:                           $confname).
  538:                       '<br />'
  539:             );
  540:             if ($switchserver) {
  541:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  542:                           '<br />'.
  543:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  544:                           '<br />'.
  545:                           &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).
  546:                           '<br />'.
  547:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  548:                 );
  549:             } else {
  550:                 $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.').
  551:                           '<br />'.
  552:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  553:                 );
  554:             }
  555:             $r->print(&Apache::loncommon::end_page());
  556:             return OK;
  557:         }
  558:         if (keys(%domconfig) == 0) {
  559:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  560:             my @ids=&Apache::lonnet::current_machine_ids();
  561:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  562:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  563:                 my @loginimages = ('img','logo','domlogo','login');
  564:                 my $custom_img_count = 0;
  565:                 foreach my $img (@loginimages) {
  566:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  567:                         $custom_img_count ++;
  568:                     }
  569:                 }
  570:                 foreach my $role (@roles) {
  571:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  572:                         $custom_img_count ++;
  573:                     }
  574:                 }
  575:                 if ($custom_img_count > 0) {
  576:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  577:                     my $switch_server = &check_switchserver($dom,$confname);
  578:                     $r->print(
  579:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  580:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  581:     &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 />'.
  582:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  583:                     if ($switch_server) {
  584:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  585:                     }
  586:                     $r->print(&Apache::loncommon::end_page());
  587:                     return OK;
  588:                 }
  589:             }
  590:         }
  591:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  592:     }
  593:     return OK;
  594: }
  595: 
  596: sub process_changes {
  597:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  598:     my %domconfig;
  599:     if (ref($values) eq 'HASH') {
  600:         %domconfig = %{$values};
  601:     }
  602:     my $output;
  603:     if ($action eq 'login') {
  604:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  605:     } elsif ($action eq 'rolecolors') {
  606:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  607:                                      $lastactref,%domconfig);
  608:     } elsif ($action eq 'quotas') {
  609:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  610:     } elsif ($action eq 'autoenroll') {
  611:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  612:     } elsif ($action eq 'autoupdate') {
  613:         $output = &modify_autoupdate($dom,%domconfig);
  614:     } elsif ($action eq 'autocreate') {
  615:         $output = &modify_autocreate($dom,%domconfig);
  616:     } elsif ($action eq 'directorysrch') {
  617:         $output = &modify_directorysrch($dom,%domconfig);
  618:     } elsif ($action eq 'usercreation') {
  619:         $output = &modify_usercreation($dom,%domconfig);
  620:     } elsif ($action eq 'selfcreation') {
  621:         $output = &modify_selfcreation($dom,%domconfig);
  622:     } elsif ($action eq 'usermodification') {
  623:         $output = &modify_usermodification($dom,%domconfig);
  624:     } elsif ($action eq 'contacts') {
  625:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  626:     } elsif ($action eq 'defaults') {
  627:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  628:     } elsif ($action eq 'scantron') {
  629:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  630:     } elsif ($action eq 'coursecategories') {
  631:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  632:     } elsif ($action eq 'serverstatuses') {
  633:         $output = &modify_serverstatuses($dom,%domconfig);
  634:     } elsif ($action eq 'requestcourses') {
  635:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  636:     } elsif ($action eq 'requestauthor') {
  637:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  638:     } elsif ($action eq 'helpsettings') {
  639:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  640:     } elsif ($action eq 'coursedefaults') {
  641:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  642:     } elsif ($action eq 'selfenrollment') {
  643:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  644:     } elsif ($action eq 'usersessions') {
  645:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  646:     } elsif ($action eq 'loadbalancing') {
  647:         $output = &modify_loadbalancing($dom,%domconfig);
  648:     }
  649:     return $output;
  650: }
  651: 
  652: sub print_config_box {
  653:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  654:     my $rowtotal = 0;
  655:     my $output;
  656:     if ($action eq 'coursecategories') {
  657:         $output = &coursecategories_javascript($settings);
  658:     } elsif ($action eq 'defaults') {
  659:         $output = &defaults_javascript($settings); 
  660:     }
  661:     $output .=
  662:          '<table class="LC_nested_outer">
  663:           <tr>
  664:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  665:            &mt($item->{text}).'&nbsp;'.
  666:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  667:           '</tr>';
  668:     $rowtotal ++;
  669:     my $numheaders = 1;
  670:     if (ref($item->{'header'}) eq 'ARRAY') {
  671:         $numheaders = scalar(@{$item->{'header'}});
  672:     }
  673:     if ($numheaders > 1) {
  674:         my $colspan = '';
  675:         my $rightcolspan = '';
  676:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  677:             (($action eq 'login') && ($numheaders < 4))) {
  678:             $colspan = ' colspan="2"';
  679:         }
  680:         if ($action eq 'usersessions') {
  681:             $rightcolspan = ' colspan="3"'; 
  682:         }
  683:         $output .= '
  684:           <tr>
  685:            <td>
  686:             <table class="LC_nested">
  687:              <tr class="LC_info_row">
  688:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  689:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  690:              </tr>';
  691:         $rowtotal ++;
  692:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  693:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  694:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
  695:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  696:         } elsif ($action eq 'coursecategories') {
  697:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  698:         } elsif ($action eq 'login') {
  699:             if ($numheaders == 4) {
  700:                 $colspan = ' colspan="2"';
  701:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  702:             } else {
  703:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  704:             }
  705:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  706:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  707:         } elsif ($action eq 'rolecolors') {
  708:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  709:         }
  710:         $output .= '
  711:            </table>
  712:           </td>
  713:          </tr>
  714:          <tr>
  715:            <td>
  716:             <table class="LC_nested">
  717:              <tr class="LC_info_row">
  718:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  719:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  720:              </tr>';
  721:             $rowtotal ++;
  722:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  723:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  724:             ($action eq 'usersessions') || ($action eq 'coursecategories')) {
  725:             if ($action eq 'coursecategories') {
  726:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  727:                 $colspan = ' colspan="2"';
  728:             } else {
  729:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  730:             }
  731:             $output .= '
  732:            </table>
  733:           </td>
  734:          </tr>
  735:          <tr>
  736:            <td>
  737:             <table class="LC_nested">
  738:              <tr class="LC_info_row">
  739:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  740:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  741:              </tr>'."\n";
  742:             if ($action eq 'coursecategories') {
  743:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  744:             } else {
  745:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  746:             }
  747:             $rowtotal ++;
  748:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  749:                   ($action eq 'defaults')) {
  750:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  751:         } elsif ($action eq 'login') {
  752:             if ($numheaders == 4) {
  753:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  754:            </table>
  755:           </td>
  756:          </tr>
  757:          <tr>
  758:            <td>
  759:             <table class="LC_nested">
  760:              <tr class="LC_info_row">
  761:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  762:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  763:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  764:                 $rowtotal ++;
  765:             } else {
  766:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  767:             }
  768:             $output .= '
  769:            </table>
  770:           </td>
  771:          </tr>
  772:          <tr>
  773:            <td>
  774:             <table class="LC_nested">
  775:              <tr class="LC_info_row">';
  776:             if ($numheaders == 4) {
  777:                 $output .= '
  778:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  779:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  780:              </tr>';
  781:             } else {
  782:                 $output .= '
  783:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  784:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  785:              </tr>';
  786:             }
  787:             $rowtotal ++;
  788:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  789:         } elsif ($action eq 'requestcourses') {
  790:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  791:             $rowtotal ++;
  792:             $output .= &print_studentcode($settings,\$rowtotal).'
  793:            </table>
  794:           </td>
  795:          </tr>
  796:          <tr>
  797:            <td>
  798:             <table class="LC_nested">
  799:              <tr class="LC_info_row">
  800:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  801:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  802:                        &textbookcourses_javascript($settings).
  803:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  804:             </table>
  805:            </td>
  806:           </tr>
  807:          <tr>
  808:            <td>
  809:             <table class="LC_nested">
  810:              <tr class="LC_info_row">
  811:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  812:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  813:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  814:             </table>
  815:            </td>
  816:           </tr>
  817:           <tr>
  818:            <td>
  819:             <table class="LC_nested">
  820:              <tr class="LC_info_row">
  821:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  822:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  823:              </tr>'.
  824:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  825:         } elsif ($action eq 'requestauthor') {
  826:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  827:             $rowtotal ++;
  828:         } elsif ($action eq 'rolecolors') {
  829:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  830:            </table>
  831:           </td>
  832:          </tr>
  833:          <tr>
  834:            <td>
  835:             <table class="LC_nested">
  836:              <tr class="LC_info_row">
  837:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  838:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  839:               <td class="LC_right_item" valign="top">'.
  840:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  841:              </tr>'.
  842:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  843:            </table>
  844:           </td>
  845:          </tr>
  846:          <tr>
  847:            <td>
  848:             <table class="LC_nested">
  849:              <tr class="LC_info_row">
  850:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  851:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  852:              </tr>'.
  853:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  854:             $rowtotal += 2;
  855:         }
  856:     } else {
  857:         $output .= '
  858:           <tr>
  859:            <td>
  860:             <table class="LC_nested">
  861:              <tr class="LC_info_row">';
  862:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  863:             $output .= '  
  864:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  865:         } elsif ($action eq 'serverstatuses') {
  866:             $output .= '
  867:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  868:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  869: 
  870:         } else {
  871:             $output .= '
  872:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  873:         }
  874:         if (defined($item->{'header'}->[0]->{'col3'})) {
  875:             $output .= '<td class="LC_left_item" valign="top">'.
  876:                        &mt($item->{'header'}->[0]->{'col2'});
  877:             if ($action eq 'serverstatuses') {
  878:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  879:             } 
  880:         } else {
  881:             $output .= '<td class="LC_right_item" valign="top">'.
  882:                        &mt($item->{'header'}->[0]->{'col2'});
  883:         }
  884:         $output .= '</td>';
  885:         if ($item->{'header'}->[0]->{'col3'}) {
  886:             if (defined($item->{'header'}->[0]->{'col4'})) {
  887:                 $output .= '<td class="LC_left_item" valign="top">'.
  888:                             &mt($item->{'header'}->[0]->{'col3'});
  889:             } else {
  890:                 $output .= '<td class="LC_right_item" valign="top">'.
  891:                            &mt($item->{'header'}->[0]->{'col3'});
  892:             }
  893:             if ($action eq 'serverstatuses') {
  894:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  895:             }
  896:             $output .= '</td>';
  897:         }
  898:         if ($item->{'header'}->[0]->{'col4'}) {
  899:             $output .= '<td class="LC_right_item" valign="top">'.
  900:                        &mt($item->{'header'}->[0]->{'col4'});
  901:         }
  902:         $output .= '</tr>';
  903:         $rowtotal ++;
  904:         if ($action eq 'quotas') {
  905:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  906:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  907:                  ($action eq 'contacts') || ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  908:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  909:         } elsif ($action eq 'scantron') {
  910:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  911:         } elsif ($action eq 'helpsettings') {
  912:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  913:         }
  914:     }
  915:     $output .= '
  916:    </table>
  917:   </td>
  918:  </tr>
  919: </table><br />';
  920:     return ($output,$rowtotal);
  921: }
  922: 
  923: sub print_login {
  924:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  925:     my ($css_class,$datatable);
  926:     my %choices = &login_choices();
  927: 
  928:     if ($caller eq 'service') {
  929:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  930:         my $choice = $choices{'disallowlogin'};
  931:         $css_class = ' class="LC_odd_row"';
  932:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  933:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  934:                       '<th>'.$choices{'server'}.'</th>'.
  935:                       '<th>'.$choices{'serverpath'}.'</th>'.
  936:                       '<th>'.$choices{'custompath'}.'</th>'.
  937:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  938:         my %disallowed;
  939:         if (ref($settings) eq 'HASH') {
  940:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  941:                %disallowed = %{$settings->{'loginvia'}};
  942:             }
  943:         }
  944:         foreach my $lonhost (sort(keys(%servers))) {
  945:             my $direct = 'selected="selected"';
  946:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  947:                 if ($disallowed{$lonhost}{'server'} ne '') {
  948:                     $direct = '';
  949:                 }
  950:             }
  951:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  952:                           '<td><select name="'.$lonhost.'_server">'.
  953:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  954:                           '</option>';
  955:             foreach my $hostid (sort(keys(%servers))) {
  956:                 next if ($servers{$hostid} eq $servers{$lonhost});
  957:                 my $selected = '';
  958:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  959:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  960:                         $selected = 'selected="selected"';
  961:                     }
  962:                 }
  963:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  964:                               $servers{$hostid}.'</option>';
  965:             }
  966:             $datatable .= '</select></td>'.
  967:                           '<td><select name="'.$lonhost.'_serverpath">';
  968:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  969:                 my $pathname = $path;
  970:                 if ($path eq 'custom') {
  971:                     $pathname = &mt('Custom Path').' ->';
  972:                 }
  973:                 my $selected = '';
  974:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  975:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  976:                         $selected = 'selected="selected"';
  977:                     }
  978:                 } elsif ($path eq '') {
  979:                     $selected = 'selected="selected"';
  980:                 }
  981:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  982:             }
  983:             $datatable .= '</select></td>';
  984:             my ($custom,$exempt);
  985:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  986:                 $custom = $disallowed{$lonhost}{'custompath'};
  987:                 $exempt = $disallowed{$lonhost}{'exempt'};
  988:             }
  989:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  990:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  991:                           '</tr>';
  992:         }
  993:         $datatable .= '</table></td></tr>';
  994:         return $datatable;
  995:     } elsif ($caller eq 'page') {
  996:         my %defaultchecked = ( 
  997:                                'coursecatalog' => 'on',
  998:                                'helpdesk'      => 'on',
  999:                                'adminmail'     => 'off',
 1000:                                'newuser'       => 'off',
 1001:                              );
 1002:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1003:         my (%checkedon,%checkedoff);
 1004:         foreach my $item (@toggles) {
 1005:             if ($defaultchecked{$item} eq 'on') { 
 1006:                 $checkedon{$item} = ' checked="checked" ';
 1007:                 $checkedoff{$item} = ' ';
 1008:             } elsif ($defaultchecked{$item} eq 'off') {
 1009:                 $checkedoff{$item} = ' checked="checked" ';
 1010:                 $checkedon{$item} = ' ';
 1011:             }
 1012:         }
 1013:         my @images = ('img','logo','domlogo','login');
 1014:         my @logintext = ('textcol','bgcol');
 1015:         my @bgs = ('pgbg','mainbg','sidebg');
 1016:         my @links = ('link','alink','vlink');
 1017:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1018:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1019:         my (%is_custom,%designs);
 1020:         my %defaults = (
 1021:                        font => $defaultdesign{'login.font'},
 1022:                        );
 1023:         foreach my $item (@images) {
 1024:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1025:             $defaults{'showlogo'}{$item} = 1;
 1026:         }
 1027:         foreach my $item (@bgs) {
 1028:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1029:         }
 1030:         foreach my $item (@logintext) {
 1031:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1032:         }
 1033:         foreach my $item (@links) {
 1034:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1035:         }
 1036:         if (ref($settings) eq 'HASH') {
 1037:             foreach my $item (@toggles) {
 1038:                 if ($settings->{$item} eq '1') {
 1039:                     $checkedon{$item} =  ' checked="checked" ';
 1040:                     $checkedoff{$item} = ' ';
 1041:                 } elsif ($settings->{$item} eq '0') {
 1042:                     $checkedoff{$item} =  ' checked="checked" ';
 1043:                     $checkedon{$item} = ' ';
 1044:                 }
 1045:             }
 1046:             foreach my $item (@images) {
 1047:                 if (defined($settings->{$item})) {
 1048:                     $designs{$item} = $settings->{$item};
 1049:                     $is_custom{$item} = 1;
 1050:                 }
 1051:                 if (defined($settings->{'showlogo'}{$item})) {
 1052:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1053:                 }
 1054:             }
 1055:             foreach my $item (@logintext) {
 1056:                 if ($settings->{$item} ne '') {
 1057:                     $designs{'logintext'}{$item} = $settings->{$item};
 1058:                     $is_custom{$item} = 1;
 1059:                 }
 1060:             }
 1061:             if ($settings->{'font'} ne '') {
 1062:                 $designs{'font'} = $settings->{'font'};
 1063:                 $is_custom{'font'} = 1;
 1064:             }
 1065:             foreach my $item (@bgs) {
 1066:                 if ($settings->{$item} ne '') {
 1067:                     $designs{'bgs'}{$item} = $settings->{$item};
 1068:                     $is_custom{$item} = 1;
 1069:                 }
 1070:             }
 1071:             foreach my $item (@links) {
 1072:                 if ($settings->{$item} ne '') {
 1073:                     $designs{'links'}{$item} = $settings->{$item};
 1074:                     $is_custom{$item} = 1;
 1075:                 }
 1076:             }
 1077:         } else {
 1078:             if ($designhash{$dom.'.login.font'} ne '') {
 1079:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1080:                 $is_custom{'font'} = 1;
 1081:             }
 1082:             foreach my $item (@images) {
 1083:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1084:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1085:                     $is_custom{$item} = 1;
 1086:                 }
 1087:             }
 1088:             foreach my $item (@bgs) {
 1089:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1090:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1091:                     $is_custom{$item} = 1;
 1092:                 }
 1093:             }
 1094:             foreach my $item (@links) {
 1095:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1096:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1097:                     $is_custom{$item} = 1;
 1098:                 }
 1099:             }
 1100:         }
 1101:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1102:                                                       logo => 'Institution Logo',
 1103:                                                       domlogo => 'Domain Logo',
 1104:                                                       login => 'Login box');
 1105:         my $itemcount = 1;
 1106:         foreach my $item (@toggles) {
 1107:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1108:             $datatable .=  
 1109:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1110:                 '</td><td>'.
 1111:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1112:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1113:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1114:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1115:                 '</tr>';
 1116:             $itemcount ++;
 1117:         }
 1118:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1119:         $datatable .= '</tr></table></td></tr>';
 1120:     } elsif ($caller eq 'help') {
 1121:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1122:         my $switchserver = &check_switchserver($dom,$confname);
 1123:         my $itemcount = 1;
 1124:         $defaulturl = '/adm/loginproblems.html';
 1125:         $defaulttype = 'default';
 1126:         %lt = &Apache::lonlocal::texthash (
 1127:                      del     => 'Delete?',
 1128:                      rep     => 'Replace:',
 1129:                      upl     => 'Upload:',
 1130:                      default => 'Default',
 1131:                      custom  => 'Custom',
 1132:                                              );
 1133:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1134:         my @currlangs;
 1135:         if (ref($settings) eq 'HASH') {
 1136:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1137:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1138:                     next if ($settings->{'helpurl'}{$key} eq '');
 1139:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1140:                     $type{$key} = 'custom';
 1141:                     unless ($key eq 'nolang') {
 1142:                         push(@currlangs,$key);
 1143:                     }
 1144:                 }
 1145:             } elsif ($settings->{'helpurl'} ne '') {
 1146:                 $type{'nolang'} = 'custom';
 1147:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1148:             }
 1149:         }
 1150:         foreach my $lang ('nolang',sort(@currlangs)) {
 1151:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1152:             $datatable .= '<tr'.$css_class.'>';
 1153:             if ($url{$lang} eq '') {
 1154:                 $url{$lang} = $defaulturl;
 1155:             }
 1156:             if ($type{$lang} eq '') {
 1157:                 $type{$lang} = $defaulttype;
 1158:             }
 1159:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1160:             if ($lang eq 'nolang') {
 1161:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1162:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1163:             } else {
 1164:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1165:                                   $langchoices{$lang},
 1166:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1167:             }
 1168:             $datatable .= '</span></td>'."\n".
 1169:                           '<td class="LC_left_item">';
 1170:             if ($type{$lang} eq 'custom') {
 1171:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1172:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1173:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1174:             } else {
 1175:                 $datatable .= $lt{'upl'};
 1176:             }
 1177:             $datatable .='<br />';
 1178:             if ($switchserver) {
 1179:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1180:             } else {
 1181:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1182:             }
 1183:             $datatable .= '</td></tr>';
 1184:             $itemcount ++;
 1185:         }
 1186:         my @addlangs;
 1187:         foreach my $lang (sort(keys(%langchoices))) {
 1188:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1189:             push(@addlangs,$lang);
 1190:         }
 1191:         if (@addlangs > 0) {
 1192:             my %toadd;
 1193:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1194:             $toadd{''} = &mt('Select');
 1195:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1196:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1197:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1198:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1199:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1200:             if ($switchserver) {
 1201:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1202:             } else {
 1203:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1204:             }
 1205:             $datatable .= '</td></tr>';
 1206:             $itemcount ++;
 1207:         }
 1208:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1209:     } elsif ($caller eq 'headtag') {
 1210:         my %domservers = &Apache::lonnet::get_servers($dom);
 1211:         my $choice = $choices{'headtag'};
 1212:         $css_class = ' class="LC_odd_row"';
 1213:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1214:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1215:                       '<th>'.$choices{'current'}.'</th>'.
 1216:                       '<th>'.$choices{'action'}.'</th>'.
 1217:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1218:         my (%currurls,%currexempt);
 1219:         if (ref($settings) eq 'HASH') {
 1220:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1221:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1222:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1223:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1224:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1225:                     }
 1226:                 }
 1227:             }
 1228:         }
 1229:         my %lt = &Apache::lonlocal::texthash(
 1230:                                                del  => 'Delete?',
 1231:                                                rep  => 'Replace:',
 1232:                                                upl  => 'Upload:',
 1233:                                                curr => 'View contents',
 1234:                                                none => 'None',
 1235:         );
 1236:         my $switchserver = &check_switchserver($dom,$confname);
 1237:         foreach my $lonhost (sort(keys(%domservers))) {
 1238:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1239:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1240:             if ($currurls{$lonhost}) {
 1241:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1242:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1243:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1244:                               '">'.$lt{'curr'}.'</a></td>'.
 1245:                               '<td><span class="LC_nobreak"><label>'.
 1246:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1247:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1248:             } else {
 1249:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1250:             }
 1251:             $datatable .='<br />';
 1252:             if ($switchserver) {
 1253:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1254:             } else {
 1255:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1256:             }
 1257:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1258:         }
 1259:         $datatable .= '</table></td></tr>';
 1260:     }
 1261:     return $datatable;
 1262: }
 1263: 
 1264: sub login_choices {
 1265:     my %choices =
 1266:         &Apache::lonlocal::texthash (
 1267:             coursecatalog => 'Display Course/Community Catalog link?',
 1268:             adminmail     => "Display Administrator's E-mail Address?",
 1269:             helpdesk      => 'Display "Contact Helpdesk" link',
 1270:             disallowlogin => "Login page requests redirected",
 1271:             hostid        => "Server",
 1272:             server        => "Redirect to:",
 1273:             serverpath    => "Path",
 1274:             custompath    => "Custom", 
 1275:             exempt        => "Exempt IP(s)",
 1276:             directlogin   => "No redirect",
 1277:             newuser       => "Link to create a user account",
 1278:             img           => "Header",
 1279:             logo          => "Main Logo",
 1280:             domlogo       => "Domain Logo",
 1281:             login         => "Log-in Header", 
 1282:             textcol       => "Text color",
 1283:             bgcol         => "Box color",
 1284:             bgs           => "Background colors",
 1285:             links         => "Link colors",
 1286:             font          => "Font color",
 1287:             pgbg          => "Header",
 1288:             mainbg        => "Page",
 1289:             sidebg        => "Login box",
 1290:             link          => "Link",
 1291:             alink         => "Active link",
 1292:             vlink         => "Visited link",
 1293:             headtag       => "Custom markup",
 1294:             action        => "Action",
 1295:             current       => "Current",
 1296:         );
 1297:     return %choices;
 1298: }
 1299: 
 1300: sub print_rolecolors {
 1301:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1302:     my %choices = &color_font_choices();
 1303:     my @bgs = ('pgbg','tabbg','sidebg');
 1304:     my @links = ('link','alink','vlink');
 1305:     my @images = ('img');
 1306:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1307:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1308:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1309:     my (%is_custom,%designs);
 1310:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1311:     if (ref($settings) eq 'HASH') {
 1312:         if (ref($settings->{$role}) eq 'HASH') {
 1313:             if ($settings->{$role}->{'img'} ne '') {
 1314:                 $designs{'img'} = $settings->{$role}->{'img'};
 1315:                 $is_custom{'img'} = 1;
 1316:             }
 1317:             if ($settings->{$role}->{'font'} ne '') {
 1318:                 $designs{'font'} = $settings->{$role}->{'font'};
 1319:                 $is_custom{'font'} = 1;
 1320:             }
 1321:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1322:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1323:                 $is_custom{'fontmenu'} = 1;
 1324:             }
 1325:             foreach my $item (@bgs) {
 1326:                 if ($settings->{$role}->{$item} ne '') {
 1327:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1328:                     $is_custom{$item} = 1;
 1329:                 }
 1330:             }
 1331:             foreach my $item (@links) {
 1332:                 if ($settings->{$role}->{$item} ne '') {
 1333:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1334:                     $is_custom{$item} = 1;
 1335:                 }
 1336:             }
 1337:         }
 1338:     } else {
 1339:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1340:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1341:             $is_custom{'img'} = 1;
 1342:         }
 1343:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1344:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1345:             $is_custom{'fontmenu'} = 1; 
 1346:         }
 1347:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1348:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1349:             $is_custom{'font'} = 1;
 1350:         }
 1351:         foreach my $item (@bgs) {
 1352:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1353:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1354:                 $is_custom{$item} = 1;
 1355:             
 1356:             }
 1357:         }
 1358:         foreach my $item (@links) {
 1359:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1360:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1361:                 $is_custom{$item} = 1;
 1362:             }
 1363:         }
 1364:     }
 1365:     my $itemcount = 1;
 1366:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1367:     $datatable .= '</tr></table></td></tr>';
 1368:     return $datatable;
 1369: }
 1370: 
 1371: sub role_defaults {
 1372:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1373:     my %defaults;
 1374:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1375:         return %defaults;
 1376:     }
 1377:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1378:     if ($role eq 'login') {
 1379:         %defaults = (
 1380:                        font => $defaultdesign{$role.'.font'},
 1381:                     );
 1382:         if (ref($logintext) eq 'ARRAY') {
 1383:             foreach my $item (@{$logintext}) {
 1384:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1385:             }
 1386:         }
 1387:         foreach my $item (@{$images}) {
 1388:             $defaults{'showlogo'}{$item} = 1;
 1389:         }
 1390:     } else {
 1391:         %defaults = (
 1392:                        img => $defaultdesign{$role.'.img'},
 1393:                        font => $defaultdesign{$role.'.font'},
 1394:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1395:                     );
 1396:     }
 1397:     foreach my $item (@{$bgs}) {
 1398:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1399:     }
 1400:     foreach my $item (@{$links}) {
 1401:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1402:     }
 1403:     foreach my $item (@{$images}) {
 1404:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1405:     }
 1406:     return %defaults;
 1407: }
 1408: 
 1409: sub display_color_options {
 1410:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1411:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1412:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1413:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1414:     my $datatable = '<tr'.$css_class.'>'.
 1415:         '<td>'.$choices->{'font'}.'</td>';
 1416:     if (!$is_custom->{'font'}) {
 1417:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1418:     } else {
 1419:         $datatable .= '<td>&nbsp;</td>';
 1420:     }
 1421:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1422: 
 1423:     $datatable .= '<td><span class="LC_nobreak">'.
 1424:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1425:                   ' value="'.$current_color.'" />&nbsp;'.
 1426:                   '&nbsp;</td></tr>';
 1427:     unless ($role eq 'login') { 
 1428:         $datatable .= '<tr'.$css_class.'>'.
 1429:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1430:         if (!$is_custom->{'fontmenu'}) {
 1431:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1432:         } else {
 1433:             $datatable .= '<td>&nbsp;</td>';
 1434:         }
 1435: 	$current_color = $designs->{'fontmenu'} ?
 1436: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1437:         $datatable .= '<td><span class="LC_nobreak">'.
 1438:                       '<input class="colorchooser" type="text" size="10" name="'
 1439: 		      .$role.'_fontmenu"'.
 1440:                       ' value="'.$current_color.'" />&nbsp;'.
 1441:                       '&nbsp;</td></tr>';
 1442:     }
 1443:     my $switchserver = &check_switchserver($dom,$confname);
 1444:     foreach my $img (@{$images}) {
 1445: 	$itemcount ++;
 1446:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1447:         $datatable .= '<tr'.$css_class.'>'.
 1448:                       '<td>'.$choices->{$img};
 1449:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1450:         if ($role eq 'login') {
 1451:             if ($img eq 'login') {
 1452:                 $login_hdr_pick =
 1453:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1454:                 $logincolors =
 1455:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1456:                                        $designs,$defaults);
 1457:             } elsif ($img ne 'domlogo') {
 1458:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1459:             }
 1460:         }
 1461:         $datatable .= '</td>';
 1462:         if ($designs->{$img} ne '') {
 1463:             $imgfile = $designs->{$img};
 1464: 	    $img_import = ($imgfile =~ m{^/adm/});
 1465:         } else {
 1466:             $imgfile = $defaults->{$img};
 1467:         }
 1468:         if ($imgfile) {
 1469:             my ($showfile,$fullsize);
 1470:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1471:                 my $urldir = $1;
 1472:                 my $filename = $2;
 1473:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1474:                 if (@info) {
 1475:                     my $thumbfile = 'tn-'.$filename;
 1476:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1477:                     if (@thumb) {
 1478:                         $showfile = $urldir.'/'.$thumbfile;
 1479:                     } else {
 1480:                         $showfile = $imgfile;
 1481:                     }
 1482:                 } else {
 1483:                     $showfile = '';
 1484:                 }
 1485:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1486:                 $showfile = $imgfile;
 1487:                 my $imgdir = $1;
 1488:                 my $filename = $2;
 1489:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1490:                     $showfile = "/$imgdir/tn-".$filename;
 1491:                 } else {
 1492:                     my $input = $londocroot.$imgfile;
 1493:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1494:                     if (!-e $output) {
 1495:                         my ($width,$height) = &thumb_dimensions();
 1496:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1497:                         if ($fullwidth ne '' && $fullheight ne '') {
 1498:                             if ($fullwidth > $width && $fullheight > $height) { 
 1499:                                 my $size = $width.'x'.$height;
 1500:                                 system("convert -sample $size $input $output");
 1501:                                 $showfile = "/$imgdir/tn-".$filename;
 1502:                             }
 1503:                         }
 1504:                     }
 1505:                 }
 1506:             }
 1507:             if ($showfile) {
 1508:                 if ($showfile =~ m{^/(adm|res)/}) {
 1509:                     if ($showfile =~ m{^/res/}) {
 1510:                         my $local_showfile =
 1511:                             &Apache::lonnet::filelocation('',$showfile);
 1512:                         &Apache::lonnet::repcopy($local_showfile);
 1513:                     }
 1514:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1515:                 }
 1516:                 if ($imgfile) {
 1517:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1518:                         if ($imgfile =~ m{^/res/}) {
 1519:                             my $local_imgfile =
 1520:                                 &Apache::lonnet::filelocation('',$imgfile);
 1521:                             &Apache::lonnet::repcopy($local_imgfile);
 1522:                         }
 1523:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1524:                     } else {
 1525:                         $fullsize = $imgfile;
 1526:                     }
 1527:                 }
 1528:                 $datatable .= '<td>';
 1529:                 if ($img eq 'login') {
 1530:                     $datatable .= $login_hdr_pick;
 1531:                 } 
 1532:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1533:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1534:             } else {
 1535:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1536:                               &mt('Upload:').'<br />';
 1537:             }
 1538:         } else {
 1539:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1540:                           &mt('Upload:').'<br />';
 1541:         }
 1542:         if ($switchserver) {
 1543:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1544:         } else {
 1545:             if ($img ne 'login') { # suppress file selection for Log-in header
 1546:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1547:             }
 1548:         }
 1549:         $datatable .= '</td></tr>';
 1550:     }
 1551:     $itemcount ++;
 1552:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1553:     $datatable .= '<tr'.$css_class.'>'.
 1554:                   '<td>'.$choices->{'bgs'}.'</td>';
 1555:     my $bgs_def;
 1556:     foreach my $item (@{$bgs}) {
 1557:         if (!$is_custom->{$item}) {
 1558:             $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>';
 1559:         }
 1560:     }
 1561:     if ($bgs_def) {
 1562:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1563:     } else {
 1564:         $datatable .= '<td>&nbsp;</td>';
 1565:     }
 1566:     $datatable .= '<td class="LC_right_item">'.
 1567:                   '<table border="0"><tr>';
 1568: 
 1569:     foreach my $item (@{$bgs}) {
 1570:         $datatable .= '<td align="center">'.$choices->{$item};
 1571: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1572:         if ($designs->{'bgs'}{$item}) {
 1573:             $datatable .= '&nbsp;';
 1574:         }
 1575:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1576:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1577:     }
 1578:     $datatable .= '</tr></table></td></tr>';
 1579:     $itemcount ++;
 1580:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1581:     $datatable .= '<tr'.$css_class.'>'.
 1582:                   '<td>'.$choices->{'links'}.'</td>';
 1583:     my $links_def;
 1584:     foreach my $item (@{$links}) {
 1585:         if (!$is_custom->{$item}) {
 1586:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1587:         }
 1588:     }
 1589:     if ($links_def) {
 1590:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1591:     } else {
 1592:         $datatable .= '<td>&nbsp;</td>';
 1593:     }
 1594:     $datatable .= '<td class="LC_right_item">'.
 1595:                   '<table border="0"><tr>';
 1596:     foreach my $item (@{$links}) {
 1597: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1598:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1599:         if ($designs->{'links'}{$item}) {
 1600:             $datatable.='&nbsp;';
 1601:         }
 1602:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1603:                       '" /></td>';
 1604:     }
 1605:     $$rowtotal += $itemcount;
 1606:     return $datatable;
 1607: }
 1608: 
 1609: sub logo_display_options {
 1610:     my ($img,$defaults,$designs) = @_;
 1611:     my $checkedon;
 1612:     if (ref($defaults) eq 'HASH') {
 1613:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1614:             if ($defaults->{'showlogo'}{$img}) {
 1615:                 $checkedon = 'checked="checked" ';     
 1616:             }
 1617:         } 
 1618:     }
 1619:     if (ref($designs) eq 'HASH') {
 1620:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1621:             if (defined($designs->{'showlogo'}{$img})) {
 1622:                 if ($designs->{'showlogo'}{$img} == 0) {
 1623:                     $checkedon = '';
 1624:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1625:                     $checkedon = 'checked="checked" ';
 1626:                 }
 1627:             }
 1628:         }
 1629:     }
 1630:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1631:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1632:            &mt('show').'</label>'."\n";
 1633: }
 1634: 
 1635: sub login_header_options  {
 1636:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1637:     my $output = '';
 1638:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1639:         $output .= &mt('Text default(s):').'<br />';
 1640:         if (!$is_custom->{'textcol'}) {
 1641:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1642:                        '&nbsp;&nbsp;&nbsp;';
 1643:         }
 1644:         if (!$is_custom->{'bgcol'}) {
 1645:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1646:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1647:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1648:         }
 1649:         $output .= '<br />';
 1650:     }
 1651:     $output .='<br />';
 1652:     return $output;
 1653: }
 1654: 
 1655: sub login_text_colors {
 1656:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1657:     my $color_menu = '<table border="0"><tr>';
 1658:     foreach my $item (@{$logintext}) {
 1659:         $color_menu .= '<td align="center">'.$choices->{$item};
 1660:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1661:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1662:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1663:     }
 1664:     $color_menu .= '</tr></table><br />';
 1665:     return $color_menu;
 1666: }
 1667: 
 1668: sub image_changes {
 1669:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1670:     my $output;
 1671:     if ($img eq 'login') {
 1672:             # suppress image for Log-in header
 1673:     } elsif (!$is_custom) {
 1674:         if ($img ne 'domlogo') {
 1675:             $output .= &mt('Default image:').'<br />';
 1676:         } else {
 1677:             $output .= &mt('Default in use:').'<br />';
 1678:         }
 1679:     }
 1680:     if ($img eq 'login') { # suppress image for Log-in header
 1681:         $output .= '<td>'.$logincolors;
 1682:     } else {
 1683:         if ($img_import) {
 1684:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1685:         }
 1686:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1687:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1688:         if ($is_custom) {
 1689:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1690:                        '<input type="checkbox" name="'.
 1691:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1692:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1693:         } else {
 1694:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1695:         }
 1696:     }
 1697:     return $output;
 1698: }
 1699: 
 1700: sub print_quotas {
 1701:     my ($dom,$settings,$rowtotal,$action) = @_;
 1702:     my $context;
 1703:     if ($action eq 'quotas') {
 1704:         $context = 'tools';
 1705:     } else {
 1706:         $context = $action;
 1707:     }
 1708:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1709:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1710:     my $typecount = 0;
 1711:     my ($css_class,%titles);
 1712:     if ($context eq 'requestcourses') {
 1713:         @usertools = ('official','unofficial','community','textbook');
 1714:         @options =('norequest','approval','validate','autolimit');
 1715:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1716:         %titles = &courserequest_titles();
 1717:     } elsif ($context eq 'requestauthor') {
 1718:         @usertools = ('author');
 1719:         @options = ('norequest','approval','automatic');
 1720:         %titles = &authorrequest_titles();
 1721:     } else {
 1722:         @usertools = ('aboutme','blog','webdav','portfolio');
 1723:         %titles = &tool_titles();
 1724:     }
 1725:     if (ref($types) eq 'ARRAY') {
 1726:         foreach my $type (@{$types}) {
 1727:             my ($currdefquota,$currauthorquota);
 1728:             unless (($context eq 'requestcourses') ||
 1729:                     ($context eq 'requestauthor')) {
 1730:                 if (ref($settings) eq 'HASH') {
 1731:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1732:                         $currdefquota = $settings->{defaultquota}->{$type};
 1733:                     } else {
 1734:                         $currdefquota = $settings->{$type};
 1735:                     }
 1736:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1737:                         $currauthorquota = $settings->{authorquota}->{$type};
 1738:                     }
 1739:                 }
 1740:             }
 1741:             if (defined($usertypes->{$type})) {
 1742:                 $typecount ++;
 1743:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1744:                 $datatable .= '<tr'.$css_class.'>'.
 1745:                               '<td>'.$usertypes->{$type}.'</td>'.
 1746:                               '<td class="LC_left_item">';
 1747:                 if ($context eq 'requestcourses') {
 1748:                     $datatable .= '<table><tr>';
 1749:                 }
 1750:                 my %cell;  
 1751:                 foreach my $item (@usertools) {
 1752:                     if ($context eq 'requestcourses') {
 1753:                         my ($curroption,$currlimit);
 1754:                         if (ref($settings) eq 'HASH') {
 1755:                             if (ref($settings->{$item}) eq 'HASH') {
 1756:                                 $curroption = $settings->{$item}->{$type};
 1757:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1758:                                     $currlimit = $1; 
 1759:                                 }
 1760:                             }
 1761:                         }
 1762:                         if (!$curroption) {
 1763:                             $curroption = 'norequest';
 1764:                         }
 1765:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1766:                         foreach my $option (@options) {
 1767:                             my $val = $option;
 1768:                             if ($option eq 'norequest') {
 1769:                                 $val = 0;  
 1770:                             }
 1771:                             if ($option eq 'validate') {
 1772:                                 my $canvalidate = 0;
 1773:                                 if (ref($validations{$item}) eq 'HASH') { 
 1774:                                     if ($validations{$item}{$type}) {
 1775:                                         $canvalidate = 1;
 1776:                                     }
 1777:                                 }
 1778:                                 next if (!$canvalidate);
 1779:                             }
 1780:                             my $checked = '';
 1781:                             if ($option eq $curroption) {
 1782:                                 $checked = ' checked="checked"';
 1783:                             } elsif ($option eq 'autolimit') {
 1784:                                 if ($curroption =~ /^autolimit/) {
 1785:                                     $checked = ' checked="checked"';
 1786:                                 }                       
 1787:                             } 
 1788:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1789:                                   '<input type="radio" name="crsreq_'.$item.
 1790:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1791:                                   $titles{$option}.'</label>';
 1792:                             if ($option eq 'autolimit') {
 1793:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1794:                                                 $item.'_limit_'.$type.'" size="1" '.
 1795:                                                 'value="'.$currlimit.'" />';
 1796:                             }
 1797:                             $cell{$item} .= '</span> ';
 1798:                             if ($option eq 'autolimit') {
 1799:                                 $cell{$item} .= $titles{'unlimited'};
 1800:                             }
 1801:                         }
 1802:                     } elsif ($context eq 'requestauthor') {
 1803:                         my $curroption;
 1804:                         if (ref($settings) eq 'HASH') {
 1805:                             $curroption = $settings->{$type};
 1806:                         }
 1807:                         if (!$curroption) {
 1808:                             $curroption = 'norequest';
 1809:                         }
 1810:                         foreach my $option (@options) {
 1811:                             my $val = $option;
 1812:                             if ($option eq 'norequest') {
 1813:                                 $val = 0;
 1814:                             }
 1815:                             my $checked = '';
 1816:                             if ($option eq $curroption) {
 1817:                                 $checked = ' checked="checked"';
 1818:                             }
 1819:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1820:                                   '<input type="radio" name="authorreq_'.$type.
 1821:                                   '" value="'.$val.'"'.$checked.' />'.
 1822:                                   $titles{$option}.'</label></span>&nbsp; ';
 1823:                         }
 1824:                     } else {
 1825:                         my $checked = 'checked="checked" ';
 1826:                         if (ref($settings) eq 'HASH') {
 1827:                             if (ref($settings->{$item}) eq 'HASH') {
 1828:                                 if ($settings->{$item}->{$type} == 0) {
 1829:                                     $checked = '';
 1830:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1831:                                     $checked =  'checked="checked" ';
 1832:                                 }
 1833:                             }
 1834:                         }
 1835:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1836:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1837:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1838:                                       '</label></span>&nbsp; ';
 1839:                     }
 1840:                 }
 1841:                 if ($context eq 'requestcourses') {
 1842:                     $datatable .= '</tr><tr>';
 1843:                     foreach my $item (@usertools) {
 1844:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1845:                     }
 1846:                     $datatable .= '</tr></table>';
 1847:                 }
 1848:                 $datatable .= '</td>';
 1849:                 unless (($context eq 'requestcourses') ||
 1850:                         ($context eq 'requestauthor')) {
 1851:                     $datatable .= 
 1852:                               '<td class="LC_right_item">'.
 1853:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1854:                               '<input type="text" name="quota_'.$type.
 1855:                               '" value="'.$currdefquota.
 1856:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1857:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1858:                               '<input type="text" name="authorquota_'.$type.
 1859:                               '" value="'.$currauthorquota.
 1860:                               '" size="5" /></span></td>';
 1861:                 }
 1862:                 $datatable .= '</tr>';
 1863:             }
 1864:         }
 1865:     }
 1866:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1867:         $defaultquota = '20';
 1868:         $authorquota = '500';
 1869:         if (ref($settings) eq 'HASH') {
 1870:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1871:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1872:             } elsif (defined($settings->{'default'})) {
 1873:                 $defaultquota = $settings->{'default'};
 1874:             }
 1875:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1876:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1877:             }
 1878:         }
 1879:     }
 1880:     $typecount ++;
 1881:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1882:     $datatable .= '<tr'.$css_class.'>'.
 1883:                   '<td>'.$othertitle.'</td>'.
 1884:                   '<td class="LC_left_item">';
 1885:     if ($context eq 'requestcourses') {
 1886:         $datatable .= '<table><tr>';
 1887:     }
 1888:     my %defcell;
 1889:     foreach my $item (@usertools) {
 1890:         if ($context eq 'requestcourses') {
 1891:             my ($curroption,$currlimit);
 1892:             if (ref($settings) eq 'HASH') {
 1893:                 if (ref($settings->{$item}) eq 'HASH') {
 1894:                     $curroption = $settings->{$item}->{'default'};
 1895:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1896:                         $currlimit = $1;
 1897:                     }
 1898:                 }
 1899:             }
 1900:             if (!$curroption) {
 1901:                 $curroption = 'norequest';
 1902:             }
 1903:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1904:             foreach my $option (@options) {
 1905:                 my $val = $option;
 1906:                 if ($option eq 'norequest') {
 1907:                     $val = 0;
 1908:                 }
 1909:                 if ($option eq 'validate') {
 1910:                     my $canvalidate = 0;
 1911:                     if (ref($validations{$item}) eq 'HASH') {
 1912:                         if ($validations{$item}{'default'}) {
 1913:                             $canvalidate = 1;
 1914:                         }
 1915:                     }
 1916:                     next if (!$canvalidate);
 1917:                 }
 1918:                 my $checked = '';
 1919:                 if ($option eq $curroption) {
 1920:                     $checked = ' checked="checked"';
 1921:                 } elsif ($option eq 'autolimit') {
 1922:                     if ($curroption =~ /^autolimit/) {
 1923:                         $checked = ' checked="checked"';
 1924:                     }
 1925:                 }
 1926:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1927:                                   '<input type="radio" name="crsreq_'.$item.
 1928:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1929:                                   $titles{$option}.'</label>';
 1930:                 if ($option eq 'autolimit') {
 1931:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1932:                                        $item.'_limit_default" size="1" '.
 1933:                                        'value="'.$currlimit.'" />';
 1934:                 }
 1935:                 $defcell{$item} .= '</span> ';
 1936:                 if ($option eq 'autolimit') {
 1937:                     $defcell{$item} .= $titles{'unlimited'};
 1938:                 }
 1939:             }
 1940:         } elsif ($context eq 'requestauthor') {
 1941:             my $curroption;
 1942:             if (ref($settings) eq 'HASH') {
 1943:                 $curroption = $settings->{'default'};
 1944:             }
 1945:             if (!$curroption) {
 1946:                 $curroption = 'norequest';
 1947:             }
 1948:             foreach my $option (@options) {
 1949:                 my $val = $option;
 1950:                 if ($option eq 'norequest') {
 1951:                     $val = 0;
 1952:                 }
 1953:                 my $checked = '';
 1954:                 if ($option eq $curroption) {
 1955:                     $checked = ' checked="checked"';
 1956:                 }
 1957:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1958:                               '<input type="radio" name="authorreq_default"'.
 1959:                               ' value="'.$val.'"'.$checked.' />'.
 1960:                               $titles{$option}.'</label></span>&nbsp; ';
 1961:             }
 1962:         } else {
 1963:             my $checked = 'checked="checked" ';
 1964:             if (ref($settings) eq 'HASH') {
 1965:                 if (ref($settings->{$item}) eq 'HASH') {
 1966:                     if ($settings->{$item}->{'default'} == 0) {
 1967:                         $checked = '';
 1968:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1969:                         $checked = 'checked="checked" ';
 1970:                     }
 1971:                 }
 1972:             }
 1973:             $datatable .= '<span class="LC_nobreak"><label>'.
 1974:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1975:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1976:                           '</label></span>&nbsp; ';
 1977:         }
 1978:     }
 1979:     if ($context eq 'requestcourses') {
 1980:         $datatable .= '</tr><tr>';
 1981:         foreach my $item (@usertools) {
 1982:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1983:         }
 1984:         $datatable .= '</tr></table>';
 1985:     }
 1986:     $datatable .= '</td>';
 1987:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1988:         $datatable .= '<td class="LC_right_item">'.
 1989:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1990:                       '<input type="text" name="defaultquota" value="'.
 1991:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1992:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1993:                       '<input type="text" name="authorquota" value="'.
 1994:                       $authorquota.'" size="5" /></span></td>';
 1995:     }
 1996:     $datatable .= '</tr>';
 1997:     $typecount ++;
 1998:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1999:     $datatable .= '<tr'.$css_class.'>'.
 2000:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2001:     if ($context eq 'requestcourses') {
 2002:         $datatable .= &mt('(overrides affiliation, if set)').
 2003:                       '</td>'.
 2004:                       '<td class="LC_left_item">'.
 2005:                       '<table><tr>';
 2006:     } else {
 2007:         $datatable .= &mt('(overrides affiliation, if checked)').
 2008:                       '</td>'.
 2009:                       '<td class="LC_left_item" colspan="2">'.
 2010:                       '<br />';
 2011:     }
 2012:     my %advcell;
 2013:     foreach my $item (@usertools) {
 2014:         if ($context eq 'requestcourses') {
 2015:             my ($curroption,$currlimit);
 2016:             if (ref($settings) eq 'HASH') {
 2017:                 if (ref($settings->{$item}) eq 'HASH') {
 2018:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2019:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2020:                         $currlimit = $1;
 2021:                     }
 2022:                 }
 2023:             }
 2024:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2025:             my $checked = '';
 2026:             if ($curroption eq '') {
 2027:                 $checked = ' checked="checked"';
 2028:             }
 2029:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2030:                                '<input type="radio" name="crsreq_'.$item.
 2031:                                '__LC_adv" value=""'.$checked.' />'.
 2032:                                &mt('No override set').'</label></span>&nbsp; ';
 2033:             foreach my $option (@options) {
 2034:                 my $val = $option;
 2035:                 if ($option eq 'norequest') {
 2036:                     $val = 0;
 2037:                 }
 2038:                 if ($option eq 'validate') {
 2039:                     my $canvalidate = 0;
 2040:                     if (ref($validations{$item}) eq 'HASH') {
 2041:                         if ($validations{$item}{'_LC_adv'}) {
 2042:                             $canvalidate = 1;
 2043:                         }
 2044:                     }
 2045:                     next if (!$canvalidate);
 2046:                 }
 2047:                 my $checked = '';
 2048:                 if ($val eq $curroption) {
 2049:                     $checked = ' checked="checked"';
 2050:                 } elsif ($option eq 'autolimit') {
 2051:                     if ($curroption =~ /^autolimit/) {
 2052:                         $checked = ' checked="checked"';
 2053:                     }
 2054:                 }
 2055:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2056:                                   '<input type="radio" name="crsreq_'.$item.
 2057:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2058:                                   $titles{$option}.'</label>';
 2059:                 if ($option eq 'autolimit') {
 2060:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2061:                                        $item.'_limit__LC_adv" size="1" '.
 2062:                                        'value="'.$currlimit.'" />';
 2063:                 }
 2064:                 $advcell{$item} .= '</span> ';
 2065:                 if ($option eq 'autolimit') {
 2066:                     $advcell{$item} .= $titles{'unlimited'};
 2067:                 }
 2068:             }
 2069:         } elsif ($context eq 'requestauthor') {
 2070:             my $curroption;
 2071:             if (ref($settings) eq 'HASH') {
 2072:                 $curroption = $settings->{'_LC_adv'};
 2073:             }
 2074:             my $checked = '';
 2075:             if ($curroption eq '') {
 2076:                 $checked = ' checked="checked"';
 2077:             }
 2078:             $datatable .= '<span class="LC_nobreak"><label>'.
 2079:                           '<input type="radio" name="authorreq__LC_adv"'.
 2080:                           ' value=""'.$checked.' />'.
 2081:                           &mt('No override set').'</label></span>&nbsp; ';
 2082:             foreach my $option (@options) {
 2083:                 my $val = $option;
 2084:                 if ($option eq 'norequest') {
 2085:                     $val = 0;
 2086:                 }
 2087:                 my $checked = '';
 2088:                 if ($val eq $curroption) {
 2089:                     $checked = ' checked="checked"';
 2090:                 }
 2091:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2092:                               '<input type="radio" name="authorreq__LC_adv"'.
 2093:                               ' value="'.$val.'"'.$checked.' />'.
 2094:                               $titles{$option}.'</label></span>&nbsp; ';
 2095:             }
 2096:         } else {
 2097:             my $checked = 'checked="checked" ';
 2098:             if (ref($settings) eq 'HASH') {
 2099:                 if (ref($settings->{$item}) eq 'HASH') {
 2100:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2101:                         $checked = '';
 2102:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2103:                         $checked = 'checked="checked" ';
 2104:                     }
 2105:                 }
 2106:             }
 2107:             $datatable .= '<span class="LC_nobreak"><label>'.
 2108:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2109:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2110:                           '</label></span>&nbsp; ';
 2111:         }
 2112:     }
 2113:     if ($context eq 'requestcourses') {
 2114:         $datatable .= '</tr><tr>';
 2115:         foreach my $item (@usertools) {
 2116:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2117:         }
 2118:         $datatable .= '</tr></table>';
 2119:     }
 2120:     $datatable .= '</td></tr>';
 2121:     $$rowtotal += $typecount;
 2122:     return $datatable;
 2123: }
 2124: 
 2125: sub print_requestmail {
 2126:     my ($dom,$action,$settings,$rowtotal) = @_;
 2127:     my ($now,$datatable,%currapp);
 2128:     $now = time;
 2129:     if (ref($settings) eq 'HASH') {
 2130:         if (ref($settings->{'notify'}) eq 'HASH') {
 2131:             if ($settings->{'notify'}{'approval'} ne '') {
 2132:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2133:             }
 2134:         }
 2135:     }
 2136:     my $numinrow = 2;
 2137:     my $css_class;
 2138:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2139:     my $text;
 2140:     if ($action eq 'requestcourses') {
 2141:         $text = &mt('Receive notification of course requests requiring approval');
 2142:     } elsif ($action eq 'requestauthor') {
 2143:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2144:     } else {
 2145:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2146:     }
 2147:     $datatable = '<tr'.$css_class.'>'.
 2148:                  ' <td>'.$text.'</td>'.
 2149:                  ' <td class="LC_left_item">';
 2150:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2151:                                                  $action.'notifyapproval',%currapp);
 2152:     if ($numdc > 0) {
 2153:         $datatable .= $table;
 2154:     } else {
 2155:         $datatable .= &mt('There are no active Domain Coordinators');
 2156:     }
 2157:     $datatable .='</td></tr>';
 2158:     return $datatable;
 2159: }
 2160: 
 2161: sub print_studentcode {
 2162:     my ($settings,$rowtotal) = @_;
 2163:     my $rownum = 0; 
 2164:     my ($output,%current);
 2165:     my @crstypes = ('official','unofficial','community','textbook');
 2166:     if (ref($settings) eq 'HASH') {
 2167:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2168:             foreach my $type (@crstypes) {
 2169:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2170:             }
 2171:         }
 2172:     }
 2173:     $output .= '<tr>'.
 2174:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2175:                '<td class="LC_left_item">';
 2176:     foreach my $type (@crstypes) {
 2177:         my $check = ' ';
 2178:         if ($current{$type}) {
 2179:             $check = ' checked="checked" ';
 2180:         }
 2181:         $output .= '<span class="LC_nobreak"><label>'.
 2182:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2183:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2184:     }
 2185:     $output .= '</td></tr>';
 2186:     $$rowtotal ++;
 2187:     return $output;
 2188: }
 2189: 
 2190: sub print_textbookcourses {
 2191:     my ($dom,$type,$settings,$rowtotal) = @_;
 2192:     my $rownum = 0;
 2193:     my $css_class;
 2194:     my $itemcount = 1;
 2195:     my $maxnum = 0;
 2196:     my $bookshash;
 2197:     if (ref($settings) eq 'HASH') {
 2198:         $bookshash = $settings->{$type};
 2199:     }
 2200:     my %ordered;
 2201:     if (ref($bookshash) eq 'HASH') {
 2202:         foreach my $item (keys(%{$bookshash})) {
 2203:             if (ref($bookshash->{$item}) eq 'HASH') {
 2204:                 my $num = $bookshash->{$item}{'order'};
 2205:                 $ordered{$num} = $item;
 2206:             }
 2207:         }
 2208:     }
 2209:     my $confname = $dom.'-domainconfig';
 2210:     my $switchserver = &check_switchserver($dom,$confname);
 2211:     my $maxnum = scalar(keys(%ordered));
 2212:     my $datatable;
 2213:     if (keys(%ordered)) {
 2214:         my @items = sort { $a <=> $b } keys(%ordered);
 2215:         for (my $i=0; $i<@items; $i++) {
 2216:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2217:             my $key = $ordered{$items[$i]};
 2218:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2219:             my $coursetitle = $coursehash{'description'};
 2220:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2221:             if (ref($bookshash->{$key}) eq 'HASH') {
 2222:                 $subject = $bookshash->{$key}->{'subject'};
 2223:                 $title = $bookshash->{$key}->{'title'};
 2224:                 if ($type eq 'textbooks') {
 2225:                     $publisher = $bookshash->{$key}->{'publisher'};
 2226:                     $author = $bookshash->{$key}->{'author'};
 2227:                     $image = $bookshash->{$key}->{'image'};
 2228:                     if ($image ne '') {
 2229:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2230:                         my $imagethumb = "$path/tn-".$imagefile;
 2231:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2232:                     }
 2233:                 }
 2234:             }
 2235:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2236:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2237:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2238:             for (my $k=0; $k<=$maxnum; $k++) {
 2239:                 my $vpos = $k+1;
 2240:                 my $selstr;
 2241:                 if ($k == $i) {
 2242:                     $selstr = ' selected="selected" ';
 2243:                 }
 2244:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2245:             }
 2246:             $datatable .= '</select>'.('&nbsp;'x2).
 2247:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2248:                 &mt('Delete?').'</label></span></td>'.
 2249:                 '<td colspan="2">'.
 2250:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2251:                 ('&nbsp;'x2).
 2252:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2253:             if ($type eq 'textbooks') {
 2254:                 $datatable .= ('&nbsp;'x2).
 2255:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2256:                               ('&nbsp;'x2).
 2257:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2258:                               ('&nbsp;'x2).
 2259:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2260:                 if ($image) {
 2261:                     $datatable .= '<span class="LC_nobreak">'.
 2262:                                   $imgsrc.
 2263:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2264:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2265:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2266:                 }
 2267:                 if ($switchserver) {
 2268:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2269:                 } else {
 2270:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2271:                 }
 2272:             }
 2273:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2274:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2275:                           $coursetitle.'</span></td></tr>'."\n";
 2276:             $itemcount ++;
 2277:         }
 2278:     }
 2279:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2280:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2281:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2282:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2283:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2284:     for (my $k=0; $k<$maxnum+1; $k++) {
 2285:         my $vpos = $k+1;
 2286:         my $selstr;
 2287:         if ($k == $maxnum) {
 2288:             $selstr = ' selected="selected" ';
 2289:         }
 2290:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2291:     }
 2292:     $datatable .= '</select>&nbsp;'."\n".
 2293:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2294:                   '<td colspan="2">'.
 2295:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2296:                   ('&nbsp;'x2).
 2297:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2298:                   ('&nbsp;'x2);
 2299:     if ($type eq 'textbooks') {
 2300:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2301:                       ('&nbsp;'x2).
 2302:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2303:                       ('&nbsp;'x2).
 2304:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2305:         if ($switchserver) {
 2306:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2307:         } else {
 2308:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2309:         }
 2310:     }
 2311:     $datatable .= '</span>'."\n".
 2312:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2313:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2314:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2315:                   &Apache::loncommon::selectcourse_link
 2316:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2317:                   '</span></td>'."\n".
 2318:                   '</tr>'."\n";
 2319:     $itemcount ++;
 2320:     return $datatable;
 2321: }
 2322: 
 2323: sub textbookcourses_javascript {
 2324:     my ($settings) = @_;
 2325:     return unless(ref($settings) eq 'HASH');
 2326:     my (%ordered,%total,%jstext);
 2327:     foreach my $type ('textbooks','templates') {
 2328:         $total{$type} = 0;
 2329:         if (ref($settings->{$type}) eq 'HASH') {
 2330:             foreach my $item (keys(%{$settings->{$type}})) {
 2331:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2332:                     my $num = $settings->{$type}->{$item}{'order'};
 2333:                     $ordered{$type}{$num} = $item;
 2334:                 }
 2335:             }
 2336:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2337:         }
 2338:         my @jsarray = ();
 2339:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2340:             push(@jsarray,$ordered{$type}{$item});
 2341:         }
 2342:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2343:     }
 2344:     return <<"ENDSCRIPT";
 2345: <script type="text/javascript">
 2346: // <![CDATA[
 2347: function reorderBooks(form,item,caller) {
 2348:     var changedVal;
 2349: $jstext{'textbooks'};
 2350: $jstext{'templates'};
 2351:     var newpos;
 2352:     var maxh;
 2353:     if (caller == 'textbooks') {  
 2354:         newpos = 'textbooks_addbook_pos';
 2355:         maxh = 1 + $total{'textbooks'};
 2356:     } else {
 2357:         newpos = 'templates_addbook_pos';
 2358:         maxh = 1 + $total{'templates'};
 2359:     }
 2360:     var current = new Array;
 2361:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2362:     if (item == newpos) {
 2363:         changedVal = newitemVal;
 2364:     } else {
 2365:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2366:         current[newitemVal] = newpos;
 2367:     }
 2368:     if (caller == 'textbooks') {
 2369:         for (var i=0; i<textbooks.length; i++) {
 2370:             var elementName = 'textbooks_'+textbooks[i];
 2371:             if (elementName != item) {
 2372:                 if (form.elements[elementName]) {
 2373:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2374:                     current[currVal] = elementName;
 2375:                 }
 2376:             }
 2377:         }
 2378:     }
 2379:     if (caller == 'templates') {
 2380:         for (var i=0; i<templates.length; i++) {
 2381:             var elementName = 'templates_'+templates[i];
 2382:             if (elementName != item) {
 2383:                 if (form.elements[elementName]) {
 2384:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2385:                     current[currVal] = elementName;
 2386:                 }
 2387:             }
 2388:         }
 2389:     }
 2390:     var oldVal;
 2391:     for (var j=0; j<maxh; j++) {
 2392:         if (current[j] == undefined) {
 2393:             oldVal = j;
 2394:         }
 2395:     }
 2396:     if (oldVal < changedVal) {
 2397:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2398:            var elementName = current[k];
 2399:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2400:         }
 2401:     } else {
 2402:         for (var k=changedVal; k<oldVal; k++) {
 2403:             var elementName = current[k];
 2404:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2405:         }
 2406:     }
 2407:     return;
 2408: }
 2409: 
 2410: // ]]>
 2411: </script>
 2412: 
 2413: ENDSCRIPT
 2414: }
 2415: 
 2416: sub print_autoenroll {
 2417:     my ($dom,$settings,$rowtotal) = @_;
 2418:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2419:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2420:     if (ref($settings) eq 'HASH') {
 2421:         if (exists($settings->{'run'})) {
 2422:             if ($settings->{'run'} eq '0') {
 2423:                 $runoff = ' checked="checked" ';
 2424:                 $runon = ' ';
 2425:             } else {
 2426:                 $runon = ' checked="checked" ';
 2427:                 $runoff = ' ';
 2428:             }
 2429:         } else {
 2430:             if ($autorun) {
 2431:                 $runon = ' checked="checked" ';
 2432:                 $runoff = ' ';
 2433:             } else {
 2434:                 $runoff = ' checked="checked" ';
 2435:                 $runon = ' ';
 2436:             }
 2437:         }
 2438:         if (exists($settings->{'co-owners'})) {
 2439:             if ($settings->{'co-owners'} eq '0') {
 2440:                 $coownersoff = ' checked="checked" ';
 2441:                 $coownerson = ' ';
 2442:             } else {
 2443:                 $coownerson = ' checked="checked" ';
 2444:                 $coownersoff = ' ';
 2445:             }
 2446:         } else {
 2447:             $coownersoff = ' checked="checked" ';
 2448:             $coownerson = ' ';
 2449:         }
 2450:         if (exists($settings->{'sender_domain'})) {
 2451:             $defdom = $settings->{'sender_domain'};
 2452:         }
 2453:     } else {
 2454:         if ($autorun) {
 2455:             $runon = ' checked="checked" ';
 2456:             $runoff = ' ';
 2457:         } else {
 2458:             $runoff = ' checked="checked" ';
 2459:             $runon = ' ';
 2460:         }
 2461:     }
 2462:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2463:     my $notif_sender;
 2464:     if (ref($settings) eq 'HASH') {
 2465:         $notif_sender = $settings->{'sender_uname'};
 2466:     }
 2467:     my $datatable='<tr class="LC_odd_row">'.
 2468:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2469:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2470:                   '<input type="radio" name="autoenroll_run"'.
 2471:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2472:                   '<label><input type="radio" name="autoenroll_run"'.
 2473:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2474:                   '</tr><tr>'.
 2475:                   '<td>'.&mt('Notification messages - sender').
 2476:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2477:                   &mt('username').':&nbsp;'.
 2478:                   '<input type="text" name="sender_uname" value="'.
 2479:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2480:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2481:                   '<tr class="LC_odd_row">'.
 2482:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2483:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2484:                   '<input type="radio" name="autoassign_coowners"'.
 2485:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2486:                   '<label><input type="radio" name="autoassign_coowners"'.
 2487:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2488:                   '</tr>';
 2489:     $$rowtotal += 3;
 2490:     return $datatable;
 2491: }
 2492: 
 2493: sub print_autoupdate {
 2494:     my ($position,$dom,$settings,$rowtotal) = @_;
 2495:     my $datatable;
 2496:     if ($position eq 'top') {
 2497:         my $updateon = ' ';
 2498:         my $updateoff = ' checked="checked" ';
 2499:         my $classlistson = ' ';
 2500:         my $classlistsoff = ' checked="checked" ';
 2501:         if (ref($settings) eq 'HASH') {
 2502:             if ($settings->{'run'} eq '1') {
 2503:                 $updateon = $updateoff;
 2504:                 $updateoff = ' ';
 2505:             }
 2506:             if ($settings->{'classlists'} eq '1') {
 2507:                 $classlistson = $classlistsoff;
 2508:                 $classlistsoff = ' ';
 2509:             }
 2510:         }
 2511:         my %title = (
 2512:                    run => 'Auto-update active?',
 2513:                    classlists => 'Update information in classlists?',
 2514:                     );
 2515:         $datatable = '<tr class="LC_odd_row">'. 
 2516:                   '<td>'.&mt($title{'run'}).'</td>'.
 2517:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2518:                   '<input type="radio" name="autoupdate_run"'.
 2519:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2520:                   '<label><input type="radio" name="autoupdate_run"'.
 2521:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2522:                   '</tr><tr>'.
 2523:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2524:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2525:                   '<label><input type="radio" name="classlists"'.
 2526:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2527:                   '<label><input type="radio" name="classlists"'.
 2528:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2529:                   '</tr>';
 2530:         $$rowtotal += 2;
 2531:     } elsif ($position eq 'middle') {
 2532:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2533:         my $numinrow = 3;
 2534:         my $locknamesettings;
 2535:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2536:                                      $dom,$numinrow,$othertitle,
 2537:                                     'lockablenames');
 2538:         $$rowtotal ++;
 2539:     } else {
 2540:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2541:         my @fields = ('lastname','firstname','middlename','generation',
 2542:                       'permanentemail','id');
 2543:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2544:         my $numrows = 0;
 2545:         if (ref($types) eq 'ARRAY') {
 2546:             if (@{$types} > 0) {
 2547:                 $datatable = 
 2548:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2549:                                          \@fields,$types,\$numrows);
 2550:                     $$rowtotal += @{$types}; 
 2551:             }
 2552:         }
 2553:         $datatable .= 
 2554:             &usertype_update_row($settings,{'default' => $othertitle},
 2555:                                  \%fieldtitles,\@fields,['default'],
 2556:                                  \$numrows);
 2557:         $$rowtotal ++;     
 2558:     }
 2559:     return $datatable;
 2560: }
 2561: 
 2562: sub print_autocreate {
 2563:     my ($dom,$settings,$rowtotal) = @_;
 2564:     my (%createon,%createoff,%currhash);
 2565:     my @types = ('xml','req');
 2566:     if (ref($settings) eq 'HASH') {
 2567:         foreach my $item (@types) {
 2568:             $createoff{$item} = ' checked="checked" ';
 2569:             $createon{$item} = ' ';
 2570:             if (exists($settings->{$item})) {
 2571:                 if ($settings->{$item}) {
 2572:                     $createon{$item} = ' checked="checked" ';
 2573:                     $createoff{$item} = ' ';
 2574:                 }
 2575:             }
 2576:         }
 2577:         if ($settings->{'xmldc'} ne '') {
 2578:             $currhash{$settings->{'xmldc'}} = 1;
 2579:         }
 2580:     } else {
 2581:         foreach my $item (@types) {
 2582:             $createoff{$item} = ' checked="checked" ';
 2583:             $createon{$item} = ' ';
 2584:         }
 2585:     }
 2586:     $$rowtotal += 2;
 2587:     my $numinrow = 2;
 2588:     my $datatable='<tr class="LC_odd_row">'.
 2589:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2590:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2591:                   '<input type="radio" name="autocreate_xml"'.
 2592:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2593:                   '<label><input type="radio" name="autocreate_xml"'.
 2594:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2595:                   '</td></tr><tr>'.
 2596:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2597:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2598:                   '<input type="radio" name="autocreate_req"'.
 2599:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2600:                   '<label><input type="radio" name="autocreate_req"'.
 2601:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2602:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2603:                                                    'autocreate_xmldc',%currhash);
 2604:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2605:     if ($numdc > 1) {
 2606:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2607:                       '</td><td class="LC_left_item">';
 2608:     } else {
 2609:         $datatable .= &mt('Course creation processed as:').
 2610:                       '</td><td class="LC_right_item">';
 2611:     }
 2612:     $datatable .= $dctable.'</td></tr>';
 2613:     $$rowtotal += $rows;
 2614:     return $datatable;
 2615: }
 2616: 
 2617: sub print_directorysrch {
 2618:     my ($dom,$settings,$rowtotal) = @_;
 2619:     my $srchon = ' ';
 2620:     my $srchoff = ' checked="checked" ';
 2621:     my ($exacton,$containson,$beginson);
 2622:     my $localon = ' ';
 2623:     my $localoff = ' checked="checked" ';
 2624:     if (ref($settings) eq 'HASH') {
 2625:         if ($settings->{'available'} eq '1') {
 2626:             $srchon = $srchoff;
 2627:             $srchoff = ' ';
 2628:         }
 2629:         if ($settings->{'localonly'} eq '1') {
 2630:             $localon = $localoff;
 2631:             $localoff = ' ';
 2632:         }
 2633:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2634:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2635:                 if ($type eq 'exact') {
 2636:                     $exacton = ' checked="checked" ';
 2637:                 } elsif ($type eq 'contains') {
 2638:                     $containson = ' checked="checked" ';
 2639:                 } elsif ($type eq 'begins') {
 2640:                     $beginson = ' checked="checked" ';
 2641:                 }
 2642:             }
 2643:         } else {
 2644:             if ($settings->{'searchtypes'} eq 'exact') {
 2645:                 $exacton = ' checked="checked" ';
 2646:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2647:                 $containson = ' checked="checked" ';
 2648:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2649:                 $exacton = ' checked="checked" ';
 2650:                 $containson = ' checked="checked" ';
 2651:             }
 2652:         }
 2653:     }
 2654:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2655:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2656: 
 2657:     my $numinrow = 4;
 2658:     my $cansrchrow = 0;
 2659:     my $datatable='<tr class="LC_odd_row">'.
 2660:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2661:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2662:                   '<input type="radio" name="dirsrch_available"'.
 2663:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2664:                   '<label><input type="radio" name="dirsrch_available"'.
 2665:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2666:                   '</tr><tr>'.
 2667:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2668:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2669:                   '<input type="radio" name="dirsrch_localonly"'.
 2670:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2671:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2672:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2673:                   '</tr>';
 2674:     $$rowtotal += 2;
 2675:     if (ref($usertypes) eq 'HASH') {
 2676:         if (keys(%{$usertypes}) > 0) {
 2677:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2678:                                          $numinrow,$othertitle,'cansearch');
 2679:             $cansrchrow = 1;
 2680:         }
 2681:     }
 2682:     if ($cansrchrow) {
 2683:         $$rowtotal ++;
 2684:         $datatable .= '<tr>';
 2685:     } else {
 2686:         $datatable .= '<tr class="LC_odd_row">';
 2687:     }
 2688:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2689:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2690:     foreach my $title (@{$titleorder}) {
 2691:         if (defined($searchtitles->{$title})) {
 2692:             my $check = ' ';
 2693:             if (ref($settings) eq 'HASH') {
 2694:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2695:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2696:                         $check = ' checked="checked" ';
 2697:                     }
 2698:                 }
 2699:             }
 2700:             $datatable .= '<td class="LC_left_item">'.
 2701:                           '<span class="LC_nobreak"><label>'.
 2702:                           '<input type="checkbox" name="searchby" '.
 2703:                           'value="'.$title.'"'.$check.'/>'.
 2704:                           $searchtitles->{$title}.'</label></span></td>';
 2705:         }
 2706:     }
 2707:     $datatable .= '</tr></table></td></tr>';
 2708:     $$rowtotal ++;
 2709:     if ($cansrchrow) {
 2710:         $datatable .= '<tr class="LC_odd_row">';
 2711:     } else {
 2712:         $datatable .= '<tr>';
 2713:     }
 2714:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2715:                   '<td class="LC_left_item" colspan="2">'.
 2716:                   '<span class="LC_nobreak"><label>'.
 2717:                   '<input type="checkbox" name="searchtypes" '.
 2718:                   $exacton.' value="exact" />'.&mt('Exact match').
 2719:                   '</label>&nbsp;'.
 2720:                   '<label><input type="checkbox" name="searchtypes" '.
 2721:                   $beginson.' value="begins" />'.&mt('Begins with').
 2722:                   '</label>&nbsp;'.
 2723:                   '<label><input type="checkbox" name="searchtypes" '.
 2724:                   $containson.' value="contains" />'.&mt('Contains').
 2725:                   '</label></span></td></tr>';
 2726:     $$rowtotal ++;
 2727:     return $datatable;
 2728: }
 2729: 
 2730: sub print_contacts {
 2731:     my ($dom,$settings,$rowtotal) = @_;
 2732:     my $datatable;
 2733:     my @contacts = ('adminemail','supportemail');
 2734:     my (%checked,%to,%otheremails,%bccemails);
 2735:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2736:                     'requestsmail','updatesmail','idconflictsmail');
 2737:     foreach my $type (@mailings) {
 2738:         $otheremails{$type} = '';
 2739:     }
 2740:     $bccemails{'helpdeskmail'} = '';
 2741:     if (ref($settings) eq 'HASH') {
 2742:         foreach my $item (@contacts) {
 2743:             if (exists($settings->{$item})) {
 2744:                 $to{$item} = $settings->{$item};
 2745:             }
 2746:         }
 2747:         foreach my $type (@mailings) {
 2748:             if (exists($settings->{$type})) {
 2749:                 if (ref($settings->{$type}) eq 'HASH') {
 2750:                     foreach my $item (@contacts) {
 2751:                         if ($settings->{$type}{$item}) {
 2752:                             $checked{$type}{$item} = ' checked="checked" ';
 2753:                         }
 2754:                     }
 2755:                     $otheremails{$type} = $settings->{$type}{'others'};
 2756:                     if ($type eq 'helpdeskmail') {
 2757:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2758:                     }
 2759:                 }
 2760:             } elsif ($type eq 'lonstatusmail') {
 2761:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2762:             }
 2763:         }
 2764:     } else {
 2765:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2766:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2767:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2768:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2769:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2770:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2771:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2772:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2773:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2774:     }
 2775:     my ($titles,$short_titles) = &contact_titles();
 2776:     my $rownum = 0;
 2777:     my $css_class;
 2778:     foreach my $item (@contacts) {
 2779:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2780:         $datatable .= '<tr'.$css_class.'>'. 
 2781:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2782:                   '</span></td><td class="LC_right_item">'.
 2783:                   '<input type="text" name="'.$item.'" value="'.
 2784:                   $to{$item}.'" /></td></tr>';
 2785:         $rownum ++;
 2786:     }
 2787:     foreach my $type (@mailings) {
 2788:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2789:         $datatable .= '<tr'.$css_class.'>'.
 2790:                       '<td><span class="LC_nobreak">'.
 2791:                       $titles->{$type}.': </span></td>'.
 2792:                       '<td class="LC_left_item">'.
 2793:                       '<span class="LC_nobreak">';
 2794:         foreach my $item (@contacts) {
 2795:             $datatable .= '<label>'.
 2796:                           '<input type="checkbox" name="'.$type.'"'.
 2797:                           $checked{$type}{$item}.
 2798:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2799:                           '</label>&nbsp;';
 2800:         }
 2801:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2802:                       '<input type="text" name="'.$type.'_others" '.
 2803:                       'value="'.$otheremails{$type}.'"  />';
 2804:         if ($type eq 'helpdeskmail') {
 2805:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2806:                           '<input type="text" name="'.$type.'_bcc" '.
 2807:                           'value="'.$bccemails{$type}.'"  />';
 2808:         }
 2809:         $datatable .= '</td></tr>'."\n";
 2810:         $rownum ++;
 2811:     }
 2812:     my %choices;
 2813:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2814:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2815:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2816:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2817:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2818:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2819:     my @toggles = ('reporterrors','reportupdates');
 2820:     my %defaultchecked = ('reporterrors'  => 'on',
 2821:                           'reportupdates' => 'on');
 2822:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2823:                                                \%choices,$rownum);
 2824:     $datatable .= $reports;
 2825:     $$rowtotal += $rownum;
 2826:     return $datatable;
 2827: }
 2828: 
 2829: sub print_helpsettings {
 2830:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2831:     my ($datatable,$itemcount);
 2832:     $itemcount = 1;
 2833:     my (%choices,%defaultchecked,@toggles);
 2834:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2835:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2836:                                  &mt('LON-CAPA bug tracker'),600,500));
 2837:     %defaultchecked = ('submitbugs' => 'on');
 2838:     @toggles = ('submitbugs',);
 2839: 
 2840:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2841:                                                  \%choices,$itemcount);
 2842:     return $datatable;
 2843: }
 2844: 
 2845: sub radiobutton_prefs {
 2846:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2847:         $additional,$align) = @_;
 2848:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2849:                    (ref($choices) eq 'HASH'));
 2850: 
 2851:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2852: 
 2853:     foreach my $item (@{$toggles}) {
 2854:         if ($defaultchecked->{$item} eq 'on') {
 2855:             $checkedon{$item} = ' checked="checked" ';
 2856:             $checkedoff{$item} = ' ';
 2857:         } elsif ($defaultchecked->{$item} eq 'off') {
 2858:             $checkedoff{$item} = ' checked="checked" ';
 2859:             $checkedon{$item} = ' ';
 2860:         }
 2861:     }
 2862:     if (ref($settings) eq 'HASH') {
 2863:         foreach my $item (@{$toggles}) {
 2864:             if ($settings->{$item} eq '1') {
 2865:                 $checkedon{$item} =  ' checked="checked" ';
 2866:                 $checkedoff{$item} = ' ';
 2867:             } elsif ($settings->{$item} eq '0') {
 2868:                 $checkedoff{$item} =  ' checked="checked" ';
 2869:                 $checkedon{$item} = ' ';
 2870:             }
 2871:         }
 2872:     }
 2873:     if ($onclick) {
 2874:         $onclick = ' onclick="'.$onclick.'"';
 2875:     }
 2876:     foreach my $item (@{$toggles}) {
 2877:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2878:         $datatable .=
 2879:             '<tr'.$css_class.'><td valign="top">'.
 2880:             '<span class="LC_nobreak">'.$choices->{$item}.
 2881:             '</span></td>';
 2882:         if ($align eq 'left') {
 2883:             $datatable .= '<td class="LC_left_item">';
 2884:         } else {
 2885:             $datatable .= '<td class="LC_right_item">';
 2886:         }
 2887:         $datatable .= 
 2888:             '<span class="LC_nobreak">'.
 2889:             '<label><input type="radio" name="'.
 2890:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2891:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2892:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2893:             '</span>'.$additional.
 2894:             '</td>'.
 2895:             '</tr>';
 2896:         $itemcount ++;
 2897:     }
 2898:     return ($datatable,$itemcount);
 2899: }
 2900: 
 2901: sub print_coursedefaults {
 2902:     my ($position,$dom,$settings,$rowtotal) = @_;
 2903:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2904:     my $itemcount = 1;
 2905:     my %choices =  &Apache::lonlocal::texthash (
 2906:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2907:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2908:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2909:         coursecredits        => 'Credits can be specified for courses',
 2910:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 2911:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 2912:         postsubmit           => 'Disable submit button/keypress following student submission',
 2913:         canclone             => "People who may clone a course (besides course's owner and coordinators)",     
 2914:     );
 2915:     my %staticdefaults = (
 2916:                            anonsurvey_threshold => 10,
 2917:                            uploadquota          => 500,
 2918:                            postsubmit           => 60,
 2919:                          );
 2920:     if ($position eq 'top') {
 2921:         %defaultchecked = (
 2922:                             'canuse_pdfforms' => 'off',
 2923:                             'uselcmath'       => 'on',
 2924:                             'usejsme'         => 'on',
 2925:                             'canclone'        => 'none', 
 2926:                           );
 2927:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 2928:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2929:                                                      \%choices,$itemcount);
 2930:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2931:         $datatable .=
 2932:             '<tr'.$css_class.'><td valign="top">'.
 2933:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 2934:             '</span></td><td class="LC_left_item">';
 2935:         my $currcanclone = 'none';
 2936:         my $onclick;
 2937:         my @cloneoptions = ('none','domain');
 2938:         my %clonetitles = (
 2939:                              none     => 'No additional course requesters',
 2940:                              domain   => "Any course requester in course's domain",
 2941:                              instcode => 'Course requests for official courses ...',
 2942:                           );
 2943:         my (%codedefaults,@code_order,@posscodes);
 2944:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 2945:                                                     \@code_order) eq 'ok') {
 2946:             if (@code_order > 0) {
 2947:                 push(@cloneoptions,'instcode');
 2948:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 2949:             }
 2950:         }
 2951:         if (ref($settings) eq 'HASH') {
 2952:             if ($settings->{'canclone'}) {
 2953:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 2954:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 2955:                         if (@code_order > 0) {
 2956:                             $currcanclone = 'instcode';
 2957:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 2958:                         }
 2959:                     }
 2960:                 } elsif ($settings->{'canclone'} eq 'domain') {
 2961:                     $currcanclone = $settings->{'canclone'};
 2962:                 }
 2963:             }
 2964:         } 
 2965:         foreach my $option (@cloneoptions) {
 2966:             my ($checked,$additional);
 2967:             if ($currcanclone eq $option) {
 2968:                 $checked = ' checked="checked"';
 2969:             }
 2970:             if ($option eq 'instcode') {
 2971:                 if (@code_order) {
 2972:                     my $show = 'none';
 2973:                     if ($checked) {
 2974:                         $show = 'block';
 2975:                     }
 2976:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 2977:                                   &mt('Institutional codes for new and cloned course have identical:').
 2978:                                   '<br />';
 2979:                     foreach my $item (@code_order) {
 2980:                         my $codechk;
 2981:                         if ($checked) {
 2982:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 2983:                                 $codechk = ' checked="checked"';
 2984:                             }
 2985:                         }
 2986:                         $additional .= '<label>'.
 2987:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 2988:                                        $item.'</label>';
 2989:                     }
 2990:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 2991:                 }
 2992:             }
 2993:             $datatable .=
 2994:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 2995:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 2996:                 '</label>&nbsp;'.$additional.'</span><br />';
 2997:         }
 2998:         $datatable .= '</td>'.
 2999:                       '</tr>';
 3000:         $itemcount ++;
 3001:     } else {
 3002:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3003:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout);
 3004:         my $currusecredits = 0;
 3005:         my $postsubmitclient = 1;
 3006:         my @types = ('official','unofficial','community','textbook');
 3007:         if (ref($settings) eq 'HASH') {
 3008:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 3009:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 3010:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 3011:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 3012:                 }
 3013:             }
 3014:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 3015:                 foreach my $type (@types) {
 3016:                     next if ($type eq 'community');
 3017:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 3018:                     if ($defcredits{$type} ne '') {
 3019:                         $currusecredits = 1;
 3020:                     }
 3021:                 }
 3022:             }
 3023:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 3024:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 3025:                     $postsubmitclient = 0;
 3026:                     foreach my $type (@types) {
 3027:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3028:                     }
 3029:                 } else {
 3030:                     foreach my $type (@types) {
 3031:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 3032:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 3033:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type}; 
 3034:                             } else {
 3035:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3036:                             }
 3037:                         } else {
 3038:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3039:                         }
 3040:                     }
 3041:                 }
 3042:             } else {
 3043:                 foreach my $type (@types) {
 3044:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3045:                 }
 3046:             }
 3047:         } else {
 3048:             foreach my $type (@types) {
 3049:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3050:             }
 3051:         }
 3052:         if (!$currdefresponder) {
 3053:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 3054:         } elsif ($currdefresponder < 1) {
 3055:             $currdefresponder = 1;
 3056:         }
 3057:         foreach my $type (@types) {
 3058:             if ($curruploadquota{$type} eq '') {
 3059:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 3060:             }
 3061:         }
 3062:         $datatable .=
 3063:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3064:                 $choices{'anonsurvey_threshold'}.
 3065:                 '</span></td>'.
 3066:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 3067:                 '<input type="text" name="anonsurvey_threshold"'.
 3068:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 3069:                 '</td></tr>'."\n";
 3070:         $itemcount ++;
 3071:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3072:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3073:                       $choices{'uploadquota'}.
 3074:                       '</span></td>'.
 3075:                       '<td align="right" class="LC_right_item">'.
 3076:                       '<table><tr>';
 3077:         foreach my $type (@types) {
 3078:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3079:                            '<input type="text" name="uploadquota_'.$type.'"'.
 3080:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 3081:         }
 3082:         $datatable .= '</tr></table></td></tr>'."\n";
 3083:         $itemcount ++;
 3084:         my $onclick = "toggleDisplay(this.form,'credits');";
 3085:         my $display = 'none';
 3086:         if ($currusecredits) {
 3087:             $display = 'block';
 3088:         }
 3089:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 3090:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 3091:         foreach my $type (@types) {
 3092:             next if ($type eq 'community');
 3093:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3094:                            '<input type="text" name="'.$type.'_credits"'.
 3095:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 3096:         }
 3097:         $additional .= '</tr></table></div>'."\n";
 3098:         %defaultchecked = ('coursecredits' => 'off');
 3099:         @toggles = ('coursecredits');
 3100:         my $current = {
 3101:                         'coursecredits' => $currusecredits,
 3102:                       };
 3103:         (my $table,$itemcount) =
 3104:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3105:                                \%choices,$itemcount,$onclick,$additional,'left');
 3106:         $datatable .= $table;
 3107:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 3108:         my $display = 'none';
 3109:         if ($postsubmitclient) {
 3110:             $display = 'block';
 3111:         }
 3112:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 3113:                       &mt('Number of seconds submit is disabled').'<br />'.
 3114:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 3115:                       '<table><tr>';
 3116:         foreach my $type (@types) {
 3117:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3118:                            '<input type="text" name="'.$type.'_timeout" value="'.
 3119:                            $deftimeout{$type}.'" size="5" /></td>';
 3120:         }
 3121:         $additional .= '</tr></table></div>'."\n";
 3122:         %defaultchecked = ('postsubmit' => 'on');
 3123:         @toggles = ('postsubmit');
 3124:         my $current = {
 3125:                         'postsubmit' => $postsubmitclient,
 3126:                       };
 3127:         ($table,$itemcount) =
 3128:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3129:                                \%choices,$itemcount,$onclick,$additional,'left');
 3130:         $datatable .= $table;
 3131:     }
 3132:     $$rowtotal += $itemcount;
 3133:     return $datatable;
 3134: }
 3135: 
 3136: sub print_selfenrollment {
 3137:     my ($position,$dom,$settings,$rowtotal) = @_;
 3138:     my ($css_class,$datatable);
 3139:     my $itemcount = 1;
 3140:     my @types = ('official','unofficial','community','textbook');
 3141:     if (($position eq 'top') || ($position eq 'middle')) {
 3142:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 3143:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 3144:         my @rows;
 3145:         my $key;
 3146:         if ($position eq 'top') {
 3147:             $key = 'admin'; 
 3148:             if (ref($rowsref) eq 'ARRAY') {
 3149:                 @rows = @{$rowsref};
 3150:             }
 3151:         } elsif ($position eq 'middle') {
 3152:             $key = 'default';
 3153:             @rows = ('types','registered','approval','limit');
 3154:         }
 3155:         foreach my $row (@rows) {
 3156:             if (defined($titlesref->{$row})) {
 3157:                 $itemcount ++;
 3158:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3159:                 $datatable .= '<tr'.$css_class.'>'.
 3160:                               '<td>'.$titlesref->{$row}.'</td>'.
 3161:                               '<td class="LC_left_item">'.
 3162:                               '<table><tr>';
 3163:                 my (%current,%currentcap);
 3164:                 if (ref($settings) eq 'HASH') {
 3165:                     if (ref($settings->{$key}) eq 'HASH') {
 3166:                         foreach my $type (@types) {
 3167:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3168:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 3169:                             }
 3170:                             if (($row eq 'limit') && ($key eq 'default')) {
 3171:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3172:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 3173:                                 }
 3174:                             }
 3175:                         }
 3176:                     }
 3177:                 }
 3178:                 my %roles = (
 3179:                              '0' => &Apache::lonnet::plaintext('dc'),
 3180:                             ); 
 3181:             
 3182:                 foreach my $type (@types) {
 3183:                     unless (($row eq 'registered') && ($key eq 'default')) {
 3184:                         $datatable .= '<th>'.&mt($type).'</th>';
 3185:                     }
 3186:                 }
 3187:                 unless (($row eq 'registered') && ($key eq 'default')) {
 3188:                     $datatable .= '</tr><tr>';
 3189:                 }
 3190:                 foreach my $type (@types) {
 3191:                     if ($type eq 'community') {
 3192:                         $roles{'1'} = &mt('Community personnel');
 3193:                     } else {
 3194:                         $roles{'1'} = &mt('Course personnel');
 3195:                     }
 3196:                     $datatable .= '<td style="vertical-align: top">';
 3197:                     if ($position eq 'top') {
 3198:                         my %checked;
 3199:                         if ($current{$type} eq '0') {
 3200:                             $checked{'0'} = ' checked="checked"';
 3201:                         } else {
 3202:                             $checked{'1'} = ' checked="checked"';
 3203:                         }
 3204:                         foreach my $role ('1','0') {
 3205:                             $datatable .= '<span class="LC_nobreak"><label>'.
 3206:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 3207:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 3208:                                           $roles{$role}.'</label></span> ';
 3209:                         }
 3210:                     } else {
 3211:                         if ($row eq 'types') {
 3212:                             my %checked;
 3213:                             if ($current{$type} =~ /^(all|dom)$/) {
 3214:                                 $checked{$1} = ' checked="checked"';
 3215:                             } else {
 3216:                                 $checked{''} = ' checked="checked"';
 3217:                             }
 3218:                             foreach my $val ('','dom','all') {
 3219:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3220:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3221:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3222:                             }
 3223:                         } elsif ($row eq 'registered') {
 3224:                             my %checked;
 3225:                             if ($current{$type} eq '1') {
 3226:                                 $checked{'1'} = ' checked="checked"';
 3227:                             } else {
 3228:                                 $checked{'0'} = ' checked="checked"';
 3229:                             }
 3230:                             foreach my $val ('0','1') {
 3231:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3232:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3233:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3234:                             }
 3235:                         } elsif ($row eq 'approval') {
 3236:                             my %checked;
 3237:                             if ($current{$type} =~ /^([12])$/) {
 3238:                                 $checked{$1} = ' checked="checked"';
 3239:                             } else {
 3240:                                 $checked{'0'} = ' checked="checked"';
 3241:                             }
 3242:                             for my $val (0..2) {
 3243:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3244:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3245:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3246:                             }
 3247:                         } elsif ($row eq 'limit') {
 3248:                             my %checked;
 3249:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3250:                                 $checked{$1} = ' checked="checked"';
 3251:                             } else {
 3252:                                 $checked{'none'} = ' checked="checked"';
 3253:                             }
 3254:                             my $cap;
 3255:                             if ($currentcap{$type} =~ /^\d+$/) {
 3256:                                 $cap = $currentcap{$type};
 3257:                             }
 3258:                             foreach my $val ('none','allstudents','selfenrolled') {
 3259:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3260:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3261:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3262:                             }
 3263:                             $datatable .= '<br />'.
 3264:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3265:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3266:                                           '</span>'; 
 3267:                         }
 3268:                     }
 3269:                     $datatable .= '</td>';
 3270:                 }
 3271:                 $datatable .= '</tr>';
 3272:             }
 3273:             $datatable .= '</table></td></tr>';
 3274:         }
 3275:     } elsif ($position eq 'bottom') {
 3276:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3277:     }
 3278:     $$rowtotal += $itemcount;
 3279:     return $datatable;
 3280: }
 3281: 
 3282: sub print_validation_rows {
 3283:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3284:     my ($itemsref,$namesref,$fieldsref);
 3285:     if ($caller eq 'selfenroll') { 
 3286:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3287:     } elsif ($caller eq 'requestcourses') {
 3288:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3289:     }
 3290:     my %currvalidation;
 3291:     if (ref($settings) eq 'HASH') {
 3292:         if (ref($settings->{'validation'}) eq 'HASH') {
 3293:             %currvalidation = %{$settings->{'validation'}};
 3294:         }
 3295:     }
 3296:     my $datatable;
 3297:     my $itemcount = 0;
 3298:     foreach my $item (@{$itemsref}) {
 3299:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3300:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3301:                       $namesref->{$item}.
 3302:                       '</span></td>'.
 3303:                       '<td class="LC_left_item">';
 3304:         if (($item eq 'url') || ($item eq 'button')) {
 3305:             $datatable .= '<span class="LC_nobreak">'.
 3306:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3307:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3308:         } elsif ($item eq 'fields') {
 3309:             my @currfields;
 3310:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3311:                 @currfields = @{$currvalidation{$item}};
 3312:             }
 3313:             foreach my $field (@{$fieldsref}) {
 3314:                 my $check = '';
 3315:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3316:                     $check = ' checked="checked"';
 3317:                 }
 3318:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3319:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3320:                               ' value="'.$field.'"'.$check.' />'.$field.
 3321:                               '</label></span> ';
 3322:             }
 3323:         } elsif ($item eq 'markup') {
 3324:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3325:                            $currvalidation{$item}.
 3326:                               '</textarea>';
 3327:         }
 3328:         $datatable .= '</td></tr>'."\n";
 3329:         if (ref($rowtotal)) {
 3330:             $itemcount ++;
 3331:         }
 3332:     }
 3333:     if ($caller eq 'requestcourses') {
 3334:         my %currhash;
 3335:         if (ref($settings) eq 'HASH') {
 3336:             if (ref($settings->{'validation'}) eq 'HASH') {
 3337:                 if ($settings->{'validation'}{'dc'} ne '') {
 3338:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 3339:                 }
 3340:             }
 3341:         }
 3342:         my $numinrow = 2;
 3343:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3344:                                                        'validationdc',%currhash);
 3345:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3346:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 3347:         if ($numdc > 1) {
 3348:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 3349:         } else {
 3350:             $datatable .=  &mt('Course creation processed as: ');
 3351:         }
 3352:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3353:         $itemcount ++;
 3354:     }
 3355:     if (ref($rowtotal)) {
 3356:         $$rowtotal += $itemcount;
 3357:     }
 3358:     return $datatable;
 3359: }
 3360: 
 3361: sub print_usersessions {
 3362:     my ($position,$dom,$settings,$rowtotal) = @_;
 3363:     my ($css_class,$datatable,%checked,%choices);
 3364:     my (%by_ip,%by_location,@intdoms);
 3365:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3366: 
 3367:     my @alldoms = &Apache::lonnet::all_domains();
 3368:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3369:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3370:     my %altids = &id_for_thisdom(%servers);
 3371:     my $itemcount = 1;
 3372:     if ($position eq 'top') {
 3373:         if (keys(%serverhomes) > 1) {
 3374:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3375:             my $curroffloadnow;
 3376:             if (ref($settings) eq 'HASH') {
 3377:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 3378:                     $curroffloadnow = $settings->{'offloadnow'};
 3379:                 }
 3380:             }
 3381:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 3382:         } else {
 3383:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3384:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3385:         }
 3386:     } else {
 3387:         if (keys(%by_location) == 0) {
 3388:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3389:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3390:         } else {
 3391:             my %lt = &usersession_titles();
 3392:             my $numinrow = 5;
 3393:             my $prefix;
 3394:             my @types;
 3395:             if ($position eq 'bottom') {
 3396:                 $prefix = 'remote';
 3397:                 @types = ('version','excludedomain','includedomain');
 3398:             } else {
 3399:                 $prefix = 'hosted';
 3400:                 @types = ('excludedomain','includedomain');
 3401:             }
 3402:             my (%current,%checkedon,%checkedoff);
 3403:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3404:             my @locations = sort(keys(%by_location));
 3405:             foreach my $type (@types) {
 3406:                 $checkedon{$type} = '';
 3407:                 $checkedoff{$type} = ' checked="checked"';
 3408:             }
 3409:             if (ref($settings) eq 'HASH') {
 3410:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3411:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3412:                         $current{$key} = $settings->{$prefix}{$key};
 3413:                         if ($key eq 'version') {
 3414:                             if ($current{$key} ne '') {
 3415:                                 $checkedon{$key} = ' checked="checked"';
 3416:                                 $checkedoff{$key} = '';
 3417:                             }
 3418:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3419:                             $checkedon{$key} = ' checked="checked"';
 3420:                             $checkedoff{$key} = '';
 3421:                         }
 3422:                     }
 3423:                 }
 3424:             }
 3425:             foreach my $type (@types) {
 3426:                 next if ($type ne 'version' && !@locations);
 3427:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3428:                 $datatable .= '<tr'.$css_class.'>
 3429:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3430:                                <span class="LC_nobreak">&nbsp;
 3431:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3432:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3433:                 if ($type eq 'version') {
 3434:                     my $selector = '<select name="'.$prefix.'_version">';
 3435:                     foreach my $version (@lcversions) {
 3436:                         my $selected = '';
 3437:                         if ($current{'version'} eq $version) {
 3438:                             $selected = ' selected="selected"';
 3439:                         }
 3440:                         $selector .= ' <option value="'.$version.'"'.
 3441:                                      $selected.'>'.$version.'</option>';
 3442:                     }
 3443:                     $selector .= '</select> ';
 3444:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3445:                 } else {
 3446:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3447:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3448:                                  ' />'.('&nbsp;'x2).
 3449:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3450:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3451:                                  "\n".
 3452:                                  '</div><div><table>';
 3453:                     my $rem;
 3454:                     for (my $i=0; $i<@locations; $i++) {
 3455:                         my ($showloc,$value,$checkedtype);
 3456:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3457:                             my $ip = $by_location{$locations[$i]}->[0];
 3458:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3459:                                  $value = join(':',@{$by_ip{$ip}});
 3460:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3461:                                 if (ref($current{$type}) eq 'ARRAY') {
 3462:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3463:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3464:                                             $checkedtype = ' checked="checked"';
 3465:                                             last;
 3466:                                         }
 3467:                                     }
 3468:                                 }
 3469:                             }
 3470:                         }
 3471:                         $rem = $i%($numinrow);
 3472:                         if ($rem == 0) {
 3473:                             if ($i > 0) {
 3474:                                 $datatable .= '</tr>';
 3475:                             }
 3476:                             $datatable .= '<tr>';
 3477:                         }
 3478:                         $datatable .= '<td class="LC_left_item">'.
 3479:                                       '<span class="LC_nobreak"><label>'.
 3480:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3481:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3482:                                       '</label></span></td>';
 3483:                     }
 3484:                     $rem = @locations%($numinrow);
 3485:                     my $colsleft = $numinrow - $rem;
 3486:                     if ($colsleft > 1 ) {
 3487:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3488:                                       '&nbsp;</td>';
 3489:                     } elsif ($colsleft == 1) {
 3490:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3491:                     }
 3492:                     $datatable .= '</tr></table>';
 3493:                 }
 3494:                 $datatable .= '</td></tr>';
 3495:                 $itemcount ++;
 3496:             }
 3497:         }
 3498:     }
 3499:     $$rowtotal += $itemcount;
 3500:     return $datatable;
 3501: }
 3502: 
 3503: sub build_location_hashes {
 3504:     my ($intdoms,$by_ip,$by_location) = @_;
 3505:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3506:                   (ref($by_location) eq 'HASH')); 
 3507:     my %iphost = &Apache::lonnet::get_iphost();
 3508:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3509:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3510:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3511:         foreach my $id (@{$iphost{$primary_ip}}) {
 3512:             my $intdom = &Apache::lonnet::internet_dom($id);
 3513:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3514:                 push(@{$intdoms},$intdom);
 3515:             }
 3516:         }
 3517:     }
 3518:     foreach my $ip (keys(%iphost)) {
 3519:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3520:             foreach my $id (@{$iphost{$ip}}) {
 3521:                 my $location = &Apache::lonnet::internet_dom($id);
 3522:                 if ($location) {
 3523:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3524:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3525:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3526:                             push(@{$by_ip->{$ip}},$location);
 3527:                         }
 3528:                     } else {
 3529:                         $by_ip->{$ip} = [$location];
 3530:                     }
 3531:                 }
 3532:             }
 3533:         }
 3534:     }
 3535:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3536:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3537:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3538:             my $first = $by_ip->{$ip}->[0];
 3539:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3540:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3541:                     push(@{$by_location->{$first}},$ip);
 3542:                 }
 3543:             } else {
 3544:                 $by_location->{$first} = [$ip];
 3545:             }
 3546:         }
 3547:     }
 3548:     return;
 3549: }
 3550: 
 3551: sub current_offloads_to {
 3552:     my ($dom,$settings,$servers) = @_;
 3553:     my (%spareid,%otherdomconfigs);
 3554:     if (ref($servers) eq 'HASH') {
 3555:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3556:             my $gotspares;
 3557:             if (ref($settings) eq 'HASH') {
 3558:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3559:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3560:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3561:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3562:                         $gotspares = 1;
 3563:                     }
 3564:                 }
 3565:             }
 3566:             unless ($gotspares) {
 3567:                 my $gotspares;
 3568:                 my $serverhomeID =
 3569:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3570:                 my $serverhomedom =
 3571:                     &Apache::lonnet::host_domain($serverhomeID);
 3572:                 if ($serverhomedom ne $dom) {
 3573:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3574:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3575:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3576:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3577:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3578:                                 $gotspares = 1;
 3579:                             }
 3580:                         }
 3581:                     } else {
 3582:                         $otherdomconfigs{$serverhomedom} =
 3583:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3584:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3585:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3586:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3587:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3588:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3589:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3590:                                         $gotspares = 1;
 3591:                                     }
 3592:                                 }
 3593:                             }
 3594:                         }
 3595:                     }
 3596:                 }
 3597:             }
 3598:             unless ($gotspares) {
 3599:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3600:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3601:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3602:                } else {
 3603:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3604:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3605:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3606:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3607:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3608:                     } else {
 3609:                         my %what = (
 3610:                              spareid => 1,
 3611:                         );
 3612:                         my ($result,$returnhash) = 
 3613:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3614:                         if ($result eq 'ok') { 
 3615:                             if (ref($returnhash) eq 'HASH') {
 3616:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3617:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3618:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3619:                                 }
 3620:                             }
 3621:                         }
 3622:                     }
 3623:                 }
 3624:             }
 3625:         }
 3626:     }
 3627:     return %spareid;
 3628: }
 3629: 
 3630: sub spares_row {
 3631:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 3632:     my $css_class;
 3633:     my $numinrow = 4;
 3634:     my $itemcount = 1;
 3635:     my $datatable;
 3636:     my %typetitles = &sparestype_titles();
 3637:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3638:         foreach my $server (sort(keys(%{$servers}))) {
 3639:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3640:             my ($othercontrol,$serverdom);
 3641:             if ($serverhome ne $server) {
 3642:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3643:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3644:             } else {
 3645:                 $serverdom = &Apache::lonnet::host_domain($server);
 3646:                 if ($serverdom ne $dom) {
 3647:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3648:                 }
 3649:             }
 3650:             next unless (ref($spareid->{$server}) eq 'HASH');
 3651:             my $checkednow;
 3652:             if (ref($curroffloadnow) eq 'HASH') {
 3653:                 if ($curroffloadnow->{$server}) {
 3654:                     $checkednow = ' checked="checked"';
 3655:                 }
 3656:             }
 3657:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3658:             $datatable .= '<tr'.$css_class.'>
 3659:                            <td rowspan="2">
 3660:                             <span class="LC_nobreak">'.
 3661:                           &mt('[_1] when busy, offloads to:'
 3662:                               ,'<b>'.$server.'</b>').'</span><br />'.
 3663:                           '<span class="LC_nobreak">'."\n". 
 3664:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 3665:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 3666:                           "\n";
 3667:             my (%current,%canselect);
 3668:             my @choices = 
 3669:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3670:             foreach my $type ('primary','default') {
 3671:                 if (ref($spareid->{$server}) eq 'HASH') {
 3672:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3673:                         my @spares = @{$spareid->{$server}{$type}};
 3674:                         if (@spares > 0) {
 3675:                             if ($othercontrol) {
 3676:                                 $current{$type} = join(', ',@spares);
 3677:                             } else {
 3678:                                 $current{$type} .= '<table>';
 3679:                                 my $numspares = scalar(@spares);
 3680:                                 for (my $i=0;  $i<@spares; $i++) {
 3681:                                     my $rem = $i%($numinrow);
 3682:                                     if ($rem == 0) {
 3683:                                         if ($i > 0) {
 3684:                                             $current{$type} .= '</tr>';
 3685:                                         }
 3686:                                         $current{$type} .= '<tr>';
 3687:                                     }
 3688:                                     $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;'.
 3689:                                                        $spareid->{$server}{$type}[$i].
 3690:                                                        '</label></td>'."\n";
 3691:                                 }
 3692:                                 my $rem = @spares%($numinrow);
 3693:                                 my $colsleft = $numinrow - $rem;
 3694:                                 if ($colsleft > 1 ) {
 3695:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3696:                                                        '" class="LC_left_item">'.
 3697:                                                        '&nbsp;</td>';
 3698:                                 } elsif ($colsleft == 1) {
 3699:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3700:                                 }
 3701:                                 $current{$type} .= '</tr></table>';
 3702:                             }
 3703:                         }
 3704:                     }
 3705:                     if ($current{$type} eq '') {
 3706:                         $current{$type} = &mt('None specified');
 3707:                     }
 3708:                     if ($othercontrol) {
 3709:                         if ($type eq 'primary') {
 3710:                             $canselect{$type} = $othercontrol;
 3711:                         }
 3712:                     } else {
 3713:                         $canselect{$type} = 
 3714:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3715:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3716:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3717:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3718:                         if (@choices > 0) {
 3719:                             foreach my $lonhost (@choices) {
 3720:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3721:                             }
 3722:                         }
 3723:                         $canselect{$type} .= '</select>'."\n";
 3724:                     }
 3725:                 } else {
 3726:                     $current{$type} = &mt('Could not be determined');
 3727:                     if ($type eq 'primary') {
 3728:                         $canselect{$type} =  $othercontrol;
 3729:                     }
 3730:                 }
 3731:                 if ($type eq 'default') {
 3732:                     $datatable .= '<tr'.$css_class.'>';
 3733:                 }
 3734:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3735:                               '<td>'.$current{$type}.'</td>'."\n".
 3736:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3737:             }
 3738:             $itemcount ++;
 3739:         }
 3740:     }
 3741:     $$rowtotal += $itemcount;
 3742:     return $datatable;
 3743: }
 3744: 
 3745: sub possible_newspares {
 3746:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3747:     my $serverhostname = &Apache::lonnet::hostname($server);
 3748:     my %excluded;
 3749:     if ($serverhostname ne '') {
 3750:         %excluded = (
 3751:                        $serverhostname => 1,
 3752:                     );
 3753:     }
 3754:     if (ref($currspares) eq 'HASH') {
 3755:         foreach my $type (keys(%{$currspares})) {
 3756:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3757:                 if (@{$currspares->{$type}} > 0) {
 3758:                     foreach my $curr (@{$currspares->{$type}}) {
 3759:                         my $hostname = &Apache::lonnet::hostname($curr);
 3760:                         $excluded{$hostname} = 1;
 3761:                     }
 3762:                 }
 3763:             }
 3764:         }
 3765:     }
 3766:     my @choices;
 3767:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3768:         if (keys(%{$serverhomes}) > 1) {
 3769:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3770:                 unless ($excluded{$name}) {
 3771:                     if (exists($altids->{$serverhomes->{$name}})) {
 3772:                         push(@choices,$altids->{$serverhomes->{$name}});
 3773:                     } else {
 3774:                         push(@choices,$serverhomes->{$name});
 3775:                     }
 3776:                 }
 3777:             }
 3778:         }
 3779:     }
 3780:     return sort(@choices);
 3781: }
 3782: 
 3783: sub print_loadbalancing {
 3784:     my ($dom,$settings,$rowtotal) = @_;
 3785:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3786:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3787:     my $numinrow = 1;
 3788:     my $datatable;
 3789:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3790:     my (%currbalancer,%currtargets,%currrules,%existing);
 3791:     if (ref($settings) eq 'HASH') {
 3792:         %existing = %{$settings};
 3793:     }
 3794:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3795:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3796:                                   \%currtargets,\%currrules);
 3797:     } else {
 3798:         return;
 3799:     }
 3800:     my ($othertitle,$usertypes,$types) =
 3801:         &Apache::loncommon::sorted_inst_types($dom);
 3802:     my $rownum = 8;
 3803:     if (ref($types) eq 'ARRAY') {
 3804:         $rownum += scalar(@{$types});
 3805:     }
 3806:     my @css_class = ('LC_odd_row','LC_even_row');
 3807:     my $balnum = 0;
 3808:     my $islast;
 3809:     my (@toshow,$disabledtext);
 3810:     if (keys(%currbalancer) > 0) {
 3811:         @toshow = sort(keys(%currbalancer));
 3812:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3813:             push(@toshow,'');
 3814:         }
 3815:     } else {
 3816:         @toshow = ('');
 3817:         $disabledtext = &mt('No existing load balancer');
 3818:     }
 3819:     foreach my $lonhost (@toshow) {
 3820:         if ($balnum == scalar(@toshow)-1) {
 3821:             $islast = 1;
 3822:         } else {
 3823:             $islast = 0;
 3824:         }
 3825:         my $cssidx = $balnum%2;
 3826:         my $targets_div_style = 'display: none';
 3827:         my $disabled_div_style = 'display: block';
 3828:         my $homedom_div_style = 'display: none';
 3829:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3830:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3831:                       '<p>';
 3832:         if ($lonhost eq '') {
 3833:             $datatable .= '<span class="LC_nobreak">';
 3834:             if (keys(%currbalancer) > 0) {
 3835:                 $datatable .= &mt('Add balancer:');
 3836:             } else {
 3837:                 $datatable .= &mt('Enable balancer:');
 3838:             }
 3839:             $datatable .= '&nbsp;'.
 3840:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3841:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3842:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3843:                           '<option value="" selected="selected">'.&mt('None').
 3844:                           '</option>'."\n";
 3845:             foreach my $server (sort(keys(%servers))) {
 3846:                 next if ($currbalancer{$server});
 3847:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3848:             }
 3849:             $datatable .=
 3850:                 '</select>'."\n".
 3851:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3852:         } else {
 3853:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3854:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3855:                            &mt('Stop balancing').'</label>'.
 3856:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3857:             $targets_div_style = 'display: block';
 3858:             $disabled_div_style = 'display: none';
 3859:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3860:                 $homedom_div_style = 'display: block';
 3861:             }
 3862:         }
 3863:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3864:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3865:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3866:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3867:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3868:         my @sparestypes = ('primary','default');
 3869:         my %typetitles = &sparestype_titles();
 3870:         foreach my $sparetype (@sparestypes) {
 3871:             my $targettable;
 3872:             for (my $i=0; $i<$numspares; $i++) {
 3873:                 my $checked;
 3874:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3875:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3876:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3877:                             $checked = ' checked="checked"';
 3878:                         }
 3879:                     }
 3880:                 }
 3881:                 my ($chkboxval,$disabled);
 3882:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3883:                     $chkboxval = $spares[$i];
 3884:                 }
 3885:                 if (exists($currbalancer{$spares[$i]})) {
 3886:                     $disabled = ' disabled="disabled"';
 3887:                 }
 3888:                 $targettable .=
 3889:                     '<td><span class="LC_nobreak"><label>'.
 3890:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3891:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3892:                     '</span></label></span></td>';
 3893:                 my $rem = $i%($numinrow);
 3894:                 if ($rem == 0) {
 3895:                     if (($i > 0) && ($i < $numspares-1)) {
 3896:                         $targettable .= '</tr>';
 3897:                     }
 3898:                     if ($i < $numspares-1) {
 3899:                         $targettable .= '<tr>';
 3900:                     }
 3901:                 }
 3902:             }
 3903:             if ($targettable ne '') {
 3904:                 my $rem = $numspares%($numinrow);
 3905:                 my $colsleft = $numinrow - $rem;
 3906:                 if ($colsleft > 1 ) {
 3907:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3908:                                     '&nbsp;</td>';
 3909:                 } elsif ($colsleft == 1) {
 3910:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3911:                 }
 3912:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3913:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3914:             }
 3915:         }
 3916:         $datatable .= '</div></td></tr>'.
 3917:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3918:                                            $othertitle,$usertypes,$types,\%servers,
 3919:                                            \%currbalancer,$lonhost,
 3920:                                            $targets_div_style,$homedom_div_style,
 3921:                                            $css_class[$cssidx],$balnum,$islast);
 3922:         $$rowtotal += $rownum;
 3923:         $balnum ++;
 3924:     }
 3925:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3926:     return $datatable;
 3927: }
 3928: 
 3929: sub get_loadbalancers_config {
 3930:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3931:     return unless ((ref($servers) eq 'HASH') &&
 3932:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3933:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3934:     if (keys(%{$existing}) > 0) {
 3935:         my $oldlonhost;
 3936:         foreach my $key (sort(keys(%{$existing}))) {
 3937:             if ($key eq 'lonhost') {
 3938:                 $oldlonhost = $existing->{'lonhost'};
 3939:                 $currbalancer->{$oldlonhost} = 1;
 3940:             } elsif ($key eq 'targets') {
 3941:                 if ($oldlonhost) {
 3942:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3943:                 }
 3944:             } elsif ($key eq 'rules') {
 3945:                 if ($oldlonhost) {
 3946:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3947:                 }
 3948:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3949:                 $currbalancer->{$key} = 1;
 3950:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3951:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3952:             }
 3953:         }
 3954:     } else {
 3955:         my ($balancerref,$targetsref) =
 3956:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3957:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3958:             foreach my $server (sort(keys(%{$balancerref}))) {
 3959:                 $currbalancer->{$server} = 1;
 3960:                 $currtargets->{$server} = $targetsref->{$server};
 3961:             }
 3962:         }
 3963:     }
 3964:     return;
 3965: }
 3966: 
 3967: sub loadbalancing_rules {
 3968:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3969:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3970:         $css_class,$balnum,$islast) = @_;
 3971:     my $output;
 3972:     my $num = 0;
 3973:     my ($alltypes,$othertypes,$titles) =
 3974:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3975:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3976:         foreach my $type (@{$alltypes}) {
 3977:             $num ++;
 3978:             my $current;
 3979:             if (ref($currrules) eq 'HASH') {
 3980:                 $current = $currrules->{$type};
 3981:             }
 3982:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3983:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3984:                     $current = '';
 3985:                 }
 3986:             }
 3987:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3988:                                              $servers,$currbalancer,$lonhost,$dom,
 3989:                                              $targets_div_style,$homedom_div_style,
 3990:                                              $css_class,$balnum,$num,$islast);
 3991:         }
 3992:     }
 3993:     return $output;
 3994: }
 3995: 
 3996: sub loadbalancing_titles {
 3997:     my ($dom,$intdom,$usertypes,$types) = @_;
 3998:     my %othertypes = (
 3999:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 4000:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 4001:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 4002:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 4003:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 4004:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 4005:                      );
 4006:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 4007:     if (ref($types) eq 'ARRAY') {
 4008:         unshift(@alltypes,@{$types},'default');
 4009:     }
 4010:     my %titles;
 4011:     foreach my $type (@alltypes) {
 4012:         if ($type =~ /^_LC_/) {
 4013:             $titles{$type} = $othertypes{$type};
 4014:         } elsif ($type eq 'default') {
 4015:             $titles{$type} = &mt('All users from [_1]',$dom);
 4016:             if (ref($types) eq 'ARRAY') {
 4017:                 if (@{$types} > 0) {
 4018:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 4019:                 }
 4020:             }
 4021:         } elsif (ref($usertypes) eq 'HASH') {
 4022:             $titles{$type} = $usertypes->{$type};
 4023:         }
 4024:     }
 4025:     return (\@alltypes,\%othertypes,\%titles);
 4026: }
 4027: 
 4028: sub loadbalance_rule_row {
 4029:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 4030:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 4031:     my @rulenames;
 4032:     my %ruletitles = &offloadtype_text();
 4033:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 4034:         @rulenames = ('balancer','offloadedto','specific');
 4035:     } else {
 4036:         @rulenames = ('default','homeserver');
 4037:         if ($type eq '_LC_external') {
 4038:             push(@rulenames,'externalbalancer');
 4039:         } else {
 4040:             push(@rulenames,'specific');
 4041:         }
 4042:         push(@rulenames,'none');
 4043:     }
 4044:     my $style = $targets_div_style;
 4045:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 4046:         $style = $homedom_div_style;
 4047:     }
 4048:     my $space;
 4049:     if ($islast && $num == 1) {
 4050:         $space = '<div display="inline-block">&nbsp;</div>';
 4051:     }
 4052:     my $output =
 4053:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 4054:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 4055:         '<td valaign="top">'.$space.
 4056:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 4057:     for (my $i=0; $i<@rulenames; $i++) {
 4058:         my $rule = $rulenames[$i];
 4059:         my ($checked,$extra);
 4060:         if ($rulenames[$i] eq 'default') {
 4061:             $rule = '';
 4062:         }
 4063:         if ($rulenames[$i] eq 'specific') {
 4064:             if (ref($servers) eq 'HASH') {
 4065:                 my $default;
 4066:                 if (($current ne '') && (exists($servers->{$current}))) {
 4067:                     $checked = ' checked="checked"';
 4068:                 }
 4069:                 unless ($checked) {
 4070:                     $default = ' selected="selected"';
 4071:                 }
 4072:                 $extra =
 4073:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4074:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4075:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 4076:                     '<option value=""'.$default.'></option>'."\n";
 4077:                 foreach my $server (sort(keys(%{$servers}))) {
 4078:                     if (ref($currbalancer) eq 'HASH') {
 4079:                         next if (exists($currbalancer->{$server}));
 4080:                     }
 4081:                     my $selected;
 4082:                     if ($server eq $current) {
 4083:                         $selected = ' selected="selected"';
 4084:                     }
 4085:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 4086:                 }
 4087:                 $extra .= '</select>';
 4088:             }
 4089:         } elsif ($rule eq $current) {
 4090:             $checked = ' checked="checked"';
 4091:         }
 4092:         $output .= '<span class="LC_nobreak"><label>'.
 4093:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 4094:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 4095:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 4096:                    ')"'.$checked.' />&nbsp;';
 4097:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 4098:             $output .= $ruletitles{'particular'};
 4099:         } else {
 4100:             $output .= $ruletitles{$rulenames[$i]};
 4101:         }
 4102:         $output .= '</label>'.$extra.'</span><br />'."\n";
 4103:     }
 4104:     $output .= '</div></td></tr>'."\n";
 4105:     return $output;
 4106: }
 4107: 
 4108: sub offloadtype_text {
 4109:     my %ruletitles = &Apache::lonlocal::texthash (
 4110:            'default'          => 'Offloads to default destinations',
 4111:            'homeserver'       => "Offloads to user's home server",
 4112:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 4113:            'specific'         => 'Offloads to specific server',
 4114:            'none'             => 'No offload',
 4115:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 4116:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 4117:            'particular'       => 'Session hosted (after re-auth) on server:',
 4118:     );
 4119:     return %ruletitles;
 4120: }
 4121: 
 4122: sub sparestype_titles {
 4123:     my %typestitles = &Apache::lonlocal::texthash (
 4124:                           'primary' => 'primary',
 4125:                           'default' => 'default',
 4126:                       );
 4127:     return %typestitles;
 4128: }
 4129: 
 4130: sub contact_titles {
 4131:     my %titles = &Apache::lonlocal::texthash (
 4132:                    'supportemail' => 'Support E-mail address',
 4133:                    'adminemail'   => 'Default Server Admin E-mail address',
 4134:                    'errormail'    => 'Error reports to be e-mailed to',
 4135:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 4136:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 4137:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 4138:                    'requestsmail' => 'E-mail from course requests requiring approval',
 4139:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 4140:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 4141:                  );
 4142:     my %short_titles = &Apache::lonlocal::texthash (
 4143:                            adminemail   => 'Admin E-mail address',
 4144:                            supportemail => 'Support E-mail',
 4145:                        );   
 4146:     return (\%titles,\%short_titles);
 4147: }
 4148: 
 4149: sub tool_titles {
 4150:     my %titles = &Apache::lonlocal::texthash (
 4151:                      aboutme    => 'Personal web page',
 4152:                      blog       => 'Blog',
 4153:                      webdav     => 'WebDAV',
 4154:                      portfolio  => 'Portfolio',
 4155:                      official   => 'Official courses (with institutional codes)',
 4156:                      unofficial => 'Unofficial courses',
 4157:                      community  => 'Communities',
 4158:                      textbook   => 'Textbook courses',
 4159:                  );
 4160:     return %titles;
 4161: }
 4162: 
 4163: sub courserequest_titles {
 4164:     my %titles = &Apache::lonlocal::texthash (
 4165:                                    official   => 'Official',
 4166:                                    unofficial => 'Unofficial',
 4167:                                    community  => 'Communities',
 4168:                                    textbook   => 'Textbook',
 4169:                                    norequest  => 'Not allowed',
 4170:                                    approval   => 'Approval by Dom. Coord.',
 4171:                                    validate   => 'With validation',
 4172:                                    autolimit  => 'Numerical limit',
 4173:                                    unlimited  => '(blank for unlimited)',
 4174:                  );
 4175:     return %titles;
 4176: }
 4177: 
 4178: sub authorrequest_titles {
 4179:     my %titles = &Apache::lonlocal::texthash (
 4180:                                    norequest  => 'Not allowed',
 4181:                                    approval   => 'Approval by Dom. Coord.',
 4182:                                    automatic  => 'Automatic approval',
 4183:                  );
 4184:     return %titles;
 4185: }
 4186: 
 4187: sub courserequest_conditions {
 4188:     my %conditions = &Apache::lonlocal::texthash (
 4189:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 4190:        validate   => '(Processing of request subject to institutional validation).',
 4191:                  );
 4192:     return %conditions;
 4193: }
 4194: 
 4195: 
 4196: sub print_usercreation {
 4197:     my ($position,$dom,$settings,$rowtotal) = @_;
 4198:     my $numinrow = 4;
 4199:     my $datatable;
 4200:     if ($position eq 'top') {
 4201:         $$rowtotal ++;
 4202:         my $rowcount = 0;
 4203:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 4204:         if (ref($rules) eq 'HASH') {
 4205:             if (keys(%{$rules}) > 0) {
 4206:                 $datatable .= &user_formats_row('username',$settings,$rules,
 4207:                                                 $ruleorder,$numinrow,$rowcount);
 4208:                 $$rowtotal ++;
 4209:                 $rowcount ++;
 4210:             }
 4211:         }
 4212:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 4213:         if (ref($idrules) eq 'HASH') {
 4214:             if (keys(%{$idrules}) > 0) {
 4215:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 4216:                                                 $idruleorder,$numinrow,$rowcount);
 4217:                 $$rowtotal ++;
 4218:                 $rowcount ++;
 4219:             }
 4220:         }
 4221:         if ($rowcount == 0) {
 4222:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 4223:             $$rowtotal ++;
 4224:             $rowcount ++;
 4225:         }
 4226:     } elsif ($position eq 'middle') {
 4227:         my @creators = ('author','course','requestcrs');
 4228:         my ($rules,$ruleorder) =
 4229:             &Apache::lonnet::inst_userrules($dom,'username');
 4230:         my %lt = &usercreation_types();
 4231:         my %checked;
 4232:         if (ref($settings) eq 'HASH') {
 4233:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 4234:                 foreach my $item (@creators) {
 4235:                     $checked{$item} = $settings->{'cancreate'}{$item};
 4236:                 }
 4237:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 4238:                 foreach my $item (@creators) {
 4239:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 4240:                         $checked{$item} = 'none';
 4241:                     }
 4242:                 }
 4243:             }
 4244:         }
 4245:         my $rownum = 0;
 4246:         foreach my $item (@creators) {
 4247:             $rownum ++;
 4248:             if ($checked{$item} eq '') {
 4249:                 $checked{$item} = 'any';
 4250:             }
 4251:             my $css_class;
 4252:             if ($rownum%2) {
 4253:                 $css_class = '';
 4254:             } else {
 4255:                 $css_class = ' class="LC_odd_row" ';
 4256:             }
 4257:             $datatable .= '<tr'.$css_class.'>'.
 4258:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 4259:                          '</span></td><td align="right">';
 4260:             my @options = ('any');
 4261:             if (ref($rules) eq 'HASH') {
 4262:                 if (keys(%{$rules}) > 0) {
 4263:                     push(@options,('official','unofficial'));
 4264:                 }
 4265:             }
 4266:             push(@options,'none');
 4267:             foreach my $option (@options) {
 4268:                 my $type = 'radio';
 4269:                 my $check = ' ';
 4270:                 if ($checked{$item} eq $option) {
 4271:                     $check = ' checked="checked" ';
 4272:                 } 
 4273:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4274:                               '<input type="'.$type.'" name="can_createuser_'.
 4275:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 4276:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 4277:             }
 4278:             $datatable .= '</td></tr>';
 4279:         }
 4280:     } else {
 4281:         my @contexts = ('author','course','domain');
 4282:         my @authtypes = ('int','krb4','krb5','loc');
 4283:         my %checked;
 4284:         if (ref($settings) eq 'HASH') {
 4285:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 4286:                 foreach my $item (@contexts) {
 4287:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 4288:                         foreach my $auth (@authtypes) {
 4289:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 4290:                                 $checked{$item}{$auth} = ' checked="checked" ';
 4291:                             }
 4292:                         }
 4293:                     }
 4294:                 }
 4295:             }
 4296:         } else {
 4297:             foreach my $item (@contexts) {
 4298:                 foreach my $auth (@authtypes) {
 4299:                     $checked{$item}{$auth} = ' checked="checked" ';
 4300:                 }
 4301:             }
 4302:         }
 4303:         my %title = &context_names();
 4304:         my %authname = &authtype_names();
 4305:         my $rownum = 0;
 4306:         my $css_class; 
 4307:         foreach my $item (@contexts) {
 4308:             if ($rownum%2) {
 4309:                 $css_class = '';
 4310:             } else {
 4311:                 $css_class = ' class="LC_odd_row" ';
 4312:             }
 4313:             $datatable .=   '<tr'.$css_class.'>'.
 4314:                             '<td>'.$title{$item}.
 4315:                             '</td><td class="LC_left_item">'.
 4316:                             '<span class="LC_nobreak">';
 4317:             foreach my $auth (@authtypes) {
 4318:                 $datatable .= '<label>'. 
 4319:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4320:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4321:                               $authname{$auth}.'</label>&nbsp;';
 4322:             }
 4323:             $datatable .= '</span></td></tr>';
 4324:             $rownum ++;
 4325:         }
 4326:         $$rowtotal += $rownum;
 4327:     }
 4328:     return $datatable;
 4329: }
 4330: 
 4331: sub print_selfcreation {
 4332:     my ($position,$dom,$settings,$rowtotal) = @_;
 4333:     my (@selfcreate,$createsettings,$processing,$datatable);
 4334:     if (ref($settings) eq 'HASH') {
 4335:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4336:             $createsettings = $settings->{'cancreate'};
 4337:             if (ref($createsettings) eq 'HASH') {
 4338:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4339:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4340:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4341:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4342:                         @selfcreate = ('email','login','sso');
 4343:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4344:                         @selfcreate = ($createsettings->{'selfcreate'});
 4345:                     }
 4346:                 }
 4347:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4348:                     $processing = $createsettings->{'selfcreateprocessing'};
 4349:                 }
 4350:             }
 4351:         }
 4352:     }
 4353:     my %radiohash;
 4354:     my $numinrow = 4;
 4355:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4356:     if ($position eq 'top') {
 4357:         my %choices = &Apache::lonlocal::texthash (
 4358:                                                       cancreate_login      => 'Institutional Login',
 4359:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4360:                                                   );
 4361:         my @toggles = sort(keys(%choices));
 4362:         my %defaultchecked = (
 4363:                                'cancreate_login' => 'off',
 4364:                                'cancreate_sso'   => 'off',
 4365:                              );
 4366:         my ($onclick,$itemcount);
 4367:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4368:                                                      \%choices,$itemcount,$onclick);
 4369:         $$rowtotal += $itemcount;
 4370:         
 4371:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4372: 
 4373:         if (ref($usertypes) eq 'HASH') {
 4374:             if (keys(%{$usertypes}) > 0) {
 4375:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4376:                                              $dom,$numinrow,$othertitle,
 4377:                                              'statustocreate',$$rowtotal);
 4378:                 $$rowtotal ++;
 4379:             }
 4380:         }
 4381:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 4382:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4383:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 4384:         my $rem;
 4385:         my $numperrow = 2;
 4386:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4387:         $datatable .= '<tr'.$css_class.'>'.
 4388:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 4389:                      '<td class="LC_left_item">'."\n".
 4390:                      '<table><tr><td>'."\n";
 4391:         for (my $i=0; $i<@fields; $i++) {
 4392:             $rem = $i%($numperrow);
 4393:             if ($rem == 0) {
 4394:                 if ($i > 0) {
 4395:                     $datatable .= '</tr>';
 4396:                 }
 4397:                 $datatable .= '<tr>';
 4398:             }
 4399:             my $currval;
 4400:             if (ref($createsettings) eq 'HASH') {
 4401:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 4402:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 4403:                 }
 4404:             }
 4405:             $datatable .= '<td class="LC_left_item">'.
 4406:                           '<span class="LC_nobreak">'.
 4407:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 4408:                           'value="'.$currval.'" size="10" />&nbsp;'.
 4409:                           $fieldtitles{$fields[$i]}.'</span></td>';
 4410:         }
 4411:         my $colsleft = $numperrow - $rem;
 4412:         if ($colsleft > 1 ) {
 4413:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4414:                          '&nbsp;</td>';
 4415:         } elsif ($colsleft == 1) {
 4416:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4417:         }
 4418:         $datatable .= '</tr></table></td></tr>';
 4419:         $$rowtotal ++;
 4420:     } elsif ($position eq 'middle') {
 4421:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4422:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4423:         $usertypes->{'default'} = $othertitle;
 4424:         if (ref($types) eq 'ARRAY') {
 4425:             push(@{$types},'default');
 4426:             $usertypes->{'default'} = $othertitle;
 4427:             foreach my $status (@{$types}) {
 4428:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4429:                                                        $numinrow,$$rowtotal,$usertypes);
 4430:                 $$rowtotal ++;
 4431:             }
 4432:         }
 4433:     } else {
 4434:         my %choices = &Apache::lonlocal::texthash (
 4435:                                                       cancreate_email => 'E-mail address as username',
 4436:                                                   );
 4437:         my @toggles = sort(keys(%choices));
 4438:         my %defaultchecked = (
 4439:                                'cancreate_email' => 'off',
 4440:                              );
 4441:         my $itemcount = 0;
 4442:         my $display = 'none';
 4443:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 4444:             $display = 'block';
 4445:         }
 4446:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 4447:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 4448:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4449:         my $usertypes = {};
 4450:         my $order = [];
 4451:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 4452:             $usertypes = $domdefaults{'inststatustypes'};
 4453:             $order = $domdefaults{'inststatusguest'};
 4454:         }
 4455:         if (ref($order) eq 'ARRAY') {
 4456:             push(@{$order},'default');
 4457:             if (@{$order} > 1) {
 4458:                 $usertypes->{'default'} = &mt('Other users');
 4459:                 $additional .= '<table><tr>';
 4460:                 foreach my $status (@{$order}) {
 4461:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 4462:                 }
 4463:                 $additional .= '</tr><tr>';
 4464:                 foreach my $status (@{$order}) {
 4465:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 4466:                 }
 4467:                 $additional .= '</tr></table>';
 4468:             } else {
 4469:                 $usertypes->{'default'} = &mt('All users');
 4470:                 $additional .= &email_as_username($rowtotal,$processing);
 4471:             }
 4472:         }
 4473:         $additional .= '</div>'."\n";
 4474: 
 4475:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4476:                                                      \%choices,$$rowtotal,$onclick,$additional);
 4477:         $$rowtotal ++;
 4478:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4479:         $$rowtotal ++;
 4480:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4481:         $numinrow = 1;
 4482:         if (ref($order) eq 'ARRAY') {
 4483:             foreach my $status (@{$order}) {
 4484:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4485:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4486:                 $$rowtotal ++;
 4487:             }
 4488:         }
 4489:         my ($emailrules,$emailruleorder) =
 4490:             &Apache::lonnet::inst_userrules($dom,'email');
 4491:         if (ref($emailrules) eq 'HASH') {
 4492:             if (keys(%{$emailrules}) > 0) {
 4493:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4494:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4495:                 $$rowtotal ++;
 4496:             }
 4497:         }
 4498:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4499:     }
 4500:     return $datatable;
 4501: }
 4502: 
 4503: sub email_as_username {
 4504:     my ($rowtotal,$processing,$type) = @_;
 4505:     my %choices =
 4506:         &Apache::lonlocal::texthash (
 4507:                                       automatic => 'Automatic approval',
 4508:                                       approval  => 'Queued for approval',
 4509:                                     );
 4510:     my $output;
 4511:     foreach my $option ('automatic','approval') {
 4512:         my $checked;
 4513:         if (ref($processing) eq 'HASH') {
 4514:             if ($type eq '') {   
 4515:                 if (!exists($processing->{'default'})) {
 4516:                     if ($option eq 'automatic') {
 4517:                         $checked = ' checked="checked"';
 4518:                     }
 4519:                 } else {
 4520:                     if ($processing->{'default'} eq $option) {
 4521:                         $checked = ' checked="checked"';
 4522:                     }
 4523:                 }
 4524:             } else {
 4525:                 if (!exists($processing->{$type})) {
 4526:                     if ($option eq 'automatic') {
 4527:                         $checked = ' checked="checked"';
 4528:                     }
 4529:                 } else {
 4530:                     if ($processing->{$type} eq $option) {
 4531:                         $checked = ' checked="checked"';
 4532:                     }
 4533:                 }
 4534:             }
 4535:         } elsif ($option eq 'automatic') {
 4536:             $checked = ' checked="checked"'; 
 4537:         }
 4538:         my $name = 'cancreate_emailprocess';
 4539:         if (($type ne '') && ($type ne 'default')) {
 4540:             $name .= '_'.$type;
 4541:         }
 4542:         $output .= '<span class="LC_nobreak"><label>'.
 4543:                    '<input type="radio" name="'.$name.'"'.
 4544:                    $checked.' value="'.$option.'" />'.
 4545:                    $choices{$option}.'</label></span>';
 4546:         if ($type eq '') {
 4547:             $output .= '&nbsp;';
 4548:         } else {
 4549:             $output .= '<br />';
 4550:         }
 4551:     }
 4552:     $$rowtotal ++;
 4553:     return $output;
 4554: }
 4555: 
 4556: sub captcha_choice {
 4557:     my ($context,$settings,$itemcount) = @_;
 4558:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4559:     my %lt = &captcha_phrases();
 4560:     $keyentry = 'hidden';
 4561:     if ($context eq 'cancreate') {
 4562:         $rowname = &mt('CAPTCHA validation');
 4563:     } elsif ($context eq 'login') {
 4564:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4565:     }
 4566:     if (ref($settings) eq 'HASH') {
 4567:         if ($settings->{'captcha'}) {
 4568:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4569:         } else {
 4570:             $checked{'original'} = ' checked="checked"';
 4571:         }
 4572:         if ($settings->{'captcha'} eq 'recaptcha') {
 4573:             $pubtext = $lt{'pub'};
 4574:             $privtext = $lt{'priv'};
 4575:             $keyentry = 'text';
 4576:         }
 4577:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4578:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4579:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4580:         }
 4581:     } else {
 4582:         $checked{'original'} = ' checked="checked"';
 4583:     }
 4584:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4585:     my $output = '<tr'.$css_class.'>'.
 4586:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4587:                  '<table><tr><td>'."\n";
 4588:     foreach my $option ('original','recaptcha','notused') {
 4589:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4590:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4591:                    $lt{$option}.'</label></span>';
 4592:         unless ($option eq 'notused') {
 4593:             $output .= ('&nbsp;'x2)."\n";
 4594:         }
 4595:     }
 4596: #
 4597: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4598: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4599: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4600: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4601: #
 4602:     $output .= '</td></tr>'."\n".
 4603:                '<tr><td>'."\n".
 4604:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4605:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4606:                $currpub.'" size="40" /></span><br />'."\n".
 4607:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4608:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4609:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4610:                '</td></tr>';
 4611:     return $output;
 4612: }
 4613: 
 4614: sub user_formats_row {
 4615:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4616:     my $output;
 4617:     my %text = (
 4618:                    'username' => 'new usernames',
 4619:                    'id'       => 'IDs',
 4620:                    'email'    => 'self-created accounts (e-mail)',
 4621:                );
 4622:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4623:     $output = '<tr '.$css_class.'>'.
 4624:               '<td><span class="LC_nobreak">';
 4625:     if ($type eq 'email') {
 4626:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4627:     } else {
 4628:         $output .= &mt("Format rules to check for $text{$type}: ");
 4629:     }
 4630:     $output .= '</span></td>'.
 4631:                '<td class="LC_left_item" colspan="2"><table>';
 4632:     my $rem;
 4633:     if (ref($ruleorder) eq 'ARRAY') {
 4634:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4635:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4636:                 my $rem = $i%($numinrow);
 4637:                 if ($rem == 0) {
 4638:                     if ($i > 0) {
 4639:                         $output .= '</tr>';
 4640:                     }
 4641:                     $output .= '<tr>';
 4642:                 }
 4643:                 my $check = ' ';
 4644:                 if (ref($settings) eq 'HASH') {
 4645:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4646:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4647:                             $check = ' checked="checked" ';
 4648:                         }
 4649:                     }
 4650:                 }
 4651:                 $output .= '<td class="LC_left_item">'.
 4652:                            '<span class="LC_nobreak"><label>'.
 4653:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4654:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4655:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4656:             }
 4657:         }
 4658:         $rem = @{$ruleorder}%($numinrow);
 4659:     }
 4660:     my $colsleft = $numinrow - $rem;
 4661:     if ($colsleft > 1 ) {
 4662:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4663:                    '&nbsp;</td>';
 4664:     } elsif ($colsleft == 1) {
 4665:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4666:     }
 4667:     $output .= '</tr></table></td></tr>';
 4668:     return $output;
 4669: }
 4670: 
 4671: sub usercreation_types {
 4672:     my %lt = &Apache::lonlocal::texthash (
 4673:                     author     => 'When adding a co-author',
 4674:                     course     => 'When adding a user to a course',
 4675:                     requestcrs => 'When requesting a course',
 4676:                     any        => 'Any',
 4677:                     official   => 'Institutional only ',
 4678:                     unofficial => 'Non-institutional only',
 4679:                     none       => 'None',
 4680:     );
 4681:     return %lt;
 4682: }
 4683: 
 4684: sub selfcreation_types {
 4685:     my %lt = &Apache::lonlocal::texthash (
 4686:                     selfcreate => 'User creates own account',
 4687:                     any        => 'Any',
 4688:                     official   => 'Institutional only ',
 4689:                     unofficial => 'Non-institutional only',
 4690:                     email      => 'E-mail address',
 4691:                     login      => 'Institutional Login',
 4692:                     sso        => 'SSO',
 4693:              );
 4694: }
 4695: 
 4696: sub authtype_names {
 4697:     my %lt = &Apache::lonlocal::texthash(
 4698:                       int    => 'Internal',
 4699:                       krb4   => 'Kerberos 4',
 4700:                       krb5   => 'Kerberos 5',
 4701:                       loc    => 'Local',
 4702:                   );
 4703:     return %lt;
 4704: }
 4705: 
 4706: sub context_names {
 4707:     my %context_title = &Apache::lonlocal::texthash(
 4708:        author => 'Creating users when an Author',
 4709:        course => 'Creating users when in a course',
 4710:        domain => 'Creating users when a Domain Coordinator',
 4711:     );
 4712:     return %context_title;
 4713: }
 4714: 
 4715: sub print_usermodification {
 4716:     my ($position,$dom,$settings,$rowtotal) = @_;
 4717:     my $numinrow = 4;
 4718:     my ($context,$datatable,$rowcount);
 4719:     if ($position eq 'top') {
 4720:         $rowcount = 0;
 4721:         $context = 'author'; 
 4722:         foreach my $role ('ca','aa') {
 4723:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4724:                                                    $numinrow,$rowcount);
 4725:             $$rowtotal ++;
 4726:             $rowcount ++;
 4727:         }
 4728:     } elsif ($position eq 'bottom') {
 4729:         $context = 'course';
 4730:         $rowcount = 0;
 4731:         foreach my $role ('st','ep','ta','in','cr') {
 4732:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4733:                                                    $numinrow,$rowcount);
 4734:             $$rowtotal ++;
 4735:             $rowcount ++;
 4736:         }
 4737:     }
 4738:     return $datatable;
 4739: }
 4740: 
 4741: sub print_defaults {
 4742:     my ($position,$dom,$settings,$rowtotal) = @_;
 4743:     my $rownum = 0;
 4744:     my ($datatable,$css_class);
 4745:     if ($position eq 'top') {
 4746:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4747:                      'datelocale_def','portal_def');
 4748:         my %defaults;
 4749:         if (ref($settings) eq 'HASH') {
 4750:             %defaults = %{$settings};
 4751:         } else {
 4752:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4753:             foreach my $item (@items) {
 4754:                 $defaults{$item} = $domdefaults{$item};
 4755:             }
 4756:         }
 4757:         my $titles = &defaults_titles($dom);
 4758:         foreach my $item (@items) {
 4759:             if ($rownum%2) {
 4760:                 $css_class = '';
 4761:             } else {
 4762:                 $css_class = ' class="LC_odd_row" ';
 4763:             }
 4764:             $datatable .= '<tr'.$css_class.'>'.
 4765:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4766:                           '</span></td><td class="LC_right_item" colspan="3">';
 4767:             if ($item eq 'auth_def') {
 4768:                 my @authtypes = ('internal','krb4','krb5','localauth');
 4769:                 my %shortauth = (
 4770:                                  internal => 'int',
 4771:                                  krb4 => 'krb4',
 4772:                                  krb5 => 'krb5',
 4773:                                  localauth  => 'loc'
 4774:                                 );
 4775:                 my %authnames = &authtype_names();
 4776:                 foreach my $auth (@authtypes) {
 4777:                     my $checked = ' ';
 4778:                     if ($defaults{$item} eq $auth) {
 4779:                         $checked = ' checked="checked" ';
 4780:                     }
 4781:                     $datatable .= '<label><input type="radio" name="'.$item.
 4782:                                   '" value="'.$auth.'"'.$checked.'/>'.
 4783:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4784:                 }
 4785:             } elsif ($item eq 'timezone_def') {
 4786:                 my $includeempty = 1;
 4787:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4788:             } elsif ($item eq 'datelocale_def') {
 4789:                 my $includeempty = 1;
 4790:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4791:             } elsif ($item eq 'lang_def') {
 4792:                 my $includeempty = 1;
 4793:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 4794:             } else {
 4795:                 my $size;
 4796:                 if ($item eq 'portal_def') {
 4797:                     $size = ' size="25"';
 4798:                 }
 4799:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4800:                               $defaults{$item}.'"'.$size.' />';
 4801:             }
 4802:             $datatable .= '</td></tr>';
 4803:             $rownum ++;
 4804:         }
 4805:     } else {
 4806:         my (%defaults);
 4807:         if (ref($settings) eq 'HASH') {
 4808:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 4809:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 4810:                 my $maxnum = @{$settings->{'inststatusorder'}};
 4811:                 for (my $i=0; $i<$maxnum; $i++) {
 4812:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 4813:                     my $item = $settings->{'inststatusorder'}->[$i];
 4814:                     my $title = $settings->{'inststatustypes'}->{$item};
 4815:                     my $guestok;
 4816:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 4817:                         $guestok = 1;
 4818:                     }
 4819:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 4820:                     $datatable .= '<tr'.$css_class.'>'.
 4821:                                   '<td><span class="LC_nobreak">'.
 4822:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 4823:                     for (my $k=0; $k<=$maxnum; $k++) {
 4824:                         my $vpos = $k+1;
 4825:                         my $selstr;
 4826:                         if ($k == $i) {
 4827:                             $selstr = ' selected="selected" ';
 4828:                         }
 4829:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4830:                     }
 4831:                     my ($checkedon,$checkedoff);
 4832:                     $checkedoff = ' checked="checked"';
 4833:                     if ($guestok) {
 4834:                         $checkedon = $checkedoff;
 4835:                         $checkedoff = ''; 
 4836:                     }
 4837:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 4838:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 4839:                                   &mt('delete').'</span></td>'.
 4840:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 4841:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 4842:                                   '</span></td>'.
 4843:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4844:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 4845:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 4846:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 4847:                                   &mt('No').'</label></span></td></tr>';
 4848:                 }
 4849:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 4850:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 4851:                 $datatable .= '<tr '.$css_class.'>'.
 4852:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 4853:                 for (my $k=0; $k<=$maxnum; $k++) {
 4854:                     my $vpos = $k+1;
 4855:                     my $selstr;
 4856:                     if ($k == $maxnum) {
 4857:                         $selstr = ' selected="selected" ';
 4858:                     }
 4859:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4860:                 }
 4861:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 4862:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 4863:                               '&nbsp;'.&mt('(new)').
 4864:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 4865:                               &mt('Name displayed:').
 4866:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 4867:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 4868:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 4869:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 4870:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 4871:                               &mt('No').'</label></span></td></tr>';
 4872:                               '</tr>'."\n";
 4873:                 $rownum ++;
 4874:             }
 4875:         }
 4876:     }
 4877:     $$rowtotal += $rownum;
 4878:     return $datatable;
 4879: }
 4880: 
 4881: sub get_languages_hash {
 4882:     my %langchoices;
 4883:     foreach my $id (&Apache::loncommon::languageids()) {
 4884:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4885:         if ($code ne '') {
 4886:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4887:         }
 4888:     }
 4889:     return %langchoices;
 4890: }
 4891: 
 4892: sub defaults_titles {
 4893:     my ($dom) = @_;
 4894:     my %titles = &Apache::lonlocal::texthash (
 4895:                    'auth_def'      => 'Default authentication type',
 4896:                    'auth_arg_def'  => 'Default authentication argument',
 4897:                    'lang_def'      => 'Default language',
 4898:                    'timezone_def'  => 'Default timezone',
 4899:                    'datelocale_def' => 'Default locale for dates',
 4900:                    'portal_def'     => 'Portal/Default URL',
 4901:                  );
 4902:     if ($dom) {
 4903:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4904:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4905:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4906:         $protocol = 'http' if ($protocol ne 'https');
 4907:         if ($uint_dom) {
 4908:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4909:                                          $uint_dom);
 4910:         }
 4911:     }
 4912:     return (\%titles);
 4913: }
 4914: 
 4915: sub print_scantronformat {
 4916:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4917:     my $itemcount = 1;
 4918:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4919:         %confhash);
 4920:     my $switchserver = &check_switchserver($dom,$confname);
 4921:     my %lt = &Apache::lonlocal::texthash (
 4922:                 default => 'Default bubblesheet format file error',
 4923:                 custom  => 'Custom bubblesheet format file error',
 4924:              );
 4925:     my %scantronfiles = (
 4926:         default => 'default.tab',
 4927:         custom => 'custom.tab',
 4928:     );
 4929:     foreach my $key (keys(%scantronfiles)) {
 4930:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4931:                               .$scantronfiles{$key};
 4932:     }
 4933:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4934:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4935:         if (!$switchserver) {
 4936:             my $servadm = $r->dir_config('lonAdmEMail');
 4937:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4938:             if ($configuserok eq 'ok') {
 4939:                 if ($author_ok eq 'ok') {
 4940:                     my %legacyfile = (
 4941:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4942:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4943:                     );
 4944:                     my %md5chk;
 4945:                     foreach my $type (keys(%legacyfile)) {
 4946:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4947:                         chomp($md5chk{$type});
 4948:                     }
 4949:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4950:                         foreach my $type (keys(%legacyfile)) {
 4951:                             ($scantronurls{$type},my $error) = 
 4952:                                 &legacy_scantronformat($r,$dom,$confname,
 4953:                                                  $type,$legacyfile{$type},
 4954:                                                  $scantronurls{$type},
 4955:                                                  $scantronfiles{$type});
 4956:                             if ($error ne '') {
 4957:                                 $error{$type} = $error;
 4958:                             }
 4959:                         }
 4960:                         if (keys(%error) == 0) {
 4961:                             $is_custom = 1;
 4962:                             $confhash{'scantron'}{'scantronformat'} = 
 4963:                                 $scantronurls{'custom'};
 4964:                             my $putresult = 
 4965:                                 &Apache::lonnet::put_dom('configuration',
 4966:                                                          \%confhash,$dom);
 4967:                             if ($putresult ne 'ok') {
 4968:                                 $error{'custom'} = 
 4969:                                     '<span class="LC_error">'.
 4970:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4971:                             }
 4972:                         }
 4973:                     } else {
 4974:                         ($scantronurls{'default'},my $error) =
 4975:                             &legacy_scantronformat($r,$dom,$confname,
 4976:                                           'default',$legacyfile{'default'},
 4977:                                           $scantronurls{'default'},
 4978:                                           $scantronfiles{'default'});
 4979:                         if ($error eq '') {
 4980:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4981:                             my $putresult =
 4982:                                 &Apache::lonnet::put_dom('configuration',
 4983:                                                          \%confhash,$dom);
 4984:                             if ($putresult ne 'ok') {
 4985:                                 $error{'default'} =
 4986:                                     '<span class="LC_error">'.
 4987:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4988:                             }
 4989:                         } else {
 4990:                             $error{'default'} = $error;
 4991:                         }
 4992:                     }
 4993:                 }
 4994:             }
 4995:         } else {
 4996:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4997:         }
 4998:     }
 4999:     if (ref($settings) eq 'HASH') {
 5000:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 5001:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 5002:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 5003:                 $scantronurl = '';
 5004:             } else {
 5005:                 $scantronurl = $settings->{'scantronformat'};
 5006:             }
 5007:             $is_custom = 1;
 5008:         } else {
 5009:             $scantronurl = $scantronurls{'default'};
 5010:         }
 5011:     } else {
 5012:         if ($is_custom) {
 5013:             $scantronurl = $scantronurls{'custom'};
 5014:         } else {
 5015:             $scantronurl = $scantronurls{'default'};
 5016:         }
 5017:     }
 5018:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5019:     $datatable .= '<tr'.$css_class.'>';
 5020:     if (!$is_custom) {
 5021:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 5022:                       '<span class="LC_nobreak">';
 5023:         if ($scantronurl) {
 5024:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 5025:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 5026:         } else {
 5027:             $datatable = &mt('File unavailable for display');
 5028:         }
 5029:         $datatable .= '</span></td>';
 5030:         if (keys(%error) == 0) { 
 5031:             $datatable .= '<td valign="bottom">';
 5032:             if (!$switchserver) {
 5033:                 $datatable .= &mt('Upload:').'<br />';
 5034:             }
 5035:         } else {
 5036:             my $errorstr;
 5037:             foreach my $key (sort(keys(%error))) {
 5038:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 5039:             }
 5040:             $datatable .= '<td>'.$errorstr;
 5041:         }
 5042:     } else {
 5043:         if (keys(%error) > 0) {
 5044:             my $errorstr;
 5045:             foreach my $key (sort(keys(%error))) {
 5046:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 5047:             } 
 5048:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 5049:         } elsif ($scantronurl) {
 5050:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 5051:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 5052:             $datatable .= '<td><span class="LC_nobreak">'.
 5053:                           $link.
 5054:                           '<label><input type="checkbox" name="scantronformat_del"'.
 5055:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 5056:                           '<td><span class="LC_nobreak">&nbsp;'.
 5057:                           &mt('Replace:').'</span><br />';
 5058:         }
 5059:     }
 5060:     if (keys(%error) == 0) {
 5061:         if ($switchserver) {
 5062:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5063:         } else {
 5064:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 5065:                          '<input type="file" name="scantronformat" /></span>';
 5066:         }
 5067:     }
 5068:     $datatable .= '</td></tr>';
 5069:     $$rowtotal ++;
 5070:     return $datatable;
 5071: }
 5072: 
 5073: sub legacy_scantronformat {
 5074:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 5075:     my ($url,$error);
 5076:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 5077:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 5078:         (my $result,$url) =
 5079:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 5080:                          '','',$newfile);
 5081:         if ($result ne 'ok') {
 5082:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 5083:         }
 5084:     }
 5085:     return ($url,$error);
 5086: }
 5087: 
 5088: sub print_coursecategories {
 5089:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 5090:     my $datatable;
 5091:     if ($position eq 'top') {
 5092:         my (%checked);
 5093:         my @catitems = ('unauth','auth');
 5094:         my @cattypes = ('std','domonly','codesrch','none');
 5095:         $checked{'unauth'} = 'std';
 5096:         $checked{'auth'} = 'std';
 5097:         if (ref($settings) eq 'HASH') {
 5098:             foreach my $type (@cattypes) {
 5099:                 if ($type eq $settings->{'unauth'}) {
 5100:                     $checked{'unauth'} = $type;
 5101:                 }
 5102:                 if ($type eq $settings->{'auth'}) {
 5103:                     $checked{'auth'} = $type;
 5104:                 }
 5105:             }
 5106:         }
 5107:         my %lt = &Apache::lonlocal::texthash (
 5108:                                                unauth   => 'Catalog type for unauthenticated users',
 5109:                                                auth     => 'Catalog type for authenticated users',
 5110:                                                none     => 'No catalog',
 5111:                                                std      => 'Standard catalog',
 5112:                                                domonly  => 'Domain-only catalog',
 5113:                                                codesrch => "Code search form",
 5114:                                              );
 5115:        my $itemcount = 0;
 5116:        foreach my $item (@catitems) {
 5117:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 5118:            $datatable .= '<tr '.$css_class.'>'.
 5119:                          '<td>'.$lt{$item}.'</td>'.
 5120:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 5121:            foreach my $type (@cattypes) {
 5122:                my $ischecked;
 5123:                if ($checked{$item} eq $type) {
 5124:                    $ischecked=' checked="checked"';
 5125:                }
 5126:                $datatable .= '<label>'.
 5127:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 5128:                              ' />'.$lt{$type}.'</label>&nbsp;';
 5129:            }
 5130:            $datatable .= '</td></tr>';
 5131:            $itemcount ++;
 5132:         }
 5133:         $$rowtotal += $itemcount;
 5134:     } elsif ($position eq 'middle') {
 5135:         my $toggle_cats_crs = ' ';
 5136:         my $toggle_cats_dom = ' checked="checked" ';
 5137:         my $can_cat_crs = ' ';
 5138:         my $can_cat_dom = ' checked="checked" ';
 5139:         my $toggle_catscomm_comm = ' ';
 5140:         my $toggle_catscomm_dom = ' checked="checked" ';
 5141:         my $can_catcomm_comm = ' ';
 5142:         my $can_catcomm_dom = ' checked="checked" ';
 5143: 
 5144:         if (ref($settings) eq 'HASH') {
 5145:             if ($settings->{'togglecats'} eq 'crs') {
 5146:                 $toggle_cats_crs = $toggle_cats_dom;
 5147:                 $toggle_cats_dom = ' ';
 5148:             }
 5149:             if ($settings->{'categorize'} eq 'crs') {
 5150:                 $can_cat_crs = $can_cat_dom;
 5151:                 $can_cat_dom = ' ';
 5152:             }
 5153:             if ($settings->{'togglecatscomm'} eq 'comm') {
 5154:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 5155:                 $toggle_catscomm_dom = ' ';
 5156:             }
 5157:             if ($settings->{'categorizecomm'} eq 'comm') {
 5158:                 $can_catcomm_comm = $can_catcomm_dom;
 5159:                 $can_catcomm_dom = ' ';
 5160:             }
 5161:         }
 5162:         my %title = &Apache::lonlocal::texthash (
 5163:                      togglecats     => 'Show/Hide a course in catalog',
 5164:                      togglecatscomm => 'Show/Hide a community in catalog',
 5165:                      categorize     => 'Assign a category to a course',
 5166:                      categorizecomm => 'Assign a category to a community',
 5167:                     );
 5168:         my %level = &Apache::lonlocal::texthash (
 5169:                      dom  => 'Set in Domain',
 5170:                      crs  => 'Set in Course',
 5171:                      comm => 'Set in Community',
 5172:                     );
 5173:         $datatable = '<tr class="LC_odd_row">'.
 5174:                   '<td>'.$title{'togglecats'}.'</td>'.
 5175:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5176:                   '<input type="radio" name="togglecats"'.
 5177:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5178:                   '<label><input type="radio" name="togglecats"'.
 5179:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5180:                   '</tr><tr>'.
 5181:                   '<td>'.$title{'categorize'}.'</td>'.
 5182:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5183:                   '<label><input type="radio" name="categorize"'.
 5184:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5185:                   '<label><input type="radio" name="categorize"'.
 5186:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5187:                   '</tr><tr class="LC_odd_row">'.
 5188:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 5189:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5190:                   '<input type="radio" name="togglecatscomm"'.
 5191:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5192:                   '<label><input type="radio" name="togglecatscomm"'.
 5193:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5194:                   '</tr><tr>'.
 5195:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 5196:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5197:                   '<label><input type="radio" name="categorizecomm"'.
 5198:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5199:                   '<label><input type="radio" name="categorizecomm"'.
 5200:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5201:                   '</tr>';
 5202:         $$rowtotal += 4;
 5203:     } else {
 5204:         my $css_class;
 5205:         my $itemcount = 1;
 5206:         my $cathash; 
 5207:         if (ref($settings) eq 'HASH') {
 5208:             $cathash = $settings->{'cats'};
 5209:         }
 5210:         if (ref($cathash) eq 'HASH') {
 5211:             my (@cats,@trails,%allitems,%idx,@jsarray);
 5212:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 5213:                                                    \%allitems,\%idx,\@jsarray);
 5214:             my $maxdepth = scalar(@cats);
 5215:             my $colattrib = '';
 5216:             if ($maxdepth > 2) {
 5217:                 $colattrib = ' colspan="2" ';
 5218:             }
 5219:             my @path;
 5220:             if (@cats > 0) {
 5221:                 if (ref($cats[0]) eq 'ARRAY') {
 5222:                     my $numtop = @{$cats[0]};
 5223:                     my $maxnum = $numtop;
 5224:                     my %default_names = (
 5225:                           instcode    => &mt('Official courses'),
 5226:                           communities => &mt('Communities'),
 5227:                     );
 5228: 
 5229:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 5230:                         ($cathash->{'instcode::0'} eq '') ||
 5231:                         (!grep(/^communities$/,@{$cats[0]})) || 
 5232:                         ($cathash->{'communities::0'} eq '')) {
 5233:                         $maxnum ++;
 5234:                     }
 5235:                     my $lastidx;
 5236:                     for (my $i=0; $i<$numtop; $i++) {
 5237:                         my $parent = $cats[0][$i];
 5238:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5239:                         my $item = &escape($parent).'::0';
 5240:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 5241:                         $lastidx = $idx{$item};
 5242:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5243:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 5244:                         for (my $k=0; $k<=$maxnum; $k++) {
 5245:                             my $vpos = $k+1;
 5246:                             my $selstr;
 5247:                             if ($k == $i) {
 5248:                                 $selstr = ' selected="selected" ';
 5249:                             }
 5250:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5251:                         }
 5252:                         $datatable .= '</select></span></td><td>';
 5253:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 5254:                             $datatable .=  '<span class="LC_nobreak">'
 5255:                                            .$default_names{$parent}.'</span>';
 5256:                             if ($parent eq 'instcode') {
 5257:                                 $datatable .= '<br /><span class="LC_nobreak">('
 5258:                                               .&mt('with institutional codes')
 5259:                                               .')</span></td><td'.$colattrib.'>';
 5260:                             } else {
 5261:                                 $datatable .= '<table><tr><td>';
 5262:                             }
 5263:                             $datatable .= '<span class="LC_nobreak">'
 5264:                                           .'<label><input type="radio" name="'
 5265:                                           .$parent.'" value="1" checked="checked" />'
 5266:                                           .&mt('Display').'</label>';
 5267:                             if ($parent eq 'instcode') {
 5268:                                 $datatable .= '&nbsp;';
 5269:                             } else {
 5270:                                 $datatable .= '</span></td></tr><tr><td>'
 5271:                                               .'<span class="LC_nobreak">';
 5272:                             }
 5273:                             $datatable .= '<label><input type="radio" name="'
 5274:                                           .$parent.'" value="0" />'
 5275:                                           .&mt('Do not display').'</label></span>';
 5276:                             if ($parent eq 'communities') {
 5277:                                 $datatable .= '</td></tr></table>';
 5278:                             }
 5279:                             $datatable .= '</td>';
 5280:                         } else {
 5281:                             $datatable .= $parent
 5282:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 5283:                                           .'<input type="checkbox" name="deletecategory" '
 5284:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 5285:                         }
 5286:                         my $depth = 1;
 5287:                         push(@path,$parent);
 5288:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 5289:                         pop(@path);
 5290:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 5291:                         $itemcount ++;
 5292:                     }
 5293:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5294:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 5295:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 5296:                     for (my $k=0; $k<=$maxnum; $k++) {
 5297:                         my $vpos = $k+1;
 5298:                         my $selstr;
 5299:                         if ($k == $numtop) {
 5300:                             $selstr = ' selected="selected" ';
 5301:                         }
 5302:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5303:                     }
 5304:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 5305:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 5306:                                   .'</tr>'."\n";
 5307:                     $itemcount ++;
 5308:                     foreach my $default ('instcode','communities') {
 5309:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 5310:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5311:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 5312:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 5313:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5314:                             for (my $k=0; $k<=$maxnum; $k++) {
 5315:                                 my $vpos = $k+1;
 5316:                                 my $selstr;
 5317:                                 if ($k == $maxnum) {
 5318:                                     $selstr = ' selected="selected" ';
 5319:                                 }
 5320:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5321:                             }
 5322:                             $datatable .= '</select></span></td>'.
 5323:                                           '<td><span class="LC_nobreak">'.
 5324:                                           $default_names{$default}.'</span>';
 5325:                             if ($default eq 'instcode') {
 5326:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5327:                                               .&mt('with institutional codes').')</span>';
 5328:                             }
 5329:                             $datatable .= '</td>'
 5330:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5331:                                           .&mt('Display').'</label>&nbsp;'
 5332:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5333:                                           .&mt('Do not display').'</label></span></td></tr>';
 5334:                         }
 5335:                     }
 5336:                 }
 5337:             } else {
 5338:                 $datatable .= &initialize_categories($itemcount);
 5339:             }
 5340:         } else {
 5341:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 5342:                           .&initialize_categories($itemcount);
 5343:         }
 5344:         $$rowtotal += $itemcount;
 5345:     }
 5346:     return $datatable;
 5347: }
 5348: 
 5349: sub print_serverstatuses {
 5350:     my ($dom,$settings,$rowtotal) = @_;
 5351:     my $datatable;
 5352:     my @pages = &serverstatus_pages();
 5353:     my (%namedaccess,%machineaccess);
 5354:     foreach my $type (@pages) {
 5355:         $namedaccess{$type} = '';
 5356:         $machineaccess{$type}= '';
 5357:     }
 5358:     if (ref($settings) eq 'HASH') {
 5359:         foreach my $type (@pages) {
 5360:             if (exists($settings->{$type})) {
 5361:                 if (ref($settings->{$type}) eq 'HASH') {
 5362:                     foreach my $key (keys(%{$settings->{$type}})) {
 5363:                         if ($key eq 'namedusers') {
 5364:                             $namedaccess{$type} = $settings->{$type}->{$key};
 5365:                         } elsif ($key eq 'machines') {
 5366:                             $machineaccess{$type} = $settings->{$type}->{$key};
 5367:                         }
 5368:                     }
 5369:                 }
 5370:             }
 5371:         }
 5372:     }
 5373:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 5374:     my $rownum = 0;
 5375:     my $css_class;
 5376:     foreach my $type (@pages) {
 5377:         $rownum ++;
 5378:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 5379:         $datatable .= '<tr'.$css_class.'>'.
 5380:                       '<td><span class="LC_nobreak">'.
 5381:                       $titles->{$type}.'</span></td>'.
 5382:                       '<td class="LC_left_item">'.
 5383:                       '<input type="text" name="'.$type.'_namedusers" '.
 5384:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 5385:                       '<td class="LC_right_item">'.
 5386:                       '<span class="LC_nobreak">'.
 5387:                       '<input type="text" name="'.$type.'_machines" '.
 5388:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 5389:                       '</td></tr>'."\n";
 5390:     }
 5391:     $$rowtotal += $rownum;
 5392:     return $datatable;
 5393: }
 5394: 
 5395: sub serverstatus_pages {
 5396:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 5397:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 5398:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 5399:             'uniquecodes','diskusage','coursecatalog');
 5400: }
 5401: 
 5402: sub defaults_javascript {
 5403:     my ($settings) = @_;
 5404:     return unless (ref($settings) eq 'HASH'); 
 5405:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5406:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 5407:         if ($maxnum eq '') {
 5408:             $maxnum = 0;
 5409:         }
 5410:         $maxnum ++;
 5411:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 5412:         return <<"ENDSCRIPT";
 5413: <script type="text/javascript">
 5414: // <![CDATA[
 5415: function reorderTypes(form,caller) {
 5416:     var changedVal;
 5417: $jstext 
 5418:     var newpos = 'addinststatus_pos';
 5419:     var current = new Array;
 5420:     var maxh = $maxnum;
 5421:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5422:     var oldVal;
 5423:     if (caller == newpos) {
 5424:         changedVal = newitemVal;
 5425:     } else {
 5426:         var curritem = 'inststatus_pos_'+caller;
 5427:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 5428:         current[newitemVal] = newpos;
 5429:     }
 5430:     for (var i=0; i<inststatuses.length; i++) {
 5431:         if (inststatuses[i] != caller) {
 5432:             var elementName = 'inststatus_pos_'+inststatuses[i];
 5433:             if (form.elements[elementName]) {
 5434:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5435:                 current[currVal] = elementName;
 5436:             }
 5437:         }
 5438:     }
 5439:     for (var j=0; j<maxh; j++) {
 5440:         if (current[j] == undefined) {
 5441:             oldVal = j;
 5442:         }
 5443:     }
 5444:     if (oldVal < changedVal) {
 5445:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5446:            var elementName = current[k];
 5447:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5448:         }
 5449:     } else {
 5450:         for (var k=changedVal; k<oldVal; k++) {
 5451:             var elementName = current[k];
 5452:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5453:         }
 5454:     }
 5455:     return;
 5456: }
 5457: 
 5458: // ]]>
 5459: </script>
 5460: 
 5461: ENDSCRIPT
 5462:     }
 5463: }
 5464: 
 5465: sub coursecategories_javascript {
 5466:     my ($settings) = @_;
 5467:     my ($output,$jstext,$cathash);
 5468:     if (ref($settings) eq 'HASH') {
 5469:         $cathash = $settings->{'cats'};
 5470:     }
 5471:     if (ref($cathash) eq 'HASH') {
 5472:         my (@cats,@jsarray,%idx);
 5473:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 5474:         if (@jsarray > 0) {
 5475:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 5476:             for (my $i=0; $i<@jsarray; $i++) {
 5477:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 5478:                     my $catstr = join('","',@{$jsarray[$i]});
 5479:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 5480:                 }
 5481:             }
 5482:         }
 5483:     } else {
 5484:         $jstext  = '    var categories = Array(1);'."\n".
 5485:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 5486:     }
 5487:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 5488:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 5489:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 5490:     &js_escape(\$instcode_reserved);
 5491:     &js_escape(\$communities_reserved);
 5492:     &js_escape(\$choose_again);
 5493:     $output = <<"ENDSCRIPT";
 5494: <script type="text/javascript">
 5495: // <![CDATA[
 5496: function reorderCats(form,parent,item,idx) {
 5497:     var changedVal;
 5498: $jstext
 5499:     var newpos = 'addcategory_pos';
 5500:     if (parent == '') {
 5501:         var has_instcode = 0;
 5502:         var maxtop = categories[idx].length;
 5503:         for (var j=0; j<maxtop; j++) {
 5504:             if (categories[idx][j] == 'instcode::0') {
 5505:                 has_instcode == 1;
 5506:             }
 5507:         }
 5508:         if (has_instcode == 0) {
 5509:             categories[idx][maxtop] = 'instcode_pos';
 5510:         }
 5511:     } else {
 5512:         newpos += '_'+parent;
 5513:     }
 5514:     var maxh = 1 + categories[idx].length;
 5515:     var current = new Array;
 5516:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5517:     if (item == newpos) {
 5518:         changedVal = newitemVal;
 5519:     } else {
 5520:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5521:         current[newitemVal] = newpos;
 5522:     }
 5523:     for (var i=0; i<categories[idx].length; i++) {
 5524:         var elementName = categories[idx][i];
 5525:         if (elementName != item) {
 5526:             if (form.elements[elementName]) {
 5527:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5528:                 current[currVal] = elementName;
 5529:             }
 5530:         }
 5531:     }
 5532:     var oldVal;
 5533:     for (var j=0; j<maxh; j++) {
 5534:         if (current[j] == undefined) {
 5535:             oldVal = j;
 5536:         }
 5537:     }
 5538:     if (oldVal < changedVal) {
 5539:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5540:            var elementName = current[k];
 5541:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5542:         }
 5543:     } else {
 5544:         for (var k=changedVal; k<oldVal; k++) {
 5545:             var elementName = current[k];
 5546:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5547:         }
 5548:     }
 5549:     return;
 5550: }
 5551: 
 5552: function categoryCheck(form) {
 5553:     if (form.elements['addcategory_name'].value == 'instcode') {
 5554:         alert('$instcode_reserved\\n$choose_again');
 5555:         return false;
 5556:     }
 5557:     if (form.elements['addcategory_name'].value == 'communities') {
 5558:         alert('$communities_reserved\\n$choose_again');
 5559:         return false;
 5560:     }
 5561:     return true;
 5562: }
 5563: 
 5564: // ]]>
 5565: </script>
 5566: 
 5567: ENDSCRIPT
 5568:     return $output;
 5569: }
 5570: 
 5571: sub initialize_categories {
 5572:     my ($itemcount) = @_;
 5573:     my ($datatable,$css_class,$chgstr);
 5574:     my %default_names = (
 5575:                       instcode    => 'Official courses (with institutional codes)',
 5576:                       communities => 'Communities',
 5577:                         );
 5578:     my $select0 = ' selected="selected"';
 5579:     my $select1 = '';
 5580:     foreach my $default ('instcode','communities') {
 5581:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5582:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 5583:         if ($default eq 'communities') {
 5584:             $select1 = $select0;
 5585:             $select0 = '';
 5586:         }
 5587:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5588:                      .'<select name="'.$default.'_pos">'
 5589:                      .'<option value="0"'.$select0.'>1</option>'
 5590:                      .'<option value="1"'.$select1.'>2</option>'
 5591:                      .'<option value="2">3</option></select>&nbsp;'
 5592:                      .$default_names{$default}
 5593:                      .'</span></td><td><span class="LC_nobreak">'
 5594:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5595:                      .&mt('Display').'</label>&nbsp;<label>'
 5596:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5597:                  .'</label></span></td></tr>';
 5598:         $itemcount ++;
 5599:     }
 5600:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5601:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5602:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5603:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5604:                   .'<option value="0">1</option>'
 5605:                   .'<option value="1">2</option>'
 5606:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5607:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5608:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5609:     return $datatable;
 5610: }
 5611: 
 5612: sub build_category_rows {
 5613:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5614:     my ($text,$name,$item,$chgstr);
 5615:     if (ref($cats) eq 'ARRAY') {
 5616:         my $maxdepth = scalar(@{$cats});
 5617:         if (ref($cats->[$depth]) eq 'HASH') {
 5618:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5619:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5620:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5621:                 $text .= '<td><table class="LC_data_table">';
 5622:                 my ($idxnum,$parent_name,$parent_item);
 5623:                 my $higher = $depth - 1;
 5624:                 if ($higher == 0) {
 5625:                     $parent_name = &escape($parent).'::'.$higher;
 5626:                 } else {
 5627:                     if (ref($path) eq 'ARRAY') {
 5628:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5629:                     }
 5630:                 }
 5631:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5632:                 for (my $j=0; $j<=$numchildren; $j++) {
 5633:                     if ($j < $numchildren) {
 5634:                         $name = $cats->[$depth]{$parent}[$j];
 5635:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5636:                         $idxnum = $idx->{$item};
 5637:                     } else {
 5638:                         $name = $parent_name;
 5639:                         $item = $parent_item;
 5640:                     }
 5641:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5642:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5643:                     for (my $i=0; $i<=$numchildren; $i++) {
 5644:                         my $vpos = $i+1;
 5645:                         my $selstr;
 5646:                         if ($j == $i) {
 5647:                             $selstr = ' selected="selected" ';
 5648:                         }
 5649:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5650:                     }
 5651:                     $text .= '</select>&nbsp;';
 5652:                     if ($j < $numchildren) {
 5653:                         my $deeper = $depth+1;
 5654:                         $text .= $name.'&nbsp;'
 5655:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5656:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5657:                         if(ref($path) eq 'ARRAY') {
 5658:                             push(@{$path},$name);
 5659:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5660:                             pop(@{$path});
 5661:                         }
 5662:                     } else {
 5663:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5664:                         if ($j == $numchildren) {
 5665:                             $text .= $name;
 5666:                         } else {
 5667:                             $text .= $item;
 5668:                         }
 5669:                         $text .= '" value="" />';
 5670:                     }
 5671:                     $text .= '</td></tr>';
 5672:                 }
 5673:                 $text .= '</table></td>';
 5674:             } else {
 5675:                 my $higher = $depth-1;
 5676:                 if ($higher == 0) {
 5677:                     $name = &escape($parent).'::'.$higher;
 5678:                 } else {
 5679:                     if (ref($path) eq 'ARRAY') {
 5680:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5681:                     }
 5682:                 }
 5683:                 my $colspan;
 5684:                 if ($parent ne 'instcode') {
 5685:                     $colspan = $maxdepth - $depth - 1;
 5686:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5687:                 }
 5688:             }
 5689:         }
 5690:     }
 5691:     return $text;
 5692: }
 5693: 
 5694: sub modifiable_userdata_row {
 5695:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5696:     my ($role,$rolename,$statustype);
 5697:     $role = $item;
 5698:     if ($context eq 'cancreate') {
 5699:         if ($item =~ /^emailusername_(.+)$/) {
 5700:             $statustype = $1;
 5701:             $role = 'emailusername';
 5702:             if (ref($usertypes) eq 'HASH') {
 5703:                 if ($usertypes->{$statustype}) {
 5704:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5705:                 } else {
 5706:                     $rolename = &mt('Data provided by user');
 5707:                 }
 5708:             }
 5709:         }
 5710:     } elsif ($context eq 'selfcreate') {
 5711:         if (ref($usertypes) eq 'HASH') {
 5712:             $rolename = $usertypes->{$role};
 5713:         } else {
 5714:             $rolename = $role;
 5715:         }
 5716:     } else {
 5717:         if ($role eq 'cr') {
 5718:             $rolename = &mt('Custom role');
 5719:         } else {
 5720:             $rolename = &Apache::lonnet::plaintext($role);
 5721:         }
 5722:     }
 5723:     my (@fields,%fieldtitles);
 5724:     if (ref($fieldsref) eq 'ARRAY') {
 5725:         @fields = @{$fieldsref};
 5726:     } else {
 5727:         @fields = ('lastname','firstname','middlename','generation',
 5728:                    'permanentemail','id');
 5729:     }
 5730:     if ((ref($titlesref) eq 'HASH')) {
 5731:         %fieldtitles = %{$titlesref};
 5732:     } else {
 5733:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5734:     }
 5735:     my $output;
 5736:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5737:     $output = '<tr '.$css_class.'>'.
 5738:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5739:               '<td class="LC_left_item" colspan="2"><table>';
 5740:     my $rem;
 5741:     my %checks;
 5742:     if (ref($settings) eq 'HASH') {
 5743:         if (ref($settings->{$context}) eq 'HASH') {
 5744:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5745:                 my $hashref = $settings->{$context}->{$role};
 5746:                 if ($role eq 'emailusername') {
 5747:                     if ($statustype) {
 5748:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5749:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5750:                             if (ref($hashref) eq 'HASH') { 
 5751:                                 foreach my $field (@fields) {
 5752:                                     if ($hashref->{$field}) {
 5753:                                         $checks{$field} = $hashref->{$field};
 5754:                                     }
 5755:                                 }
 5756:                             }
 5757:                         }
 5758:                     }
 5759:                 } else {
 5760:                     if (ref($hashref) eq 'HASH') {
 5761:                         foreach my $field (@fields) {
 5762:                             if ($hashref->{$field}) {
 5763:                                 $checks{$field} = ' checked="checked" ';
 5764:                             }
 5765:                         }
 5766:                     }
 5767:                 }
 5768:             }
 5769:         }
 5770:     }
 5771:      
 5772:     for (my $i=0; $i<@fields; $i++) {
 5773:         my $rem = $i%($numinrow);
 5774:         if ($rem == 0) {
 5775:             if ($i > 0) {
 5776:                 $output .= '</tr>';
 5777:             }
 5778:             $output .= '<tr>';
 5779:         }
 5780:         my $check = ' ';
 5781:         unless ($role eq 'emailusername') {
 5782:             if (exists($checks{$fields[$i]})) {
 5783:                 $check = $checks{$fields[$i]}
 5784:             } else {
 5785:                 if ($role eq 'st') {
 5786:                     if (ref($settings) ne 'HASH') {
 5787:                         $check = ' checked="checked" '; 
 5788:                     }
 5789:                 }
 5790:             }
 5791:         }
 5792:         $output .= '<td class="LC_left_item">'.
 5793:                    '<span class="LC_nobreak">';
 5794:         if ($role eq 'emailusername') {
 5795:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5796:                 $checks{$fields[$i]} = 'omit';
 5797:             }
 5798:             foreach my $option ('required','optional','omit') {
 5799:                 my $checked='';
 5800:                 if ($checks{$fields[$i]} eq $option) {
 5801:                     $checked='checked="checked" ';
 5802:                 }
 5803:                 $output .= '<label>'.
 5804:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5805:                            &mt($option).'</label>'.('&nbsp;' x2);
 5806:             }
 5807:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5808:         } else {
 5809:             $output .= '<label>'.
 5810:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5811:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5812:                        '</label>';
 5813:         }
 5814:         $output .= '</span></td>';
 5815:         $rem = @fields%($numinrow);
 5816:     }
 5817:     my $colsleft = $numinrow - $rem;
 5818:     if ($colsleft > 1 ) {
 5819:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5820:                    '&nbsp;</td>';
 5821:     } elsif ($colsleft == 1) {
 5822:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5823:     }
 5824:     $output .= '</tr></table></td></tr>';
 5825:     return $output;
 5826: }
 5827: 
 5828: sub insttypes_row {
 5829:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5830:     my %lt = &Apache::lonlocal::texthash (
 5831:                       cansearch => 'Users allowed to search',
 5832:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5833:                       lockablenames => 'User preference to lock name',
 5834:              );
 5835:     my $showdom;
 5836:     if ($context eq 'cansearch') {
 5837:         $showdom = ' ('.$dom.')';
 5838:     }
 5839:     my $class = 'LC_left_item';
 5840:     if ($context eq 'statustocreate') {
 5841:         $class = 'LC_right_item';
 5842:     }
 5843:     my $css_class = ' class="LC_odd_row"';
 5844:     if ($rownum ne '') { 
 5845:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5846:     }
 5847:     my $output = '<tr'.$css_class.'>'.
 5848:                  '<td>'.$lt{$context}.$showdom.
 5849:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5850:     my $rem;
 5851:     if (ref($types) eq 'ARRAY') {
 5852:         for (my $i=0; $i<@{$types}; $i++) {
 5853:             if (defined($usertypes->{$types->[$i]})) {
 5854:                 my $rem = $i%($numinrow);
 5855:                 if ($rem == 0) {
 5856:                     if ($i > 0) {
 5857:                         $output .= '</tr>';
 5858:                     }
 5859:                     $output .= '<tr>';
 5860:                 }
 5861:                 my $check = ' ';
 5862:                 if (ref($settings) eq 'HASH') {
 5863:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5864:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5865:                             $check = ' checked="checked" ';
 5866:                         }
 5867:                     } elsif ($context eq 'statustocreate') {
 5868:                         $check = ' checked="checked" ';
 5869:                     }
 5870:                 }
 5871:                 $output .= '<td class="LC_left_item">'.
 5872:                            '<span class="LC_nobreak"><label>'.
 5873:                            '<input type="checkbox" name="'.$context.'" '.
 5874:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5875:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5876:             }
 5877:         }
 5878:         $rem = @{$types}%($numinrow);
 5879:     }
 5880:     my $colsleft = $numinrow - $rem;
 5881:     if (($rem == 0) && (@{$types} > 0)) {
 5882:         $output .= '<tr>';
 5883:     }
 5884:     if ($colsleft > 1) {
 5885:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5886:     } else {
 5887:         $output .= '<td class="LC_left_item">';
 5888:     }
 5889:     my $defcheck = ' ';
 5890:     if (ref($settings) eq 'HASH') {  
 5891:         if (ref($settings->{$context}) eq 'ARRAY') {
 5892:             if (grep(/^default$/,@{$settings->{$context}})) {
 5893:                 $defcheck = ' checked="checked" ';
 5894:             }
 5895:         } elsif ($context eq 'statustocreate') {
 5896:             $defcheck = ' checked="checked" ';
 5897:         }
 5898:     }
 5899:     $output .= '<span class="LC_nobreak"><label>'.
 5900:                '<input type="checkbox" name="'.$context.'" '.
 5901:                'value="default"'.$defcheck.'/>'.
 5902:                $othertitle.'</label></span></td>'.
 5903:                '</tr></table></td></tr>';
 5904:     return $output;
 5905: }
 5906: 
 5907: sub sorted_searchtitles {
 5908:     my %searchtitles = &Apache::lonlocal::texthash(
 5909:                          'uname' => 'username',
 5910:                          'lastname' => 'last name',
 5911:                          'lastfirst' => 'last name, first name',
 5912:                      );
 5913:     my @titleorder = ('uname','lastname','lastfirst');
 5914:     return (\%searchtitles,\@titleorder);
 5915: }
 5916: 
 5917: sub sorted_searchtypes {
 5918:     my %srchtypes_desc = (
 5919:                            exact    => 'is exact match',
 5920:                            contains => 'contains ..',
 5921:                            begins   => 'begins with ..',
 5922:                          );
 5923:     my @srchtypeorder = ('exact','begins','contains');
 5924:     return (\%srchtypes_desc,\@srchtypeorder);
 5925: }
 5926: 
 5927: sub usertype_update_row {
 5928:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5929:     my $datatable;
 5930:     my $numinrow = 4;
 5931:     foreach my $type (@{$types}) {
 5932:         if (defined($usertypes->{$type})) {
 5933:             $$rownums ++;
 5934:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5935:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5936:                           '</td><td class="LC_left_item"><table>';
 5937:             for (my $i=0; $i<@{$fields}; $i++) {
 5938:                 my $rem = $i%($numinrow);
 5939:                 if ($rem == 0) {
 5940:                     if ($i > 0) {
 5941:                         $datatable .= '</tr>';
 5942:                     }
 5943:                     $datatable .= '<tr>';
 5944:                 }
 5945:                 my $check = ' ';
 5946:                 if (ref($settings) eq 'HASH') {
 5947:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5948:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5949:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5950:                                 $check = ' checked="checked" ';
 5951:                             }
 5952:                         }
 5953:                     }
 5954:                 }
 5955: 
 5956:                 if ($i == @{$fields}-1) {
 5957:                     my $colsleft = $numinrow - $rem;
 5958:                     if ($colsleft > 1) {
 5959:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5960:                     } else {
 5961:                         $datatable .= '<td>';
 5962:                     }
 5963:                 } else {
 5964:                     $datatable .= '<td>';
 5965:                 }
 5966:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5967:                               '<input type="checkbox" name="updateable_'.$type.
 5968:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5969:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5970:             }
 5971:             $datatable .= '</tr></table></td></tr>';
 5972:         }
 5973:     }
 5974:     return $datatable;
 5975: }
 5976: 
 5977: sub modify_login {
 5978:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5979:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5980:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5981:     %title = ( coursecatalog => 'Display course catalog',
 5982:                adminmail => 'Display administrator E-mail address',
 5983:                helpdesk  => 'Display "Contact Helpdesk" link',
 5984:                newuser => 'Link for visitors to create a user account',
 5985:                loginheader => 'Log-in box header');
 5986:     @offon = ('off','on');
 5987:     if (ref($domconfig{login}) eq 'HASH') {
 5988:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5989:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5990:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5991:             }
 5992:         }
 5993:     }
 5994:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5995:                                            \%domconfig,\%loginhash);
 5996:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5997:     foreach my $item (@toggles) {
 5998:         $loginhash{login}{$item} = $env{'form.'.$item};
 5999:     }
 6000:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 6001:     if (ref($colchanges{'login'}) eq 'HASH') {  
 6002:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 6003:                                          \%loginhash);
 6004:     }
 6005: 
 6006:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6007:     my %domservers = &Apache::lonnet::get_servers($dom);
 6008:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 6009:     if (keys(%servers) > 1) {
 6010:         foreach my $lonhost (keys(%servers)) {
 6011:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 6012:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 6013:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 6014:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 6015:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 6016:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6017:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6018:                         $changes{'loginvia'}{$lonhost} = 1;
 6019:                     } else {
 6020:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 6021:                         $changes{'loginvia'}{$lonhost} = 1;
 6022:                     }
 6023:                 } else {
 6024:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6025:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6026:                         $changes{'loginvia'}{$lonhost} = 1;
 6027:                     }
 6028:                 }
 6029:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 6030:                     foreach my $item (@loginvia_attribs) {
 6031:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 6032:                     }
 6033:                 } else {
 6034:                     foreach my $item (@loginvia_attribs) {
 6035:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 6036:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 6037:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 6038:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 6039:                                 $new = '/';
 6040:                             }
 6041:                         }
 6042:                         if (($item eq 'custompath') && 
 6043:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 6044:                             $new = '';
 6045:                         }
 6046:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 6047:                             $changes{'loginvia'}{$lonhost} = 1;
 6048:                         }
 6049:                         if ($item eq 'exempt') {
 6050:                             $new = &check_exempt_addresses($new);
 6051:                         }
 6052:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 6053:                     }
 6054:                 }
 6055:             } else {
 6056:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6057:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6058:                     $changes{'loginvia'}{$lonhost} = 1;
 6059:                     foreach my $item (@loginvia_attribs) {
 6060:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 6061:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 6062:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 6063:                                 $new = '/';
 6064:                             }
 6065:                         }
 6066:                         if (($item eq 'custompath') && 
 6067:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 6068:                             $new = '';
 6069:                         }
 6070:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 6071:                     }
 6072:                 }
 6073:             }
 6074:         }
 6075:     }
 6076: 
 6077:     my $servadm = $r->dir_config('lonAdmEMail');
 6078:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 6079:     if (ref($domconfig{'login'}) eq 'HASH') {
 6080:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 6081:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 6082:                 if ($lang eq 'nolang') {
 6083:                     push(@currlangs,$lang);
 6084:                 } elsif (defined($langchoices{$lang})) {
 6085:                     push(@currlangs,$lang);
 6086:                 } else {
 6087:                     next;
 6088:                 }
 6089:             }
 6090:         }
 6091:     }
 6092:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 6093:     if (@currlangs > 0) {
 6094:         foreach my $lang (@currlangs) {
 6095:             if (grep(/^\Q$lang\E$/,@delurls)) {
 6096:                 $changes{'helpurl'}{$lang} = 1;
 6097:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 6098:                 $changes{'helpurl'}{$lang} = 1;
 6099:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 6100:                 push(@newlangs,$lang);
 6101:             } else {
 6102:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 6103:             }
 6104:         }
 6105:     }
 6106:     unless (grep(/^nolang$/,@currlangs)) {
 6107:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 6108:             $changes{'helpurl'}{'nolang'} = 1;
 6109:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 6110:             push(@newlangs,'nolang');
 6111:         }
 6112:     }
 6113:     if ($env{'form.loginhelpurl_add_lang'}) {
 6114:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 6115:             ($env{'form.loginhelpurl_add_file.filename'})) {
 6116:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 6117:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 6118:         }
 6119:     }
 6120:     if ((@newlangs > 0) || ($addedfile)) {
 6121:         my $error;
 6122:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6123:         if ($configuserok eq 'ok') {
 6124:             if ($switchserver) {
 6125:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 6126:             } elsif ($author_ok eq 'ok') {
 6127:                 my @allnew = @newlangs;
 6128:                 if ($addedfile ne '') {
 6129:                     push(@allnew,$addedfile);
 6130:                 }
 6131:                 foreach my $lang (@allnew) {
 6132:                     my $formelem = 'loginhelpurl_'.$lang;
 6133:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 6134:                         $formelem = 'loginhelpurl_add_file';
 6135:                     }
 6136:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 6137:                                                                "help/$lang",'','',$newfile{$lang});
 6138:                     if ($result eq 'ok') {
 6139:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 6140:                         $changes{'helpurl'}{$lang} = 1;
 6141:                     } else {
 6142:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 6143:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 6144:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 6145:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 6146:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 6147:                         }
 6148:                     }
 6149:                 }
 6150:             } else {
 6151:                 $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);
 6152:             }
 6153:         } else {
 6154:             $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);
 6155:         }
 6156:         if ($error) {
 6157:             &Apache::lonnet::logthis($error);
 6158:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6159:         }
 6160:     }
 6161: 
 6162:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 6163:     if (ref($domconfig{'login'}) eq 'HASH') {
 6164:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 6165:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 6166:                 if ($domservers{$lonhost}) {
 6167:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 6168:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 6169:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtagexempt'}{$lonhost}{'exempt'}
 6170:                     }
 6171:                 }
 6172:             }
 6173:         }
 6174:     }
 6175:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 6176:     foreach my $lonhost (sort(keys(%domservers))) {
 6177:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 6178:             $changes{'headtag'}{$lonhost} = 1;
 6179:         } else {
 6180:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 6181:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 6182:             }
 6183:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 6184:                 push(@newhosts,$lonhost);
 6185:             } elsif ($currheadtagurls{$lonhost}) {
 6186:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 6187:                 if ($currexempt{$lonhost}) {
 6188:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) { 
 6189:                         $changes{'headtag'}{$lonhost} = 1;
 6190:                     }
 6191:                 } elsif ($possexempt{$lonhost}) {
 6192:                     $changes{'headtag'}{$lonhost} = 1;
 6193:                 }
 6194:                 if ($possexempt{$lonhost}) {
 6195:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6196:                 }
 6197:             }
 6198:         }
 6199:     }
 6200:     if (@newhosts) {
 6201:         my $error;
 6202:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6203:         if ($configuserok eq 'ok') {
 6204:             if ($switchserver) {
 6205:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 6206:             } elsif ($author_ok eq 'ok') {
 6207:                 foreach my $lonhost (@newhosts) {
 6208:                     my $formelem = 'loginheadtag_'.$lonhost;
 6209:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 6210:                                                                           "login/headtag/$lonhost",'','',
 6211:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 6212:                     if ($result eq 'ok') {
 6213:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 6214:                         $changes{'headtag'}{$lonhost} = 1;
 6215:                         if ($possexempt{$lonhost}) {
 6216:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6217:                         }
 6218:                     } else {
 6219:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 6220:                                            $newheadtagurls{$lonhost},$result);
 6221:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 6222:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 6223:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 6224:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 6225:                         }
 6226:                     }
 6227:                 }
 6228:             } else {
 6229:                 $error = &mt("Upload of custom markup 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);
 6230:             }
 6231:         } else {
 6232:             $error = &mt("Upload of custom markup file(s) failed because a Domain Configuration user ([_1]) could not be created in domain: [_2].  Error was: [_3].",$confname,$dom,$configuserok);
 6233:         }
 6234:         if ($error) {
 6235:             &Apache::lonnet::logthis($error);
 6236:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6237:         }
 6238:     }
 6239:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 6240: 
 6241:     my $defaulthelpfile = '/adm/loginproblems.html';
 6242:     my $defaulttext = &mt('Default in use');
 6243: 
 6244:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 6245:                                              $dom);
 6246:     if ($putresult eq 'ok') {
 6247:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 6248:         my %defaultchecked = (
 6249:                     'coursecatalog' => 'on',
 6250:                     'helpdesk'      => 'on',
 6251:                     'adminmail'     => 'off',
 6252:                     'newuser'       => 'off',
 6253:         );
 6254:         if (ref($domconfig{'login'}) eq 'HASH') {
 6255:             foreach my $item (@toggles) {
 6256:                 if ($defaultchecked{$item} eq 'on') { 
 6257:                     if (($domconfig{'login'}{$item} eq '0') &&
 6258:                         ($env{'form.'.$item} eq '1')) {
 6259:                         $changes{$item} = 1;
 6260:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 6261:                               $domconfig{'login'}{$item} eq '1') &&
 6262:                              ($env{'form.'.$item} eq '0')) {
 6263:                         $changes{$item} = 1;
 6264:                     }
 6265:                 } elsif ($defaultchecked{$item} eq 'off') {
 6266:                     if (($domconfig{'login'}{$item} eq '1') &&
 6267:                         ($env{'form.'.$item} eq '0')) {
 6268:                         $changes{$item} = 1;
 6269:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 6270:                               $domconfig{'login'}{$item} eq '0') &&
 6271:                              ($env{'form.'.$item} eq '1')) {
 6272:                         $changes{$item} = 1;
 6273:                     }
 6274:                 }
 6275:             }
 6276:         }
 6277:         if (keys(%changes) > 0 || $colchgtext) {
 6278:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6279:             if (ref($lastactref) eq 'HASH') {
 6280:                 $lastactref->{'domainconfig'} = 1;
 6281:             }
 6282:             $resulttext = &mt('Changes made:').'<ul>';
 6283:             foreach my $item (sort(keys(%changes))) {
 6284:                 if ($item eq 'loginvia') {
 6285:                     if (ref($changes{$item}) eq 'HASH') {
 6286:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 6287:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 6288:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 6289:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 6290:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 6291:                                     $protocol = 'http' if ($protocol ne 'https');
 6292:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 6293: 
 6294:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 6295:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 6296:                                     } else {
 6297:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 6298:                                     }
 6299:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 6300:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 6301:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 6302:                                     }
 6303:                                     $resulttext .= '</li>';
 6304:                                 } else {
 6305:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 6306:                                 }
 6307:                             } else {
 6308:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 6309:                             }
 6310:                         }
 6311:                         $resulttext .= '</ul></li>';
 6312:                     }
 6313:                 } elsif ($item eq 'helpurl') {
 6314:                     if (ref($changes{$item}) eq 'HASH') {
 6315:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 6316:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 6317:                                 my ($chg,$link);
 6318:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 6319:                                 if ($lang eq 'nolang') {
 6320:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 6321:                                 } else {
 6322:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 6323:                                 }
 6324:                                 $resulttext .= '<li>'.$chg.'</li>';
 6325:                             } else {
 6326:                                 my $chg;
 6327:                                 if ($lang eq 'nolang') {
 6328:                                     $chg = &mt('custom log-in help file for no preferred language');
 6329:                                 } else {
 6330:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 6331:                                 }
 6332:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 6333:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 6334:                                                       '?inhibitmenu=yes',$chg,600,500).
 6335:                                                '</li>';
 6336:                             }
 6337:                         }
 6338:                     }
 6339:                 } elsif ($item eq 'headtag') {
 6340:                     if (ref($changes{$item}) eq 'HASH') {
 6341:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 6342:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 6343:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 6344:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 6345:                                 $resulttext .= '<li><a href="'.
 6346:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 6347:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 6348:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 6349:                                 if ($possexempt{$lonhost}) {
 6350:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 6351:                                 } else {
 6352:                                     $resulttext .= &mt('included for any client IP');
 6353:                                 }
 6354:                                 $resulttext .= '</li>';
 6355:                             }
 6356:                         }
 6357:                     }
 6358:                 } elsif ($item eq 'captcha') {
 6359:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6360:                         my $chgtxt;
 6361:                         if ($loginhash{'login'}{$item} eq 'notused') {
 6362:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 6363:                         } else {
 6364:                             my %captchas = &captcha_phrases();
 6365:                             if ($captchas{$loginhash{'login'}{$item}}) {
 6366:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 6367:                             } else {
 6368:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 6369:                             }
 6370:                         }
 6371:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6372:                     }
 6373:                 } elsif ($item eq 'recaptchakeys') {
 6374:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6375:                         my ($privkey,$pubkey);
 6376:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 6377:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 6378:                             $privkey = $loginhash{'login'}{$item}{'private'};
 6379:                         }
 6380:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 6381:                         if (!$pubkey) {
 6382:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 6383:                         } else {
 6384:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6385:                         }
 6386:                         if (!$privkey) {
 6387:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 6388:                         } else {
 6389:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 6390:                         }
 6391:                         $chgtxt .= '</ul>';
 6392:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6393:                     }
 6394:                 } else {
 6395:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 6396:                 }
 6397:             }
 6398:             $resulttext .= $colchgtext.'</ul>';
 6399:         } else {
 6400:             $resulttext = &mt('No changes made to log-in page settings');
 6401:         }
 6402:     } else {
 6403:         $resulttext = '<span class="LC_error">'.
 6404: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6405:     }
 6406:     if ($errors) {
 6407:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 6408:                        $errors.'</ul>';
 6409:     }
 6410:     return $resulttext;
 6411: }
 6412: 
 6413: 
 6414: sub check_exempt_addresses {
 6415:     my ($iplist) = @_;
 6416:     $iplist =~ s/^\s+//;
 6417:     $iplist =~ s/\s+$//;
 6418:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 6419:     my (@okips,$new);
 6420:     foreach my $ip (@poss_ips) {
 6421:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 6422:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 6423:                 push(@okips,$ip);
 6424:             }
 6425:         }
 6426:     }
 6427:     if (@okips > 0) {
 6428:         $new = join(',',@okips);
 6429:     } else {
 6430:         $new = '';
 6431:     }
 6432:     return $new;
 6433: }
 6434: 
 6435: sub color_font_choices {
 6436:     my %choices =
 6437:         &Apache::lonlocal::texthash (
 6438:             img => "Header",
 6439:             bgs => "Background colors",
 6440:             links => "Link colors",
 6441:             images => "Images",
 6442:             font => "Font color",
 6443:             fontmenu => "Font menu",
 6444:             pgbg => "Page",
 6445:             tabbg => "Header",
 6446:             sidebg => "Border",
 6447:             link => "Link",
 6448:             alink => "Active link",
 6449:             vlink => "Visited link",
 6450:         );
 6451:     return %choices;
 6452: }
 6453: 
 6454: sub modify_rolecolors {
 6455:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 6456:     my ($resulttext,%rolehash);
 6457:     $rolehash{'rolecolors'} = {};
 6458:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 6459:         if ($domconfig{'rolecolors'} eq '') {
 6460:             $domconfig{'rolecolors'} = {};
 6461:         }
 6462:     }
 6463:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 6464:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 6465:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 6466:                                              $dom);
 6467:     if ($putresult eq 'ok') {
 6468:         if (keys(%changes) > 0) {
 6469:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6470:             if (ref($lastactref) eq 'HASH') {
 6471:                 $lastactref->{'domainconfig'} = 1;
 6472:             }
 6473:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 6474:                                              $rolehash{'rolecolors'});
 6475:         } else {
 6476:             $resulttext = &mt('No changes made to default color schemes');
 6477:         }
 6478:     } else {
 6479:         $resulttext = '<span class="LC_error">'.
 6480: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6481:     }
 6482:     if ($errors) {
 6483:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6484:                        $errors.'</ul>';
 6485:     }
 6486:     return $resulttext;
 6487: }
 6488: 
 6489: sub modify_colors {
 6490:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 6491:     my (%changes,%choices);
 6492:     my @bgs;
 6493:     my @links = ('link','alink','vlink');
 6494:     my @logintext;
 6495:     my @images;
 6496:     my $servadm = $r->dir_config('lonAdmEMail');
 6497:     my $errors;
 6498:     my %defaults;
 6499:     foreach my $role (@{$roles}) {
 6500:         if ($role eq 'login') {
 6501:             %choices = &login_choices();
 6502:             @logintext = ('textcol','bgcol');
 6503:         } else {
 6504:             %choices = &color_font_choices();
 6505:         }
 6506:         if ($role eq 'login') {
 6507:             @images = ('img','logo','domlogo','login');
 6508:             @bgs = ('pgbg','mainbg','sidebg');
 6509:         } else {
 6510:             @images = ('img');
 6511:             @bgs = ('pgbg','tabbg','sidebg');
 6512:         }
 6513:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 6514:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 6515:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 6516:         }
 6517:         if ($role eq 'login') {
 6518:             foreach my $item (@logintext) {
 6519:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6520:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6521:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6522:                 }
 6523:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 6524:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6525:                 }
 6526:             }
 6527:         } else {
 6528:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 6529:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 6530:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 6531:             }
 6532:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 6533:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 6534:             }
 6535:         }
 6536:         foreach my $item (@bgs) {
 6537:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6538:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6539:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6540:             }
 6541:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 6542:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6543:             }
 6544:         }
 6545:         foreach my $item (@links) {
 6546:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6547:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6548:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6549:             }
 6550:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 6551:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6552:             }
 6553:         }
 6554:         my ($configuserok,$author_ok,$switchserver) = 
 6555:             &config_check($dom,$confname,$servadm);
 6556:         my ($width,$height) = &thumb_dimensions();
 6557:         if (ref($domconfig->{$role}) ne 'HASH') {
 6558:             $domconfig->{$role} = {};
 6559:         }
 6560:         foreach my $img (@images) {
 6561:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 6562:                 if (defined($env{'form.login_showlogo_'.$img})) {
 6563:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 6564:                 } else { 
 6565:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 6566:                 }
 6567:             } 
 6568: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 6569: 		 && !defined($domconfig->{$role}{$img})
 6570: 		 && !$env{'form.'.$role.'_del_'.$img}
 6571: 		 && $env{'form.'.$role.'_import_'.$img}) {
 6572: 		# import the old configured image from the .tab setting
 6573: 		# if they haven't provided a new one 
 6574: 		$domconfig->{$role}{$img} = 
 6575: 		    $env{'form.'.$role.'_import_'.$img};
 6576: 	    }
 6577:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 6578:                 my $error;
 6579:                 if ($configuserok eq 'ok') {
 6580:                     if ($switchserver) {
 6581:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 6582:                     } else {
 6583:                         if ($author_ok eq 'ok') {
 6584:                             my ($result,$logourl) = 
 6585:                                 &publishlogo($r,'upload',$role.'_'.$img,
 6586:                                            $dom,$confname,$img,$width,$height);
 6587:                             if ($result eq 'ok') {
 6588:                                 $confhash->{$role}{$img} = $logourl;
 6589:                                 $changes{$role}{'images'}{$img} = 1;
 6590:                             } else {
 6591:                                 $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);
 6592:                             }
 6593:                         } else {
 6594:                             $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);
 6595:                         }
 6596:                     }
 6597:                 } else {
 6598:                     $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);
 6599:                 }
 6600:                 if ($error) {
 6601:                     &Apache::lonnet::logthis($error);
 6602:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6603:                 }
 6604:             } elsif ($domconfig->{$role}{$img} ne '') {
 6605:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 6606:                     my $error;
 6607:                     if ($configuserok eq 'ok') {
 6608: # is confname an author?
 6609:                         if ($switchserver eq '') {
 6610:                             if ($author_ok eq 'ok') {
 6611:                                 my ($result,$logourl) = 
 6612:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 6613:                                             $dom,$confname,$img,$width,$height);
 6614:                                 if ($result eq 'ok') {
 6615:                                     $confhash->{$role}{$img} = $logourl;
 6616: 				    $changes{$role}{'images'}{$img} = 1;
 6617:                                 }
 6618:                             }
 6619:                         }
 6620:                     }
 6621:                 }
 6622:             }
 6623:         }
 6624:         if (ref($domconfig) eq 'HASH') {
 6625:             if (ref($domconfig->{$role}) eq 'HASH') {
 6626:                 foreach my $img (@images) {
 6627:                     if ($domconfig->{$role}{$img} ne '') {
 6628:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6629:                             $confhash->{$role}{$img} = '';
 6630:                             $changes{$role}{'images'}{$img} = 1;
 6631:                         } else {
 6632:                             if ($confhash->{$role}{$img} eq '') {
 6633:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 6634:                             }
 6635:                         }
 6636:                     } else {
 6637:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6638:                             $confhash->{$role}{$img} = '';
 6639:                             $changes{$role}{'images'}{$img} = 1;
 6640:                         } 
 6641:                     }
 6642:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 6643:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 6644:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 6645:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 6646:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 6647:                             }
 6648:                         } else {
 6649:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6650:                                 $changes{$role}{'showlogo'}{$img} = 1;
 6651:                             }
 6652:                         }
 6653:                     }
 6654:                 }
 6655:                 if ($domconfig->{$role}{'font'} ne '') {
 6656:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 6657:                         $changes{$role}{'font'} = 1;
 6658:                     }
 6659:                 } else {
 6660:                     if ($confhash->{$role}{'font'}) {
 6661:                         $changes{$role}{'font'} = 1;
 6662:                     }
 6663:                 }
 6664:                 if ($role ne 'login') {
 6665:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 6666:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 6667:                             $changes{$role}{'fontmenu'} = 1;
 6668:                         }
 6669:                     } else {
 6670:                         if ($confhash->{$role}{'fontmenu'}) {
 6671:                             $changes{$role}{'fontmenu'} = 1;
 6672:                         }
 6673:                     }
 6674:                 }
 6675:                 foreach my $item (@bgs) {
 6676:                     if ($domconfig->{$role}{$item} ne '') {
 6677:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6678:                             $changes{$role}{'bgs'}{$item} = 1;
 6679:                         } 
 6680:                     } else {
 6681:                         if ($confhash->{$role}{$item}) {
 6682:                             $changes{$role}{'bgs'}{$item} = 1;
 6683:                         }
 6684:                     }
 6685:                 }
 6686:                 foreach my $item (@links) {
 6687:                     if ($domconfig->{$role}{$item} ne '') {
 6688:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6689:                             $changes{$role}{'links'}{$item} = 1;
 6690:                         }
 6691:                     } else {
 6692:                         if ($confhash->{$role}{$item}) {
 6693:                             $changes{$role}{'links'}{$item} = 1;
 6694:                         }
 6695:                     }
 6696:                 }
 6697:                 foreach my $item (@logintext) {
 6698:                     if ($domconfig->{$role}{$item} ne '') {
 6699:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6700:                             $changes{$role}{'logintext'}{$item} = 1;
 6701:                         }
 6702:                     } else {
 6703:                         if ($confhash->{$role}{$item}) {
 6704:                             $changes{$role}{'logintext'}{$item} = 1;
 6705:                         }
 6706:                     }
 6707:                 }
 6708:             } else {
 6709:                 &default_change_checker($role,\@images,\@links,\@bgs,
 6710:                                         \@logintext,$confhash,\%changes); 
 6711:             }
 6712:         } else {
 6713:             &default_change_checker($role,\@images,\@links,\@bgs,
 6714:                                     \@logintext,$confhash,\%changes); 
 6715:         }
 6716:     }
 6717:     return ($errors,%changes);
 6718: }
 6719: 
 6720: sub config_check {
 6721:     my ($dom,$confname,$servadm) = @_;
 6722:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6723:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6724:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6725:                                                    $confname,$servadm);
 6726:     if ($configuserok eq 'ok') {
 6727:         $switchserver = &check_switchserver($dom,$confname);
 6728:         if ($switchserver eq '') {
 6729:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6730:         }
 6731:     }
 6732:     return ($configuserok,$author_ok,$switchserver);
 6733: }
 6734: 
 6735: sub default_change_checker {
 6736:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6737:     foreach my $item (@{$links}) {
 6738:         if ($confhash->{$role}{$item}) {
 6739:             $changes->{$role}{'links'}{$item} = 1;
 6740:         }
 6741:     }
 6742:     foreach my $item (@{$bgs}) {
 6743:         if ($confhash->{$role}{$item}) {
 6744:             $changes->{$role}{'bgs'}{$item} = 1;
 6745:         }
 6746:     }
 6747:     foreach my $item (@{$logintext}) {
 6748:         if ($confhash->{$role}{$item}) {
 6749:             $changes->{$role}{'logintext'}{$item} = 1;
 6750:         }
 6751:     }
 6752:     foreach my $img (@{$images}) {
 6753:         if ($env{'form.'.$role.'_del_'.$img}) {
 6754:             $confhash->{$role}{$img} = '';
 6755:             $changes->{$role}{'images'}{$img} = 1;
 6756:         }
 6757:         if ($role eq 'login') {
 6758:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6759:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6760:             }
 6761:         }
 6762:     }
 6763:     if ($confhash->{$role}{'font'}) {
 6764:         $changes->{$role}{'font'} = 1;
 6765:     }
 6766: }
 6767: 
 6768: sub display_colorchgs {
 6769:     my ($dom,$changes,$roles,$confhash) = @_;
 6770:     my (%choices,$resulttext);
 6771:     if (!grep(/^login$/,@{$roles})) {
 6772:         $resulttext = &mt('Changes made:').'<br />';
 6773:     }
 6774:     foreach my $role (@{$roles}) {
 6775:         if ($role eq 'login') {
 6776:             %choices = &login_choices();
 6777:         } else {
 6778:             %choices = &color_font_choices();
 6779:         }
 6780:         if (ref($changes->{$role}) eq 'HASH') {
 6781:             if ($role ne 'login') {
 6782:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6783:             }
 6784:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6785:                 if ($role ne 'login') {
 6786:                     $resulttext .= '<ul>';
 6787:                 }
 6788:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6789:                     if ($role ne 'login') {
 6790:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6791:                     }
 6792:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6793:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6794:                             if ($confhash->{$role}{$key}{$item}) {
 6795:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6796:                             } else {
 6797:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6798:                             }
 6799:                         } elsif ($confhash->{$role}{$item} eq '') {
 6800:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6801:                         } else {
 6802:                             my $newitem = $confhash->{$role}{$item};
 6803:                             if ($key eq 'images') {
 6804:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6805:                             }
 6806:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6807:                         }
 6808:                     }
 6809:                     if ($role ne 'login') {
 6810:                         $resulttext .= '</ul></li>';
 6811:                     }
 6812:                 } else {
 6813:                     if ($confhash->{$role}{$key} eq '') {
 6814:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6815:                     } else {
 6816:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6817:                     }
 6818:                 }
 6819:                 if ($role ne 'login') {
 6820:                     $resulttext .= '</ul>';
 6821:                 }
 6822:             }
 6823:         }
 6824:     }
 6825:     return $resulttext;
 6826: }
 6827: 
 6828: sub thumb_dimensions {
 6829:     return ('200','50');
 6830: }
 6831: 
 6832: sub check_dimensions {
 6833:     my ($inputfile) = @_;
 6834:     my ($fullwidth,$fullheight);
 6835:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6836:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6837:             my $imageinfo = <PIPE>;
 6838:             if (!close(PIPE)) {
 6839:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6840:             }
 6841:             chomp($imageinfo);
 6842:             my ($fullsize) = 
 6843:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6844:             if ($fullsize) {
 6845:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6846:             }
 6847:         }
 6848:     }
 6849:     return ($fullwidth,$fullheight);
 6850: }
 6851: 
 6852: sub check_configuser {
 6853:     my ($uhome,$dom,$confname,$servadm) = @_;
 6854:     my ($configuserok,%currroles);
 6855:     if ($uhome eq 'no_host') {
 6856:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6857:         my $configpass = &LONCAPA::Enrollment::create_password();
 6858:         $configuserok = 
 6859:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6860:                              $configpass,'','','','','',undef,$servadm);
 6861:     } else {
 6862:         $configuserok = 'ok';
 6863:         %currroles = 
 6864:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6865:     }
 6866:     return ($configuserok,%currroles);
 6867: }
 6868: 
 6869: sub check_authorstatus {
 6870:     my ($dom,$confname,%currroles) = @_;
 6871:     my $author_ok;
 6872:     if (!$currroles{':'.$dom.':au'}) {
 6873:         my $start = time;
 6874:         my $end = 0;
 6875:         $author_ok = 
 6876:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6877:                                         'au',$end,$start,'','','domconfig');
 6878:     } else {
 6879:         $author_ok = 'ok';
 6880:     }
 6881:     return $author_ok;
 6882: }
 6883: 
 6884: sub publishlogo {
 6885:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6886:     my ($output,$fname,$logourl);
 6887:     if ($action eq 'upload') {
 6888:         $fname=$env{'form.'.$formname.'.filename'};
 6889:         chop($env{'form.'.$formname});
 6890:     } else {
 6891:         ($fname) = ($formname =~ /([^\/]+)$/);
 6892:     }
 6893:     if ($savefileas ne '') {
 6894:         $fname = $savefileas;
 6895:     }
 6896:     $fname=&Apache::lonnet::clean_filename($fname);
 6897: # See if there is anything left
 6898:     unless ($fname) { return ('error: no uploaded file'); }
 6899:     $fname="$subdir/$fname";
 6900:     my $docroot=$r->dir_config('lonDocRoot');
 6901:     my $filepath="$docroot/priv";
 6902:     my $relpath = "$dom/$confname";
 6903:     my ($fnamepath,$file,$fetchthumb);
 6904:     $file=$fname;
 6905:     if ($fname=~m|/|) {
 6906:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6907:     }
 6908:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6909:     my $count;
 6910:     for ($count=5;$count<=$#parts;$count++) {
 6911:         $filepath.="/$parts[$count]";
 6912:         if ((-e $filepath)!=1) {
 6913:             mkdir($filepath,02770);
 6914:         }
 6915:     }
 6916:     # Check for bad extension and disallow upload
 6917:     if ($file=~/\.(\w+)$/ &&
 6918:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6919:         $output = 
 6920:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6921:     } elsif ($file=~/\.(\w+)$/ &&
 6922:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6923:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6924:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6925:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6926:     } elsif (-d "$filepath/$file") {
 6927:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6928:     } else {
 6929:         my $source = $filepath.'/'.$file;
 6930:         my $logfile;
 6931:         if (!open($logfile,">>$source".'.log')) {
 6932:             return (&mt('No write permission to Authoring Space'));
 6933:         }
 6934:         print $logfile
 6935: "\n================= Publish ".localtime()." ================\n".
 6936: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6937: # Save the file
 6938:         if (!open(FH,'>'.$source)) {
 6939:             &Apache::lonnet::logthis('Failed to create '.$source);
 6940:             return (&mt('Failed to create file'));
 6941:         }
 6942:         if ($action eq 'upload') {
 6943:             if (!print FH ($env{'form.'.$formname})) {
 6944:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6945:                 return (&mt('Failed to write file'));
 6946:             }
 6947:         } else {
 6948:             my $original = &Apache::lonnet::filelocation('',$formname);
 6949:             if(!copy($original,$source)) {
 6950:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6951:                 return (&mt('Failed to write file'));
 6952:             }
 6953:         }
 6954:         close(FH);
 6955:         chmod(0660, $source); # Permissions to rw-rw---.
 6956: 
 6957:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6958:         my $copyfile=$targetdir.'/'.$file;
 6959: 
 6960:         my @parts=split(/\//,$targetdir);
 6961:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6962:         for (my $count=5;$count<=$#parts;$count++) {
 6963:             $path.="/$parts[$count]";
 6964:             if (!-e $path) {
 6965:                 print $logfile "\nCreating directory ".$path;
 6966:                 mkdir($path,02770);
 6967:             }
 6968:         }
 6969:         my $versionresult;
 6970:         if (-e $copyfile) {
 6971:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6972:         } else {
 6973:             $versionresult = 'ok';
 6974:         }
 6975:         if ($versionresult eq 'ok') {
 6976:             if (copy($source,$copyfile)) {
 6977:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6978:                 $output = 'ok';
 6979:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6980:                 push(@{$modified_urls},[$copyfile,$source]);
 6981:                 my $metaoutput = 
 6982:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6983:                 unless ($registered_cleanup) {
 6984:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6985:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6986:                     $registered_cleanup=1;
 6987:                 }
 6988:             } else {
 6989:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6990:                 $output = &mt('Failed to copy file to RES space').", $!";
 6991:             }
 6992:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6993:                 my $inputfile = $filepath.'/'.$file;
 6994:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6995:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6996:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6997:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6998:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6999:                         system("convert -sample $thumbsize $inputfile $outfile");
 7000:                         chmod(0660, $filepath.'/tn-'.$file);
 7001:                         if (-e $outfile) {
 7002:                             my $copyfile=$targetdir.'/tn-'.$file;
 7003:                             if (copy($outfile,$copyfile)) {
 7004:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 7005:                                 my $thumb_metaoutput = 
 7006:                                     &write_metadata($dom,$confname,$formname,
 7007:                                                     $targetdir,'tn-'.$file,$logfile);
 7008:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 7009:                                 unless ($registered_cleanup) {
 7010:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 7011:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 7012:                                     $registered_cleanup=1;
 7013:                                 }
 7014:                             } else {
 7015:                                 print $logfile "\nUnable to write ".$copyfile.
 7016:                                                ':'.$!."\n";
 7017:                             }
 7018:                         }
 7019:                     }
 7020:                 }
 7021:             }
 7022:         } else {
 7023:             $output = $versionresult;
 7024:         }
 7025:     }
 7026:     return ($output,$logourl);
 7027: }
 7028: 
 7029: sub logo_versioning {
 7030:     my ($targetdir,$file,$logfile) = @_;
 7031:     my $target = $targetdir.'/'.$file;
 7032:     my ($maxversion,$fn,$extn,$output);
 7033:     $maxversion = 0;
 7034:     if ($file =~ /^(.+)\.(\w+)$/) {
 7035:         $fn=$1;
 7036:         $extn=$2;
 7037:     }
 7038:     opendir(DIR,$targetdir);
 7039:     while (my $filename=readdir(DIR)) {
 7040:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 7041:             $maxversion=($1>$maxversion)?$1:$maxversion;
 7042:         }
 7043:     }
 7044:     $maxversion++;
 7045:     print $logfile "\nCreating old version ".$maxversion."\n";
 7046:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 7047:     if (copy($target,$copyfile)) {
 7048:         print $logfile "Copied old target to ".$copyfile."\n";
 7049:         $copyfile=$copyfile.'.meta';
 7050:         if (copy($target.'.meta',$copyfile)) {
 7051:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 7052:             $output = 'ok';
 7053:         } else {
 7054:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 7055:             $output = &mt('Failed to copy old meta').", $!, ";
 7056:         }
 7057:     } else {
 7058:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 7059:         $output = &mt('Failed to copy old target').", $!, ";
 7060:     }
 7061:     return $output;
 7062: }
 7063: 
 7064: sub write_metadata {
 7065:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 7066:     my (%metadatafields,%metadatakeys,$output);
 7067:     $metadatafields{'title'}=$formname;
 7068:     $metadatafields{'creationdate'}=time;
 7069:     $metadatafields{'lastrevisiondate'}=time;
 7070:     $metadatafields{'copyright'}='public';
 7071:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 7072:                                          $env{'user.domain'};
 7073:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 7074:     $metadatafields{'domain'}=$dom;
 7075:     {
 7076:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 7077:         my $mfh;
 7078:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 7079:             foreach (sort(keys(%metadatafields))) {
 7080:                 unless ($_=~/\./) {
 7081:                     my $unikey=$_;
 7082:                     $unikey=~/^([A-Za-z]+)/;
 7083:                     my $tag=$1;
 7084:                     $tag=~tr/A-Z/a-z/;
 7085:                     print $mfh "\n\<$tag";
 7086:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 7087:                         my $value=$metadatafields{$unikey.'.'.$_};
 7088:                         $value=~s/\"/\'\'/g;
 7089:                         print $mfh ' '.$_.'="'.$value.'"';
 7090:                     }
 7091:                     print $mfh '>'.
 7092:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 7093:                             .'</'.$tag.'>';
 7094:                 }
 7095:             }
 7096:             $output = 'ok';
 7097:             print $logfile "\nWrote metadata";
 7098:             close($mfh);
 7099:         } else {
 7100:             print $logfile "\nFailed to open metadata file";
 7101:             $output = &mt('Could not write metadata');
 7102:         }
 7103:     }
 7104:     return $output;
 7105: }
 7106: 
 7107: sub notifysubscribed {
 7108:     foreach my $targetsource (@{$modified_urls}){
 7109:         next unless (ref($targetsource) eq 'ARRAY');
 7110:         my ($target,$source)=@{$targetsource};
 7111:         if ($source ne '') {
 7112:             if (open(my $logfh,'>>'.$source.'.log')) {
 7113:                 print $logfh "\nCleanup phase: Notifications\n";
 7114:                 my @subscribed=&subscribed_hosts($target);
 7115:                 foreach my $subhost (@subscribed) {
 7116:                     print $logfh "\nNotifying host ".$subhost.':';
 7117:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 7118:                     print $logfh $reply;
 7119:                 }
 7120:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 7121:                 foreach my $subhost (@subscribedmeta) {
 7122:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 7123:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 7124:                                                         $subhost);
 7125:                     print $logfh $reply;
 7126:                 }
 7127:                 print $logfh "\n============ Done ============\n";
 7128:                 close($logfh);
 7129:             }
 7130:         }
 7131:     }
 7132:     return OK;
 7133: }
 7134: 
 7135: sub subscribed_hosts {
 7136:     my ($target) = @_;
 7137:     my @subscribed;
 7138:     if (open(my $fh,"<$target.subscription")) {
 7139:         while (my $subline=<$fh>) {
 7140:             if ($subline =~ /^($match_lonid):/) {
 7141:                 my $host = $1;
 7142:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 7143:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 7144:                         push(@subscribed,$host);
 7145:                     }
 7146:                 }
 7147:             }
 7148:         }
 7149:     }
 7150:     return @subscribed;
 7151: }
 7152: 
 7153: sub check_switchserver {
 7154:     my ($dom,$confname) = @_;
 7155:     my ($allowed,$switchserver);
 7156:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 7157:     if ($home eq 'no_host') {
 7158:         $home = &Apache::lonnet::domain($dom,'primary');
 7159:     }
 7160:     my @ids=&Apache::lonnet::current_machine_ids();
 7161:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 7162:     if (!$allowed) {
 7163: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 7164:     }
 7165:     return $switchserver;
 7166: }
 7167: 
 7168: sub modify_quotas {
 7169:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 7170:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 7171:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 7172:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 7173:         $validationfieldsref);
 7174:     if ($action eq 'quotas') {
 7175:         $context = 'tools'; 
 7176:     } else {
 7177:         $context = $action;
 7178:     }
 7179:     if ($context eq 'requestcourses') {
 7180:         @usertools = ('official','unofficial','community','textbook');
 7181:         @options =('norequest','approval','validate','autolimit');
 7182:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 7183:         %titles = &courserequest_titles();
 7184:         $toolregexp = join('|',@usertools);
 7185:         %conditions = &courserequest_conditions();
 7186:         $confname = $dom.'-domainconfig';
 7187:         my $servadm = $r->dir_config('lonAdmEMail');
 7188:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7189:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 7190:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7191:     } elsif ($context eq 'requestauthor') {
 7192:         @usertools = ('author');
 7193:         %titles = &authorrequest_titles();
 7194:     } else {
 7195:         @usertools = ('aboutme','blog','webdav','portfolio');
 7196:         %titles = &tool_titles();
 7197:     }
 7198:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7199:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7200:     foreach my $key (keys(%env)) {
 7201:         if ($context eq 'requestcourses') {
 7202:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 7203:                 my $item = $1;
 7204:                 my $type = $2;
 7205:                 if ($type =~ /^limit_(.+)/) {
 7206:                     $limithash{$item}{$1} = $env{$key};
 7207:                 } else {
 7208:                     $confhash{$item}{$type} = $env{$key};
 7209:                 }
 7210:             }
 7211:         } elsif ($context eq 'requestauthor') {
 7212:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 7213:                 $confhash{$1} = $env{$key};
 7214:             }
 7215:         } else {
 7216:             if ($key =~ /^form\.quota_(.+)$/) {
 7217:                 $confhash{'defaultquota'}{$1} = $env{$key};
 7218:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 7219:                 $confhash{'authorquota'}{$1} = $env{$key};
 7220:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 7221:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 7222:             }
 7223:         }
 7224:     }
 7225:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7226:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 7227:         @approvalnotify = sort(@approvalnotify);
 7228:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 7229:         my @crstypes = ('official','unofficial','community','textbook');
 7230:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 7231:         foreach my $type (@hasuniquecode) {
 7232:             if (grep(/^\Q$type\E$/,@crstypes)) {
 7233:                 $confhash{'uniquecode'}{$type} = 1;
 7234:             }
 7235:         }
 7236:         my (%newbook,%allpos);
 7237:         if ($context eq 'requestcourses') {
 7238:             foreach my $type ('textbooks','templates') {
 7239:                 @{$allpos{$type}} = (); 
 7240:                 my $invalid;
 7241:                 if ($type eq 'textbooks') {
 7242:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 7243:                 } else {
 7244:                     $invalid = &mt('Invalid LON-CAPA course for template');
 7245:                 }
 7246:                 if ($env{'form.'.$type.'_addbook'}) {
 7247:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 7248:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 7249:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 7250:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 7251:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 7252:                         } else {
 7253:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 7254:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 7255:                             $position =~ s/\D+//g;
 7256:                             if ($position ne '') {
 7257:                                 $allpos{$type}[$position] = $newbook{$type};
 7258:                             }
 7259:                         }
 7260:                     } else {
 7261:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 7262:                     }
 7263:                 }
 7264:             } 
 7265:         }
 7266:         if (ref($domconfig{$action}) eq 'HASH') {
 7267:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 7268:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 7269:                     $changes{'notify'}{'approval'} = 1;
 7270:                 }
 7271:             } else {
 7272:                 if ($confhash{'notify'}{'approval'}) {
 7273:                     $changes{'notify'}{'approval'} = 1;
 7274:                 }
 7275:             }
 7276:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 7277:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7278:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 7279:                         unless ($confhash{'uniquecode'}{$crstype}) {
 7280:                             $changes{'uniquecode'} = 1;
 7281:                         }
 7282:                     }
 7283:                     unless ($changes{'uniquecode'}) {
 7284:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 7285:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 7286:                                 $changes{'uniquecode'} = 1;
 7287:                             }
 7288:                         }
 7289:                     }
 7290:                } else {
 7291:                    $changes{'uniquecode'} = 1;
 7292:                }
 7293:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 7294:                 $changes{'uniquecode'} = 1;
 7295:             }
 7296:             if ($context eq 'requestcourses') {
 7297:                 foreach my $type ('textbooks','templates') {
 7298:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7299:                         my %deletions;
 7300:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 7301:                         if (@todelete) {
 7302:                             map { $deletions{$_} = 1; } @todelete;
 7303:                         }
 7304:                         my %imgdeletions;
 7305:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 7306:                         if (@todeleteimages) {
 7307:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 7308:                         }
 7309:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 7310:                         for (my $i=0; $i<=$maxnum; $i++) {
 7311:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 7312:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 7313:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 7314:                                 if ($deletions{$key}) {
 7315:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 7316:                                         #FIXME need to obsolete item in RES space
 7317:                                     }
 7318:                                     next;
 7319:                                 } else {
 7320:                                     my $newpos = $env{'form.'.$itemid};
 7321:                                     $newpos =~ s/\D+//g;
 7322:                                     foreach my $item ('subject','title','publisher','author') {
 7323:                                         next if ((($item eq 'author') || ($item eq 'publisher')) && 
 7324:                                                  ($type eq 'templates'));
 7325:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 7326:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 7327:                                             $changes{$type}{$key} = 1;
 7328:                                         }
 7329:                                     }
 7330:                                     $allpos{$type}[$newpos] = $key;
 7331:                                 }
 7332:                                 if ($imgdeletions{$key}) {
 7333:                                     $changes{$type}{$key} = 1;
 7334:                                     #FIXME need to obsolete item in RES space
 7335:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 7336:                                     my ($cdom,$cnum) = split(/_/,$key);
 7337:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 7338:                                                                                   $cdom,$cnum,$type,$configuserok,
 7339:                                                                                   $switchserver,$author_ok);
 7340:                                     if ($imgurl) {
 7341:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 7342:                                         $changes{$type}{$key} = 1; 
 7343:                                     }
 7344:                                     if ($error) {
 7345:                                         &Apache::lonnet::logthis($error);
 7346:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7347:                                     } 
 7348:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 7349:                                     $confhash{$type}{$key}{'image'} = 
 7350:                                         $domconfig{$action}{$type}{$key}{'image'};
 7351:                                 }
 7352:                             }
 7353:                         }
 7354:                     }
 7355:                 }
 7356:             }
 7357:         } else {
 7358:             if ($confhash{'notify'}{'approval'}) {
 7359:                 $changes{'notify'}{'approval'} = 1;
 7360:             }
 7361:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 7362:                 $changes{'uniquecode'} = 1;
 7363:             }
 7364:         }
 7365:         if ($context eq 'requestcourses') {
 7366:             foreach my $type ('textbooks','templates') {
 7367:                 if ($newbook{$type}) {
 7368:                     $changes{$type}{$newbook{$type}} = 1;
 7369:                     foreach my $item ('subject','title','publisher','author') {
 7370:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7371:                                  ($type eq 'template'));
 7372:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 7373:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 7374:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 7375:                         }
 7376:                     }
 7377:                     if ($type eq 'textbooks') {
 7378:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 7379:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 7380:                             my ($imageurl,$error) =
 7381:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 7382:                                                         $configuserok,$switchserver,$author_ok);
 7383:                             if ($imageurl) {
 7384:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 7385:                             }
 7386:                             if ($error) {
 7387:                                 &Apache::lonnet::logthis($error);
 7388:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7389:                             }
 7390:                         }
 7391:                     }
 7392:                 }
 7393:                 if (@{$allpos{$type}} > 0) {
 7394:                     my $idx = 0;
 7395:                     foreach my $item (@{$allpos{$type}}) {
 7396:                         if ($item ne '') {
 7397:                             $confhash{$type}{$item}{'order'} = $idx;
 7398:                             if (ref($domconfig{$action}) eq 'HASH') {
 7399:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7400:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 7401:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 7402:                                             $changes{$type}{$item} = 1;
 7403:                                         }
 7404:                                     }
 7405:                                 }
 7406:                             }
 7407:                             $idx ++;
 7408:                         }
 7409:                     }
 7410:                 }
 7411:             }
 7412:             if (ref($validationitemsref) eq 'ARRAY') {
 7413:                 foreach my $item (@{$validationitemsref}) {
 7414:                     if ($item eq 'fields') {
 7415:                         my @changed;
 7416:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 7417:                         if (@{$confhash{'validation'}{$item}} > 0) {
 7418:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 7419:                         }
 7420:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7421:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7422:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 7423:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 7424:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 7425:                                 } else {
 7426:                                     @changed = @{$confhash{'validation'}{$item}};
 7427:                                 }
 7428:                             } else {
 7429:                                 @changed = @{$confhash{'validation'}{$item}};
 7430:                             }
 7431:                         } else {
 7432:                             @changed = @{$confhash{'validation'}{$item}};
 7433:                         }
 7434:                         if (@changed) {
 7435:                             if ($confhash{'validation'}{$item}) {
 7436:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 7437:                             } else {
 7438:                                 $changes{'validation'}{$item} = &mt('None');
 7439:                             }
 7440:                         }
 7441:                     } else {
 7442:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 7443:                         if ($item eq 'markup') {
 7444:                             if ($env{'form.requestcourses_validation_'.$item}) {
 7445:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 7446:                             }
 7447:                         }
 7448:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7449:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7450:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 7451:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7452:                                 }
 7453:                             } else {
 7454:                                 if ($confhash{'validation'}{$item} ne '') {
 7455:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7456:                                 }
 7457:                             }
 7458:                         } else {
 7459:                             if ($confhash{'validation'}{$item} ne '') {
 7460:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7461:                             }
 7462:                         }
 7463:                     }
 7464:                 }
 7465:             }
 7466:             if ($env{'form.validationdc'}) {
 7467:                 my $newval = $env{'form.validationdc'};
 7468:                 my %domcoords = &get_active_dcs($dom);
 7469:                 if (exists($domcoords{$newval})) {
 7470:                     $confhash{'validation'}{'dc'} = $newval;
 7471:                 }
 7472:             }
 7473:             if (ref($confhash{'validation'}) eq 'HASH') {
 7474:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7475:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7476:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7477:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7478:                                 if ($confhash{'validation'}{'dc'} eq '') {
 7479:                                     $changes{'validation'}{'dc'} = &mt('None');
 7480:                                 } else {
 7481:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7482:                                 }
 7483:                             }
 7484:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 7485:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7486:                         }
 7487:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 7488:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7489:                     }
 7490:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 7491:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7492:                 }  
 7493:             } else {
 7494:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7495:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7496:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7497:                             $changes{'validation'}{'dc'} = &mt('None');
 7498:                         }
 7499:                     }
 7500:                 }
 7501:             }
 7502:         }
 7503:     } else {
 7504:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 7505:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 7506:     }
 7507:     foreach my $item (@usertools) {
 7508:         foreach my $type (@{$types},'default','_LC_adv') {
 7509:             my $unset; 
 7510:             if ($context eq 'requestcourses') {
 7511:                 $unset = '0';
 7512:                 if ($type eq '_LC_adv') {
 7513:                     $unset = '';
 7514:                 }
 7515:                 if ($confhash{$item}{$type} eq 'autolimit') {
 7516:                     $confhash{$item}{$type} .= '=';
 7517:                     unless ($limithash{$item}{$type} =~ /\D/) {
 7518:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 7519:                     }
 7520:                 }
 7521:             } elsif ($context eq 'requestauthor') {
 7522:                 $unset = '0';
 7523:                 if ($type eq '_LC_adv') {
 7524:                     $unset = '';
 7525:                 }
 7526:             } else {
 7527:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 7528:                     $confhash{$item}{$type} = 1;
 7529:                 } else {
 7530:                     $confhash{$item}{$type} = 0;
 7531:                 }
 7532:             }
 7533:             if (ref($domconfig{$action}) eq 'HASH') {
 7534:                 if ($action eq 'requestauthor') {
 7535:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 7536:                         $changes{$type} = 1;
 7537:                     }
 7538:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 7539:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 7540:                         $changes{$item}{$type} = 1;
 7541:                     }
 7542:                 } else {
 7543:                     if ($context eq 'requestcourses') {
 7544:                         if ($confhash{$item}{$type} ne $unset) {
 7545:                             $changes{$item}{$type} = 1;
 7546:                         }
 7547:                     } else {
 7548:                         if (!$confhash{$item}{$type}) {
 7549:                             $changes{$item}{$type} = 1;
 7550:                         }
 7551:                     }
 7552:                 }
 7553:             } else {
 7554:                 if ($context eq 'requestcourses') {
 7555:                     if ($confhash{$item}{$type} ne $unset) {
 7556:                         $changes{$item}{$type} = 1;
 7557:                     }
 7558:                 } elsif ($context eq 'requestauthor') {
 7559:                     if ($confhash{$type} ne $unset) {
 7560:                         $changes{$type} = 1;
 7561:                     }
 7562:                 } else {
 7563:                     if (!$confhash{$item}{$type}) {
 7564:                         $changes{$item}{$type} = 1;
 7565:                     }
 7566:                 }
 7567:             }
 7568:         }
 7569:     }
 7570:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7571:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 7572:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7573:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 7574:                     if (exists($confhash{'defaultquota'}{$key})) {
 7575:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 7576:                             $changes{'defaultquota'}{$key} = 1;
 7577:                         }
 7578:                     } else {
 7579:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 7580:                     }
 7581:                 }
 7582:             } else {
 7583:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 7584:                     if (exists($confhash{'defaultquota'}{$key})) {
 7585:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 7586:                             $changes{'defaultquota'}{$key} = 1;
 7587:                         }
 7588:                     } else {
 7589:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 7590:                     }
 7591:                 }
 7592:             }
 7593:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7594:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 7595:                     if (exists($confhash{'authorquota'}{$key})) {
 7596:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 7597:                             $changes{'authorquota'}{$key} = 1;
 7598:                         }
 7599:                     } else {
 7600:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 7601:                     }
 7602:                 }
 7603:             }
 7604:         }
 7605:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 7606:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 7607:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7608:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7609:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 7610:                             $changes{'defaultquota'}{$key} = 1;
 7611:                         }
 7612:                     } else {
 7613:                         if (!exists($domconfig{'quotas'}{$key})) {
 7614:                             $changes{'defaultquota'}{$key} = 1;
 7615:                         }
 7616:                     }
 7617:                 } else {
 7618:                     $changes{'defaultquota'}{$key} = 1;
 7619:                 }
 7620:             }
 7621:         }
 7622:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 7623:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 7624:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7625:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7626:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 7627:                             $changes{'authorquota'}{$key} = 1;
 7628:                         }
 7629:                     } else {
 7630:                         $changes{'authorquota'}{$key} = 1;
 7631:                     }
 7632:                 } else {
 7633:                     $changes{'authorquota'}{$key} = 1;
 7634:                 }
 7635:             }
 7636:         }
 7637:     }
 7638: 
 7639:     if ($context eq 'requestauthor') {
 7640:         $domdefaults{'requestauthor'} = \%confhash;
 7641:     } else {
 7642:         foreach my $key (keys(%confhash)) {
 7643:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 7644:                 $domdefaults{$key} = $confhash{$key};
 7645:             }
 7646:         }
 7647:     }
 7648: 
 7649:     my %quotahash = (
 7650:                       $action => { %confhash }
 7651:                     );
 7652:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 7653:                                              $dom);
 7654:     if ($putresult eq 'ok') {
 7655:         if (keys(%changes) > 0) {
 7656:             my $cachetime = 24*60*60;
 7657:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7658:             if (ref($lastactref) eq 'HASH') {
 7659:                 $lastactref->{'domdefaults'} = 1;
 7660:             }
 7661:             $resulttext = &mt('Changes made:').'<ul>';
 7662:             unless (($context eq 'requestcourses') ||
 7663:                     ($context eq 'requestauthor')) {
 7664:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 7665:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 7666:                     foreach my $type (@{$types},'default') {
 7667:                         if (defined($changes{'defaultquota'}{$type})) {
 7668:                             my $typetitle = $usertypes->{$type};
 7669:                             if ($type eq 'default') {
 7670:                                 $typetitle = $othertitle;
 7671:                             }
 7672:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 7673:                         }
 7674:                     }
 7675:                     $resulttext .= '</ul></li>';
 7676:                 }
 7677:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 7678:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 7679:                     foreach my $type (@{$types},'default') {
 7680:                         if (defined($changes{'authorquota'}{$type})) {
 7681:                             my $typetitle = $usertypes->{$type};
 7682:                             if ($type eq 'default') {
 7683:                                 $typetitle = $othertitle;
 7684:                             }
 7685:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 7686:                         }
 7687:                     }
 7688:                     $resulttext .= '</ul></li>';
 7689:                 }
 7690:             }
 7691:             my %newenv;
 7692:             foreach my $item (@usertools) {
 7693:                 my (%haschgs,%inconf);
 7694:                 if ($context eq 'requestauthor') {
 7695:                     %haschgs = %changes;
 7696:                     %inconf = %confhash;
 7697:                 } else {
 7698:                     if (ref($changes{$item}) eq 'HASH') {
 7699:                         %haschgs = %{$changes{$item}};
 7700:                     }
 7701:                     if (ref($confhash{$item}) eq 'HASH') {
 7702:                         %inconf = %{$confhash{$item}};
 7703:                     }
 7704:                 }
 7705:                 if (keys(%haschgs) > 0) {
 7706:                     my $newacc = 
 7707:                         &Apache::lonnet::usertools_access($env{'user.name'},
 7708:                                                           $env{'user.domain'},
 7709:                                                           $item,'reload',$context);
 7710:                     if (($context eq 'requestcourses') ||
 7711:                         ($context eq 'requestauthor')) {
 7712:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 7713:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 7714:                         }
 7715:                     } else {
 7716:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 7717:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 7718:                         }
 7719:                     }
 7720:                     unless ($context eq 'requestauthor') {
 7721:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 7722:                     }
 7723:                     foreach my $type (@{$types},'default','_LC_adv') {
 7724:                         if ($haschgs{$type}) {
 7725:                             my $typetitle = $usertypes->{$type};
 7726:                             if ($type eq 'default') {
 7727:                                 $typetitle = $othertitle;
 7728:                             } elsif ($type eq '_LC_adv') {
 7729:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 7730:                             }
 7731:                             if ($inconf{$type}) {
 7732:                                 if ($context eq 'requestcourses') {
 7733:                                     my $cond;
 7734:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 7735:                                         if ($1 eq '') {
 7736:                                             $cond = &mt('(Automatic processing of any request).');
 7737:                                         } else {
 7738:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 7739:                                         }
 7740:                                     } else { 
 7741:                                         $cond = $conditions{$inconf{$type}};
 7742:                                     }
 7743:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 7744:                                 } elsif ($context eq 'requestauthor') {
 7745:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 7746:                                                              $titles{$inconf{$type}},$typetitle);
 7747: 
 7748:                                 } else {
 7749:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7750:                                 }
 7751:                             } else {
 7752:                                 if ($type eq '_LC_adv') {
 7753:                                     if ($inconf{$type} eq '0') {
 7754:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7755:                                     } else { 
 7756:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7757:                                     }
 7758:                                 } else {
 7759:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7760:                                 }
 7761:                             }
 7762:                         }
 7763:                     }
 7764:                     unless ($context eq 'requestauthor') {
 7765:                         $resulttext .= '</ul></li>';
 7766:                     }
 7767:                 }
 7768:             }
 7769:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7770:                 if (ref($changes{'notify'}) eq 'HASH') {
 7771:                     if ($changes{'notify'}{'approval'}) {
 7772:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7773:                             if ($confhash{'notify'}{'approval'}) {
 7774:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7775:                             } else {
 7776:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7777:                             }
 7778:                         }
 7779:                     }
 7780:                 }
 7781:             }
 7782:             if ($action eq 'requestcourses') {
 7783:                 my @offon = ('off','on');
 7784:                 if ($changes{'uniquecode'}) {
 7785:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7786:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7787:                         $resulttext .= '<li>'.
 7788:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7789:                                        '</li>';
 7790:                     } else {
 7791:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7792:                                        '</li>';
 7793:                     }
 7794:                 }
 7795:                 foreach my $type ('textbooks','templates') {
 7796:                     if (ref($changes{$type}) eq 'HASH') {
 7797:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 7798:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 7799:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 7800:                             my $coursetitle = $coursehash{'description'};
 7801:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 7802:                             $resulttext .= '<li>';
 7803:                             foreach my $item ('subject','title','publisher','author') {
 7804:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7805:                                          ($type eq 'templates'));
 7806:                                 my $name = $item.':';
 7807:                                 $name =~ s/^(\w)/\U$1/;
 7808:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 7809:                             }
 7810:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7811:                             if ($type eq 'textbooks') {
 7812:                                 if ($confhash{$type}{$key}{'image'}) {
 7813:                                     $resulttext .= ' '.&mt('Image: [_1]',
 7814:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 7815:                                                    ' alt="Textbook cover" />').'<br />';
 7816:                                 }
 7817:                             }
 7818:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7819:                         }
 7820:                         $resulttext .= '</ul></li>';
 7821:                     }
 7822:                 }
 7823:                 if (ref($changes{'validation'}) eq 'HASH') {
 7824:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7825:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7826:                         foreach my $item (@{$validationitemsref}) {
 7827:                             if (exists($changes{'validation'}{$item})) {
 7828:                                 if ($item eq 'markup') {
 7829:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7830:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7831:                                 } else {
 7832:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7833:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7834:                                 }
 7835:                             }
 7836:                         }
 7837:                         if (exists($changes{'validation'}{'dc'})) {
 7838:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7839:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7840:                         }
 7841:                     }
 7842:                 }
 7843:             }
 7844:             $resulttext .= '</ul>';
 7845:             if (keys(%newenv)) {
 7846:                 &Apache::lonnet::appenv(\%newenv);
 7847:             }
 7848:         } else {
 7849:             if ($context eq 'requestcourses') {
 7850:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7851:             } elsif ($context eq 'requestauthor') {
 7852:                 $resulttext = &mt('No changes made to rights to request author space.');
 7853:             } else {
 7854:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7855:             }
 7856:         }
 7857:     } else {
 7858:         $resulttext = '<span class="LC_error">'.
 7859: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7860:     }
 7861:     if ($errors) {
 7862:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7863:                        '<ul>'.$errors.'</ul></p>';
 7864:     }
 7865:     return $resulttext;
 7866: }
 7867: 
 7868: sub process_textbook_image {
 7869:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 7870:     my $filename = $env{'form.'.$caller.'.filename'};
 7871:     my ($error,$url);
 7872:     my ($width,$height) = (50,50);
 7873:     if ($configuserok eq 'ok') {
 7874:         if ($switchserver) {
 7875:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7876:                          $switchserver);
 7877:         } elsif ($author_ok eq 'ok') {
 7878:             my ($result,$imageurl) =
 7879:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7880:                              "$type/$dom/$cnum/cover",$width,$height);
 7881:             if ($result eq 'ok') {
 7882:                 $url = $imageurl;
 7883:             } else {
 7884:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7885:             }
 7886:         } else {
 7887:             $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);
 7888:         }
 7889:     } else {
 7890:         $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);
 7891:     }
 7892:     return ($url,$error);
 7893: }
 7894: 
 7895: sub modify_autoenroll {
 7896:     my ($dom,$lastactref,%domconfig) = @_;
 7897:     my ($resulttext,%changes);
 7898:     my %currautoenroll;
 7899:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7900:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7901:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7902:         }
 7903:     }
 7904:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7905:     my %title = ( run => 'Auto-enrollment active',
 7906:                   sender => 'Sender for notification messages',
 7907:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7908:     my @offon = ('off','on');
 7909:     my $sender_uname = $env{'form.sender_uname'};
 7910:     my $sender_domain = $env{'form.sender_domain'};
 7911:     if ($sender_domain eq '') {
 7912:         $sender_uname = '';
 7913:     } elsif ($sender_uname eq '') {
 7914:         $sender_domain = '';
 7915:     }
 7916:     my $coowners = $env{'form.autoassign_coowners'};
 7917:     my %autoenrollhash =  (
 7918:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7919:                                        'sender_uname' => $sender_uname,
 7920:                                        'sender_domain' => $sender_domain,
 7921:                                        'co-owners' => $coowners,
 7922:                                 }
 7923:                      );
 7924:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7925:                                              $dom);
 7926:     if ($putresult eq 'ok') {
 7927:         if (exists($currautoenroll{'run'})) {
 7928:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7929:                  $changes{'run'} = 1;
 7930:              }
 7931:         } elsif ($autorun) {
 7932:             if ($env{'form.autoenroll_run'} ne '1') {
 7933:                  $changes{'run'} = 1;
 7934:             }
 7935:         }
 7936:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7937:             $changes{'sender'} = 1;
 7938:         }
 7939:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7940:             $changes{'sender'} = 1;
 7941:         }
 7942:         if ($currautoenroll{'co-owners'} ne '') {
 7943:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7944:                 $changes{'coowners'} = 1;
 7945:             }
 7946:         } elsif ($coowners) {
 7947:             $changes{'coowners'} = 1;
 7948:         }      
 7949:         if (keys(%changes) > 0) {
 7950:             $resulttext = &mt('Changes made:').'<ul>';
 7951:             if ($changes{'run'}) {
 7952:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7953:             }
 7954:             if ($changes{'sender'}) {
 7955:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7956:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7957:                 } else {
 7958:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7959:                 }
 7960:             }
 7961:             if ($changes{'coowners'}) {
 7962:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7963:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7964:                 if (ref($lastactref) eq 'HASH') {
 7965:                     $lastactref->{'domainconfig'} = 1;
 7966:                 }
 7967:             }
 7968:             $resulttext .= '</ul>';
 7969:         } else {
 7970:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7971:         }
 7972:     } else {
 7973:         $resulttext = '<span class="LC_error">'.
 7974: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7975:     }
 7976:     return $resulttext;
 7977: }
 7978: 
 7979: sub modify_autoupdate {
 7980:     my ($dom,%domconfig) = @_;
 7981:     my ($resulttext,%currautoupdate,%fields,%changes);
 7982:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7983:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7984:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7985:         }
 7986:     }
 7987:     my @offon = ('off','on');
 7988:     my %title = &Apache::lonlocal::texthash (
 7989:                    run => 'Auto-update:',
 7990:                    classlists => 'Updates to user information in classlists?'
 7991:                 );
 7992:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7993:     my %fieldtitles = &Apache::lonlocal::texthash (
 7994:                         id => 'Student/Employee ID',
 7995:                         permanentemail => 'E-mail address',
 7996:                         lastname => 'Last Name',
 7997:                         firstname => 'First Name',
 7998:                         middlename => 'Middle Name',
 7999:                         generation => 'Generation',
 8000:                       );
 8001:     $othertitle = &mt('All users');
 8002:     if (keys(%{$usertypes}) >  0) {
 8003:         $othertitle = &mt('Other users');
 8004:     }
 8005:     foreach my $key (keys(%env)) {
 8006:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 8007:             my ($usertype,$item) = ($1,$2);
 8008:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 8009:                 if ($usertype eq 'default') {   
 8010:                     push(@{$fields{$1}},$2);
 8011:                 } elsif (ref($types) eq 'ARRAY') {
 8012:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 8013:                         push(@{$fields{$1}},$2);
 8014:                     }
 8015:                 }
 8016:             }
 8017:         }
 8018:     }
 8019:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 8020:     @lockablenames = sort(@lockablenames);
 8021:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 8022:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 8023:         if (@changed) {
 8024:             $changes{'lockablenames'} = 1;
 8025:         }
 8026:     } else {
 8027:         if (@lockablenames) {
 8028:             $changes{'lockablenames'} = 1;
 8029:         }
 8030:     }
 8031:     my %updatehash = (
 8032:                       autoupdate => { run => $env{'form.autoupdate_run'},
 8033:                                       classlists => $env{'form.classlists'},
 8034:                                       fields => {%fields},
 8035:                                       lockablenames => \@lockablenames,
 8036:                                     }
 8037:                      );
 8038:     foreach my $key (keys(%currautoupdate)) {
 8039:         if (($key eq 'run') || ($key eq 'classlists')) {
 8040:             if (exists($updatehash{autoupdate}{$key})) {
 8041:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 8042:                     $changes{$key} = 1;
 8043:                 }
 8044:             }
 8045:         } elsif ($key eq 'fields') {
 8046:             if (ref($currautoupdate{$key}) eq 'HASH') {
 8047:                 foreach my $item (@{$types},'default') {
 8048:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 8049:                         my $change = 0;
 8050:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 8051:                             if (!exists($fields{$item})) {
 8052:                                 $change = 1;
 8053:                                 last;
 8054:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 8055:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 8056:                                     $change = 1;
 8057:                                     last;
 8058:                                 }
 8059:                             }
 8060:                         }
 8061:                         if ($change) {
 8062:                             push(@{$changes{$key}},$item);
 8063:                         }
 8064:                     } 
 8065:                 }
 8066:             }
 8067:         } elsif ($key eq 'lockablenames') {
 8068:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 8069:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 8070:                 if (@changed) {
 8071:                     $changes{'lockablenames'} = 1;
 8072:                 }
 8073:             } else {
 8074:                 if (@lockablenames) {
 8075:                     $changes{'lockablenames'} = 1;
 8076:                 }
 8077:             }
 8078:         }
 8079:     }
 8080:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 8081:         if (@lockablenames) {
 8082:             $changes{'lockablenames'} = 1;
 8083:         }
 8084:     }
 8085:     foreach my $item (@{$types},'default') {
 8086:         if (defined($fields{$item})) {
 8087:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 8088:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 8089:                     my $change = 0;
 8090:                     if (ref($fields{$item}) eq 'ARRAY') {
 8091:                         foreach my $type (@{$fields{$item}}) {
 8092:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 8093:                                 $change = 1;
 8094:                                 last;
 8095:                             }
 8096:                         }
 8097:                     }
 8098:                     if ($change) {
 8099:                         push(@{$changes{'fields'}},$item);
 8100:                     }
 8101:                 } else {
 8102:                     push(@{$changes{'fields'}},$item);
 8103:                 }
 8104:             } else {
 8105:                 push(@{$changes{'fields'}},$item);
 8106:             }
 8107:         }
 8108:     }
 8109:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 8110:                                              $dom);
 8111:     if ($putresult eq 'ok') {
 8112:         if (keys(%changes) > 0) {
 8113:             $resulttext = &mt('Changes made:').'<ul>';
 8114:             foreach my $key (sort(keys(%changes))) {
 8115:                 if ($key eq 'lockablenames') {
 8116:                     $resulttext .= '<li>';
 8117:                     if (@lockablenames) {
 8118:                         $usertypes->{'default'} = $othertitle;
 8119:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 8120:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 8121:                     } else {
 8122:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 8123:                     }
 8124:                     $resulttext .= '</li>';
 8125:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 8126:                     foreach my $item (@{$changes{$key}}) {
 8127:                         my @newvalues;
 8128:                         foreach my $type (@{$fields{$item}}) {
 8129:                             push(@newvalues,$fieldtitles{$type});
 8130:                         }
 8131:                         my $newvaluestr;
 8132:                         if (@newvalues > 0) {
 8133:                             $newvaluestr = join(', ',@newvalues);
 8134:                         } else {
 8135:                             $newvaluestr = &mt('none');
 8136:                         }
 8137:                         if ($item eq 'default') {
 8138:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 8139:                         } else {
 8140:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 8141:                         }
 8142:                     }
 8143:                 } else {
 8144:                     my $newvalue;
 8145:                     if ($key eq 'run') {
 8146:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 8147:                     } else {
 8148:                         $newvalue = $offon[$env{'form.'.$key}];
 8149:                     }
 8150:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 8151:                 }
 8152:             }
 8153:             $resulttext .= '</ul>';
 8154:         } else {
 8155:             $resulttext = &mt('No changes made to autoupdates');
 8156:         }
 8157:     } else {
 8158:         $resulttext = '<span class="LC_error">'.
 8159: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8160:     }
 8161:     return $resulttext;
 8162: }
 8163: 
 8164: sub modify_autocreate {
 8165:     my ($dom,%domconfig) = @_;
 8166:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 8167:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 8168:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 8169:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 8170:         }
 8171:     }
 8172:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 8173:                  req => 'Auto-creation of validated requests for official courses',
 8174:                  xmldc => 'Identity of course creator of courses from XML files',
 8175:                );
 8176:     my @types = ('xml','req');
 8177:     foreach my $item (@types) {
 8178:         $newvals{$item} = $env{'form.autocreate_'.$item};
 8179:         $newvals{$item} =~ s/\D//g;
 8180:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 8181:     }
 8182:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 8183:     my %domcoords = &get_active_dcs($dom);
 8184:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 8185:         $newvals{'xmldc'} = '';
 8186:     } 
 8187:     %autocreatehash =  (
 8188:                         autocreate => { xml => $newvals{'xml'},
 8189:                                         req => $newvals{'req'},
 8190:                                       }
 8191:                        );
 8192:     if ($newvals{'xmldc'} ne '') {
 8193:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 8194:     }
 8195:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 8196:                                              $dom);
 8197:     if ($putresult eq 'ok') {
 8198:         my @items = @types;
 8199:         if ($newvals{'xml'}) {
 8200:             push(@items,'xmldc');
 8201:         }
 8202:         foreach my $item (@items) {
 8203:             if (exists($currautocreate{$item})) {
 8204:                 if ($currautocreate{$item} ne $newvals{$item}) {
 8205:                     $changes{$item} = 1;
 8206:                 }
 8207:             } elsif ($newvals{$item}) {
 8208:                 $changes{$item} = 1;
 8209:             }
 8210:         }
 8211:         if (keys(%changes) > 0) {
 8212:             my @offon = ('off','on'); 
 8213:             $resulttext = &mt('Changes made:').'<ul>';
 8214:             foreach my $item (@types) {
 8215:                 if ($changes{$item}) {
 8216:                     my $newtxt = $offon[$newvals{$item}];
 8217:                     $resulttext .= '<li>'.
 8218:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 8219:                                        '<b>','</b>').
 8220:                                    '</li>';
 8221:                 }
 8222:             }
 8223:             if ($changes{'xmldc'}) {
 8224:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 8225:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 8226:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 8227:             }
 8228:             $resulttext .= '</ul>';
 8229:         } else {
 8230:             $resulttext = &mt('No changes made to auto-creation settings');
 8231:         }
 8232:     } else {
 8233:         $resulttext = '<span class="LC_error">'.
 8234:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8235:     }
 8236:     return $resulttext;
 8237: }
 8238: 
 8239: sub modify_directorysrch {
 8240:     my ($dom,%domconfig) = @_;
 8241:     my ($resulttext,%changes);
 8242:     my %currdirsrch;
 8243:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 8244:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 8245:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 8246:         }
 8247:     }
 8248:     my %title = ( available => 'Directory search available',
 8249:                   localonly => 'Other domains can search',
 8250:                   searchby => 'Search types',
 8251:                   searchtypes => 'Search latitude');
 8252:     my @offon = ('off','on');
 8253:     my @otherdoms = ('Yes','No');
 8254: 
 8255:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 8256:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 8257:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 8258: 
 8259:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8260:     if (keys(%{$usertypes}) == 0) {
 8261:         @cansearch = ('default');
 8262:     } else {
 8263:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 8264:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 8265:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 8266:                     push(@{$changes{'cansearch'}},$type);
 8267:                 }
 8268:             }
 8269:             foreach my $type (@cansearch) {
 8270:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 8271:                     push(@{$changes{'cansearch'}},$type);
 8272:                 }
 8273:             }
 8274:         } else {
 8275:             push(@{$changes{'cansearch'}},@cansearch);
 8276:         }
 8277:     }
 8278: 
 8279:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 8280:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 8281:             if (!grep(/^\Q$by\E$/,@searchby)) {
 8282:                 push(@{$changes{'searchby'}},$by);
 8283:             }
 8284:         }
 8285:         foreach my $by (@searchby) {
 8286:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 8287:                 push(@{$changes{'searchby'}},$by);
 8288:             }
 8289:         }
 8290:     } else {
 8291:         push(@{$changes{'searchby'}},@searchby);
 8292:     }
 8293: 
 8294:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 8295:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 8296:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 8297:                 push(@{$changes{'searchtypes'}},$type);
 8298:             }
 8299:         }
 8300:         foreach my $type (@searchtypes) {
 8301:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 8302:                 push(@{$changes{'searchtypes'}},$type);
 8303:             }
 8304:         }
 8305:     } else {
 8306:         if (exists($currdirsrch{'searchtypes'})) {
 8307:             foreach my $type (@searchtypes) {  
 8308:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 8309:                     push(@{$changes{'searchtypes'}},$type);
 8310:                 }
 8311:             }
 8312:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 8313:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 8314:             }   
 8315:         } else {
 8316:             push(@{$changes{'searchtypes'}},@searchtypes); 
 8317:         }
 8318:     }
 8319: 
 8320:     my %dirsrch_hash =  (
 8321:             directorysrch => { available => $env{'form.dirsrch_available'},
 8322:                                cansearch => \@cansearch,
 8323:                                localonly => $env{'form.dirsrch_localonly'},
 8324:                                searchby => \@searchby,
 8325:                                searchtypes => \@searchtypes,
 8326:                              }
 8327:             );
 8328:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 8329:                                              $dom);
 8330:     if ($putresult eq 'ok') {
 8331:         if (exists($currdirsrch{'available'})) {
 8332:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 8333:                  $changes{'available'} = 1;
 8334:              }
 8335:         } else {
 8336:             if ($env{'form.dirsrch_available'} eq '1') {
 8337:                 $changes{'available'} = 1;
 8338:             }
 8339:         }
 8340:         if (exists($currdirsrch{'localonly'})) {
 8341:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 8342:                  $changes{'localonly'} = 1;
 8343:              }
 8344:         } else {
 8345:             if ($env{'form.dirsrch_localonly'} eq '1') {
 8346:                 $changes{'localonly'} = 1;
 8347:             }
 8348:         }
 8349:         if (keys(%changes) > 0) {
 8350:             $resulttext = &mt('Changes made:').'<ul>';
 8351:             if ($changes{'available'}) {
 8352:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 8353:             }
 8354:             if ($changes{'localonly'}) {
 8355:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 8356:             }
 8357: 
 8358:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 8359:                 my $chgtext;
 8360:                 if (ref($usertypes) eq 'HASH') {
 8361:                     if (keys(%{$usertypes}) > 0) {
 8362:                         foreach my $type (@{$types}) {
 8363:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 8364:                                 $chgtext .= $usertypes->{$type}.'; ';
 8365:                             }
 8366:                         }
 8367:                         if (grep(/^default$/,@cansearch)) {
 8368:                             $chgtext .= $othertitle;
 8369:                         } else {
 8370:                             $chgtext =~ s/\; $//;
 8371:                         }
 8372:                         $resulttext .=
 8373:                             '<li>'.
 8374:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 8375:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 8376:                             '</li>';
 8377:                     }
 8378:                 }
 8379:             }
 8380:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 8381:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 8382:                 my $chgtext;
 8383:                 foreach my $type (@{$titleorder}) {
 8384:                     if (grep(/^\Q$type\E$/,@searchby)) {
 8385:                         if (defined($searchtitles->{$type})) {
 8386:                             $chgtext .= $searchtitles->{$type}.'; ';
 8387:                         }
 8388:                     }
 8389:                 }
 8390:                 $chgtext =~ s/\; $//;
 8391:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 8392:             }
 8393:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 8394:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 8395:                 my $chgtext;
 8396:                 foreach my $type (@{$srchtypeorder}) {
 8397:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 8398:                         if (defined($srchtypes_desc->{$type})) {
 8399:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 8400:                         }
 8401:                     }
 8402:                 }
 8403:                 $chgtext =~ s/\; $//;
 8404:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 8405:             }
 8406:             $resulttext .= '</ul>';
 8407:         } else {
 8408:             $resulttext = &mt('No changes made to institution directory search settings');
 8409:         }
 8410:     } else {
 8411:         $resulttext = '<span class="LC_error">'.
 8412:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 8413:     }
 8414:     return $resulttext;
 8415: }
 8416: 
 8417: sub modify_contacts {
 8418:     my ($dom,$lastactref,%domconfig) = @_;
 8419:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 8420:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 8421:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 8422:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 8423:         }
 8424:     }
 8425:     my (%others,%to,%bcc);
 8426:     my @contacts = ('supportemail','adminemail');
 8427:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 8428:                     'requestsmail','updatesmail','idconflictsmail');
 8429:     my @toggles = ('reporterrors','reportupdates');
 8430:     foreach my $type (@mailings) {
 8431:         @{$newsetting{$type}} = 
 8432:             &Apache::loncommon::get_env_multiple('form.'.$type);
 8433:         foreach my $item (@contacts) {
 8434:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 8435:                 $contacts_hash{contacts}{$type}{$item} = 1;
 8436:             } else {
 8437:                 $contacts_hash{contacts}{$type}{$item} = 0;
 8438:             }
 8439:         }  
 8440:         $others{$type} = $env{'form.'.$type.'_others'};
 8441:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 8442:         if ($type eq 'helpdeskmail') {
 8443:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 8444:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 8445:         }
 8446:     }
 8447:     foreach my $item (@contacts) {
 8448:         $to{$item} = $env{'form.'.$item};
 8449:         $contacts_hash{'contacts'}{$item} = $to{$item};
 8450:     }
 8451:     foreach my $item (@toggles) {
 8452:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 8453:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 8454:         }
 8455:     }
 8456:     if (keys(%currsetting) > 0) {
 8457:         foreach my $item (@contacts) {
 8458:             if ($to{$item} ne $currsetting{$item}) {
 8459:                 $changes{$item} = 1;
 8460:             }
 8461:         }
 8462:         foreach my $type (@mailings) {
 8463:             foreach my $item (@contacts) {
 8464:                 if (ref($currsetting{$type}) eq 'HASH') {
 8465:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 8466:                         push(@{$changes{$type}},$item);
 8467:                     }
 8468:                 } else {
 8469:                     push(@{$changes{$type}},@{$newsetting{$type}});
 8470:                 }
 8471:             }
 8472:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 8473:                 push(@{$changes{$type}},'others');
 8474:             }
 8475:             if ($type eq 'helpdeskmail') {   
 8476:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 8477:                     push(@{$changes{$type}},'bcc'); 
 8478:                 }
 8479:             }
 8480:         }
 8481:     } else {
 8482:         my %default;
 8483:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 8484:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 8485:         $default{'errormail'} = 'adminemail';
 8486:         $default{'packagesmail'} = 'adminemail';
 8487:         $default{'helpdeskmail'} = 'supportemail';
 8488:         $default{'lonstatusmail'} = 'adminemail';
 8489:         $default{'requestsmail'} = 'adminemail';
 8490:         $default{'updatesmail'} = 'adminemail';
 8491:         foreach my $item (@contacts) {
 8492:            if ($to{$item} ne $default{$item}) {
 8493:               $changes{$item} = 1;
 8494:            }
 8495:         }
 8496:         foreach my $type (@mailings) {
 8497:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 8498:                
 8499:                 push(@{$changes{$type}},@{$newsetting{$type}});
 8500:             }
 8501:             if ($others{$type} ne '') {
 8502:                 push(@{$changes{$type}},'others');
 8503:             }
 8504:             if ($type eq 'helpdeskmail') {
 8505:                 if ($bcc{$type} ne '') {
 8506:                     push(@{$changes{$type}},'bcc');
 8507:                 }
 8508:             }
 8509:         }
 8510:     }
 8511:     foreach my $item (@toggles) {
 8512:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 8513:             $changes{$item} = 1;
 8514:         } elsif ((!$env{'form.'.$item}) &&
 8515:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 8516:             $changes{$item} = 1;
 8517:         }
 8518:     }
 8519:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 8520:                                              $dom);
 8521:     if ($putresult eq 'ok') {
 8522:         if (keys(%changes) > 0) {
 8523:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8524:             if (ref($lastactref) eq 'HASH') {
 8525:                 $lastactref->{'domainconfig'} = 1;
 8526:             }
 8527:             my ($titles,$short_titles)  = &contact_titles();
 8528:             $resulttext = &mt('Changes made:').'<ul>';
 8529:             foreach my $item (@contacts) {
 8530:                 if ($changes{$item}) {
 8531:                     $resulttext .= '<li>'.$titles->{$item}.
 8532:                                     &mt(' set to: ').
 8533:                                     '<span class="LC_cusr_emph">'.
 8534:                                     $to{$item}.'</span></li>';
 8535:                 }
 8536:             }
 8537:             foreach my $type (@mailings) {
 8538:                 if (ref($changes{$type}) eq 'ARRAY') {
 8539:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 8540:                     my @text;
 8541:                     foreach my $item (@{$newsetting{$type}}) {
 8542:                         push(@text,$short_titles->{$item});
 8543:                     }
 8544:                     if ($others{$type} ne '') {
 8545:                         push(@text,$others{$type});
 8546:                     }
 8547:                     $resulttext .= '<span class="LC_cusr_emph">'.
 8548:                                    join(', ',@text).'</span>';
 8549:                     if ($type eq 'helpdeskmail') {
 8550:                         if ($bcc{$type} ne '') {
 8551:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 8552:                         }
 8553:                     }
 8554:                     $resulttext .= '</li>';
 8555:                 }
 8556:             }
 8557:             my @offon = ('off','on');
 8558:             if ($changes{'reporterrors'}) {
 8559:                 $resulttext .= '<li>'.
 8560:                                &mt('E-mail error reports to [_1] set to "'.
 8561:                                    $offon[$env{'form.reporterrors'}].'".',
 8562:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8563:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 8564:                                '</li>';
 8565:             }
 8566:             if ($changes{'reportupdates'}) {
 8567:                 $resulttext .= '<li>'.
 8568:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 8569:                                     $offon[$env{'form.reportupdates'}].'".',
 8570:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8571:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 8572:                                 '</li>';
 8573:             }
 8574:             $resulttext .= '</ul>';
 8575:         } else {
 8576:             $resulttext = &mt('No changes made to contact information');
 8577:         }
 8578:     } else {
 8579:         $resulttext = '<span class="LC_error">'.
 8580:             &mt('An error occurred: [_1].',$putresult).'</span>';
 8581:     }
 8582:     return $resulttext;
 8583: }
 8584: 
 8585: sub modify_usercreation {
 8586:     my ($dom,%domconfig) = @_;
 8587:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 8588:     my $warningmsg;
 8589:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8590:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8591:             if ($key eq 'cancreate') {
 8592:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8593:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8594:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8595:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 8596:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8597:                         } else {
 8598:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8599:                         }
 8600:                     }
 8601:                 }
 8602:             } elsif ($key eq 'email_rule') {
 8603:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8604:             } else {
 8605:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8606:             }
 8607:         }
 8608:     }
 8609:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 8610:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 8611:     my @contexts = ('author','course','requestcrs');
 8612:     foreach my $item(@contexts) {
 8613:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 8614:     }
 8615:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8616:         foreach my $item (@contexts) {
 8617:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 8618:                 push(@{$changes{'cancreate'}},$item);
 8619:             }
 8620:         }
 8621:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 8622:         foreach my $item (@contexts) {
 8623:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 8624:                 if ($cancreate{$item} ne 'any') {
 8625:                     push(@{$changes{'cancreate'}},$item);
 8626:                 }
 8627:             } else {
 8628:                 if ($cancreate{$item} ne 'none') {
 8629:                     push(@{$changes{'cancreate'}},$item);
 8630:                 }
 8631:             }
 8632:         }
 8633:     } else {
 8634:         foreach my $item (@contexts)  {
 8635:             push(@{$changes{'cancreate'}},$item);
 8636:         }
 8637:     }
 8638: 
 8639:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 8640:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 8641:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 8642:                 push(@{$changes{'username_rule'}},$type);
 8643:             }
 8644:         }
 8645:         foreach my $type (@username_rule) {
 8646:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 8647:                 push(@{$changes{'username_rule'}},$type);
 8648:             }
 8649:         }
 8650:     } else {
 8651:         push(@{$changes{'username_rule'}},@username_rule);
 8652:     }
 8653: 
 8654:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 8655:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 8656:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 8657:                 push(@{$changes{'id_rule'}},$type);
 8658:             }
 8659:         }
 8660:         foreach my $type (@id_rule) {
 8661:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 8662:                 push(@{$changes{'id_rule'}},$type);
 8663:             }
 8664:         }
 8665:     } else {
 8666:         push(@{$changes{'id_rule'}},@id_rule);
 8667:     }
 8668: 
 8669:     my @authen_contexts = ('author','course','domain');
 8670:     my @authtypes = ('int','krb4','krb5','loc');
 8671:     my %authhash;
 8672:     foreach my $item (@authen_contexts) {
 8673:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 8674:         foreach my $auth (@authtypes) {
 8675:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 8676:                 $authhash{$item}{$auth} = 1;
 8677:             } else {
 8678:                 $authhash{$item}{$auth} = 0;
 8679:             }
 8680:         }
 8681:     }
 8682:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 8683:         foreach my $item (@authen_contexts) {
 8684:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 8685:                 foreach my $auth (@authtypes) {
 8686:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 8687:                         push(@{$changes{'authtypes'}},$item);
 8688:                         last;
 8689:                     }
 8690:                 }
 8691:             }
 8692:         }
 8693:     } else {
 8694:         foreach my $item (@authen_contexts) {
 8695:             push(@{$changes{'authtypes'}},$item);
 8696:         }
 8697:     }
 8698: 
 8699:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 8700:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 8701:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 8702:     $save_usercreate{'id_rule'} = \@id_rule;
 8703:     $save_usercreate{'username_rule'} = \@username_rule,
 8704:     $save_usercreate{'authtypes'} = \%authhash;
 8705: 
 8706:     my %usercreation_hash =  (
 8707:         usercreation     => \%save_usercreate,
 8708:     );
 8709: 
 8710:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 8711:                                              $dom);
 8712: 
 8713:     if ($putresult eq 'ok') {
 8714:         if (keys(%changes) > 0) {
 8715:             $resulttext = &mt('Changes made:').'<ul>';
 8716:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8717:                 my %lt = &usercreation_types();
 8718:                 foreach my $type (@{$changes{'cancreate'}}) {
 8719:                     my $chgtext = $lt{$type}.', ';
 8720:                     if ($cancreate{$type} eq 'none') {
 8721:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 8722:                     } elsif ($cancreate{$type} eq 'any') {
 8723:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 8724:                     } elsif ($cancreate{$type} eq 'official') {
 8725:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 8726:                     } elsif ($cancreate{$type} eq 'unofficial') {
 8727:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 8728:                     }
 8729:                     $resulttext .= '<li>'.$chgtext.'</li>';
 8730:                 }
 8731:             }
 8732:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 8733:                 my ($rules,$ruleorder) = 
 8734:                     &Apache::lonnet::inst_userrules($dom,'username');
 8735:                 my $chgtext = '<ul>';
 8736:                 foreach my $type (@username_rule) {
 8737:                     if (ref($rules->{$type}) eq 'HASH') {
 8738:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 8739:                     }
 8740:                 }
 8741:                 $chgtext .= '</ul>';
 8742:                 if (@username_rule > 0) {
 8743:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 8744:                 } else {
 8745:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 8746:                 }
 8747:             }
 8748:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 8749:                 my ($idrules,$idruleorder) = 
 8750:                     &Apache::lonnet::inst_userrules($dom,'id');
 8751:                 my $chgtext = '<ul>';
 8752:                 foreach my $type (@id_rule) {
 8753:                     if (ref($idrules->{$type}) eq 'HASH') {
 8754:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8755:                     }
 8756:                 }
 8757:                 $chgtext .= '</ul>';
 8758:                 if (@id_rule > 0) {
 8759:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8760:                 } else {
 8761:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8762:                 }
 8763:             }
 8764:             my %authname = &authtype_names();
 8765:             my %context_title = &context_names();
 8766:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8767:                 my $chgtext = '<ul>';
 8768:                 foreach my $type (@{$changes{'authtypes'}}) {
 8769:                     my @allowed;
 8770:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8771:                     foreach my $auth (@authtypes) {
 8772:                         if ($authhash{$type}{$auth}) {
 8773:                             push(@allowed,$authname{$auth});
 8774:                         }
 8775:                     }
 8776:                     if (@allowed > 0) {
 8777:                         $chgtext .= join(', ',@allowed).'</li>';
 8778:                     } else {
 8779:                         $chgtext .= &mt('none').'</li>';
 8780:                     }
 8781:                 }
 8782:                 $chgtext .= '</ul>';
 8783:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8784:                 $resulttext .= '</li>';
 8785:             }
 8786:             $resulttext .= '</ul>';
 8787:         } else {
 8788:             $resulttext = &mt('No changes made to user creation settings');
 8789:         }
 8790:     } else {
 8791:         $resulttext = '<span class="LC_error">'.
 8792:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8793:     }
 8794:     if ($warningmsg ne '') {
 8795:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8796:     }
 8797:     return $resulttext;
 8798: }
 8799: 
 8800: sub modify_selfcreation {
 8801:     my ($dom,%domconfig) = @_;
 8802:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8803:     my (%save_usercreate,%save_usermodify);
 8804:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8805:     if (ref($types) eq 'ARRAY') {
 8806:         $usertypes->{'default'} = $othertitle;
 8807:         push(@{$types},'default');
 8808:     }
 8809: #
 8810: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8811: #
 8812:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8813:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8814:             if ($key eq 'cancreate') {
 8815:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8816:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8817:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8818:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8819:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 8820:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 8821:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8822:                         } else {
 8823:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8824:                         }
 8825:                     }
 8826:                 }
 8827:             } elsif ($key eq 'email_rule') {
 8828:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8829:             } else {
 8830:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8831:             }
 8832:         }
 8833:     }
 8834: #
 8835: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8836: #
 8837:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8838:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8839:             if ($key eq 'selfcreate') {
 8840:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8841:             } else {
 8842:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8843:             }
 8844:         }
 8845:     }
 8846: 
 8847:     my @contexts = ('selfcreate');
 8848:     @{$cancreate{'selfcreate'}} = ();
 8849:     %{$cancreate{'emailusername'}} = ();
 8850:     @{$cancreate{'statustocreate'}} = ();
 8851:     %{$cancreate{'selfcreateprocessing'}} = ();
 8852:     %{$cancreate{'shibenv'}} = ();
 8853:     my %selfcreatetypes = (
 8854:                              sso   => 'users authenticated by institutional single sign on',
 8855:                              login => 'users authenticated by institutional log-in',
 8856:                              email => 'users who provide a valid e-mail address for use as username',
 8857:                           );
 8858: #
 8859: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8860: # is permitted.
 8861: #
 8862: 
 8863:     my @statuses;
 8864:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 8865:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 8866:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 8867:         }
 8868:     }
 8869:     push(@statuses,'default');
 8870: 
 8871:     foreach my $item ('login','sso','email') {
 8872:         if ($item eq 'email') {
 8873:             if ($env{'form.cancreate_email'}) {
 8874:                 push(@{$cancreate{'selfcreate'}},'email');
 8875:                 push(@contexts,'selfcreateprocessing');
 8876:                 foreach my $type (@statuses) {
 8877:                     if ($type eq 'default') {
 8878:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 8879:                     } else { 
 8880:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 8881:                     }
 8882:                 }
 8883:             }
 8884:         } else {
 8885:             if ($env{'form.cancreate_'.$item}) {
 8886:                 push(@{$cancreate{'selfcreate'}},$item);
 8887:             }
 8888:         }
 8889:     }
 8890:     my (@email_rule,%userinfo,%savecaptcha);
 8891:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8892: #
 8893: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8894: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8895: #
 8896: 
 8897:     if ($env{'form.cancreate_email'}) {
 8898:         push(@contexts,'emailusername');
 8899:         if (ref($types) eq 'ARRAY') {
 8900:             foreach my $type (@{$types}) {
 8901:                 if (ref($infofields) eq 'ARRAY') {
 8902:                     foreach my $field (@{$infofields}) {
 8903:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8904:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8905:                         }
 8906:                     }
 8907:                 }
 8908:             }
 8909:         }
 8910: #
 8911: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8912: # queued requests for self-creation of account using e-mail address as username
 8913: #
 8914: 
 8915:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8916:         @approvalnotify = sort(@approvalnotify);
 8917:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8918:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8919:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8920:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8921:                     push(@{$changes{'cancreate'}},'notify');
 8922:                 }
 8923:             } else {
 8924:                 if ($cancreate{'notify'}{'approval'}) {
 8925:                     push(@{$changes{'cancreate'}},'notify');
 8926:                 }
 8927:             }
 8928:         } elsif ($cancreate{'notify'}{'approval'}) {
 8929:             push(@{$changes{'cancreate'}},'notify');
 8930:         }
 8931: 
 8932: #
 8933: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8934: #
 8935:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8936:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8937:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8938:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8939:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8940:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8941:                         push(@{$changes{'email_rule'}},$type);
 8942:                     }
 8943:                 }
 8944:             }
 8945:             if (@email_rule > 0) {
 8946:                 foreach my $type (@email_rule) {
 8947:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8948:                         push(@{$changes{'email_rule'}},$type);
 8949:                     }
 8950:                 }
 8951:             }
 8952:         } elsif (@email_rule > 0) {
 8953:             push(@{$changes{'email_rule'}},@email_rule);
 8954:         }
 8955:     }
 8956: #  
 8957: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 8958: # institutional log-in.
 8959: #
 8960:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8961:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8962:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8963:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8964:             $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.').' '.
 8965:                           &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.');
 8966:         }
 8967:     }
 8968:     my @fields = ('lastname','firstname','middlename','generation',
 8969:                   'permanentemail','id');
 8970:     my @shibfields = (@fields,'inststatus');
 8971:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8972: #
 8973: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8974: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8975: # may self-create accounts 
 8976: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8977: # which the user may supply, if institutional data is unavailable.
 8978: #
 8979:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8980:         if (ref($types) eq 'ARRAY') {
 8981:             if (@{$types} > 1) {
 8982:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8983:                 push(@contexts,'statustocreate');
 8984:             } else {
 8985:                 undef($cancreate{'statustocreate'});
 8986:             } 
 8987:             foreach my $type (@{$types}) {
 8988:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8989:                 foreach my $field (@fields) {
 8990:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8991:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8992:                     } else {
 8993:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8994:                     }
 8995:                 }
 8996:             }
 8997:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8998:                 foreach my $type (@{$types}) {
 8999:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 9000:                         foreach my $field (@fields) {
 9001:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 9002:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 9003:                                 push(@{$changes{'selfcreate'}},$type);
 9004:                                 last;
 9005:                             }
 9006:                         }
 9007:                     }
 9008:                 }
 9009:             } else {
 9010:                 foreach my $type (@{$types}) {
 9011:                     push(@{$changes{'selfcreate'}},$type);
 9012:                 }
 9013:             }
 9014:         }
 9015:         foreach my $field (@shibfields) {
 9016:             if ($env{'form.shibenv_'.$field} ne '') {
 9017:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 9018:             }
 9019:         }
 9020:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 9021:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 9022:                 foreach my $field (@shibfields) {
 9023:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 9024:                         push(@{$changes{'cancreate'}},'shibenv');
 9025:                     }
 9026:                 }
 9027:             } else {
 9028:                 foreach my $field (@shibfields) {
 9029:                     if ($env{'form.shibenv_'.$field}) {
 9030:                         push(@{$changes{'cancreate'}},'shibenv');
 9031:                         last;
 9032:                     }
 9033:                 }
 9034:             }
 9035:         }
 9036:     }
 9037:     foreach my $item (@contexts) {
 9038:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 9039:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 9040:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 9041:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 9042:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9043:                             push(@{$changes{'cancreate'}},$item);
 9044:                         }
 9045:                     }
 9046:                 }
 9047:             }
 9048:             if (ref($cancreate{$item}) eq 'ARRAY') {
 9049:                 foreach my $type (@{$cancreate{$item}}) {
 9050:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 9051:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9052:                             push(@{$changes{'cancreate'}},$item);
 9053:                         }
 9054:                     }
 9055:                 }
 9056:             }
 9057:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 9058:             if (ref($cancreate{$item}) eq 'HASH') {
 9059:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 9060:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 9061:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 9062:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 9063:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9064:                                     push(@{$changes{'cancreate'}},$item);
 9065:                                 }
 9066:                             }
 9067:                         }
 9068:                     } elsif ($item eq 'selfcreateprocessing') {
 9069:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 9070:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9071:                                 push(@{$changes{'cancreate'}},$item);
 9072:                             }
 9073:                         }
 9074:                     } else {
 9075:                         if (!$cancreate{$item}{$curr}) {
 9076:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9077:                                 push(@{$changes{'cancreate'}},$item);
 9078:                             }
 9079:                         }
 9080:                     }
 9081:                 }
 9082:                 foreach my $field (keys(%{$cancreate{$item}})) {
 9083:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 9084:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 9085:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 9086:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 9087:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9088:                                         push(@{$changes{'cancreate'}},$item);
 9089:                                     }
 9090:                                 }
 9091:                             } else {
 9092:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9093:                                     push(@{$changes{'cancreate'}},$item);
 9094:                                 }
 9095:                             }
 9096:                         }
 9097:                     } elsif ($item eq 'selfcreateprocessing') {
 9098:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 9099:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9100:                                 push(@{$changes{'cancreate'}},$item);
 9101:                             }
 9102:                         }
 9103:                     } else {
 9104:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 9105:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9106:                                 push(@{$changes{'cancreate'}},$item);
 9107:                             }
 9108:                         }
 9109:                     }
 9110:                 }
 9111:             }
 9112:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 9113:             if (ref($cancreate{$item}) eq 'ARRAY') {
 9114:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 9115:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9116:                         push(@{$changes{'cancreate'}},$item);
 9117:                     }
 9118:                 }
 9119:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 9120:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 9121:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9122:                         push(@{$changes{'cancreate'}},$item);
 9123:                     }
 9124:                 }
 9125:             }
 9126:         } elsif ($item eq 'emailusername') {
 9127:             if (ref($cancreate{$item}) eq 'HASH') {
 9128:                 foreach my $type (keys(%{$cancreate{$item}})) {
 9129:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 9130:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 9131:                             if ($cancreate{$item}{$type}{$field}) {
 9132:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9133:                                     push(@{$changes{'cancreate'}},$item);
 9134:                                 }
 9135:                                 last;
 9136:                             }
 9137:                         }
 9138:                     }
 9139:                 }
 9140:             }
 9141:         }
 9142:     }
 9143: #
 9144: # Populate %save_usercreate hash with updates to self-creation configuration.
 9145: #
 9146:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 9147:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 9148:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 9149:     if (ref($cancreate{'notify'}) eq 'HASH') {
 9150:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 9151:     }
 9152:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
 9153:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
 9154:     }
 9155:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 9156:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 9157:     }
 9158:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
 9159:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
 9160:     }
 9161:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 9162:     $save_usercreate{'emailrule'} = \@email_rule;
 9163: 
 9164:     my %userconfig_hash = (
 9165:             usercreation     => \%save_usercreate,
 9166:             usermodification => \%save_usermodify,
 9167:     );
 9168:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 9169:                                              $dom);
 9170: #
 9171: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 9172: #
 9173:     if ($putresult eq 'ok') {
 9174:         if (keys(%changes) > 0) {
 9175:             $resulttext = &mt('Changes made:').'<ul>';
 9176:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 9177:                 my %lt = &selfcreation_types();
 9178:                 foreach my $type (@{$changes{'cancreate'}}) {
 9179:                     my $chgtext;
 9180:                     if ($type eq 'selfcreate') {
 9181:                         if (@{$cancreate{$type}} == 0) {
 9182:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 9183:                         } else {
 9184:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 9185:                                         '<ul>';
 9186:                             foreach my $case (@{$cancreate{$type}}) {
 9187:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 9188:                             }
 9189:                             $chgtext .= '</ul>';
 9190:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 9191:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 9192:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 9193:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 9194:                                             $chgtext .= '<br />'.
 9195:                                                         '<span class="LC_warning">'.
 9196:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 9197:                                                         '</span>';
 9198:                                         }
 9199:                                     }
 9200:                                 }
 9201:                             }
 9202:                         }
 9203:                     } elsif ($type eq 'shibenv') {
 9204:                         if (keys(%{$cancreate{$type}}) == 0) {
 9205:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
 9206:                         } else {
 9207:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
 9208:                                         '<ul>';
 9209:                             foreach my $field (@shibfields) {
 9210:                                 next if ($cancreate{$type}{$field} eq '');
 9211:                                 if ($field eq 'inststatus') {
 9212:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
 9213:                                 } else {
 9214:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
 9215:                                 }
 9216:                             }
 9217:                             $chgtext .= '</ul>';
 9218:                         }  
 9219:                     } elsif ($type eq 'statustocreate') {
 9220:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 9221:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 9222:                             if (@{$cancreate{'selfcreate'}} > 0) {
 9223:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 9224:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 9225:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 9226:                                         $chgtext .= '<br />'.
 9227:                                                     '<span class="LC_warning">'.
 9228:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 9229:                                                     '</span>';
 9230:                                     }
 9231:                                 } elsif (ref($usertypes) eq 'HASH') {
 9232:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 9233:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 9234:                                     } else {
 9235:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 9236:                                     }
 9237:                                     $chgtext .= '<ul>';
 9238:                                     foreach my $case (@{$cancreate{$type}}) {
 9239:                                         if ($case eq 'default') {
 9240:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 9241:                                         } else {
 9242:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 9243:                                         }
 9244:                                     }
 9245:                                     $chgtext .= '</ul>';
 9246:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 9247:                                         $chgtext .= '<br /><span class="LC_warning">'.
 9248:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 9249:                                                     '</span>';
 9250:                                     }
 9251:                                 }
 9252:                             } else {
 9253:                                 if (@{$cancreate{$type}} == 0) {
 9254:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 9255:                                 } else {
 9256:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 9257:                                 }
 9258:                             }
 9259:                         }
 9260:                     } elsif ($type eq 'selfcreateprocessing') {
 9261:                         my %choices = &Apache::lonlocal::texthash (
 9262:                                                                     automatic => 'Automatic approval',
 9263:                                                                     approval  => 'Queued for approval',
 9264:                                                                   );
 9265:                         if (@statuses > 1) {
 9266:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
 9267:                                         '<ul>';
 9268:                            foreach my $type (@statuses) {
 9269:                                if ($type eq 'default') {
 9270:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 9271:                                } else {
 9272:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 9273:                                }
 9274:                            }
 9275:                            $chgtext .= '</ul>';
 9276:                         } else {
 9277:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
 9278:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
 9279:                         }
 9280:                     } elsif ($type eq 'captcha') {
 9281:                         if ($savecaptcha{$type} eq 'notused') {
 9282:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 9283:                         } else {
 9284:                             my %captchas = &captcha_phrases();
 9285:                             if ($captchas{$savecaptcha{$type}}) {
 9286:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 9287:                             } else {
 9288:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 9289:                             }
 9290:                         }
 9291:                     } elsif ($type eq 'recaptchakeys') {
 9292:                         my ($privkey,$pubkey);
 9293:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 9294:                             $pubkey = $savecaptcha{$type}{'public'};
 9295:                             $privkey = $savecaptcha{$type}{'private'};
 9296:                         }
 9297:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 9298:                         if (!$pubkey) {
 9299:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 9300:                         } else {
 9301:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 9302:                         }
 9303:                         if (!$privkey) {
 9304:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 9305:                         } else {
 9306:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 9307:                         }
 9308:                         $chgtext .= '</ul>';
 9309:                     } elsif ($type eq 'emailusername') {
 9310:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 9311:                             if (ref($types) eq 'ARRAY') {
 9312:                                 foreach my $type (@{$types}) {
 9313:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 9314:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 9315:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
 9316:                                                     '<ul>';
 9317:                                             foreach my $field (@{$infofields}) {
 9318:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 9319:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 9320:                                                 }
 9321:                                             }
 9322:                                             $chgtext .= '</ul>';
 9323:                                         } else {
 9324:                                             $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 />';
 9325:                                         }
 9326:                                     } else {
 9327:                                         $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 />';
 9328:                                     }
 9329:                                 }
 9330:                             }
 9331:                         }
 9332:                     } elsif ($type eq 'notify') {
 9333:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 9334:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 9335:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 9336:                                 if ($cancreate{'notify'}{'approval'}) {
 9337:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 9338:                                 }
 9339:                             }
 9340:                         }
 9341:                     }
 9342:                     if ($chgtext) {
 9343:                         $resulttext .= '<li>'.$chgtext.'</li>';
 9344:                     }
 9345:                 }
 9346:             }
 9347:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 9348:                 my ($emailrules,$emailruleorder) =
 9349:                     &Apache::lonnet::inst_userrules($dom,'email');
 9350:                 my $chgtext = '<ul>';
 9351:                 foreach my $type (@email_rule) {
 9352:                     if (ref($emailrules->{$type}) eq 'HASH') {
 9353:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 9354:                     }
 9355:                 }
 9356:                 $chgtext .= '</ul>';
 9357:                 if (@email_rule > 0) {
 9358:                     $resulttext .= '<li>'.
 9359:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 9360:                                        $chgtext.
 9361:                                    '</li>';
 9362:                 } else {
 9363:                     $resulttext .= '<li>'.
 9364:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 9365:                                    '</li>';
 9366:                 }
 9367:             }
 9368:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 9369:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 9370:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9371:                 foreach my $type (@{$changes{'selfcreate'}}) {
 9372:                     my $typename = $type;
 9373:                     if (ref($usertypes) eq 'HASH') {
 9374:                         if ($usertypes->{$type} ne '') {
 9375:                             $typename = $usertypes->{$type};
 9376:                         }
 9377:                     }
 9378:                     my @modifiable;
 9379:                     $resulttext .= '<li>'.
 9380:                                     &mt('Self-creation of account by users with status: [_1]',
 9381:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 9382:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 9383:                     foreach my $field (@fields) {
 9384:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 9385:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 9386:                         }
 9387:                     }
 9388:                     if (@modifiable > 0) {
 9389:                         $resulttext .= join(', ',@modifiable);
 9390:                     } else {
 9391:                         $resulttext .= &mt('none');
 9392:                     }
 9393:                     $resulttext .= '</li>';
 9394:                 }
 9395:                 $resulttext .= '</ul></li>';
 9396:             }
 9397:             $resulttext .= '</ul>';
 9398:         } else {
 9399:             $resulttext = &mt('No changes made to self-creation settings');
 9400:         }
 9401:     } else {
 9402:         $resulttext = '<span class="LC_error">'.
 9403:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9404:     }
 9405:     if ($warningmsg ne '') {
 9406:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9407:     }
 9408:     return $resulttext;
 9409: }
 9410: 
 9411: sub process_captcha {
 9412:     my ($container,$changes,$newsettings,$current) = @_;
 9413:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 9414:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 9415:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 9416:         $newsettings->{'captcha'} = 'original';
 9417:     }
 9418:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 9419:         if ($container eq 'cancreate') {
 9420:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9421:                 push(@{$changes->{'cancreate'}},'captcha');
 9422:             } elsif (!defined($changes->{'cancreate'})) {
 9423:                 $changes->{'cancreate'} = ['captcha'];
 9424:             }
 9425:         } else {
 9426:             $changes->{'captcha'} = 1;
 9427:         }
 9428:     }
 9429:     my ($newpub,$newpriv,$currpub,$currpriv);
 9430:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 9431:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 9432:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 9433:         $newpub =~ s/[^\w\-]//g;
 9434:         $newpriv =~ s/[^\w\-]//g;
 9435:         $newsettings->{'recaptchakeys'} = {
 9436:                                              public  => $newpub,
 9437:                                              private => $newpriv,
 9438:                                           };
 9439:     }
 9440:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 9441:         $currpub = $current->{'recaptchakeys'}{'public'};
 9442:         $currpriv = $current->{'recaptchakeys'}{'private'};
 9443:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 9444:             $newsettings->{'recaptchakeys'} = {
 9445:                                                  public  => '',
 9446:                                                  private => '',
 9447:                                               }
 9448:         }
 9449:     }
 9450:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 9451:         if ($container eq 'cancreate') {
 9452:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9453:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 9454:             } elsif (!defined($changes->{'cancreate'})) {
 9455:                 $changes->{'cancreate'} = ['recaptchakeys'];
 9456:             }
 9457:         } else {
 9458:             $changes->{'recaptchakeys'} = 1;
 9459:         }
 9460:     }
 9461:     return;
 9462: }
 9463: 
 9464: sub modify_usermodification {
 9465:     my ($dom,%domconfig) = @_;
 9466:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 9467:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9468:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9469:             if ($key eq 'selfcreate') {
 9470:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 9471:             } else {  
 9472:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 9473:             }
 9474:         }
 9475:     }
 9476:     my @contexts = ('author','course');
 9477:     my %context_title = (
 9478:                            author => 'In author context',
 9479:                            course => 'In course context',
 9480:                         );
 9481:     my @fields = ('lastname','firstname','middlename','generation',
 9482:                   'permanentemail','id');
 9483:     my %roles = (
 9484:                   author => ['ca','aa'],
 9485:                   course => ['st','ep','ta','in','cr'],
 9486:                 );
 9487:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9488:     foreach my $context (@contexts) {
 9489:         foreach my $role (@{$roles{$context}}) {
 9490:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 9491:             foreach my $item (@fields) {
 9492:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 9493:                     $modifyhash{$context}{$role}{$item} = 1;
 9494:                 } else {
 9495:                     $modifyhash{$context}{$role}{$item} = 0;
 9496:                 }
 9497:             }
 9498:         }
 9499:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 9500:             foreach my $role (@{$roles{$context}}) {
 9501:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 9502:                     foreach my $field (@fields) {
 9503:                         if ($modifyhash{$context}{$role}{$field} ne 
 9504:                                 $curr_usermodification{$context}{$role}{$field}) {
 9505:                             push(@{$changes{$context}},$role);
 9506:                             last;
 9507:                         }
 9508:                     }
 9509:                 }
 9510:             }
 9511:         } else {
 9512:             foreach my $context (@contexts) {
 9513:                 foreach my $role (@{$roles{$context}}) {
 9514:                     push(@{$changes{$context}},$role);
 9515:                 }
 9516:             }
 9517:         }
 9518:     }
 9519:     my %usermodification_hash =  (
 9520:                                    usermodification => \%modifyhash,
 9521:                                  );
 9522:     my $putresult = &Apache::lonnet::put_dom('configuration',
 9523:                                              \%usermodification_hash,$dom);
 9524:     if ($putresult eq 'ok') {
 9525:         if (keys(%changes) > 0) {
 9526:             $resulttext = &mt('Changes made: ').'<ul>';
 9527:             foreach my $context (@contexts) {
 9528:                 if (ref($changes{$context}) eq 'ARRAY') {
 9529:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 9530:                     if (ref($changes{$context}) eq 'ARRAY') {
 9531:                         foreach my $role (@{$changes{$context}}) {
 9532:                             my $rolename;
 9533:                             if ($role eq 'cr') {
 9534:                                 $rolename = &mt('Custom');
 9535:                             } else {
 9536:                                 $rolename = &Apache::lonnet::plaintext($role);
 9537:                             }
 9538:                             my @modifiable;
 9539:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 9540:                             foreach my $field (@fields) {
 9541:                                 if ($modifyhash{$context}{$role}{$field}) {
 9542:                                     push(@modifiable,$fieldtitles{$field});
 9543:                                 }
 9544:                             }
 9545:                             if (@modifiable > 0) {
 9546:                                 $resulttext .= join(', ',@modifiable);
 9547:                             } else {
 9548:                                 $resulttext .= &mt('none'); 
 9549:                             }
 9550:                             $resulttext .= '</li>';
 9551:                         }
 9552:                         $resulttext .= '</ul></li>';
 9553:                     }
 9554:                 }
 9555:             }
 9556:             $resulttext .= '</ul>';
 9557:         } else {
 9558:             $resulttext = &mt('No changes made to user modification settings');
 9559:         }
 9560:     } else {
 9561:         $resulttext = '<span class="LC_error">'.
 9562:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9563:     }
 9564:     return $resulttext;
 9565: }
 9566: 
 9567: sub modify_defaults {
 9568:     my ($dom,$lastactref,%domconfig) = @_;
 9569:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 9570:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9571:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 9572:     my @authtypes = ('internal','krb4','krb5','localauth');
 9573:     foreach my $item (@items) {
 9574:         $newvalues{$item} = $env{'form.'.$item};
 9575:         if ($item eq 'auth_def') {
 9576:             if ($newvalues{$item} ne '') {
 9577:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 9578:                     push(@errors,$item);
 9579:                 }
 9580:             }
 9581:         } elsif ($item eq 'lang_def') {
 9582:             if ($newvalues{$item} ne '') {
 9583:                 if ($newvalues{$item} =~ /^(\w+)/) {
 9584:                     my $langcode = $1;
 9585:                     if ($langcode ne 'x_chef') {
 9586:                         if (code2language($langcode) eq '') {
 9587:                             push(@errors,$item);
 9588:                         }
 9589:                     }
 9590:                 } else {
 9591:                     push(@errors,$item);
 9592:                 }
 9593:             }
 9594:         } elsif ($item eq 'timezone_def') {
 9595:             if ($newvalues{$item} ne '') {
 9596:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 9597:                     push(@errors,$item);   
 9598:                 }
 9599:             }
 9600:         } elsif ($item eq 'datelocale_def') {
 9601:             if ($newvalues{$item} ne '') {
 9602:                 my @datelocale_ids = DateTime::Locale->ids();
 9603:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 9604:                     push(@errors,$item);
 9605:                 }
 9606:             }
 9607:         } elsif ($item eq 'portal_def') {
 9608:             if ($newvalues{$item} ne '') {
 9609:                 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])\/?$/) {
 9610:                     push(@errors,$item);
 9611:                 }
 9612:             }
 9613:         }
 9614:         if (grep(/^\Q$item\E$/,@errors)) {
 9615:             $newvalues{$item} = $domdefaults{$item};
 9616:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 9617:             $changes{$item} = 1;
 9618:         }
 9619:         $domdefaults{$item} = $newvalues{$item};
 9620:     }
 9621:     my %defaults_hash = (
 9622:                          defaults => \%newvalues,
 9623:                         );
 9624:     my $title = &defaults_titles();
 9625: 
 9626:     my $currinststatus;
 9627:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9628:         $currinststatus = $domconfig{'inststatus'};
 9629:     } else {
 9630:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9631:         $currinststatus = {
 9632:                              inststatustypes => $usertypes,
 9633:                              inststatusorder => $types,
 9634:                              inststatusguest => [],
 9635:                           };
 9636:     }
 9637:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
 9638:     my @allpos;
 9639:     my %guests;
 9640:     my %alltypes;
 9641:     my ($currtitles,$currguests,$currorder);
 9642:     if (ref($currinststatus) eq 'HASH') {
 9643:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
 9644:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
 9645:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
 9646:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
 9647:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
 9648:                     }
 9649:                 }
 9650:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
 9651:                     my $position = $env{'form.inststatus_pos_'.$type};
 9652:                     $position =~ s/\D+//g;
 9653:                     $allpos[$position] = $type;
 9654:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
 9655:                     $alltypes{$type} =~ s/`//g;
 9656:                     if ($env{'form.inststatus_guest_'.$type}) {
 9657:                         $guests{$type} = 1;
 9658:                     }
 9659:                 }
 9660:             }
 9661:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
 9662:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
 9663:             }
 9664:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
 9665:             $currtitles =~ s/,$//;
 9666:         }
 9667:     }
 9668:     if ($env{'form.addinststatus'}) {
 9669:         my $newtype = $env{'form.addinststatus'};
 9670:         $newtype =~ s/\W//g;
 9671:         unless (exists($alltypes{$newtype})) {
 9672:             if ($env{'form.addinststatus_guest'}) {
 9673:                 $guests{$newtype} = 1;
 9674:             }
 9675:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
 9676:             $alltypes{$newtype} =~ s/`//g; 
 9677:             my $position = $env{'form.addinststatus_pos'};
 9678:             $position =~ s/\D+//g;
 9679:             if ($position ne '') {
 9680:                 $allpos[$position] = $newtype;
 9681:             }
 9682:         }
 9683:     }
 9684:     my (@orderedstatus,@orderedguests);
 9685:     foreach my $type (@allpos) {
 9686:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
 9687:             push(@orderedstatus,$type);
 9688:             if ($guests{$type}) {
 9689:                 push(@orderedguests,$type);
 9690:             }
 9691:         }
 9692:     }
 9693:     foreach my $type (keys(%alltypes)) {
 9694:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
 9695:             delete($alltypes{$type});
 9696:         }
 9697:     }
 9698:     $defaults_hash{'inststatus'} = {
 9699:                                      inststatustypes => \%alltypes,
 9700:                                      inststatusorder => \@orderedstatus,
 9701:                                      inststatusguest => \@orderedguests,
 9702:                                    };
 9703:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
 9704:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 9705:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
 9706:         }
 9707:     }
 9708:     if ($currorder ne join(',',@orderedstatus)) {
 9709:         $changes{'inststatus'}{'inststatusorder'} = 1;
 9710:     }
 9711:     if ($currguests ne join(',',@orderedguests)) {
 9712:         $changes{'inststatus'}{'inststatusguest'} = 1;
 9713:     }
 9714:     my $newtitles;
 9715:     foreach my $item (@orderedstatus) {
 9716:         $newtitles .= $alltypes{$item}.',';
 9717:     }
 9718:     $newtitles =~ s/,$//;
 9719:     if ($currtitles ne $newtitles) {
 9720:         $changes{'inststatus'}{'inststatustypes'} = 1;
 9721:     }
 9722:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 9723:                                              $dom);
 9724:     if ($putresult eq 'ok') {
 9725:         if (keys(%changes) > 0) {
 9726:             $resulttext = &mt('Changes made:').'<ul>';
 9727:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 9728:             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";
 9729:             foreach my $item (sort(keys(%changes))) {
 9730:                 if ($item eq 'inststatus') {
 9731:                     if (ref($changes{'inststatus'}) eq 'HASH') {
 9732:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
 9733:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
 9734:                             foreach my $type (@orderedstatus) { 
 9735:                                 $resulttext .= $alltypes{$type}.', ';
 9736:                             }
 9737:                             $resulttext =~ s/, $//;
 9738:                             $resulttext .= '</li>';
 9739:                         }
 9740:                         if ($changes{'inststatus'}{'inststatusguest'}) {
 9741:                             $resulttext .= '<li>'; 
 9742:                             if (@orderedguests) {
 9743:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
 9744:                                 foreach my $type (@orderedguests) {
 9745:                                     $resulttext .= $alltypes{$type}.', ';
 9746:                                 }
 9747:                                 $resulttext =~ s/, $//;
 9748:                             } else {
 9749:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
 9750:                             }
 9751:                             $resulttext .= '</li>';
 9752:                         }
 9753:                     }
 9754:                 } else {
 9755:                     my $value = $env{'form.'.$item};
 9756:                     if ($value eq '') {
 9757:                         $value = &mt('none');
 9758:                     } elsif ($item eq 'auth_def') {
 9759:                         my %authnames = &authtype_names();
 9760:                         my %shortauth = (
 9761:                                           internal   => 'int',
 9762:                                           krb4       => 'krb4',
 9763:                                           krb5       => 'krb5',
 9764:                                           localauth  => 'loc',
 9765:                         );
 9766:                         $value = $authnames{$shortauth{$value}};
 9767:                     }
 9768:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 9769:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
 9770:                 }
 9771:             }
 9772:             $resulttext .= '</ul>';
 9773:             $mailmsgtext .= "\n";
 9774:             my $cachetime = 24*60*60;
 9775:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9776:             if (ref($lastactref) eq 'HASH') {
 9777:                 $lastactref->{'domdefaults'} = 1;
 9778:             }
 9779:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 9780:                 my $notify = 1;
 9781:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 9782:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 9783:                         $notify = 0;
 9784:                     }
 9785:                 }
 9786:                 if ($notify) {
 9787:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 9788:                                                "LON-CAPA Domain Settings Change - $dom",
 9789:                                                $mailmsgtext);
 9790:                 }
 9791:             }
 9792:         } else {
 9793:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 9794:         }
 9795:     } else {
 9796:         $resulttext = '<span class="LC_error">'.
 9797:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9798:     }
 9799:     if (@errors > 0) {
 9800:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 9801:         foreach my $item (@errors) {
 9802:             $resulttext .= ' "'.$title->{$item}.'",';
 9803:         }
 9804:         $resulttext =~ s/,$//;
 9805:     }
 9806:     return $resulttext;
 9807: }
 9808: 
 9809: sub modify_scantron {
 9810:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9811:     my ($resulttext,%confhash,%changes,$errors);
 9812:     my $custom = 'custom.tab';
 9813:     my $default = 'default.tab';
 9814:     my $servadm = $r->dir_config('lonAdmEMail');
 9815:     my ($configuserok,$author_ok,$switchserver) = 
 9816:         &config_check($dom,$confname,$servadm);
 9817:     if ($env{'form.scantronformat.filename'} ne '') {
 9818:         my $error;
 9819:         if ($configuserok eq 'ok') {
 9820:             if ($switchserver) {
 9821:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 9822:             } else {
 9823:                 if ($author_ok eq 'ok') {
 9824:                     my ($result,$scantronurl) =
 9825:                         &publishlogo($r,'upload','scantronformat',$dom,
 9826:                                      $confname,'scantron','','',$custom);
 9827:                     if ($result eq 'ok') {
 9828:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 9829:                         $changes{'scantronformat'} = 1;
 9830:                     } else {
 9831:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 9832:                     }
 9833:                 } else {
 9834:                     $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);
 9835:                 }
 9836:             }
 9837:         } else {
 9838:             $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);
 9839:         }
 9840:         if ($error) {
 9841:             &Apache::lonnet::logthis($error);
 9842:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9843:         }
 9844:     }
 9845:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 9846:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 9847:             if ($env{'form.scantronformat_del'}) {
 9848:                 $confhash{'scantron'}{'scantronformat'} = '';
 9849:                 $changes{'scantronformat'} = 1;
 9850:             }
 9851:         }
 9852:     }
 9853:     if (keys(%confhash) > 0) {
 9854:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 9855:                                                  $dom);
 9856:         if ($putresult eq 'ok') {
 9857:             if (keys(%changes) > 0) {
 9858:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 9859:                     $resulttext = &mt('Changes made:').'<ul>';
 9860:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 9861:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 9862:                     } else {
 9863:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 9864:                     }
 9865:                     $resulttext .= '</ul>';
 9866:                 } else {
 9867:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 9868:                 }
 9869:                 $resulttext .= '</ul>';
 9870:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9871:                 if (ref($lastactref) eq 'HASH') {
 9872:                     $lastactref->{'domainconfig'} = 1;
 9873:                 }
 9874:             } else {
 9875:                 $resulttext = &mt('No changes made to bubblesheet format file');
 9876:             }
 9877:         } else {
 9878:             $resulttext = '<span class="LC_error">'.
 9879:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 9880:         }
 9881:     } else {
 9882:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 9883:     }
 9884:     if ($errors) {
 9885:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9886:                        $errors.'</ul>';
 9887:     }
 9888:     return $resulttext;
 9889: }
 9890: 
 9891: sub modify_coursecategories {
 9892:     my ($dom,$lastactref,%domconfig) = @_;
 9893:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 9894:         $cathash);
 9895:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 9896:     my @catitems = ('unauth','auth');
 9897:     my @cattypes = ('std','domonly','codesrch','none');
 9898:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9899:         $cathash = $domconfig{'coursecategories'}{'cats'};
 9900:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 9901:             $changes{'togglecats'} = 1;
 9902:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 9903:         }
 9904:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 9905:             $changes{'categorize'} = 1;
 9906:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 9907:         }
 9908:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 9909:             $changes{'togglecatscomm'} = 1;
 9910:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 9911:         }
 9912:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 9913:             $changes{'categorizecomm'} = 1;
 9914:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 9915:         }
 9916:         foreach my $item (@catitems) {
 9917:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9918:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
 9919:                     $changes{$item} = 1;
 9920:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9921:                 }
 9922:             }
 9923:         }
 9924:     } else {
 9925:         $changes{'togglecats'} = 1;
 9926:         $changes{'categorize'} = 1;
 9927:         $changes{'togglecatscomm'} = 1;
 9928:         $changes{'categorizecomm'} = 1;
 9929:         $domconfig{'coursecategories'} = {
 9930:                                              togglecats => $env{'form.togglecats'},
 9931:                                              categorize => $env{'form.categorize'},
 9932:                                              togglecatscomm => $env{'form.togglecatscomm'},
 9933:                                              categorizecomm => $env{'form.categorizecomm'},
 9934:                                          };
 9935:         foreach my $item (@catitems) {
 9936:             if ($env{'form.coursecat_'.$item} ne 'std') {
 9937:                 $changes{$item} = 1;
 9938:             }
 9939:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9940:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9941:             }
 9942:         }
 9943:     }
 9944:     if (ref($cathash) eq 'HASH') {
 9945:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 9946:             push (@deletecategory,'instcode::0');
 9947:         }
 9948:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 9949:             push(@deletecategory,'communities::0');
 9950:         }
 9951:     }
 9952:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 9953:     if (ref($cathash) eq 'HASH') {
 9954:         if (@deletecategory > 0) {
 9955:             #FIXME Need to remove category from all courses using a deleted category 
 9956:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 9957:             foreach my $item (@deletecategory) {
 9958:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 9959:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 9960:                     $deletions{$item} = 1;
 9961:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 9962:                 }
 9963:             }
 9964:         }
 9965:         foreach my $item (keys(%{$cathash})) {
 9966:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9967:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 9968:                 $reorderings{$item} = 1;
 9969:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 9970:             }
 9971:             if ($env{'form.addcategory_name_'.$item} ne '') {
 9972:                 my $newcat = $env{'form.addcategory_name_'.$item};
 9973:                 my $newdepth = $depth+1;
 9974:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9975:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 9976:                 $adds{$newitem} = 1; 
 9977:             }
 9978:             if ($env{'form.subcat_'.$item} ne '') {
 9979:                 my $newcat = $env{'form.subcat_'.$item};
 9980:                 my $newdepth = $depth+1;
 9981:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9982:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 9983:                 $adds{$newitem} = 1;
 9984:             }
 9985:         }
 9986:     }
 9987:     if ($env{'form.instcode'} eq '1') {
 9988:         if (ref($cathash) eq 'HASH') {
 9989:             my $newitem = 'instcode::0';
 9990:             if ($cathash->{$newitem} eq '') {  
 9991:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9992:                 $adds{$newitem} = 1;
 9993:             }
 9994:         } else {
 9995:             my $newitem = 'instcode::0';
 9996:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9997:             $adds{$newitem} = 1;
 9998:         }
 9999:     }
10000:     if ($env{'form.communities'} eq '1') {
10001:         if (ref($cathash) eq 'HASH') {
10002:             my $newitem = 'communities::0';
10003:             if ($cathash->{$newitem} eq '') {
10004:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
10005:                 $adds{$newitem} = 1;
10006:             }
10007:         } else {
10008:             my $newitem = 'communities::0';
10009:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
10010:             $adds{$newitem} = 1;
10011:         }
10012:     }
10013:     if ($env{'form.addcategory_name'} ne '') {
10014:         if (($env{'form.addcategory_name'} ne 'instcode') &&
10015:             ($env{'form.addcategory_name'} ne 'communities')) {
10016:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
10017:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
10018:             $adds{$newitem} = 1;
10019:         }
10020:     }
10021:     my $putresult;
10022:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10023:         if (keys(%deletions) > 0) {
10024:             foreach my $key (keys(%deletions)) {
10025:                 if ($predelallitems{$key} ne '') {
10026:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
10027:                 }
10028:             }
10029:         }
10030:         my (@chkcats,@chktrails,%chkallitems);
10031:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
10032:         if (ref($chkcats[0]) eq 'ARRAY') {
10033:             my $depth = 0;
10034:             my $chg = 0;
10035:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
10036:                 my $name = $chkcats[0][$i];
10037:                 my $item;
10038:                 if ($name eq '') {
10039:                     $chg ++;
10040:                 } else {
10041:                     $item = &escape($name).'::0';
10042:                     if ($chg) {
10043:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
10044:                     }
10045:                     $depth ++; 
10046:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
10047:                     $depth --;
10048:                 }
10049:             }
10050:         }
10051:     }
10052:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10053:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
10054:         if ($putresult eq 'ok') {
10055:             my %title = (
10056:                          togglecats     => 'Show/Hide a course in catalog',
10057:                          categorize     => 'Assign a category to a course',
10058:                          togglecatscomm => 'Show/Hide a community in catalog',
10059:                          categorizecomm => 'Assign a category to a community',
10060:                         );
10061:             my %level = (
10062:                          dom  => 'set in Domain ("Modify Course/Community")',
10063:                          crs  => 'set in Course ("Course Configuration")',
10064:                          comm => 'set in Community ("Community Configuration")',
10065:                          none     => 'No catalog',
10066:                          std      => 'Standard catalog',
10067:                          domonly  => 'Domain-only catalog',
10068:                          codesrch => 'Code search form',
10069:                         );
10070:             $resulttext = &mt('Changes made:').'<ul>';
10071:             if ($changes{'togglecats'}) {
10072:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
10073:             }
10074:             if ($changes{'categorize'}) {
10075:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
10076:             }
10077:             if ($changes{'togglecatscomm'}) {
10078:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
10079:             }
10080:             if ($changes{'categorizecomm'}) {
10081:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
10082:             }
10083:             if ($changes{'unauth'}) {
10084:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
10085:             }
10086:             if ($changes{'auth'}) {
10087:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
10088:             }
10089:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10090:                 my $cathash;
10091:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
10092:                     $cathash = $domconfig{'coursecategories'}{'cats'};
10093:                 } else {
10094:                     $cathash = {};
10095:                 } 
10096:                 my (@cats,@trails,%allitems);
10097:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
10098:                 if (keys(%deletions) > 0) {
10099:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
10100:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
10101:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
10102:                     }
10103:                     $resulttext .= '</ul></li>';
10104:                 }
10105:                 if (keys(%reorderings) > 0) {
10106:                     my %sort_by_trail;
10107:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
10108:                     foreach my $key (keys(%reorderings)) {
10109:                         if ($allitems{$key} ne '') {
10110:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
10111:                         }
10112:                     }
10113:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
10114:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
10115:                     }
10116:                     $resulttext .= '</ul></li>';
10117:                 }
10118:                 if (keys(%adds) > 0) {
10119:                     my %sort_by_trail;
10120:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
10121:                     foreach my $key (keys(%adds)) {
10122:                         if ($allitems{$key} ne '') {
10123:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
10124:                         }
10125:                     }
10126:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
10127:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
10128:                     }
10129:                     $resulttext .= '</ul></li>';
10130:                 }
10131:             }
10132:             $resulttext .= '</ul>';
10133:             if ($changes{'unauth'} || $changes{'auth'}) {
10134:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
10135:                 if ($changes{'auth'}) {
10136:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
10137:                 }
10138:                 if ($changes{'unauth'}) {
10139:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
10140:                 }
10141:                 my $cachetime = 24*60*60;
10142:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10143:                 if (ref($lastactref) eq 'HASH') {
10144:                     $lastactref->{'domdefaults'} = 1;
10145:                 }
10146:             }
10147:         } else {
10148:             $resulttext = '<span class="LC_error">'.
10149:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10150:         }
10151:     } else {
10152:         $resulttext = &mt('No changes made to course and community categories');
10153:     }
10154:     return $resulttext;
10155: }
10156: 
10157: sub modify_serverstatuses {
10158:     my ($dom,%domconfig) = @_;
10159:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
10160:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
10161:         %currserverstatus = %{$domconfig{'serverstatuses'}};
10162:     }
10163:     my @pages = &serverstatus_pages();
10164:     foreach my $type (@pages) {
10165:         $newserverstatus{$type}{'namedusers'} = '';
10166:         $newserverstatus{$type}{'machines'} = '';
10167:         if (defined($env{'form.'.$type.'_namedusers'})) {
10168:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
10169:             my @okusers;
10170:             foreach my $user (@users) {
10171:                 my ($uname,$udom) = split(/:/,$user);
10172:                 if (($udom =~ /^$match_domain$/) &&   
10173:                     (&Apache::lonnet::domain($udom)) &&
10174:                     ($uname =~ /^$match_username$/)) {
10175:                     if (!grep(/^\Q$user\E/,@okusers)) {
10176:                         push(@okusers,$user);
10177:                     }
10178:                 }
10179:             }
10180:             if (@okusers > 0) {
10181:                  @okusers = sort(@okusers);
10182:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
10183:             }
10184:         }
10185:         if (defined($env{'form.'.$type.'_machines'})) {
10186:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
10187:             my @okmachines;
10188:             foreach my $ip (@machines) {
10189:                 my @parts = split(/\./,$ip);
10190:                 next if (@parts < 4);
10191:                 my $badip = 0;
10192:                 for (my $i=0; $i<4; $i++) {
10193:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
10194:                         $badip = 1;
10195:                         last;
10196:                     }
10197:                 }
10198:                 if (!$badip) {
10199:                     push(@okmachines,$ip);     
10200:                 }
10201:             }
10202:             @okmachines = sort(@okmachines);
10203:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
10204:         }
10205:     }
10206:     my %serverstatushash =  (
10207:                                 serverstatuses => \%newserverstatus,
10208:                             );
10209:     foreach my $type (@pages) {
10210:         foreach my $setting ('namedusers','machines') {
10211:             my (@current,@new);
10212:             if (ref($currserverstatus{$type}) eq 'HASH') {
10213:                 if ($currserverstatus{$type}{$setting} ne '') { 
10214:                     @current = split(/,/,$currserverstatus{$type}{$setting});
10215:                 }
10216:             }
10217:             if ($newserverstatus{$type}{$setting} ne '') {
10218:                 @new = split(/,/,$newserverstatus{$type}{$setting});
10219:             }
10220:             if (@current > 0) {
10221:                 if (@new > 0) {
10222:                     foreach my $item (@current) {
10223:                         if (!grep(/^\Q$item\E$/,@new)) {
10224:                             $changes{$type}{$setting} = 1;
10225:                             last;
10226:                         }
10227:                     }
10228:                     foreach my $item (@new) {
10229:                         if (!grep(/^\Q$item\E$/,@current)) {
10230:                             $changes{$type}{$setting} = 1;
10231:                             last;
10232:                         }
10233:                     }
10234:                 } else {
10235:                     $changes{$type}{$setting} = 1;
10236:                 }
10237:             } elsif (@new > 0) {
10238:                 $changes{$type}{$setting} = 1;
10239:             }
10240:         }
10241:     }
10242:     if (keys(%changes) > 0) {
10243:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
10244:         my $putresult = &Apache::lonnet::put_dom('configuration',
10245:                                                  \%serverstatushash,$dom);
10246:         if ($putresult eq 'ok') {
10247:             $resulttext .= &mt('Changes made:').'<ul>';
10248:             foreach my $type (@pages) {
10249:                 if (ref($changes{$type}) eq 'HASH') {
10250:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
10251:                     if ($changes{$type}{'namedusers'}) {
10252:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
10253:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
10254:                         } else {
10255:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
10256:                         }
10257:                     }
10258:                     if ($changes{$type}{'machines'}) {
10259:                         if ($newserverstatus{$type}{'machines'} eq '') {
10260:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
10261:                         } else {
10262:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
10263:                         }
10264: 
10265:                     }
10266:                     $resulttext .= '</ul></li>';
10267:                 }
10268:             }
10269:             $resulttext .= '</ul>';
10270:         } else {
10271:             $resulttext = '<span class="LC_error">'.
10272:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
10273: 
10274:         }
10275:     } else {
10276:         $resulttext = &mt('No changes made to access to server status pages');
10277:     }
10278:     return $resulttext;
10279: }
10280: 
10281: sub modify_helpsettings {
10282:     my ($r,$dom,$confname,%domconfig) = @_;
10283:     my ($resulttext,$errors,%changes,%helphash);
10284:     my %defaultchecked = ('submitbugs' => 'on');
10285:     my @offon = ('off','on');
10286:     my @toggles = ('submitbugs');
10287:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
10288:         foreach my $item (@toggles) {
10289:             if ($defaultchecked{$item} eq 'on') { 
10290:                 if ($domconfig{'helpsettings'}{$item} eq '') {
10291:                     if ($env{'form.'.$item} eq '0') {
10292:                         $changes{$item} = 1;
10293:                     }
10294:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
10295:                     $changes{$item} = 1;
10296:                 }
10297:             } elsif ($defaultchecked{$item} eq 'off') {
10298:                 if ($domconfig{'helpsettings'}{$item} eq '') {
10299:                     if ($env{'form.'.$item} eq '1') {
10300:                         $changes{$item} = 1;
10301:                     }
10302:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
10303:                     $changes{$item} = 1;
10304:                 }
10305:             }
10306:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
10307:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
10308:             }
10309:         }
10310:     }
10311:     my $putresult;
10312:     if (keys(%changes) > 0) {
10313:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
10314:         if ($putresult eq 'ok') {
10315:             $resulttext = &mt('Changes made:').'<ul>';
10316:             foreach my $item (sort(keys(%changes))) {
10317:                 if ($item eq 'submitbugs') {
10318:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
10319:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
10320:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
10321:                 }
10322:             }
10323:             $resulttext .= '</ul>';
10324:         } else {
10325:             $resulttext = &mt('No changes made to help settings');
10326:             $errors .= '<li><span class="LC_error">'.
10327:                        &mt('An error occurred storing the settings: [_1]',
10328:                            $putresult).'</span></li>';
10329:         }
10330:     }
10331:     if ($errors) {
10332:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
10333:                        $errors.'</ul>';
10334:     }
10335:     return $resulttext;
10336: }
10337: 
10338: sub modify_coursedefaults {
10339:     my ($dom,$lastactref,%domconfig) = @_;
10340:     my ($resulttext,$errors,%changes,%defaultshash);
10341:     my %defaultchecked = (
10342:                            'canuse_pdfforms' => 'off',
10343:                            'uselcmath'       => 'on',
10344:                            'usejsme'         => 'on'
10345:                          );
10346:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
10347:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
10348:                    'uploadquota_community','uploadquota_textbook');
10349:     my @types = ('official','unofficial','community','textbook');
10350:     my %staticdefaults = (
10351:                            anonsurvey_threshold => 10,
10352:                            uploadquota          => 500,
10353:                            postsubmit           => 60,
10354:                          );
10355: 
10356:     $defaultshash{'coursedefaults'} = {};
10357: 
10358:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
10359:         if ($domconfig{'coursedefaults'} eq '') {
10360:             $domconfig{'coursedefaults'} = {};
10361:         }
10362:     }
10363: 
10364:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
10365:         foreach my $item (@toggles) {
10366:             if ($defaultchecked{$item} eq 'on') {
10367:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
10368:                     ($env{'form.'.$item} eq '0')) {
10369:                     $changes{$item} = 1;
10370:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10371:                     $changes{$item} = 1;
10372:                 }
10373:             } elsif ($defaultchecked{$item} eq 'off') {
10374:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
10375:                     ($env{'form.'.$item} eq '1')) {
10376:                     $changes{$item} = 1;
10377:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10378:                     $changes{$item} = 1;
10379:                 }
10380:             }
10381:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
10382:         }
10383:         foreach my $item (@numbers) {
10384:             my ($currdef,$newdef);
10385:             $newdef = $env{'form.'.$item};
10386:             if ($item eq 'anonsurvey_threshold') {
10387:                 $currdef = $domconfig{'coursedefaults'}{$item};
10388:                 $newdef =~ s/\D//g;
10389:                 if ($newdef eq '' || $newdef < 1) {
10390:                     $newdef = 1;
10391:                 }
10392:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
10393:             } else {
10394:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
10395:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10396:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
10397:                 }
10398:                 $newdef =~ s/[^\w.\-]//g;
10399:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
10400:             }
10401:             if ($currdef ne $newdef) {
10402:                 my $staticdef;
10403:                 if ($item eq 'anonsurvey_threshold') {
10404:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
10405:                         $changes{$item} = 1;
10406:                     }
10407:                 } else {
10408:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
10409:                         $changes{'uploadquota'} = 1;
10410:                     }
10411:                 }
10412:             }
10413:         }
10414:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
10415:         my @currclonecode;
10416:         if (ref($currclone) eq 'HASH') {
10417:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
10418:                 @currclonecode = @{$currclone->{'instcode'}};
10419:             }
10420:         }
10421:         my $newclone;
10422:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) { 
10423:             $newclone = $env{'form.canclone'};
10424:         }
10425:         if ($newclone eq 'instcode') {
10426:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
10427:             my (%codedefaults,@code_order,@clonecode);
10428:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
10429:                                                     \@code_order);
10430:             foreach my $item (@code_order) {
10431:                 if (grep(/^\Q$item\E$/,@newcodes)) {
10432:                     push(@clonecode,$item);
10433:                 }
10434:             }
10435:             if (@clonecode) {
10436:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
10437:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
10438:                 if (@diffs) {
10439:                     $changes{'canclone'} = 1;
10440:                 }
10441:             } else {
10442:                 $newclone eq '';
10443:             }
10444:         } elsif ($newclone ne '') {
10445:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone; 
10446:         } 
10447:         if ($newclone ne $currclone) {
10448:             $changes{'canclone'} = 1;
10449:         }
10450:         my %credits;
10451:         foreach my $type (@types) {
10452:             unless ($type eq 'community') {
10453:                 $credits{$type} = $env{'form.'.$type.'_credits'};
10454:                 $credits{$type} =~ s/[^\d.]+//g;
10455:             }
10456:         }
10457:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
10458:             ($env{'form.coursecredits'} eq '1')) {
10459:             $changes{'coursecredits'} = 1;
10460:             foreach my $type (keys(%credits)) {
10461:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
10462:             }
10463:         } else {
10464:             if ($env{'form.coursecredits'} eq '1') { 
10465:                 foreach my $type (@types) {
10466:                     unless ($type eq 'community') {
10467:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {        
10468:                             $changes{'coursecredits'} = 1;
10469:                         }
10470:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
10471:                     }
10472:                 }
10473:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10474:                 foreach my $type (@types) {
10475:                     unless ($type eq 'community') {
10476:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
10477:                             $changes{'coursecredits'} = 1;
10478:                             last;
10479:                         }
10480:                     }
10481:                 }
10482:             }
10483:         }
10484:         if ($env{'form.postsubmit'} eq '1') {
10485:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
10486:             my %currtimeout;
10487:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
10488:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
10489:                     $changes{'postsubmit'} = 1;
10490:                 }
10491:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
10492:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
10493:                 }
10494:             } else {
10495:                 $changes{'postsubmit'} = 1;
10496:             }
10497:             foreach my $type (@types) {
10498:                 my $timeout = $env{'form.'.$type.'_timeout'};
10499:                 $timeout =~ s/\D//g;
10500:                 if ($timeout == $staticdefaults{'postsubmit'}) {
10501:                     $timeout = '';
10502:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
10503:                     $timeout = '0';
10504:                 }
10505:                 unless ($timeout eq '') {
10506:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
10507:                 }
10508:                 if (exists($currtimeout{$type})) {
10509:                     if ($timeout ne $currtimeout{$type}) {
10510:                         $changes{'postsubmit'} = 1; 
10511:                     }
10512:                 } elsif ($timeout ne '') {
10513:                     $changes{'postsubmit'} = 1;
10514:                 }
10515:             }
10516:         } else {
10517:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
10518:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
10519:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
10520:                     $changes{'postsubmit'} = 1;
10521:                 }
10522:             } else {
10523:                 $changes{'postsubmit'} = 1;
10524:             }
10525:         }
10526:     }
10527:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10528:                                              $dom);
10529:     if ($putresult eq 'ok') {
10530:         if (keys(%changes) > 0) {
10531:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10532:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
10533:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
10534:                 ($changes{'canclone'})) {
10535:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme') { 
10536:                     if ($changes{$item}) {
10537:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
10538:                     }
10539:                 } 
10540:                 if ($changes{'coursecredits'}) {
10541:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10542:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
10543:                             $domdefaults{$type.'credits'} =
10544:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
10545:                         }
10546:                     }
10547:                 }
10548:                 if ($changes{'postsubmit'}) {
10549:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
10550:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
10551:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
10552:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
10553:                                 $domdefaults{$type.'postsubtimeout'} =
10554:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
10555:                             }
10556:                         }
10557:                     }
10558:                 }
10559:                 if ($changes{'uploadquota'}) {
10560:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10561:                         foreach my $type (@types) {
10562:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
10563:                         }
10564:                     }
10565:                 }
10566:                 if ($changes{'canclone'}) {
10567:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
10568:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
10569:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
10570:                             if (@clonecodes) {
10571:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
10572:                             }
10573:                         }
10574:                     } else {
10575:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
10576:                     }
10577:                 }
10578:                 my $cachetime = 24*60*60;
10579:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10580:                 if (ref($lastactref) eq 'HASH') {
10581:                     $lastactref->{'domdefaults'} = 1;
10582:                 }
10583:             }
10584:             $resulttext = &mt('Changes made:').'<ul>';
10585:             foreach my $item (sort(keys(%changes))) {
10586:                 if ($item eq 'canuse_pdfforms') {
10587:                     if ($env{'form.'.$item} eq '1') {
10588:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
10589:                     } else {
10590:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
10591:                     }
10592:                 } elsif ($item eq 'uselcmath') {
10593:                     if ($env{'form.'.$item} eq '1') {
10594:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
10595:                     } else {
10596:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
10597:                     }
10598:                 } elsif ($item eq 'usejsme') {
10599:                     if ($env{'form.'.$item} eq '1') {
10600:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
10601:                     } else {
10602:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>'; 
10603:                     }
10604:                 } elsif ($item eq 'anonsurvey_threshold') {
10605:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
10606:                 } elsif ($item eq 'uploadquota') {
10607:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10608:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
10609:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
10610:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
10611:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
10612: 
10613:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
10614:                                        '</ul>'.
10615:                                        '</li>';
10616:                     } else {
10617:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
10618:                     }
10619:                 } elsif ($item eq 'postsubmit') {
10620:                     if ($domdefaults{'postsubmit'} eq 'off') {
10621:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
10622:                     } else {
10623:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
10624:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') { 
10625:                             $resulttext .= &mt('durations:').'<ul>';
10626:                             foreach my $type (@types) {
10627:                                 $resulttext .= '<li>';
10628:                                 my $timeout;
10629:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
10630:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
10631:                                 }
10632:                                 my $display;
10633:                                 if ($timeout eq '0') {
10634:                                     $display = &mt('unlimited');
10635:                                 } elsif ($timeout eq '') {
10636:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
10637:                                 } else {
10638:                                     $display = &mt('[quant,_1,second]',$timeout);
10639:                                 }
10640:                                 if ($type eq 'community') {
10641:                                     $resulttext .= &mt('Communities');
10642:                                 } elsif ($type eq 'official') {
10643:                                     $resulttext .= &mt('Official courses');
10644:                                 } elsif ($type eq 'unofficial') {
10645:                                     $resulttext .= &mt('Unofficial courses');
10646:                                 } elsif ($type eq 'textbook') {
10647:                                     $resulttext .= &mt('Textbook courses');
10648:                                 }
10649:                                 $resulttext .= ' -- '.$display.'</li>';
10650:                             }
10651:                             $resulttext .= '</ul>';
10652:                         }
10653:                         $resulttext .= '</li>';    
10654:                     }
10655:                 } elsif ($item eq 'coursecredits') {
10656:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10657:                         if (($domdefaults{'officialcredits'} eq '') &&
10658:                             ($domdefaults{'unofficialcredits'} eq '') &&
10659:                             ($domdefaults{'textbookcredits'} eq '')) {
10660:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10661:                         } else {
10662:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
10663:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
10664:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
10665:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
10666:                                            '</ul>'.
10667:                                            '</li>';
10668:                         }
10669:                     } else {
10670:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10671:                     }
10672:                 } elsif ($item eq 'canclone') {
10673:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
10674:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
10675:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
10676:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
10677:                         }
10678:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
10679:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
10680:                     } else {
10681:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>'; 
10682:                     }
10683:                 }
10684:             }
10685:             $resulttext .= '</ul>';
10686:         } else {
10687:             $resulttext = &mt('No changes made to course defaults');
10688:         }
10689:     } else {
10690:         $resulttext = '<span class="LC_error">'.
10691:             &mt('An error occurred: [_1]',$putresult).'</span>';
10692:     }
10693:     return $resulttext;
10694: }
10695: 
10696: sub modify_selfenrollment {
10697:     my ($dom,$lastactref,%domconfig) = @_;
10698:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
10699:     my @types = ('official','unofficial','community','textbook');
10700:     my %titles = &tool_titles();
10701:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
10702:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
10703:     $ordered{'default'} = ['types','registered','approval','limit'];
10704: 
10705:     my (%roles,%shown,%toplevel);
10706:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
10707: 
10708:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
10709:         if ($domconfig{'selfenrollment'} eq '') {
10710:             $domconfig{'selfenrollment'} = {};
10711:         }
10712:     }
10713:     %toplevel = (
10714:                   admin      => 'Configuration Rights',
10715:                   default    => 'Default settings',
10716:                   validation => 'Validation of self-enrollment requests',
10717:                 );
10718:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
10719: 
10720:     if (ref($ordered{'admin'}) eq 'ARRAY') {
10721:         foreach my $item (@{$ordered{'admin'}}) {
10722:             foreach my $type (@types) {
10723:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
10724:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
10725:                 } else {
10726:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
10727:                 }
10728:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
10729:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
10730:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
10731:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
10732:                             push(@{$changes{'admin'}{$type}},$item);
10733:                         }
10734:                     } else {
10735:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
10736:                             push(@{$changes{'admin'}{$type}},$item);
10737:                         }
10738:                     }
10739:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
10740:                     push(@{$changes{'admin'}{$type}},$item);
10741:                 }
10742:             }
10743:         }
10744:     }
10745: 
10746:     foreach my $item (@{$ordered{'default'}}) {
10747:         foreach my $type (@types) {
10748:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
10749:             if ($item eq 'types') {
10750:                 unless (($value eq 'all') || ($value eq 'dom')) {
10751:                     $value = '';
10752:                 }
10753:             } elsif ($item eq 'registered') {
10754:                 unless ($value eq '1') {
10755:                     $value = 0;
10756:                 }
10757:             } elsif ($item eq 'approval') {
10758:                 unless ($value =~ /^[012]$/) {
10759:                     $value = 0;
10760:                 }
10761:             } else {
10762:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10763:                     $value = 'none';
10764:                 }
10765:             }
10766:             $selfenrollhash{'default'}{$type}{$item} = $value;
10767:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
10768:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10769:                     if ($selfenrollhash{'default'}{$type}{$item} ne
10770:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
10771:                          push(@{$changes{'default'}{$type}},$item);
10772:                     }
10773:                 } else {
10774:                     push(@{$changes{'default'}{$type}},$item);
10775:                 }
10776:             } else {
10777:                 push(@{$changes{'default'}{$type}},$item);
10778:             }
10779:             if ($item eq 'limit') {
10780:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10781:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
10782:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
10783:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
10784:                     }
10785:                 } else {
10786:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
10787:                 }
10788:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10789:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
10790:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
10791:                          push(@{$changes{'default'}{$type}},'cap');
10792:                     }
10793:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
10794:                     push(@{$changes{'default'}{$type}},'cap');
10795:                 }
10796:             }
10797:         }
10798:     }
10799: 
10800:     foreach my $item (@{$itemsref}) {
10801:         if ($item eq 'fields') {
10802:             my @changed;
10803:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
10804:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
10805:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
10806:             }
10807:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10808:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
10809:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
10810:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
10811:                 } else {
10812:                     @changed = @{$selfenrollhash{'validation'}{$item}};
10813:                 }
10814:             } else {
10815:                 @changed = @{$selfenrollhash{'validation'}{$item}};
10816:             }
10817:             if (@changed) {
10818:                 if ($selfenrollhash{'validation'}{$item}) { 
10819:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
10820:                 } else {
10821:                     $changes{'validation'}{$item} = &mt('None');
10822:                 }
10823:             }
10824:         } else {
10825:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
10826:             if ($item eq 'markup') {
10827:                if ($env{'form.selfenroll_validation_'.$item}) {
10828:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10829:                }
10830:             }
10831:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10832:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
10833:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
10834:                 }
10835:             }
10836:         }
10837:     }
10838: 
10839:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
10840:                                              $dom);
10841:     if ($putresult eq 'ok') {
10842:         if (keys(%changes) > 0) {
10843:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10844:             $resulttext = &mt('Changes made:').'<ul>';
10845:             foreach my $key ('admin','default','validation') {
10846:                 if (ref($changes{$key}) eq 'HASH') {
10847:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
10848:                     if ($key eq 'validation') {
10849:                         foreach my $item (@{$itemsref}) {
10850:                             if (exists($changes{$key}{$item})) {
10851:                                 if ($item eq 'markup') {
10852:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10853:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
10854:                                 } else {  
10855:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10856:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
10857:                                 }
10858:                             }
10859:                         }
10860:                     } else {
10861:                         foreach my $type (@types) {
10862:                             if ($type eq 'community') {
10863:                                 $roles{'1'} = &mt('Community personnel');
10864:                             } else {
10865:                                 $roles{'1'} = &mt('Course personnel');
10866:                             }
10867:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
10868:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
10869:                                     if ($key eq 'admin') {
10870:                                         my @mgrdc = ();
10871:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10872:                                             foreach my $item (@{$ordered{'admin'}}) {
10873:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
10874:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
10875:                                                         push(@mgrdc,$item);
10876:                                                     }
10877:                                                 }
10878:                                             }
10879:                                             if (@mgrdc) {
10880:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
10881:                                             } else {
10882:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
10883:                                             }
10884:                                         }
10885:                                     } else {
10886:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10887:                                             foreach my $item (@{$ordered{$key}}) {
10888:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10889:                                                     $domdefaults{$type.'selfenroll'.$item} =
10890:                                                         $selfenrollhash{$key}{$type}{$item};
10891:                                                 }
10892:                                             }
10893:                                         }
10894:                                     }
10895:                                 }
10896:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
10897:                                 foreach my $item (@{$ordered{$key}}) {
10898:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10899:                                         $resulttext .= '<li>';
10900:                                         if ($key eq 'admin') {
10901:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
10902:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
10903:                                         } else {
10904:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
10905:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
10906:                                         }
10907:                                         $resulttext .= '</li>';
10908:                                     }
10909:                                 }
10910:                                 $resulttext .= '</ul></li>';
10911:                             }
10912:                         }
10913:                         $resulttext .= '</ul></li>'; 
10914:                     }
10915:                 }
10916:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
10917:                     my $cachetime = 24*60*60;
10918:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10919:                     if (ref($lastactref) eq 'HASH') {
10920:                         $lastactref->{'domdefaults'} = 1;
10921:                     }
10922:                 }
10923:             }
10924:             $resulttext .= '</ul>';
10925:         } else {
10926:             $resulttext = &mt('No changes made to self-enrollment settings');
10927:         }
10928:     } else {
10929:         $resulttext = '<span class="LC_error">'.
10930:             &mt('An error occurred: [_1]',$putresult).'</span>';
10931:     }
10932:     return $resulttext;
10933: }
10934: 
10935: sub modify_usersessions {
10936:     my ($dom,$lastactref,%domconfig) = @_;
10937:     my @hostingtypes = ('version','excludedomain','includedomain');
10938:     my @offloadtypes = ('primary','default');
10939:     my %types = (
10940:                   remote => \@hostingtypes,
10941:                   hosted => \@hostingtypes,
10942:                   spares => \@offloadtypes,
10943:                 );
10944:     my @prefixes = ('remote','hosted','spares');
10945:     my @lcversions = &Apache::lonnet::all_loncaparevs();
10946:     my (%by_ip,%by_location,@intdoms);
10947:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
10948:     my @locations = sort(keys(%by_location));
10949:     my (%defaultshash,%changes);
10950:     foreach my $prefix (@prefixes) {
10951:         $defaultshash{'usersessions'}{$prefix} = {};
10952:     }
10953:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10954:     my $resulttext;
10955:     my %iphost = &Apache::lonnet::get_iphost();
10956:     foreach my $prefix (@prefixes) {
10957:         next if ($prefix eq 'spares');
10958:         foreach my $type (@{$types{$prefix}}) {
10959:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
10960:             if ($type eq 'version') {
10961:                 my $value = $env{'form.'.$prefix.'_'.$type};
10962:                 my $okvalue;
10963:                 if ($value ne '') {
10964:                     if (grep(/^\Q$value\E$/,@lcversions)) {
10965:                         $okvalue = $value;
10966:                     }
10967:                 }
10968:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10969:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10970:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
10971:                             if ($inuse == 0) {
10972:                                 $changes{$prefix}{$type} = 1;
10973:                             } else {
10974:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
10975:                                     $changes{$prefix}{$type} = 1;
10976:                                 }
10977:                                 if ($okvalue ne '') {
10978:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10979:                                 } 
10980:                             }
10981:                         } else {
10982:                             if (($inuse == 1) && ($okvalue ne '')) {
10983:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10984:                                 $changes{$prefix}{$type} = 1;
10985:                             }
10986:                         }
10987:                     } else {
10988:                         if (($inuse == 1) && ($okvalue ne '')) {
10989:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10990:                             $changes{$prefix}{$type} = 1;
10991:                         }
10992:                     }
10993:                 } else {
10994:                     if (($inuse == 1) && ($okvalue ne '')) {
10995:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10996:                         $changes{$prefix}{$type} = 1;
10997:                     }
10998:                 }
10999:             } else {
11000:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
11001:                 my @okvals;
11002:                 foreach my $val (@vals) {
11003:                     if ($val =~ /:/) {
11004:                         my @items = split(/:/,$val);
11005:                         foreach my $item (@items) {
11006:                             if (ref($by_location{$item}) eq 'ARRAY') {
11007:                                 push(@okvals,$item);
11008:                             }
11009:                         }
11010:                     } else {
11011:                         if (ref($by_location{$val}) eq 'ARRAY') {
11012:                             push(@okvals,$val);
11013:                         }
11014:                     }
11015:                 }
11016:                 @okvals = sort(@okvals);
11017:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
11018:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
11019:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
11020:                             if ($inuse == 0) {
11021:                                 $changes{$prefix}{$type} = 1; 
11022:                             } else {
11023:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11024:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
11025:                                 if (@changed > 0) {
11026:                                     $changes{$prefix}{$type} = 1;
11027:                                 }
11028:                             }
11029:                         } else {
11030:                             if ($inuse == 1) {
11031:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11032:                                 $changes{$prefix}{$type} = 1;
11033:                             }
11034:                         } 
11035:                     } else {
11036:                         if ($inuse == 1) {
11037:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11038:                             $changes{$prefix}{$type} = 1;
11039:                         }
11040:                     }
11041:                 } else {
11042:                     if ($inuse == 1) {
11043:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11044:                         $changes{$prefix}{$type} = 1;
11045:                     }
11046:                 }
11047:             }
11048:         }
11049:     }
11050: 
11051:     my @alldoms = &Apache::lonnet::all_domains();
11052:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11053:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
11054:     my $savespares;
11055: 
11056:     foreach my $lonhost (sort(keys(%servers))) {
11057:         my $serverhomeID =
11058:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
11059:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
11060:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
11061:         my %spareschg;
11062:         foreach my $type (@{$types{'spares'}}) {
11063:             my @okspares;
11064:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
11065:             foreach my $server (@checked) {
11066:                 if (&Apache::lonnet::hostname($server) ne '') {
11067:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
11068:                         unless (grep(/^\Q$server\E$/,@okspares)) {
11069:                             push(@okspares,$server);
11070:                         }
11071:                     }
11072:                 }
11073:             }
11074:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
11075:             my $newspare;
11076:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
11077:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
11078:                     $newspare = $new;
11079:                 }
11080:             }
11081:             my @spares;
11082:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
11083:                 @spares = sort(@okspares,$newspare);
11084:             } else {
11085:                 @spares = sort(@okspares);
11086:             }
11087:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
11088:             if (ref($spareid{$lonhost}) eq 'HASH') {
11089:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
11090:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
11091:                     if (@diffs > 0) {
11092:                         $spareschg{$type} = 1;
11093:                     }
11094:                 }
11095:             }
11096:         }
11097:         if (keys(%spareschg) > 0) {
11098:             $changes{'spares'}{$lonhost} = \%spareschg;
11099:         }
11100:     }
11101:     $defaultshash{'usersessions'}{'offloadnow'} = {};
11102:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
11103:     my @okoffload;
11104:     if (@offloadnow) {
11105:         foreach my $server (@offloadnow) {
11106:             if (&Apache::lonnet::hostname($server) ne '') {
11107:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
11108:                     push(@okoffload,$server);
11109:                 }
11110:             }
11111:         }
11112:         if (@okoffload) {
11113:             foreach my $lonhost (@okoffload) {
11114:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
11115:             }
11116:         }
11117:     }
11118:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
11119:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
11120:             if (ref($changes{'spares'}) eq 'HASH') {
11121:                 if (keys(%{$changes{'spares'}}) > 0) {
11122:                     $savespares = 1;
11123:                 }
11124:             }
11125:         } else {
11126:             $savespares = 1;
11127:         }
11128:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
11129:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
11130:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
11131:                     $changes{'offloadnow'} = 1;
11132:                     last;
11133:                 }
11134:             }
11135:             unless ($changes{'offloadnow'}) {
11136:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) { 
11137:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
11138:                         $changes{'offloadnow'} = 1;
11139:                         last;
11140:                     }
11141:                 }
11142:             }
11143:         } elsif (@okoffload) {
11144:             $changes{'offloadnow'} = 1;
11145:         }
11146:     } elsif (@okoffload) {
11147:         $changes{'offloadnow'} = 1;
11148:     }
11149:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
11150:     if ((keys(%changes) > 0) || ($savespares)) {
11151:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
11152:                                                  $dom);
11153:         if ($putresult eq 'ok') {
11154:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
11155:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
11156:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
11157:                 }
11158:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
11159:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
11160:                 }
11161:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
11162:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
11163:                 }
11164:             }
11165:             my $cachetime = 24*60*60;
11166:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11167:             if (ref($lastactref) eq 'HASH') {
11168:                 $lastactref->{'domdefaults'} = 1;
11169:             }
11170:             if (keys(%changes) > 0) {
11171:                 my %lt = &usersession_titles();
11172:                 $resulttext = &mt('Changes made:').'<ul>';
11173:                 foreach my $prefix (@prefixes) {
11174:                     if (ref($changes{$prefix}) eq 'HASH') {
11175:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
11176:                         if ($prefix eq 'spares') {
11177:                             if (ref($changes{$prefix}) eq 'HASH') {
11178:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
11179:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
11180:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
11181:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
11182:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
11183:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
11184:                                         foreach my $type (@{$types{$prefix}}) {
11185:                                             if ($changes{$prefix}{$lonhost}{$type}) {
11186:                                                 my $offloadto = &mt('None');
11187:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
11188:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
11189:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
11190:                                                     }
11191:                                                 }
11192:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
11193:                                             }
11194:                                         }
11195:                                     }
11196:                                     $resulttext .= '</li>';
11197:                                 }
11198:                             }
11199:                         } else {
11200:                             foreach my $type (@{$types{$prefix}}) {
11201:                                 if (defined($changes{$prefix}{$type})) {
11202:                                     my $newvalue;
11203:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
11204:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
11205:                                             if ($type eq 'version') {
11206:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
11207:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
11208:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
11209:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
11210:                                                 }
11211:                                             }
11212:                                         }
11213:                                     }
11214:                                     if ($newvalue eq '') {
11215:                                         if ($type eq 'version') {
11216:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
11217:                                         } else {
11218:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
11219:                                         }
11220:                                     } else {
11221:                                         if ($type eq 'version') {
11222:                                             $newvalue .= ' '.&mt('(or later)'); 
11223:                                         }
11224:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
11225:                                     }
11226:                                 }
11227:                             }
11228:                         }
11229:                         $resulttext .= '</ul>';
11230:                     }
11231:                 }
11232:                 if ($changes{'offloadnow'}) {
11233:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
11234:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
11235:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
11236:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
11237:                                 $resulttext .= '<li>'.$lonhost.'</li>';
11238:                             }
11239:                             $resulttext .= '</ul>';
11240:                         } else {
11241:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
11242:                         }
11243:                     } else {
11244:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
11245:                     }
11246:                 }
11247:                 $resulttext .= '</ul>';
11248:             } else {
11249:                 $resulttext = $nochgmsg;
11250:             }
11251:         } else {
11252:             $resulttext = '<span class="LC_error">'.
11253:                           &mt('An error occurred: [_1]',$putresult).'</span>';
11254:         }
11255:     } else {
11256:         $resulttext = $nochgmsg;
11257:     }
11258:     return $resulttext;
11259: }
11260: 
11261: sub modify_loadbalancing {
11262:     my ($dom,%domconfig) = @_;
11263:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
11264:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
11265:     my ($othertitle,$usertypes,$types) =
11266:         &Apache::loncommon::sorted_inst_types($dom);
11267:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11268:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
11269:     my @sparestypes = ('primary','default');
11270:     my %typetitles = &sparestype_titles();
11271:     my $resulttext;
11272:     my (%currbalancer,%currtargets,%currrules,%existing);
11273:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
11274:         %existing = %{$domconfig{'loadbalancing'}};
11275:     }
11276:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
11277:                               \%currtargets,\%currrules);
11278:     my ($saveloadbalancing,%defaultshash,%changes);
11279:     my ($alltypes,$othertypes,$titles) =
11280:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
11281:     my %ruletitles = &offloadtype_text();
11282:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
11283:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
11284:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
11285:         if ($balancer eq '') {
11286:             next;
11287:         }
11288:         if (!exists($servers{$balancer})) {
11289:             if (exists($currbalancer{$balancer})) {
11290:                 push(@{$changes{'delete'}},$balancer);
11291:             }
11292:             next;
11293:         }
11294:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
11295:             push(@{$changes{'delete'}},$balancer);
11296:             next;
11297:         }
11298:         if (!exists($currbalancer{$balancer})) {
11299:             push(@{$changes{'add'}},$balancer);
11300:         }
11301:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
11302:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
11303:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
11304:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
11305:             $saveloadbalancing = 1;
11306:         }
11307:         foreach my $sparetype (@sparestypes) {
11308:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
11309:             my @offloadto;
11310:             foreach my $target (@targets) {
11311:                 if (($servers{$target}) && ($target ne $balancer)) {
11312:                     if ($sparetype eq 'default') {
11313:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
11314:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
11315:                         }
11316:                     }
11317:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
11318:                         push(@offloadto,$target);
11319:                     }
11320:                 }
11321:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
11322:             }
11323:         }
11324:         if (ref($currtargets{$balancer}) eq 'HASH') {
11325:             foreach my $sparetype (@sparestypes) {
11326:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
11327:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
11328:                     if (@targetdiffs > 0) {
11329:                         $changes{'curr'}{$balancer}{'targets'} = 1;
11330:                     }
11331:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
11332:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
11333:                         $changes{'curr'}{$balancer}{'targets'} = 1;
11334:                     }
11335:                 }
11336:             }
11337:         } else {
11338:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
11339:                 foreach my $sparetype (@sparestypes) {
11340:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
11341:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
11342:                             $changes{'curr'}{$balancer}{'targets'} = 1;
11343:                         }
11344:                     }
11345:                 }
11346:             }
11347:         }
11348:         my $ishomedom;
11349:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
11350:             $ishomedom = 1;
11351:         }
11352:         if (ref($alltypes) eq 'ARRAY') {
11353:             foreach my $type (@{$alltypes}) {
11354:                 my $rule;
11355:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
11356:                          (!$ishomedom)) {
11357:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
11358:                 }
11359:                 if ($rule eq 'specific') {
11360:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
11361:                     if (exists($servers{$specifiedhost})) { 
11362:                         $rule = $specifiedhost;
11363:                     }
11364:                 }
11365:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
11366:                 if (ref($currrules{$balancer}) eq 'HASH') {
11367:                     if ($rule ne $currrules{$balancer}{$type}) {
11368:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
11369:                     }
11370:                 } elsif ($rule ne '') {
11371:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
11372:                 }
11373:             }
11374:         }
11375:     }
11376:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
11377:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
11378:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
11379:             $defaultshash{'loadbalancing'} = {};
11380:         }
11381:         my $putresult = &Apache::lonnet::put_dom('configuration',
11382:                                                  \%defaultshash,$dom);
11383:         if ($putresult eq 'ok') {
11384:             if (keys(%changes) > 0) {
11385:                 my %toupdate;
11386:                 if (ref($changes{'delete'}) eq 'ARRAY') {
11387:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
11388:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
11389:                         $toupdate{$balancer} = 1;
11390:                     }
11391:                 }
11392:                 if (ref($changes{'add'}) eq 'ARRAY') {
11393:                     foreach my $balancer (sort(@{$changes{'add'}})) {
11394:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
11395:                         $toupdate{$balancer} = 1;
11396:                     }
11397:                 }
11398:                 if (ref($changes{'curr'}) eq 'HASH') {
11399:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
11400:                         $toupdate{$balancer} = 1;
11401:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
11402:                             if ($changes{'curr'}{$balancer}{'targets'}) {
11403:                                 my %offloadstr;
11404:                                 foreach my $sparetype (@sparestypes) {
11405:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
11406:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
11407:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
11408:                                         }
11409:                                     }
11410:                                 }
11411:                                 if (keys(%offloadstr) == 0) {
11412:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
11413:                                 } else {
11414:                                     my $showoffload;
11415:                                     foreach my $sparetype (@sparestypes) {
11416:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
11417:                                         if (defined($offloadstr{$sparetype})) {
11418:                                             $showoffload .= $offloadstr{$sparetype};
11419:                                         } else {
11420:                                             $showoffload .= &mt('None');
11421:                                         }
11422:                                         $showoffload .= ('&nbsp;'x3);
11423:                                     }
11424:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
11425:                                 }
11426:                             }
11427:                         }
11428:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
11429:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
11430:                                 foreach my $type (@{$alltypes}) {
11431:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
11432:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
11433:                                         my $balancetext;
11434:                                         if ($rule eq '') {
11435:                                             $balancetext =  $ruletitles{'default'};
11436:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
11437:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) { 
11438:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
11439:                                                 foreach my $sparetype (@sparestypes) {
11440:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
11441:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
11442:                                                     }
11443:                                                 }
11444:                                                 foreach my $item (@{$alltypes}) {
11445:                                                     next if ($item =~  /^_LC_ipchange/);
11446:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
11447:                                                     if ($hasrule eq 'homeserver') {
11448:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
11449:                                                     } else {
11450:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
11451:                                                             if ($servers{$hasrule}) {
11452:                                                                 $toupdate{$hasrule} = 1;
11453:                                                             }
11454:                                                         }
11455:                                                     }
11456:                                                 }
11457:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
11458:                                                     $balancetext =  $ruletitles{$rule};
11459:                                                 } else {
11460:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
11461:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
11462:                                                     if ($receiver) {
11463:                                                         $toupdate{$receiver};
11464:                                                     }
11465:                                                 }
11466:                                             } else {
11467:                                                 $balancetext =  $ruletitles{$rule};
11468:                                             }
11469:                                         } else {
11470:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
11471:                                         }
11472:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
11473:                                     }
11474:                                 }
11475:                             }
11476:                         }
11477:                         if (keys(%toupdate)) {
11478:                             my %thismachine;
11479:                             my $updatedhere;
11480:                             my $cachetime = 60*60*24;
11481:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
11482:                             foreach my $lonhost (keys(%toupdate)) {
11483:                                 if ($thismachine{$lonhost}) {
11484:                                     unless ($updatedhere) {
11485:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
11486:                                                                       $defaultshash{'loadbalancing'},
11487:                                                                       $cachetime);
11488:                                         $updatedhere = 1;
11489:                                     }
11490:                                 } else {
11491:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
11492:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
11493:                                 }
11494:                             }
11495:                         }
11496:                     }
11497:                 }
11498:                 if ($resulttext ne '') {
11499:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
11500:                 } else {
11501:                     $resulttext = $nochgmsg;
11502:                 }
11503:             } else {
11504:                 $resulttext = $nochgmsg;
11505:             }
11506:         } else {
11507:             $resulttext = '<span class="LC_error">'.
11508:                           &mt('An error occurred: [_1]',$putresult).'</span>';
11509:         }
11510:     } else {
11511:         $resulttext = $nochgmsg;
11512:     }
11513:     return $resulttext;
11514: }
11515: 
11516: sub recurse_check {
11517:     my ($chkcats,$categories,$depth,$name) = @_;
11518:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
11519:         my $chg = 0;
11520:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
11521:             my $category = $chkcats->[$depth]{$name}[$j];
11522:             my $item;
11523:             if ($category eq '') {
11524:                 $chg ++;
11525:             } else {
11526:                 my $deeper = $depth + 1;
11527:                 $item = &escape($category).':'.&escape($name).':'.$depth;
11528:                 if ($chg) {
11529:                     $categories->{$item} -= $chg;
11530:                 }
11531:                 &recurse_check($chkcats,$categories,$deeper,$category);
11532:                 $deeper --;
11533:             }
11534:         }
11535:     }
11536:     return;
11537: }
11538: 
11539: sub recurse_cat_deletes {
11540:     my ($item,$coursecategories,$deletions) = @_;
11541:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
11542:     my $subdepth = $depth + 1;
11543:     if (ref($coursecategories) eq 'HASH') {
11544:         foreach my $subitem (keys(%{$coursecategories})) {
11545:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
11546:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
11547:                 delete($coursecategories->{$subitem});
11548:                 $deletions->{$subitem} = 1;
11549:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
11550:             }
11551:         }
11552:     }
11553:     return;
11554: }
11555: 
11556: sub get_active_dcs {
11557:     my ($dom) = @_;
11558:     my $now = time;
11559:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
11560:     my %domcoords;
11561:     my $numdcs = 0;
11562:     foreach my $server (keys(%dompersonnel)) {
11563:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
11564:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
11565:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
11566:         }
11567:     }
11568:     return %domcoords;
11569: }
11570: 
11571: sub active_dc_picker {
11572:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
11573:     my %domcoords = &get_active_dcs($dom);
11574:     my @domcoord = keys(%domcoords);
11575:     if (keys(%currhash)) {
11576:         foreach my $dc (keys(%currhash)) {
11577:             unless (exists($domcoords{$dc})) {
11578:                 push(@domcoord,$dc);
11579:             }
11580:         }
11581:     }
11582:     @domcoord = sort(@domcoord);
11583:     my $numdcs = scalar(@domcoord);
11584:     my $rows = 0;
11585:     my $table;
11586:     if ($numdcs > 1) {
11587:         $table = '<table>';
11588:         for (my $i=0; $i<@domcoord; $i++) {
11589:             my $rem = $i%($numinrow);
11590:             if ($rem == 0) {
11591:                 if ($i > 0) {
11592:                     $table .= '</tr>';
11593:                 }
11594:                 $table .= '<tr>';
11595:                 $rows ++;
11596:             }
11597:             my $check = '';
11598:             if ($inputtype eq 'radio') {
11599:                 if (keys(%currhash) == 0) {
11600:                     if (!$i) {
11601:                         $check = ' checked="checked"';
11602:                     }
11603:                 } elsif (exists($currhash{$domcoord[$i]})) {
11604:                     $check = ' checked="checked"';
11605:                 }
11606:             } else {
11607:                 if (exists($currhash{$domcoord[$i]})) {
11608:                     $check = ' checked="checked"';
11609:                 }
11610:             }
11611:             if ($i == @domcoord - 1) {
11612:                 my $colsleft = $numinrow - $rem;
11613:                 if ($colsleft > 1) {
11614:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
11615:                 } else {
11616:                     $table .= '<td class="LC_left_item">';
11617:                 }
11618:             } else {
11619:                 $table .= '<td class="LC_left_item">';
11620:             }
11621:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
11622:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
11623:             $table .= '<span class="LC_nobreak"><label>'.
11624:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
11625:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
11626:             if ($user ne $dcname.':'.$dcdom) {
11627:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11628:             }
11629:             $table .= '</label></span></td>';
11630:         }
11631:         $table .= '</tr></table>';
11632:     } elsif ($numdcs == 1) {
11633:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
11634:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
11635:         if ($inputtype eq 'radio') {
11636:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
11637:             if ($user ne $dcname.':'.$dcdom) {
11638:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11639:             }
11640:         } else {
11641:             my $check;
11642:             if (exists($currhash{$domcoord[0]})) {
11643:                 $check = ' checked="checked"';
11644:             }
11645:             $table = '<span class="LC_nobreak"><label>'.
11646:                      '<input type="checkbox" name="'.$name.'" '.
11647:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
11648:             if ($user ne $dcname.':'.$dcdom) {
11649:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11650:             }
11651:             $table .= '</label></span>';
11652:             $rows ++;
11653:         }
11654:     }
11655:     return ($numdcs,$table,$rows);
11656: }
11657: 
11658: sub usersession_titles {
11659:     return &Apache::lonlocal::texthash(
11660:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
11661:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
11662:                spares => 'Servers offloaded to, when busy',
11663:                version => 'LON-CAPA version requirement',
11664:                excludedomain => 'Allow all, but exclude specific domains',
11665:                includedomain => 'Deny all, but include specific domains',
11666:                primary => 'Primary (checked first)',
11667:                default => 'Default',
11668:            );
11669: }
11670: 
11671: sub id_for_thisdom {
11672:     my (%servers) = @_;
11673:     my %altids;
11674:     foreach my $server (keys(%servers)) {
11675:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
11676:         if ($serverhome ne $server) {
11677:             $altids{$serverhome} = $server;
11678:         }
11679:     }
11680:     return %altids;
11681: }
11682: 
11683: sub count_servers {
11684:     my ($currbalancer,%servers) = @_;
11685:     my (@spares,$numspares);
11686:     foreach my $lonhost (sort(keys(%servers))) {
11687:         next if ($currbalancer eq $lonhost);
11688:         push(@spares,$lonhost);
11689:     }
11690:     if ($currbalancer) {
11691:         $numspares = scalar(@spares);
11692:     } else {
11693:         $numspares = scalar(@spares) - 1;
11694:     }
11695:     return ($numspares,@spares);
11696: }
11697: 
11698: sub lonbalance_targets_js {
11699:     my ($dom,$types,$servers,$settings) = @_;
11700:     my $select = &mt('Select');
11701:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
11702:     if (ref($servers) eq 'HASH') {
11703:         $alltargets = join("','",sort(keys(%{$servers})));
11704:         my @homedoms;
11705:         foreach my $server (sort(keys(%{$servers}))) {
11706:             if (&Apache::lonnet::host_domain($server) eq $dom) {
11707:                 push(@homedoms,'1');
11708:             } else {
11709:                 push(@homedoms,'0');
11710:             }
11711:         }
11712:         $allishome = join("','",@homedoms);
11713:     }
11714:     if (ref($types) eq 'ARRAY') {
11715:         if (@{$types} > 0) {
11716:             @alltypes = @{$types};
11717:         }
11718:     }
11719:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
11720:     $allinsttypes = join("','",@alltypes);
11721:     my (%currbalancer,%currtargets,%currrules,%existing);
11722:     if (ref($settings) eq 'HASH') {
11723:         %existing = %{$settings};
11724:     }
11725:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
11726:                               \%currtargets,\%currrules);
11727:     my $balancers = join("','",sort(keys(%currbalancer)));
11728:     return <<"END";
11729: 
11730: <script type="text/javascript">
11731: // <![CDATA[
11732: 
11733: currBalancers = new Array('$balancers');
11734: 
11735: function toggleTargets(balnum) {
11736:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11737:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
11738:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
11739:     var prevbalancer = prevhostitem.value;
11740:     var baltotal = document.getElementById('loadbalancing_total').value;
11741:     prevhostitem.value = balancer;
11742:     if (prevbalancer != '') {
11743:         var prevIdx = currBalancers.indexOf(prevbalancer);
11744:         if (prevIdx != -1) {
11745:             currBalancers.splice(prevIdx,1);
11746:         }
11747:     }
11748:     if (balancer == '') {
11749:         hideSpares(balnum);
11750:     } else {
11751:         var currIdx = currBalancers.indexOf(balancer);
11752:         if (currIdx == -1) {
11753:             currBalancers.push(balancer);
11754:         }
11755:         var homedoms = new Array('$allishome');
11756:         var ishomedom = homedoms[lonhostitem.selectedIndex];
11757:         showSpares(balancer,ishomedom,balnum);
11758:     }
11759:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
11760:     return;
11761: }
11762: 
11763: function showSpares(balancer,ishomedom,balnum) {
11764:     var alltargets = new Array('$alltargets');
11765:     var insttypes = new Array('$allinsttypes');
11766:     var offloadtypes = new Array('primary','default');
11767: 
11768:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
11769:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
11770:  
11771:     for (var i=0; i<offloadtypes.length; i++) {
11772:         var count = 0;
11773:         for (var j=0; j<alltargets.length; j++) {
11774:             if (alltargets[j] != balancer) {
11775:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
11776:                 item.value = alltargets[j];
11777:                 item.style.textAlign='left';
11778:                 item.style.textFace='normal';
11779:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
11780:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
11781:                     item.disabled = '';
11782:                 } else {
11783:                     item.disabled = 'disabled';
11784:                     item.checked = false;
11785:                 }
11786:                 count ++;
11787:             }
11788:         }
11789:     }
11790:     for (var k=0; k<insttypes.length; k++) {
11791:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
11792:             if (ishomedom == 1) {
11793:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11794:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11795:             } else {
11796:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11797:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11798:             }
11799:         } else {
11800:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11801:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11802:         }
11803:         if ((insttypes[k] != '_LC_external') && 
11804:             ((insttypes[k] != '_LC_internetdom') ||
11805:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
11806:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
11807:             item.options.length = 0;
11808:             item.options[0] = new Option("","",true,true);
11809:             var idx = 0;
11810:             for (var m=0; m<alltargets.length; m++) {
11811:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
11812:                     idx ++;
11813:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11814:                 }
11815:             }
11816:         }
11817:     }
11818:     return;
11819: }
11820: 
11821: function hideSpares(balnum) {
11822:     var alltargets = new Array('$alltargets');
11823:     var insttypes = new Array('$allinsttypes');
11824:     var offloadtypes = new Array('primary','default');
11825: 
11826:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
11827:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
11828: 
11829:     var total = alltargets.length - 1;
11830:     for (var i=0; i<offloadtypes; i++) {
11831:         for (var j=0; j<total; j++) {
11832:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
11833:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
11834:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
11835:         }
11836:     }
11837:     for (var k=0; k<insttypes.length; k++) {
11838:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11839:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11840:         if (insttypes[k] != '_LC_external') {
11841:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
11842:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
11843:         }
11844:     }
11845:     return;
11846: }
11847: 
11848: function checkOffloads(item,balnum,type) {
11849:     var alltargets = new Array('$alltargets');
11850:     var offloadtypes = new Array('primary','default');
11851:     if (item.checked) {
11852:         var total = alltargets.length - 1;
11853:         var other;
11854:         if (type == offloadtypes[0]) {
11855:             other = offloadtypes[1];
11856:         } else {
11857:             other = offloadtypes[0];
11858:         }
11859:         for (var i=0; i<total; i++) {
11860:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
11861:             if (server == item.value) {
11862:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
11863:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
11864:                 }
11865:             }
11866:         }
11867:     }
11868:     return;
11869: }
11870: 
11871: function singleServerToggle(balnum,type) {
11872:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
11873:     if (offloadtoSelIdx == 0) {
11874:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
11875:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11876: 
11877:     } else {
11878:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
11879:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11880:     }
11881:     return;
11882: }
11883: 
11884: function balanceruleChange(formname,balnum,type) {
11885:     if (type == '_LC_external') {
11886:         return;
11887:     }
11888:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
11889:     for (var i=0; i<typesRules.length; i++) {
11890:         if (formname.elements[typesRules[i]].checked) {
11891:             if (formname.elements[typesRules[i]].value != 'specific') {
11892:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
11893:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11894:             } else {
11895:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11896:             }
11897:         }
11898:     }
11899:     return;
11900: }
11901: 
11902: function balancerDeleteChange(balnum) {
11903:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11904:     var baltotal = document.getElementById('loadbalancing_total').value;
11905:     var addtarget;
11906:     var removetarget;
11907:     var action = 'delete';
11908:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
11909:         var lonhost = hostitem.value;
11910:         var currIdx = currBalancers.indexOf(lonhost);
11911:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
11912:             if (currIdx != -1) {
11913:                 currBalancers.splice(currIdx,1);
11914:             }
11915:             addtarget = lonhost;
11916:         } else {
11917:             if (currIdx == -1) {
11918:                 currBalancers.push(lonhost);
11919:             }
11920:             removetarget = lonhost;
11921:             action = 'undelete';
11922:         }
11923:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
11924:     }
11925:     return;
11926: }
11927: 
11928: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
11929:     if (baltotal > 1) {
11930:         var offloadtypes = new Array('primary','default');
11931:         var alltargets = new Array('$alltargets');
11932:         var insttypes = new Array('$allinsttypes');
11933:         for (var i=0; i<baltotal; i++) {
11934:             if (i != balnum) {
11935:                 for (var j=0; j<offloadtypes.length; j++) {
11936:                     var total = alltargets.length - 1;
11937:                     for (var k=0; k<total; k++) {
11938:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
11939:                         var server = serveritem.value;
11940:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
11941:                             if (server == addtarget) {
11942:                                 serveritem.disabled = '';
11943:                             }
11944:                         }
11945:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11946:                             if (server == removetarget) {
11947:                                 serveritem.disabled = 'disabled';
11948:                                 serveritem.checked = false;
11949:                             }
11950:                         }
11951:                     }
11952:                 }
11953:                 for (var j=0; j<insttypes.length; j++) {
11954:                     if (insttypes[j] != '_LC_external') {
11955:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
11956:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
11957:                             var currSel = singleserver.selectedIndex;
11958:                             var currVal = singleserver.options[currSel].value;
11959:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
11960:                                 var numoptions = singleserver.options.length;
11961:                                 var needsnew = 1;
11962:                                 for (var k=0; k<numoptions; k++) {
11963:                                     if (singleserver.options[k] == addtarget) {
11964:                                         needsnew = 0;
11965:                                         break;
11966:                                     }
11967:                                 }
11968:                                 if (needsnew == 1) {
11969:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
11970:                                 }
11971:                             }
11972:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11973:                                 singleserver.options.length = 0;
11974:                                 if ((currVal) && (currVal != removetarget)) {
11975:                                     singleserver.options[0] = new Option("","",false,false);
11976:                                 } else {
11977:                                     singleserver.options[0] = new Option("","",true,true);
11978:                                 }
11979:                                 var idx = 0;
11980:                                 for (var m=0; m<alltargets.length; m++) {
11981:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
11982:                                         idx ++;
11983:                                         if (currVal == alltargets[m]) {
11984:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
11985:                                         } else {
11986:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11987:                                         }
11988:                                     }
11989:                                 }
11990:                             }
11991:                         }
11992:                     }
11993:                 }
11994:             }
11995:         }
11996:     }
11997:     return;
11998: }
11999: 
12000: // ]]>
12001: </script>
12002: 
12003: END
12004: }
12005: 
12006: sub new_spares_js {
12007:     my @sparestypes = ('primary','default');
12008:     my $types = join("','",@sparestypes);
12009:     my $select = &mt('Select');
12010:     return <<"END";
12011: 
12012: <script type="text/javascript">
12013: // <![CDATA[
12014: 
12015: function updateNewSpares(formname,lonhost) {
12016:     var types = new Array('$types');
12017:     var include = new Array();
12018:     var exclude = new Array();
12019:     for (var i=0; i<types.length; i++) {
12020:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
12021:         for (var j=0; j<spareboxes.length; j++) {
12022:             if (formname.elements[spareboxes[j]].checked) {
12023:                 exclude.push(formname.elements[spareboxes[j]].value);
12024:             } else {
12025:                 include.push(formname.elements[spareboxes[j]].value);
12026:             }
12027:         }
12028:     }
12029:     for (var i=0; i<types.length; i++) {
12030:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
12031:         var selIdx = newSpare.selectedIndex;
12032:         var currnew = newSpare.options[selIdx].value;
12033:         var okSpares = new Array();
12034:         for (var j=0; j<newSpare.options.length; j++) {
12035:             var possible = newSpare.options[j].value;
12036:             if (possible != '') {
12037:                 if (exclude.indexOf(possible) == -1) {
12038:                     okSpares.push(possible);
12039:                 } else {
12040:                     if (currnew == possible) {
12041:                         selIdx = 0;
12042:                     }
12043:                 }
12044:             }
12045:         }
12046:         for (var k=0; k<include.length; k++) {
12047:             if (okSpares.indexOf(include[k]) == -1) {
12048:                 okSpares.push(include[k]);
12049:             }
12050:         }
12051:         okSpares.sort();
12052:         newSpare.options.length = 0;
12053:         if (selIdx == 0) {
12054:             newSpare.options[0] = new Option("$select","",true,true);
12055:         } else {
12056:             newSpare.options[0] = new Option("$select","",false,false);
12057:         }
12058:         for (var m=0; m<okSpares.length; m++) {
12059:             var idx = m+1;
12060:             var selThis = 0;
12061:             if (selIdx != 0) {
12062:                 if (okSpares[m] == currnew) {
12063:                     selThis = 1;
12064:                 }
12065:             }
12066:             if (selThis == 1) {
12067:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
12068:             } else {
12069:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
12070:             }
12071:         }
12072:     }
12073:     return;
12074: }
12075: 
12076: function checkNewSpares(lonhost,type) {
12077:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
12078:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
12079:     if (chosen != '') { 
12080:         var othertype;
12081:         var othernewSpare;
12082:         if (type == 'primary') {
12083:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
12084:         }
12085:         if (type == 'default') {
12086:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
12087:         }
12088:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
12089:             othernewSpare.selectedIndex = 0;
12090:         }
12091:     }
12092:     return;
12093: }
12094: 
12095: // ]]>
12096: </script>
12097: 
12098: END
12099: 
12100: }
12101: 
12102: sub common_domprefs_js {
12103:     return <<"END";
12104: 
12105: <script type="text/javascript">
12106: // <![CDATA[
12107: 
12108: function getIndicesByName(formname,item) {
12109:     var group = new Array();
12110:     for (var i=0;i<formname.elements.length;i++) {
12111:         if (formname.elements[i].name == item) {
12112:             group.push(formname.elements[i].id);
12113:         }
12114:     }
12115:     return group;
12116: }
12117: 
12118: // ]]>
12119: </script>
12120: 
12121: END
12122: 
12123: }
12124: 
12125: sub recaptcha_js {
12126:     my %lt = &captcha_phrases();
12127:     return <<"END";
12128: 
12129: <script type="text/javascript">
12130: // <![CDATA[
12131: 
12132: function updateCaptcha(caller,context) {
12133:     var privitem;
12134:     var pubitem;
12135:     var privtext;
12136:     var pubtext;
12137:     if (document.getElementById(context+'_recaptchapub')) {
12138:         pubitem = document.getElementById(context+'_recaptchapub');
12139:     } else {
12140:         return;
12141:     }
12142:     if (document.getElementById(context+'_recaptchapriv')) {
12143:         privitem = document.getElementById(context+'_recaptchapriv');
12144:     } else {
12145:         return;
12146:     }
12147:     if (document.getElementById(context+'_recaptchapubtxt')) {
12148:         pubtext = document.getElementById(context+'_recaptchapubtxt');
12149:     } else {
12150:         return;
12151:     }
12152:     if (document.getElementById(context+'_recaptchaprivtxt')) {
12153:         privtext = document.getElementById(context+'_recaptchaprivtxt');
12154:     } else {
12155:         return;
12156:     }
12157:     if (caller.checked) {
12158:         if (caller.value == 'recaptcha') {
12159:             pubitem.type = 'text';
12160:             privitem.type = 'text';
12161:             pubitem.size = '40';
12162:             privitem.size = '40';
12163:             pubtext.innerHTML = "$lt{'pub'}";
12164:             privtext.innerHTML = "$lt{'priv'}";
12165:         } else {
12166:             pubitem.type = 'hidden';
12167:             privitem.type = 'hidden';
12168:             pubtext.innerHTML = '';
12169:             privtext.innerHTML = '';
12170:         }
12171:     }
12172:     return;
12173: }
12174: 
12175: // ]]>
12176: </script>
12177: 
12178: END
12179: 
12180: }
12181: 
12182: sub toggle_display_js {
12183:     return <<"END";
12184: 
12185: <script type="text/javascript">
12186: // <![CDATA[
12187: 
12188: function toggleDisplay(domForm,caller) {
12189:     if (document.getElementById(caller)) {
12190:         var divitem = document.getElementById(caller);
12191:         var optionsElement = domForm.coursecredits;
12192:         var checkval = 1;
12193:         var dispval = 'block';
12194:         if (caller == 'emailoptions') {
12195:             optionsElement = domForm.cancreate_email; 
12196:         }
12197:         if (caller == 'studentsubmission') {
12198:             optionsElement = domForm.postsubmit;
12199:         }
12200:         if (caller == 'cloneinstcode') {
12201:             optionsElement = domForm.canclone;
12202:             checkval = 'instcode';
12203:         }
12204:         if (optionsElement.length) {
12205:             var currval;
12206:             for (var i=0; i<optionsElement.length; i++) {
12207:                 if (optionsElement[i].checked) {
12208:                    currval = optionsElement[i].value;
12209:                 }
12210:             }
12211:             if (currval == checkval) {
12212:                 divitem.style.display = dispval;
12213:             } else {
12214:                 divitem.style.display = 'none';
12215:             }
12216:         }
12217:     }
12218:     return;
12219: }
12220: 
12221: // ]]>
12222: </script>
12223: 
12224: END
12225: 
12226: }
12227: 
12228: sub captcha_phrases {
12229:     return &Apache::lonlocal::texthash (
12230:                  priv => 'Private key',
12231:                  pub  => 'Public key',
12232:                  original  => 'original (CAPTCHA)',
12233:                  recaptcha => 'successor (ReCAPTCHA)',
12234:                  notused   => 'unused',
12235:     );
12236: }
12237: 
12238: sub devalidate_remote_domconfs {
12239:     my ($dom,$cachekeys) = @_;
12240:     return unless (ref($cachekeys) eq 'HASH');
12241:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12242:     my %thismachine;
12243:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
12244:     my @posscached = ('domainconfig','domdefaults');
12245:     if (keys(%servers)) {
12246:         foreach my $server (keys(%servers)) {
12247:             next if ($thismachine{$server});
12248:             my @cached;
12249:             foreach my $name (@posscached) {
12250:                 if ($cachekeys->{$name}) {
12251:                     push(@cached,&escape($name).':'.&escape($dom));
12252:                 }
12253:             }
12254:             if (@cached) {
12255:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
12256:             }
12257:         }
12258:     }
12259:     return;
12260: }
12261: 
12262: 1;

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