File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.242: download - view: text, annotated - select for diffs
Fri May 9 15:02:31 2014 UTC (10 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- "Textbook" course requests can specify cloning from one of two types of
  prefabricated courses: either (a) "textbooks" or (b) "templates".

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.242 2014/05/09 15:02:31 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affiliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas, 
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course, and to display/store
   98: default quota sizes for Authoring Spaces.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial, community, and textbook).  In each case the radio buttons 
  107: allow the selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use Apache::lonuserutils();
  168: use Apache::loncoursequeueadmin();
  169: use LONCAPA qw(:DEFAULT :match);
  170: use LONCAPA::Enrollment;
  171: use LONCAPA::lonauthcgi();
  172: use File::Copy;
  173: use Locale::Language;
  174: use DateTime::TimeZone;
  175: use DateTime::Locale;
  176: 
  177: my $registered_cleanup;
  178: my $modified_urls;
  179: 
  180: sub handler {
  181:     my $r=shift;
  182:     if ($r->header_only) {
  183:         &Apache::loncommon::content_type($r,'text/html');
  184:         $r->send_http_header;
  185:         return OK;
  186:     }
  187: 
  188:     my $context = 'domain';
  189:     my $dom = $env{'request.role.domain'};
  190:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  191:     if (&Apache::lonnet::allowed('mau',$dom)) {
  192:         &Apache::loncommon::content_type($r,'text/html');
  193:         $r->send_http_header;
  194:     } else {
  195:         $env{'user.error.msg'}=
  196:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  197:         return HTTP_NOT_ACCEPTABLE;
  198:     }
  199: 
  200:     $registered_cleanup=0;
  201:     @{$modified_urls}=();
  202: 
  203:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  204:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  205:                                             ['phase','actions']);
  206:     my $phase = 'pickactions';
  207:     if ( exists($env{'form.phase'}) ) {
  208:         $phase = $env{'form.phase'};
  209:     }
  210:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  211:     my %domconfig =
  212:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  213:                 'quotas','autoenroll','autoupdate','autocreate',
  214:                 'directorysrch','usercreation','usermodification',
  215:                 'contacts','defaults','scantron','coursecategories',
  216:                 'serverstatuses','requestcourses','helpsettings',
  217:                 'coursedefaults','usersessions','loadbalancing',
  218:                 'requestauthor','selfenrollment','inststatus'],$dom);
  219:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  220:                        'autoupdate','autocreate','directorysrch','contacts',
  221:                        'usercreation','selfcreation','usermodification','scantron',
  222:                        'requestcourses','requestauthor','coursecategories',
  223:                        'serverstatuses','helpsettings',
  224:                        'coursedefaults','selfenrollment','usersessions');
  225:     my %existing;
  226:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  227:         %existing = %{$domconfig{'loadbalancing'}};
  228:     }
  229:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  230:         push(@prefs_order,'loadbalancing');
  231:     }
  232:     my %prefs = (
  233:         'rolecolors' =>
  234:                    { text => 'Default color schemes',
  235:                      help => 'Domain_Configuration_Color_Schemes',
  236:                      header => [{col1 => 'Student Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Coordinator Settings',
  239:                                  col2 => '',},
  240:                                 {col1 => 'Author Settings',
  241:                                  col2 => '',},
  242:                                 {col1 => 'Administrator Settings',
  243:                                  col2 => '',}],
  244:                       print => \&print_rolecolors,
  245:                       modify => \&modify_rolecolors,
  246:                     },
  247:         'login' =>
  248:                     { text => 'Log-in page options',
  249:                       help => 'Domain_Configuration_Login_Page',
  250:                       header => [{col1 => 'Log-in Page Items',
  251:                                   col2 => '',},
  252:                                  {col1 => 'Log-in Help',
  253:                                   col2 => 'Value'}],
  254:                       print => \&print_login,
  255:                       modify => \&modify_login,
  256:                     },
  257:         'defaults' => 
  258:                     { text => 'Default authentication/language/timezone/portal/types',
  259:                       help => 'Domain_Configuration_LangTZAuth',
  260:                       header => [{col1 => 'Setting',
  261:                                   col2 => 'Value'},
  262:                                  {col1 => 'Institutional user types',
  263:                                   col2 => 'Assignable to e-mail usernames'}],
  264:                       print => \&print_defaults,
  265:                       modify => \&modify_defaults,
  266:                     },
  267:         'quotas' => 
  268:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  269:                       help => 'Domain_Configuration_Quotas',
  270:                       header => [{col1 => 'User affiliation',
  271:                                   col2 => 'Available tools',
  272:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  273:                       print => \&print_quotas,
  274:                       modify => \&modify_quotas,
  275:                     },
  276:         'autoenroll' =>
  277:                    { text => 'Auto-enrollment settings',
  278:                      help => 'Domain_Configuration_Auto_Enrollment',
  279:                      header => [{col1 => 'Configuration setting',
  280:                                  col2 => 'Value(s)'}],
  281:                      print => \&print_autoenroll,
  282:                      modify => \&modify_autoenroll,
  283:                    },
  284:         'autoupdate' => 
  285:                    { text => 'Auto-update settings',
  286:                      help => 'Domain_Configuration_Auto_Updates',
  287:                      header => [{col1 => 'Setting',
  288:                                  col2 => 'Value',},
  289:                                 {col1 => 'Setting',
  290:                                  col2 => 'Affiliation'},
  291:                                 {col1 => 'User population',
  292:                                  col2 => 'Updatable user data'}],
  293:                      print => \&print_autoupdate,
  294:                      modify => \&modify_autoupdate,
  295:                   },
  296:         'autocreate' => 
  297:                   { text => 'Auto-course creation settings',
  298:                      help => 'Domain_Configuration_Auto_Creation',
  299:                      header => [{col1 => 'Configuration Setting',
  300:                                  col2 => 'Value',}],
  301:                      print => \&print_autocreate,
  302:                      modify => \&modify_autocreate,
  303:                   },
  304:         'directorysrch' => 
  305:                   { text => 'Institutional directory searches',
  306:                     help => 'Domain_Configuration_InstDirectory_Search',
  307:                     header => [{col1 => 'Setting',
  308:                                 col2 => 'Value',}],
  309:                     print => \&print_directorysrch,
  310:                     modify => \&modify_directorysrch,
  311:                   },
  312:         'contacts' =>
  313:                   { text => 'Contact Information',
  314:                     help => 'Domain_Configuration_Contact_Info',
  315:                     header => [{col1 => 'Setting',
  316:                                 col2 => 'Value',}],
  317:                     print => \&print_contacts,
  318:                     modify => \&modify_contacts,
  319:                   },
  320:         'usercreation' => 
  321:                   { text => 'User creation',
  322:                     help => 'Domain_Configuration_User_Creation',
  323:                     header => [{col1 => 'Format rule type',
  324:                                 col2 => 'Format rules in force'},
  325:                                {col1 => 'User account creation',
  326:                                 col2 => 'Usernames which may be created',},
  327:                                {col1 => 'Context',
  328:                                 col2 => 'Assignable authentication types'}],
  329:                     print => \&print_usercreation,
  330:                     modify => \&modify_usercreation,
  331:                   },
  332:         'selfcreation' => 
  333:                   { text => 'Users self-creating accounts',
  334:                     help => 'Domain_Configuration_Self_Creation', 
  335:                     header => [{col1 => 'Self-creation with institutional username',
  336:                                 col2 => 'Enabled?'},
  337:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  338:                                 col2 => 'Information user can enter'},
  339:                                {col1 => 'Self-creation with e-mail as username',
  340:                                 col2 => 'Settings'}],
  341:                     print => \&print_selfcreation,
  342:                     modify => \&modify_selfcreation,
  343:                   },
  344:         'usermodification' =>
  345:                   { text => 'User modification',
  346:                     help => 'Domain_Configuration_User_Modification',
  347:                     header => [{col1 => 'Target user has role',
  348:                                 col2 => 'User information updatable in author context'},
  349:                                {col1 => 'Target user has role',
  350:                                 col2 => 'User information updatable in course context'}],
  351:                     print => \&print_usermodification,
  352:                     modify => \&modify_usermodification,
  353:                   },
  354:         'scantron' =>
  355:                   { text => 'Bubblesheet format file',
  356:                     help => 'Domain_Configuration_Scantron_Format',
  357:                     header => [ {col1 => 'Item',
  358:                                  col2 => '',
  359:                               }],
  360:                     print => \&print_scantron,
  361:                     modify => \&modify_scantron,
  362:                   },
  363:         'requestcourses' => 
  364:                  {text => 'Request creation of courses',
  365:                   help => 'Domain_Configuration_Request_Courses',
  366:                   header => [{col1 => 'User affiliation',
  367:                               col2 => 'Availability/Processing of requests',},
  368:                              {col1 => 'Setting',
  369:                               col2 => 'Value'},
  370:                              {col1 => 'Available textbooks',
  371:                               col2 => ''},
  372:                              {col1 => 'Available templates',
  373:                               col2 => ''},
  374:                              {col1 => 'Validation (not official courses)',
  375:                               col2 => 'Value'},],
  376:                   print => \&print_quotas,
  377:                   modify => \&modify_quotas,
  378:                  },
  379:         'requestauthor' =>
  380:                  {text => 'Request Authoring Space',
  381:                   help => 'Domain_Configuration_Request_Author',
  382:                   header => [{col1 => 'User affiliation',
  383:                               col2 => 'Availability/Processing of requests',},
  384:                              {col1 => 'Setting',
  385:                               col2 => 'Value'}],
  386:                   print => \&print_quotas,
  387:                   modify => \&modify_quotas,
  388:                  },
  389:         'coursecategories' =>
  390:                   { text => 'Cataloging of courses/communities',
  391:                     help => 'Domain_Configuration_Cataloging_Courses',
  392:                     header => [{col1 => 'Catalog type/availability',
  393:                                 col2 => '',},
  394:                                {col1 => 'Category settings for standard catalog',
  395:                                 col2 => '',},
  396:                                {col1 => 'Categories',
  397:                                 col2 => '',
  398:                                }],
  399:                     print => \&print_coursecategories,
  400:                     modify => \&modify_coursecategories,
  401:                   },
  402:         'serverstatuses' =>
  403:                  {text   => 'Access to server status pages',
  404:                   help   => 'Domain_Configuration_Server_Status',
  405:                   header => [{col1 => 'Status Page',
  406:                               col2 => 'Other named users',
  407:                               col3 => 'Specific IPs',
  408:                             }],
  409:                   print => \&print_serverstatuses,
  410:                   modify => \&modify_serverstatuses,
  411:                  },
  412:         'helpsettings' =>
  413:                  {text   => 'Help page settings',
  414:                   help   => 'Domain_Configuration_Help_Settings',
  415:                   header => [{col1 => 'Help Settings (logged-in users)',
  416:                               col2 => 'Value'}],
  417:                   print  => \&print_helpsettings,
  418:                   modify => \&modify_helpsettings,
  419:                  },
  420:         'coursedefaults' => 
  421:                  {text => 'Course/Community defaults',
  422:                   help => 'Domain_Configuration_Course_Defaults',
  423:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  424:                               col2 => 'Value',},
  425:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  426:                               col2 => 'Value',},],
  427:                   print => \&print_coursedefaults,
  428:                   modify => \&modify_coursedefaults,
  429:                  },
  430:         'selfenrollment' => 
  431:                  {text   => 'Self-enrollment in Course/Community',
  432:                   help   => 'Domain_Configuration_Selfenrollment',
  433:                   header => [{col1 => 'Configuration Rights',
  434:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  435:                              {col1 => 'Defaults',
  436:                               col2 => 'Value'},
  437:                              {col1 => 'Self-enrollment validation (optional)',
  438:                               col2 => 'Value'},],
  439:                   print => \&print_selfenrollment,
  440:                   modify => \&modify_selfenrollment,
  441:                  },
  442:         'privacy' => 
  443:                  {text   => 'User Privacy',
  444:                   help   => 'Domain_Configuration_User_Privacy',
  445:                   header => [{col1 => 'Setting',
  446:                               col2 => 'Value',}],
  447:                   print => \&print_privacy,
  448:                   modify => \&modify_privacy,
  449:                  },
  450:         'usersessions' =>
  451:                  {text  => 'User session hosting/offloading',
  452:                   help  => 'Domain_Configuration_User_Sessions',
  453:                   header => [{col1 => 'Domain server',
  454:                               col2 => 'Servers to offload sessions to when busy'},
  455:                              {col1 => 'Hosting of users from other domains',
  456:                               col2 => 'Rules'},
  457:                              {col1 => "Hosting domain's own users elsewhere",
  458:                               col2 => 'Rules'}],
  459:                   print => \&print_usersessions,
  460:                   modify => \&modify_usersessions,
  461:                  },
  462:          'loadbalancing' =>
  463:                  {text  => 'Dedicated Load Balancer(s)',
  464:                   help  => 'Domain_Configuration_Load_Balancing',
  465:                   header => [{col1 => 'Balancers',
  466:                               col2 => 'Default destinations',
  467:                               col3 => 'User affiliation',
  468:                               col4 => 'Overrides'},
  469:                             ],
  470:                   print => \&print_loadbalancing,
  471:                   modify => \&modify_loadbalancing,
  472:                  },
  473:     );
  474:     if (keys(%servers) > 1) {
  475:         $prefs{'login'}  = { text   => 'Log-in page options',
  476:                              help   => 'Domain_Configuration_Login_Page',
  477:                             header => [{col1 => 'Log-in Service',
  478:                                         col2 => 'Server Setting',},
  479:                                        {col1 => 'Log-in Page Items',
  480:                                         col2 => ''},
  481:                                        {col1 => 'Log-in Help',
  482:                                         col2 => 'Value'}],
  483:                             print => \&print_login,
  484:                             modify => \&modify_login,
  485:                            };
  486:     }
  487: 
  488:     my @roles = ('student','coordinator','author','admin');
  489:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  490:     &Apache::lonhtmlcommon::add_breadcrumb
  491:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  492:       text=>"Settings to display/modify"});
  493:     my $confname = $dom.'-domainconfig';
  494: 
  495:     if ($phase eq 'process') {
  496:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  497:                                                               \%prefs,\%domconfig,$confname,\@roles);
  498:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  499:             $r->rflush();
  500:             &devalidate_remote_domconfs($dom,$result);
  501:         }
  502:     } elsif ($phase eq 'display') {
  503:         my $js = &recaptcha_js().
  504:                  &toggle_display_js();
  505:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  506:             my ($othertitle,$usertypes,$types) =
  507:                 &Apache::loncommon::sorted_inst_types($dom);
  508:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  509:                                           $domconfig{'loadbalancing'}).
  510:                    &new_spares_js().
  511:                    &common_domprefs_js().
  512:                    &Apache::loncommon::javascript_array_indexof();
  513:         }
  514:         if (grep(/^requestcourses$/,@actions)) {
  515:             my $javascript_validations;
  516:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  517:             $js .= <<END;
  518: <script type="text/javascript">
  519: $javascript_validations
  520: </script>
  521: $coursebrowserjs
  522: END
  523:         }
  524:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  525:     } else {
  526: # check if domconfig user exists for the domain.
  527:         my $servadm = $r->dir_config('lonAdmEMail');
  528:         my ($configuserok,$author_ok,$switchserver) =
  529:             &config_check($dom,$confname,$servadm);
  530:         unless ($configuserok eq 'ok') {
  531:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  532:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  533:                           $confname).
  534:                       '<br />'
  535:             );
  536:             if ($switchserver) {
  537:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  538:                           '<br />'.
  539:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  540:                           '<br />'.
  541:                           &mt('The "[_1]" user can be created automatically when a Domain Coordinator visits the web-based "Set domain configuration" screen, in a session hosted on the primary library server.',$confname).
  542:                           '<br />'.
  543:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  544:                 );
  545:             } else {
  546:                 $r->print(&mt('To create that user from the command line run the ./UPDATE script found in the top level directory of the extracted LON-CAPA tarball.').
  547:                           '<br />'.
  548:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  549:                 );
  550:             }
  551:             $r->print(&Apache::loncommon::end_page());
  552:             return OK;
  553:         }
  554:         if (keys(%domconfig) == 0) {
  555:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  556:             my @ids=&Apache::lonnet::current_machine_ids();
  557:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  558:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  559:                 my @loginimages = ('img','logo','domlogo','login');
  560:                 my $custom_img_count = 0;
  561:                 foreach my $img (@loginimages) {
  562:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  563:                         $custom_img_count ++;
  564:                     }
  565:                 }
  566:                 foreach my $role (@roles) {
  567:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  568:                         $custom_img_count ++;
  569:                     }
  570:                 }
  571:                 if ($custom_img_count > 0) {
  572:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  573:                     my $switch_server = &check_switchserver($dom,$confname);
  574:                     $r->print(
  575:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  576:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  577:     &mt("Thereafter, (with a Domain Coordinator role selected in the domain) you will be able to update settings when logged in to any server in the LON-CAPA network.").'<br />'.
  578:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  579:                     if ($switch_server) {
  580:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  581:                     }
  582:                     $r->print(&Apache::loncommon::end_page());
  583:                     return OK;
  584:                 }
  585:             }
  586:         }
  587:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  588:     }
  589:     return OK;
  590: }
  591: 
  592: sub process_changes {
  593:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  594:     my %domconfig;
  595:     if (ref($values) eq 'HASH') {
  596:         %domconfig = %{$values};
  597:     }
  598:     my $output;
  599:     if ($action eq 'login') {
  600:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  601:     } elsif ($action eq 'rolecolors') {
  602:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  603:                                      $lastactref,%domconfig);
  604:     } elsif ($action eq 'quotas') {
  605:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  606:     } elsif ($action eq 'autoenroll') {
  607:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  608:     } elsif ($action eq 'autoupdate') {
  609:         $output = &modify_autoupdate($dom,%domconfig);
  610:     } elsif ($action eq 'autocreate') {
  611:         $output = &modify_autocreate($dom,%domconfig);
  612:     } elsif ($action eq 'directorysrch') {
  613:         $output = &modify_directorysrch($dom,%domconfig);
  614:     } elsif ($action eq 'usercreation') {
  615:         $output = &modify_usercreation($dom,%domconfig);
  616:     } elsif ($action eq 'selfcreation') {
  617:         $output = &modify_selfcreation($dom,%domconfig);
  618:     } elsif ($action eq 'usermodification') {
  619:         $output = &modify_usermodification($dom,%domconfig);
  620:     } elsif ($action eq 'contacts') {
  621:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  622:     } elsif ($action eq 'defaults') {
  623:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  624:     } elsif ($action eq 'scantron') {
  625:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  626:     } elsif ($action eq 'coursecategories') {
  627:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  628:     } elsif ($action eq 'serverstatuses') {
  629:         $output = &modify_serverstatuses($dom,%domconfig);
  630:     } elsif ($action eq 'requestcourses') {
  631:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  632:     } elsif ($action eq 'requestauthor') {
  633:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  634:     } elsif ($action eq 'helpsettings') {
  635:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  636:     } elsif ($action eq 'coursedefaults') {
  637:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  638:     } elsif ($action eq 'selfenrollment') {
  639:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  640:     } elsif ($action eq 'usersessions') {
  641:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  642:     } elsif ($action eq 'loadbalancing') {
  643:         $output = &modify_loadbalancing($dom,%domconfig);
  644:     }
  645:     return $output;
  646: }
  647: 
  648: sub print_config_box {
  649:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  650:     my $rowtotal = 0;
  651:     my $output;
  652:     if ($action eq 'coursecategories') {
  653:         $output = &coursecategories_javascript($settings);
  654:     } elsif ($action eq 'defaults') {
  655:         $output = &defaults_javascript($settings); 
  656:     }
  657:     $output .=
  658:          '<table class="LC_nested_outer">
  659:           <tr>
  660:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  661:            &mt($item->{text}).'&nbsp;'.
  662:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  663:           '</tr>';
  664:     $rowtotal ++;
  665:     my $numheaders = 1;
  666:     if (ref($item->{'header'}) eq 'ARRAY') {
  667:         $numheaders = scalar(@{$item->{'header'}});
  668:     }
  669:     if ($numheaders > 1) {
  670:         my $colspan = '';
  671:         my $rightcolspan = '';
  672:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  673:             (($action eq 'login') && ($numheaders < 3))) {
  674:             $colspan = ' colspan="2"';
  675:         }
  676:         if ($action eq 'usersessions') {
  677:             $rightcolspan = ' colspan="3"'; 
  678:         }
  679:         $output .= '
  680:           <tr>
  681:            <td>
  682:             <table class="LC_nested">
  683:              <tr class="LC_info_row">
  684:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  685:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  686:              </tr>';
  687:         $rowtotal ++;
  688:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  689:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  690:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
  691:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  692:         } elsif ($action eq 'coursecategories') {
  693:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  694:         } elsif ($action eq 'login') {
  695:             if ($numheaders == 3) {
  696:                 $colspan = ' colspan="2"';
  697:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  698:             } else {
  699:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  700:             }
  701:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  702:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  703:         } elsif ($action eq 'rolecolors') {
  704:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  705:         }
  706:         $output .= '
  707:            </table>
  708:           </td>
  709:          </tr>
  710:          <tr>
  711:            <td>
  712:             <table class="LC_nested">
  713:              <tr class="LC_info_row">
  714:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  715:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  716:              </tr>';
  717:             $rowtotal ++;
  718:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  719:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  720:             ($action eq 'usersessions') || ($action eq 'coursecategories')) {
  721:             if ($action eq 'coursecategories') {
  722:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  723:                 $colspan = ' colspan="2"';
  724:             } else {
  725:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  726:             }
  727:             $output .= '
  728:            </table>
  729:           </td>
  730:          </tr>
  731:          <tr>
  732:            <td>
  733:             <table class="LC_nested">
  734:              <tr class="LC_info_row">
  735:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  736:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  737:              </tr>'."\n";
  738:             if ($action eq 'coursecategories') {
  739:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  740:             } else {
  741:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  742:             }
  743:             $rowtotal ++;
  744:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  745:                   ($action eq 'defaults')) {
  746:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  747:         } elsif ($action eq 'login') {
  748:             if ($numheaders == 3) {
  749:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  750:            </table>
  751:           </td>
  752:          </tr>
  753:          <tr>
  754:            <td>
  755:             <table class="LC_nested">
  756:              <tr class="LC_info_row">
  757:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  758:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  759:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  760:                 $rowtotal ++;
  761:             } else {
  762:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  763:             }
  764:         } elsif ($action eq 'requestcourses') {
  765:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal).
  766:                        &print_studentcode($settings,\$rowtotal).'
  767:            </table>
  768:           </td>
  769:          </tr>
  770:          <tr>
  771:            <td>
  772:             <table class="LC_nested">
  773:              <tr class="LC_info_row">
  774:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  775:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  776:                        &textbookcourses_javascript($settings).
  777:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  778:             </table>
  779:            </td>
  780:           </tr>
  781:          <tr>
  782:            <td>
  783:             <table class="LC_nested">
  784:              <tr class="LC_info_row">
  785:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  786:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  787:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  788:             </table>
  789:            </td>
  790:           </tr>
  791:           <tr>
  792:            <td>
  793:             <table class="LC_nested">
  794:              <tr class="LC_info_row">
  795:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  796:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  797:              </tr>'.
  798:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  799:         } elsif ($action eq 'requestauthor') {
  800:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  801:         } elsif ($action eq 'rolecolors') {
  802:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  803:            </table>
  804:           </td>
  805:          </tr>
  806:          <tr>
  807:            <td>
  808:             <table class="LC_nested">
  809:              <tr class="LC_info_row">
  810:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  811:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  812:               <td class="LC_right_item" valign="top">'.
  813:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  814:              </tr>'.
  815:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  816:            </table>
  817:           </td>
  818:          </tr>
  819:          <tr>
  820:            <td>
  821:             <table class="LC_nested">
  822:              <tr class="LC_info_row">
  823:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  824:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  825:              </tr>'.
  826:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  827:             $rowtotal += 2;
  828:         }
  829:     } else {
  830:         $output .= '
  831:           <tr>
  832:            <td>
  833:             <table class="LC_nested">
  834:              <tr class="LC_info_row">';
  835:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  836:             $output .= '  
  837:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  838:         } elsif ($action eq 'serverstatuses') {
  839:             $output .= '
  840:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  841:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  842: 
  843:         } else {
  844:             $output .= '
  845:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  846:         }
  847:         if (defined($item->{'header'}->[0]->{'col3'})) {
  848:             $output .= '<td class="LC_left_item" valign="top">'.
  849:                        &mt($item->{'header'}->[0]->{'col2'});
  850:             if ($action eq 'serverstatuses') {
  851:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  852:             } 
  853:         } else {
  854:             $output .= '<td class="LC_right_item" valign="top">'.
  855:                        &mt($item->{'header'}->[0]->{'col2'});
  856:         }
  857:         $output .= '</td>';
  858:         if ($item->{'header'}->[0]->{'col3'}) {
  859:             if (defined($item->{'header'}->[0]->{'col4'})) {
  860:                 $output .= '<td class="LC_left_item" valign="top">'.
  861:                             &mt($item->{'header'}->[0]->{'col3'});
  862:             } else {
  863:                 $output .= '<td class="LC_right_item" valign="top">'.
  864:                            &mt($item->{'header'}->[0]->{'col3'});
  865:             }
  866:             if ($action eq 'serverstatuses') {
  867:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  868:             }
  869:             $output .= '</td>';
  870:         }
  871:         if ($item->{'header'}->[0]->{'col4'}) {
  872:             $output .= '<td class="LC_right_item" valign="top">'.
  873:                        &mt($item->{'header'}->[0]->{'col4'});
  874:         }
  875:         $output .= '</tr>';
  876:         $rowtotal ++;
  877:         if ($action eq 'quotas') {
  878:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  879:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  880:                  ($action eq 'contacts') || ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  881:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  882:         } elsif ($action eq 'scantron') {
  883:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  884:         } elsif ($action eq 'helpsettings') {
  885:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  886:         }
  887:     }
  888:     $output .= '
  889:    </table>
  890:   </td>
  891:  </tr>
  892: </table><br />';
  893:     return ($output,$rowtotal);
  894: }
  895: 
  896: sub print_login {
  897:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  898:     my ($css_class,$datatable);
  899:     my %choices = &login_choices();
  900: 
  901:     if ($caller eq 'service') {
  902:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  903:         my $choice = $choices{'disallowlogin'};
  904:         $css_class = ' class="LC_odd_row"';
  905:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  906:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  907:                       '<th>'.$choices{'server'}.'</th>'.
  908:                       '<th>'.$choices{'serverpath'}.'</th>'.
  909:                       '<th>'.$choices{'custompath'}.'</th>'.
  910:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  911:         my %disallowed;
  912:         if (ref($settings) eq 'HASH') {
  913:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  914:                %disallowed = %{$settings->{'loginvia'}};
  915:             }
  916:         }
  917:         foreach my $lonhost (sort(keys(%servers))) {
  918:             my $direct = 'selected="selected"';
  919:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  920:                 if ($disallowed{$lonhost}{'server'} ne '') {
  921:                     $direct = '';
  922:                 }
  923:             }
  924:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  925:                           '<td><select name="'.$lonhost.'_server">'.
  926:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  927:                           '</option>';
  928:             foreach my $hostid (sort(keys(%servers))) {
  929:                 next if ($servers{$hostid} eq $servers{$lonhost});
  930:                 my $selected = '';
  931:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  932:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  933:                         $selected = 'selected="selected"';
  934:                     }
  935:                 }
  936:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  937:                               $servers{$hostid}.'</option>';
  938:             }
  939:             $datatable .= '</select></td>'.
  940:                           '<td><select name="'.$lonhost.'_serverpath">';
  941:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  942:                 my $pathname = $path;
  943:                 if ($path eq 'custom') {
  944:                     $pathname = &mt('Custom Path').' ->';
  945:                 }
  946:                 my $selected = '';
  947:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  948:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  949:                         $selected = 'selected="selected"';
  950:                     }
  951:                 } elsif ($path eq '') {
  952:                     $selected = 'selected="selected"';
  953:                 }
  954:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  955:             }
  956:             $datatable .= '</select></td>';
  957:             my ($custom,$exempt);
  958:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  959:                 $custom = $disallowed{$lonhost}{'custompath'};
  960:                 $exempt = $disallowed{$lonhost}{'exempt'};
  961:             }
  962:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  963:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  964:                           '</tr>';
  965:         }
  966:         $datatable .= '</table></td></tr>';
  967:         return $datatable;
  968:     } elsif ($caller eq 'page') {
  969:         my %defaultchecked = ( 
  970:                                'coursecatalog' => 'on',
  971:                                'helpdesk'      => 'on',
  972:                                'adminmail'     => 'off',
  973:                                'newuser'       => 'off',
  974:                              );
  975:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  976:         my (%checkedon,%checkedoff);
  977:         foreach my $item (@toggles) {
  978:             if ($defaultchecked{$item} eq 'on') { 
  979:                 $checkedon{$item} = ' checked="checked" ';
  980:                 $checkedoff{$item} = ' ';
  981:             } elsif ($defaultchecked{$item} eq 'off') {
  982:                 $checkedoff{$item} = ' checked="checked" ';
  983:                 $checkedon{$item} = ' ';
  984:             }
  985:         }
  986:         my @images = ('img','logo','domlogo','login');
  987:         my @logintext = ('textcol','bgcol');
  988:         my @bgs = ('pgbg','mainbg','sidebg');
  989:         my @links = ('link','alink','vlink');
  990:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  991:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  992:         my (%is_custom,%designs);
  993:         my %defaults = (
  994:                        font => $defaultdesign{'login.font'},
  995:                        );
  996:         foreach my $item (@images) {
  997:             $defaults{$item} = $defaultdesign{'login.'.$item};
  998:             $defaults{'showlogo'}{$item} = 1;
  999:         }
 1000:         foreach my $item (@bgs) {
 1001:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1002:         }
 1003:         foreach my $item (@logintext) {
 1004:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1005:         }
 1006:         foreach my $item (@links) {
 1007:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1008:         }
 1009:         if (ref($settings) eq 'HASH') {
 1010:             foreach my $item (@toggles) {
 1011:                 if ($settings->{$item} eq '1') {
 1012:                     $checkedon{$item} =  ' checked="checked" ';
 1013:                     $checkedoff{$item} = ' ';
 1014:                 } elsif ($settings->{$item} eq '0') {
 1015:                     $checkedoff{$item} =  ' checked="checked" ';
 1016:                     $checkedon{$item} = ' ';
 1017:                 }
 1018:             }
 1019:             foreach my $item (@images) {
 1020:                 if (defined($settings->{$item})) {
 1021:                     $designs{$item} = $settings->{$item};
 1022:                     $is_custom{$item} = 1;
 1023:                 }
 1024:                 if (defined($settings->{'showlogo'}{$item})) {
 1025:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1026:                 }
 1027:             }
 1028:             foreach my $item (@logintext) {
 1029:                 if ($settings->{$item} ne '') {
 1030:                     $designs{'logintext'}{$item} = $settings->{$item};
 1031:                     $is_custom{$item} = 1;
 1032:                 }
 1033:             }
 1034:             if ($settings->{'font'} ne '') {
 1035:                 $designs{'font'} = $settings->{'font'};
 1036:                 $is_custom{'font'} = 1;
 1037:             }
 1038:             foreach my $item (@bgs) {
 1039:                 if ($settings->{$item} ne '') {
 1040:                     $designs{'bgs'}{$item} = $settings->{$item};
 1041:                     $is_custom{$item} = 1;
 1042:                 }
 1043:             }
 1044:             foreach my $item (@links) {
 1045:                 if ($settings->{$item} ne '') {
 1046:                     $designs{'links'}{$item} = $settings->{$item};
 1047:                     $is_custom{$item} = 1;
 1048:                 }
 1049:             }
 1050:         } else {
 1051:             if ($designhash{$dom.'.login.font'} ne '') {
 1052:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1053:                 $is_custom{'font'} = 1;
 1054:             }
 1055:             foreach my $item (@images) {
 1056:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1057:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1058:                     $is_custom{$item} = 1;
 1059:                 }
 1060:             }
 1061:             foreach my $item (@bgs) {
 1062:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1063:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1064:                     $is_custom{$item} = 1;
 1065:                 }
 1066:             }
 1067:             foreach my $item (@links) {
 1068:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1069:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1070:                     $is_custom{$item} = 1;
 1071:                 }
 1072:             }
 1073:         }
 1074:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1075:                                                       logo => 'Institution Logo',
 1076:                                                       domlogo => 'Domain Logo',
 1077:                                                       login => 'Login box');
 1078:         my $itemcount = 1;
 1079:         foreach my $item (@toggles) {
 1080:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1081:             $datatable .=  
 1082:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1083:                 '</td><td>'.
 1084:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1085:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1086:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1087:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1088:                 '</tr>';
 1089:             $itemcount ++;
 1090:         }
 1091:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1092:         $datatable .= '</tr></table></td></tr>';
 1093:     } elsif ($caller eq 'help') {
 1094:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1095:         my $switchserver = &check_switchserver($dom,$confname);
 1096:         my $itemcount = 1;
 1097:         $defaulturl = '/adm/loginproblems.html';
 1098:         $defaulttype = 'default';
 1099:         %lt = &Apache::lonlocal::texthash (
 1100:                      del     => 'Delete?',
 1101:                      rep     => 'Replace:',
 1102:                      upl     => 'Upload:',
 1103:                      default => 'Default',
 1104:                      custom  => 'Custom',
 1105:                                              );
 1106:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1107:         my @currlangs;
 1108:         if (ref($settings) eq 'HASH') {
 1109:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1110:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1111:                     next if ($settings->{'helpurl'}{$key} eq '');
 1112:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1113:                     $type{$key} = 'custom';
 1114:                     unless ($key eq 'nolang') {
 1115:                         push(@currlangs,$key);
 1116:                     }
 1117:                 }
 1118:             } elsif ($settings->{'helpurl'} ne '') {
 1119:                 $type{'nolang'} = 'custom';
 1120:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1121:             }
 1122:         }
 1123:         foreach my $lang ('nolang',sort(@currlangs)) {
 1124:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1125:             $datatable .= '<tr'.$css_class.'>';
 1126:             if ($url{$lang} eq '') {
 1127:                 $url{$lang} = $defaulturl;
 1128:             }
 1129:             if ($type{$lang} eq '') {
 1130:                 $type{$lang} = $defaulttype;
 1131:             }
 1132:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1133:             if ($lang eq 'nolang') {
 1134:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1135:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1136:             } else {
 1137:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1138:                                   $langchoices{$lang},
 1139:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1140:             }
 1141:             $datatable .= '</span></td>'."\n".
 1142:                           '<td class="LC_left_item">';
 1143:             if ($type{$lang} eq 'custom') {
 1144:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1145:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1146:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1147:             } else {
 1148:                 $datatable .= $lt{'upl'};
 1149:             }
 1150:             $datatable .='<br />';
 1151:             if ($switchserver) {
 1152:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1153:             } else {
 1154:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1155:             }
 1156:             $datatable .= '</td></tr>';
 1157:             $itemcount ++;
 1158:         }
 1159:         my @addlangs;
 1160:         foreach my $lang (sort(keys(%langchoices))) {
 1161:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1162:             push(@addlangs,$lang);
 1163:         }
 1164:         if (@addlangs > 0) {
 1165:             my %toadd;
 1166:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1167:             $toadd{''} = &mt('Select');
 1168:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1169:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1170:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1171:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1172:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1173:             if ($switchserver) {
 1174:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1175:             } else {
 1176:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1177:             }
 1178:             $datatable .= '</td></tr>';
 1179:             $itemcount ++;
 1180:         }
 1181:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1182:     }
 1183:     return $datatable;
 1184: }
 1185: 
 1186: sub login_choices {
 1187:     my %choices =
 1188:         &Apache::lonlocal::texthash (
 1189:             coursecatalog => 'Display Course/Community Catalog link?',
 1190:             adminmail     => "Display Administrator's E-mail Address?",
 1191:             helpdesk      => 'Display "Contact Helpdesk" link',
 1192:             disallowlogin => "Login page requests redirected",
 1193:             hostid        => "Server",
 1194:             server        => "Redirect to:",
 1195:             serverpath    => "Path",
 1196:             custompath    => "Custom", 
 1197:             exempt        => "Exempt IP(s)",
 1198:             directlogin   => "No redirect",
 1199:             newuser       => "Link to create a user account",
 1200:             img           => "Header",
 1201:             logo          => "Main Logo",
 1202:             domlogo       => "Domain Logo",
 1203:             login         => "Log-in Header", 
 1204:             textcol       => "Text color",
 1205:             bgcol         => "Box color",
 1206:             bgs           => "Background colors",
 1207:             links         => "Link colors",
 1208:             font          => "Font color",
 1209:             pgbg          => "Header",
 1210:             mainbg        => "Page",
 1211:             sidebg        => "Login box",
 1212:             link          => "Link",
 1213:             alink         => "Active link",
 1214:             vlink         => "Visited link",
 1215:         );
 1216:     return %choices;
 1217: }
 1218: 
 1219: sub print_rolecolors {
 1220:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1221:     my %choices = &color_font_choices();
 1222:     my @bgs = ('pgbg','tabbg','sidebg');
 1223:     my @links = ('link','alink','vlink');
 1224:     my @images = ('img');
 1225:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1226:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1227:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1228:     my (%is_custom,%designs);
 1229:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1230:     if (ref($settings) eq 'HASH') {
 1231:         if (ref($settings->{$role}) eq 'HASH') {
 1232:             if ($settings->{$role}->{'img'} ne '') {
 1233:                 $designs{'img'} = $settings->{$role}->{'img'};
 1234:                 $is_custom{'img'} = 1;
 1235:             }
 1236:             if ($settings->{$role}->{'font'} ne '') {
 1237:                 $designs{'font'} = $settings->{$role}->{'font'};
 1238:                 $is_custom{'font'} = 1;
 1239:             }
 1240:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1241:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1242:                 $is_custom{'fontmenu'} = 1;
 1243:             }
 1244:             foreach my $item (@bgs) {
 1245:                 if ($settings->{$role}->{$item} ne '') {
 1246:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1247:                     $is_custom{$item} = 1;
 1248:                 }
 1249:             }
 1250:             foreach my $item (@links) {
 1251:                 if ($settings->{$role}->{$item} ne '') {
 1252:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1253:                     $is_custom{$item} = 1;
 1254:                 }
 1255:             }
 1256:         }
 1257:     } else {
 1258:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1259:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1260:             $is_custom{'img'} = 1;
 1261:         }
 1262:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1263:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1264:             $is_custom{'fontmenu'} = 1; 
 1265:         }
 1266:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1267:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1268:             $is_custom{'font'} = 1;
 1269:         }
 1270:         foreach my $item (@bgs) {
 1271:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1272:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1273:                 $is_custom{$item} = 1;
 1274:             
 1275:             }
 1276:         }
 1277:         foreach my $item (@links) {
 1278:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1279:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1280:                 $is_custom{$item} = 1;
 1281:             }
 1282:         }
 1283:     }
 1284:     my $itemcount = 1;
 1285:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1286:     $datatable .= '</tr></table></td></tr>';
 1287:     return $datatable;
 1288: }
 1289: 
 1290: sub role_defaults {
 1291:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1292:     my %defaults;
 1293:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1294:         return %defaults;
 1295:     }
 1296:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1297:     if ($role eq 'login') {
 1298:         %defaults = (
 1299:                        font => $defaultdesign{$role.'.font'},
 1300:                     );
 1301:         if (ref($logintext) eq 'ARRAY') {
 1302:             foreach my $item (@{$logintext}) {
 1303:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1304:             }
 1305:         }
 1306:         foreach my $item (@{$images}) {
 1307:             $defaults{'showlogo'}{$item} = 1;
 1308:         }
 1309:     } else {
 1310:         %defaults = (
 1311:                        img => $defaultdesign{$role.'.img'},
 1312:                        font => $defaultdesign{$role.'.font'},
 1313:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1314:                     );
 1315:     }
 1316:     foreach my $item (@{$bgs}) {
 1317:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1318:     }
 1319:     foreach my $item (@{$links}) {
 1320:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1321:     }
 1322:     foreach my $item (@{$images}) {
 1323:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1324:     }
 1325:     return %defaults;
 1326: }
 1327: 
 1328: sub display_color_options {
 1329:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1330:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1331:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1332:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1333:     my $datatable = '<tr'.$css_class.'>'.
 1334:         '<td>'.$choices->{'font'}.'</td>';
 1335:     if (!$is_custom->{'font'}) {
 1336:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1337:     } else {
 1338:         $datatable .= '<td>&nbsp;</td>';
 1339:     }
 1340:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1341: 
 1342:     $datatable .= '<td><span class="LC_nobreak">'.
 1343:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1344:                   ' value="'.$current_color.'" />&nbsp;'.
 1345:                   '&nbsp;</td></tr>';
 1346:     unless ($role eq 'login') { 
 1347:         $datatable .= '<tr'.$css_class.'>'.
 1348:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1349:         if (!$is_custom->{'fontmenu'}) {
 1350:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1351:         } else {
 1352:             $datatable .= '<td>&nbsp;</td>';
 1353:         }
 1354: 	$current_color = $designs->{'fontmenu'} ?
 1355: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1356:         $datatable .= '<td><span class="LC_nobreak">'.
 1357:                       '<input class="colorchooser" type="text" size="10" name="'
 1358: 		      .$role.'_fontmenu"'.
 1359:                       ' value="'.$current_color.'" />&nbsp;'.
 1360:                       '&nbsp;</td></tr>';
 1361:     }
 1362:     my $switchserver = &check_switchserver($dom,$confname);
 1363:     foreach my $img (@{$images}) {
 1364: 	$itemcount ++;
 1365:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1366:         $datatable .= '<tr'.$css_class.'>'.
 1367:                       '<td>'.$choices->{$img};
 1368:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1369:         if ($role eq 'login') {
 1370:             if ($img eq 'login') {
 1371:                 $login_hdr_pick =
 1372:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1373:                 $logincolors =
 1374:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1375:                                        $designs,$defaults);
 1376:             } elsif ($img ne 'domlogo') {
 1377:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1378:             }
 1379:         }
 1380:         $datatable .= '</td>';
 1381:         if ($designs->{$img} ne '') {
 1382:             $imgfile = $designs->{$img};
 1383: 	    $img_import = ($imgfile =~ m{^/adm/});
 1384:         } else {
 1385:             $imgfile = $defaults->{$img};
 1386:         }
 1387:         if ($imgfile) {
 1388:             my ($showfile,$fullsize);
 1389:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1390:                 my $urldir = $1;
 1391:                 my $filename = $2;
 1392:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1393:                 if (@info) {
 1394:                     my $thumbfile = 'tn-'.$filename;
 1395:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1396:                     if (@thumb) {
 1397:                         $showfile = $urldir.'/'.$thumbfile;
 1398:                     } else {
 1399:                         $showfile = $imgfile;
 1400:                     }
 1401:                 } else {
 1402:                     $showfile = '';
 1403:                 }
 1404:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1405:                 $showfile = $imgfile;
 1406:                 my $imgdir = $1;
 1407:                 my $filename = $2;
 1408:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1409:                     $showfile = "/$imgdir/tn-".$filename;
 1410:                 } else {
 1411:                     my $input = $londocroot.$imgfile;
 1412:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1413:                     if (!-e $output) {
 1414:                         my ($width,$height) = &thumb_dimensions();
 1415:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1416:                         if ($fullwidth ne '' && $fullheight ne '') {
 1417:                             if ($fullwidth > $width && $fullheight > $height) { 
 1418:                                 my $size = $width.'x'.$height;
 1419:                                 system("convert -sample $size $input $output");
 1420:                                 $showfile = "/$imgdir/tn-".$filename;
 1421:                             }
 1422:                         }
 1423:                     }
 1424:                 }
 1425:             }
 1426:             if ($showfile) {
 1427:                 if ($showfile =~ m{^/(adm|res)/}) {
 1428:                     if ($showfile =~ m{^/res/}) {
 1429:                         my $local_showfile =
 1430:                             &Apache::lonnet::filelocation('',$showfile);
 1431:                         &Apache::lonnet::repcopy($local_showfile);
 1432:                     }
 1433:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1434:                 }
 1435:                 if ($imgfile) {
 1436:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1437:                         if ($imgfile =~ m{^/res/}) {
 1438:                             my $local_imgfile =
 1439:                                 &Apache::lonnet::filelocation('',$imgfile);
 1440:                             &Apache::lonnet::repcopy($local_imgfile);
 1441:                         }
 1442:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1443:                     } else {
 1444:                         $fullsize = $imgfile;
 1445:                     }
 1446:                 }
 1447:                 $datatable .= '<td>';
 1448:                 if ($img eq 'login') {
 1449:                     $datatable .= $login_hdr_pick;
 1450:                 } 
 1451:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1452:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1453:             } else {
 1454:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1455:                               &mt('Upload:').'<br />';
 1456:             }
 1457:         } else {
 1458:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1459:                           &mt('Upload:').'<br />';
 1460:         }
 1461:         if ($switchserver) {
 1462:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1463:         } else {
 1464:             if ($img ne 'login') { # suppress file selection for Log-in header
 1465:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1466:             }
 1467:         }
 1468:         $datatable .= '</td></tr>';
 1469:     }
 1470:     $itemcount ++;
 1471:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1472:     $datatable .= '<tr'.$css_class.'>'.
 1473:                   '<td>'.$choices->{'bgs'}.'</td>';
 1474:     my $bgs_def;
 1475:     foreach my $item (@{$bgs}) {
 1476:         if (!$is_custom->{$item}) {
 1477:             $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>';
 1478:         }
 1479:     }
 1480:     if ($bgs_def) {
 1481:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1482:     } else {
 1483:         $datatable .= '<td>&nbsp;</td>';
 1484:     }
 1485:     $datatable .= '<td class="LC_right_item">'.
 1486:                   '<table border="0"><tr>';
 1487: 
 1488:     foreach my $item (@{$bgs}) {
 1489:         $datatable .= '<td align="center">'.$choices->{$item};
 1490: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1491:         if ($designs->{'bgs'}{$item}) {
 1492:             $datatable .= '&nbsp;';
 1493:         }
 1494:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1495:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1496:     }
 1497:     $datatable .= '</tr></table></td></tr>';
 1498:     $itemcount ++;
 1499:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1500:     $datatable .= '<tr'.$css_class.'>'.
 1501:                   '<td>'.$choices->{'links'}.'</td>';
 1502:     my $links_def;
 1503:     foreach my $item (@{$links}) {
 1504:         if (!$is_custom->{$item}) {
 1505:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1506:         }
 1507:     }
 1508:     if ($links_def) {
 1509:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1510:     } else {
 1511:         $datatable .= '<td>&nbsp;</td>';
 1512:     }
 1513:     $datatable .= '<td class="LC_right_item">'.
 1514:                   '<table border="0"><tr>';
 1515:     foreach my $item (@{$links}) {
 1516: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1517:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1518:         if ($designs->{'links'}{$item}) {
 1519:             $datatable.='&nbsp;';
 1520:         }
 1521:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1522:                       '" /></td>';
 1523:     }
 1524:     $$rowtotal += $itemcount;
 1525:     return $datatable;
 1526: }
 1527: 
 1528: sub logo_display_options {
 1529:     my ($img,$defaults,$designs) = @_;
 1530:     my $checkedon;
 1531:     if (ref($defaults) eq 'HASH') {
 1532:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1533:             if ($defaults->{'showlogo'}{$img}) {
 1534:                 $checkedon = 'checked="checked" ';     
 1535:             }
 1536:         } 
 1537:     }
 1538:     if (ref($designs) eq 'HASH') {
 1539:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1540:             if (defined($designs->{'showlogo'}{$img})) {
 1541:                 if ($designs->{'showlogo'}{$img} == 0) {
 1542:                     $checkedon = '';
 1543:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1544:                     $checkedon = 'checked="checked" ';
 1545:                 }
 1546:             }
 1547:         }
 1548:     }
 1549:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1550:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1551:            &mt('show').'</label>'."\n";
 1552: }
 1553: 
 1554: sub login_header_options  {
 1555:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1556:     my $output = '';
 1557:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1558:         $output .= &mt('Text default(s):').'<br />';
 1559:         if (!$is_custom->{'textcol'}) {
 1560:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1561:                        '&nbsp;&nbsp;&nbsp;';
 1562:         }
 1563:         if (!$is_custom->{'bgcol'}) {
 1564:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1565:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1566:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1567:         }
 1568:         $output .= '<br />';
 1569:     }
 1570:     $output .='<br />';
 1571:     return $output;
 1572: }
 1573: 
 1574: sub login_text_colors {
 1575:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1576:     my $color_menu = '<table border="0"><tr>';
 1577:     foreach my $item (@{$logintext}) {
 1578:         $color_menu .= '<td align="center">'.$choices->{$item};
 1579:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1580:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1581:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1582:     }
 1583:     $color_menu .= '</tr></table><br />';
 1584:     return $color_menu;
 1585: }
 1586: 
 1587: sub image_changes {
 1588:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1589:     my $output;
 1590:     if ($img eq 'login') {
 1591:             # suppress image for Log-in header
 1592:     } elsif (!$is_custom) {
 1593:         if ($img ne 'domlogo') {
 1594:             $output .= &mt('Default image:').'<br />';
 1595:         } else {
 1596:             $output .= &mt('Default in use:').'<br />';
 1597:         }
 1598:     }
 1599:     if ($img eq 'login') { # suppress image for Log-in header
 1600:         $output .= '<td>'.$logincolors;
 1601:     } else {
 1602:         if ($img_import) {
 1603:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1604:         }
 1605:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1606:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1607:         if ($is_custom) {
 1608:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1609:                        '<input type="checkbox" name="'.
 1610:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1611:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1612:         } else {
 1613:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1614:         }
 1615:     }
 1616:     return $output;
 1617: }
 1618: 
 1619: sub print_quotas {
 1620:     my ($dom,$settings,$rowtotal,$action) = @_;
 1621:     my $context;
 1622:     if ($action eq 'quotas') {
 1623:         $context = 'tools';
 1624:     } else {
 1625:         $context = $action;
 1626:     }
 1627:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1628:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1629:     my $typecount = 0;
 1630:     my ($css_class,%titles);
 1631:     if ($context eq 'requestcourses') {
 1632:         @usertools = ('official','unofficial','community','textbook');
 1633:         @options =('norequest','approval','validate','autolimit');
 1634:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1635:         %titles = &courserequest_titles();
 1636:     } elsif ($context eq 'requestauthor') {
 1637:         @usertools = ('author');
 1638:         @options = ('norequest','approval','automatic');
 1639:         %titles = &authorrequest_titles();
 1640:     } else {
 1641:         @usertools = ('aboutme','blog','webdav','portfolio');
 1642:         %titles = &tool_titles();
 1643:     }
 1644:     if (ref($types) eq 'ARRAY') {
 1645:         foreach my $type (@{$types}) {
 1646:             my ($currdefquota,$currauthorquota);
 1647:             unless (($context eq 'requestcourses') ||
 1648:                     ($context eq 'requestauthor')) {
 1649:                 if (ref($settings) eq 'HASH') {
 1650:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1651:                         $currdefquota = $settings->{defaultquota}->{$type};
 1652:                     } else {
 1653:                         $currdefquota = $settings->{$type};
 1654:                     }
 1655:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1656:                         $currauthorquota = $settings->{authorquota}->{$type};
 1657:                     }
 1658:                 }
 1659:             }
 1660:             if (defined($usertypes->{$type})) {
 1661:                 $typecount ++;
 1662:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1663:                 $datatable .= '<tr'.$css_class.'>'.
 1664:                               '<td>'.$usertypes->{$type}.'</td>'.
 1665:                               '<td class="LC_left_item">';
 1666:                 if ($context eq 'requestcourses') {
 1667:                     $datatable .= '<table><tr>';
 1668:                 }
 1669:                 my %cell;  
 1670:                 foreach my $item (@usertools) {
 1671:                     if ($context eq 'requestcourses') {
 1672:                         my ($curroption,$currlimit);
 1673:                         if (ref($settings) eq 'HASH') {
 1674:                             if (ref($settings->{$item}) eq 'HASH') {
 1675:                                 $curroption = $settings->{$item}->{$type};
 1676:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1677:                                     $currlimit = $1; 
 1678:                                 }
 1679:                             }
 1680:                         }
 1681:                         if (!$curroption) {
 1682:                             $curroption = 'norequest';
 1683:                         }
 1684:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1685:                         foreach my $option (@options) {
 1686:                             my $val = $option;
 1687:                             if ($option eq 'norequest') {
 1688:                                 $val = 0;  
 1689:                             }
 1690:                             if ($option eq 'validate') {
 1691:                                 my $canvalidate = 0;
 1692:                                 if (ref($validations{$item}) eq 'HASH') { 
 1693:                                     if ($validations{$item}{$type}) {
 1694:                                         $canvalidate = 1;
 1695:                                     }
 1696:                                 }
 1697:                                 next if (!$canvalidate);
 1698:                             }
 1699:                             my $checked = '';
 1700:                             if ($option eq $curroption) {
 1701:                                 $checked = ' checked="checked"';
 1702:                             } elsif ($option eq 'autolimit') {
 1703:                                 if ($curroption =~ /^autolimit/) {
 1704:                                     $checked = ' checked="checked"';
 1705:                                 }                       
 1706:                             } 
 1707:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1708:                                   '<input type="radio" name="crsreq_'.$item.
 1709:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1710:                                   $titles{$option}.'</label>';
 1711:                             if ($option eq 'autolimit') {
 1712:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1713:                                                 $item.'_limit_'.$type.'" size="1" '.
 1714:                                                 'value="'.$currlimit.'" />';
 1715:                             }
 1716:                             $cell{$item} .= '</span> ';
 1717:                             if ($option eq 'autolimit') {
 1718:                                 $cell{$item} .= $titles{'unlimited'};
 1719:                             }
 1720:                         }
 1721:                     } elsif ($context eq 'requestauthor') {
 1722:                         my $curroption;
 1723:                         if (ref($settings) eq 'HASH') {
 1724:                             $curroption = $settings->{$type};
 1725:                         }
 1726:                         if (!$curroption) {
 1727:                             $curroption = 'norequest';
 1728:                         }
 1729:                         foreach my $option (@options) {
 1730:                             my $val = $option;
 1731:                             if ($option eq 'norequest') {
 1732:                                 $val = 0;
 1733:                             }
 1734:                             my $checked = '';
 1735:                             if ($option eq $curroption) {
 1736:                                 $checked = ' checked="checked"';
 1737:                             }
 1738:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1739:                                   '<input type="radio" name="authorreq_'.$type.
 1740:                                   '" value="'.$val.'"'.$checked.' />'.
 1741:                                   $titles{$option}.'</label></span>&nbsp; ';
 1742:                         }
 1743:                     } else {
 1744:                         my $checked = 'checked="checked" ';
 1745:                         if (ref($settings) eq 'HASH') {
 1746:                             if (ref($settings->{$item}) eq 'HASH') {
 1747:                                 if ($settings->{$item}->{$type} == 0) {
 1748:                                     $checked = '';
 1749:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1750:                                     $checked =  'checked="checked" ';
 1751:                                 }
 1752:                             }
 1753:                         }
 1754:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1755:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1756:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1757:                                       '</label></span>&nbsp; ';
 1758:                     }
 1759:                 }
 1760:                 if ($context eq 'requestcourses') {
 1761:                     $datatable .= '</tr><tr>';
 1762:                     foreach my $item (@usertools) {
 1763:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1764:                     }
 1765:                     $datatable .= '</tr></table>';
 1766:                 }
 1767:                 $datatable .= '</td>';
 1768:                 unless (($context eq 'requestcourses') ||
 1769:                         ($context eq 'requestauthor')) {
 1770:                     $datatable .= 
 1771:                               '<td class="LC_right_item">'.
 1772:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1773:                               '<input type="text" name="quota_'.$type.
 1774:                               '" value="'.$currdefquota.
 1775:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1776:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1777:                               '<input type="text" name="authorquota_'.$type.
 1778:                               '" value="'.$currauthorquota.
 1779:                               '" size="5" /></span></td>';
 1780:                 }
 1781:                 $datatable .= '</tr>';
 1782:             }
 1783:         }
 1784:     }
 1785:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1786:         $defaultquota = '20';
 1787:         $authorquota = '500';
 1788:         if (ref($settings) eq 'HASH') {
 1789:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1790:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1791:             } elsif (defined($settings->{'default'})) {
 1792:                 $defaultquota = $settings->{'default'};
 1793:             }
 1794:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1795:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1796:             }
 1797:         }
 1798:     }
 1799:     $typecount ++;
 1800:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1801:     $datatable .= '<tr'.$css_class.'>'.
 1802:                   '<td>'.$othertitle.'</td>'.
 1803:                   '<td class="LC_left_item">';
 1804:     if ($context eq 'requestcourses') {
 1805:         $datatable .= '<table><tr>';
 1806:     }
 1807:     my %defcell;
 1808:     foreach my $item (@usertools) {
 1809:         if ($context eq 'requestcourses') {
 1810:             my ($curroption,$currlimit);
 1811:             if (ref($settings) eq 'HASH') {
 1812:                 if (ref($settings->{$item}) eq 'HASH') {
 1813:                     $curroption = $settings->{$item}->{'default'};
 1814:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1815:                         $currlimit = $1;
 1816:                     }
 1817:                 }
 1818:             }
 1819:             if (!$curroption) {
 1820:                 $curroption = 'norequest';
 1821:             }
 1822:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1823:             foreach my $option (@options) {
 1824:                 my $val = $option;
 1825:                 if ($option eq 'norequest') {
 1826:                     $val = 0;
 1827:                 }
 1828:                 if ($option eq 'validate') {
 1829:                     my $canvalidate = 0;
 1830:                     if (ref($validations{$item}) eq 'HASH') {
 1831:                         if ($validations{$item}{'default'}) {
 1832:                             $canvalidate = 1;
 1833:                         }
 1834:                     }
 1835:                     next if (!$canvalidate);
 1836:                 }
 1837:                 my $checked = '';
 1838:                 if ($option eq $curroption) {
 1839:                     $checked = ' checked="checked"';
 1840:                 } elsif ($option eq 'autolimit') {
 1841:                     if ($curroption =~ /^autolimit/) {
 1842:                         $checked = ' checked="checked"';
 1843:                     }
 1844:                 }
 1845:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1846:                                   '<input type="radio" name="crsreq_'.$item.
 1847:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1848:                                   $titles{$option}.'</label>';
 1849:                 if ($option eq 'autolimit') {
 1850:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1851:                                        $item.'_limit_default" size="1" '.
 1852:                                        'value="'.$currlimit.'" />';
 1853:                 }
 1854:                 $defcell{$item} .= '</span> ';
 1855:                 if ($option eq 'autolimit') {
 1856:                     $defcell{$item} .= $titles{'unlimited'};
 1857:                 }
 1858:             }
 1859:         } elsif ($context eq 'requestauthor') {
 1860:             my $curroption;
 1861:             if (ref($settings) eq 'HASH') {
 1862:                 $curroption = $settings->{'default'};
 1863:             }
 1864:             if (!$curroption) {
 1865:                 $curroption = 'norequest';
 1866:             }
 1867:             foreach my $option (@options) {
 1868:                 my $val = $option;
 1869:                 if ($option eq 'norequest') {
 1870:                     $val = 0;
 1871:                 }
 1872:                 my $checked = '';
 1873:                 if ($option eq $curroption) {
 1874:                     $checked = ' checked="checked"';
 1875:                 }
 1876:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1877:                               '<input type="radio" name="authorreq_default"'.
 1878:                               ' value="'.$val.'"'.$checked.' />'.
 1879:                               $titles{$option}.'</label></span>&nbsp; ';
 1880:             }
 1881:         } else {
 1882:             my $checked = 'checked="checked" ';
 1883:             if (ref($settings) eq 'HASH') {
 1884:                 if (ref($settings->{$item}) eq 'HASH') {
 1885:                     if ($settings->{$item}->{'default'} == 0) {
 1886:                         $checked = '';
 1887:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1888:                         $checked = 'checked="checked" ';
 1889:                     }
 1890:                 }
 1891:             }
 1892:             $datatable .= '<span class="LC_nobreak"><label>'.
 1893:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1894:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1895:                           '</label></span>&nbsp; ';
 1896:         }
 1897:     }
 1898:     if ($context eq 'requestcourses') {
 1899:         $datatable .= '</tr><tr>';
 1900:         foreach my $item (@usertools) {
 1901:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1902:         }
 1903:         $datatable .= '</tr></table>';
 1904:     }
 1905:     $datatable .= '</td>';
 1906:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1907:         $datatable .= '<td class="LC_right_item">'.
 1908:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1909:                       '<input type="text" name="defaultquota" value="'.
 1910:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1911:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1912:                       '<input type="text" name="authorquota" value="'.
 1913:                       $authorquota.'" size="5" /></span></td>';
 1914:     }
 1915:     $datatable .= '</tr>';
 1916:     $typecount ++;
 1917:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1918:     $datatable .= '<tr'.$css_class.'>'.
 1919:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1920:     if ($context eq 'requestcourses') {
 1921:         $datatable .= &mt('(overrides affiliation, if set)').
 1922:                       '</td>'.
 1923:                       '<td class="LC_left_item">'.
 1924:                       '<table><tr>';
 1925:     } else {
 1926:         $datatable .= &mt('(overrides affiliation, if checked)').
 1927:                       '</td>'.
 1928:                       '<td class="LC_left_item" colspan="2">'.
 1929:                       '<br />';
 1930:     }
 1931:     my %advcell;
 1932:     foreach my $item (@usertools) {
 1933:         if ($context eq 'requestcourses') {
 1934:             my ($curroption,$currlimit);
 1935:             if (ref($settings) eq 'HASH') {
 1936:                 if (ref($settings->{$item}) eq 'HASH') {
 1937:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1938:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1939:                         $currlimit = $1;
 1940:                     }
 1941:                 }
 1942:             }
 1943:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1944:             my $checked = '';
 1945:             if ($curroption eq '') {
 1946:                 $checked = ' checked="checked"';
 1947:             }
 1948:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1949:                                '<input type="radio" name="crsreq_'.$item.
 1950:                                '__LC_adv" value=""'.$checked.' />'.
 1951:                                &mt('No override set').'</label></span>&nbsp; ';
 1952:             foreach my $option (@options) {
 1953:                 my $val = $option;
 1954:                 if ($option eq 'norequest') {
 1955:                     $val = 0;
 1956:                 }
 1957:                 if ($option eq 'validate') {
 1958:                     my $canvalidate = 0;
 1959:                     if (ref($validations{$item}) eq 'HASH') {
 1960:                         if ($validations{$item}{'_LC_adv'}) {
 1961:                             $canvalidate = 1;
 1962:                         }
 1963:                     }
 1964:                     next if (!$canvalidate);
 1965:                 }
 1966:                 my $checked = '';
 1967:                 if ($val eq $curroption) {
 1968:                     $checked = ' checked="checked"';
 1969:                 } elsif ($option eq 'autolimit') {
 1970:                     if ($curroption =~ /^autolimit/) {
 1971:                         $checked = ' checked="checked"';
 1972:                     }
 1973:                 }
 1974:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1975:                                   '<input type="radio" name="crsreq_'.$item.
 1976:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1977:                                   $titles{$option}.'</label>';
 1978:                 if ($option eq 'autolimit') {
 1979:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1980:                                        $item.'_limit__LC_adv" size="1" '.
 1981:                                        'value="'.$currlimit.'" />';
 1982:                 }
 1983:                 $advcell{$item} .= '</span> ';
 1984:                 if ($option eq 'autolimit') {
 1985:                     $advcell{$item} .= $titles{'unlimited'};
 1986:                 }
 1987:             }
 1988:         } elsif ($context eq 'requestauthor') {
 1989:             my $curroption;
 1990:             if (ref($settings) eq 'HASH') {
 1991:                 $curroption = $settings->{'_LC_adv'};
 1992:             }
 1993:             my $checked = '';
 1994:             if ($curroption eq '') {
 1995:                 $checked = ' checked="checked"';
 1996:             }
 1997:             $datatable .= '<span class="LC_nobreak"><label>'.
 1998:                           '<input type="radio" name="authorreq__LC_adv"'.
 1999:                           ' value=""'.$checked.' />'.
 2000:                           &mt('No override set').'</label></span>&nbsp; ';
 2001:             foreach my $option (@options) {
 2002:                 my $val = $option;
 2003:                 if ($option eq 'norequest') {
 2004:                     $val = 0;
 2005:                 }
 2006:                 my $checked = '';
 2007:                 if ($val eq $curroption) {
 2008:                     $checked = ' checked="checked"';
 2009:                 }
 2010:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2011:                               '<input type="radio" name="authorreq__LC_adv"'.
 2012:                               ' value="'.$val.'"'.$checked.' />'.
 2013:                               $titles{$option}.'</label></span>&nbsp; ';
 2014:             }
 2015:         } else {
 2016:             my $checked = 'checked="checked" ';
 2017:             if (ref($settings) eq 'HASH') {
 2018:                 if (ref($settings->{$item}) eq 'HASH') {
 2019:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2020:                         $checked = '';
 2021:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2022:                         $checked = 'checked="checked" ';
 2023:                     }
 2024:                 }
 2025:             }
 2026:             $datatable .= '<span class="LC_nobreak"><label>'.
 2027:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2028:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2029:                           '</label></span>&nbsp; ';
 2030:         }
 2031:     }
 2032:     if ($context eq 'requestcourses') {
 2033:         $datatable .= '</tr><tr>';
 2034:         foreach my $item (@usertools) {
 2035:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2036:         }
 2037:         $datatable .= '</tr></table>';
 2038:     }
 2039:     $datatable .= '</td></tr>';
 2040:     $$rowtotal += $typecount;
 2041:     return $datatable;
 2042: }
 2043: 
 2044: sub print_requestmail {
 2045:     my ($dom,$action,$settings,$rowtotal) = @_;
 2046:     my ($now,$datatable,%currapp);
 2047:     $now = time;
 2048:     if (ref($settings) eq 'HASH') {
 2049:         if (ref($settings->{'notify'}) eq 'HASH') {
 2050:             if ($settings->{'notify'}{'approval'} ne '') {
 2051:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2052:             }
 2053:         }
 2054:     }
 2055:     my $numinrow = 2;
 2056:     my $css_class;
 2057:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2058:     my $text;
 2059:     if ($action eq 'requestcourses') {
 2060:         $text = &mt('Receive notification of course requests requiring approval');
 2061:     } elsif ($action eq 'requestauthor') {
 2062:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2063:     } else {
 2064:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2065:     }
 2066:     $datatable = '<tr'.$css_class.'>'.
 2067:                  ' <td>'.$text.'</td>'.
 2068:                  ' <td class="LC_left_item">';
 2069:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2070:                                                  $action.'notifyapproval',%currapp);
 2071:     if ($numdc > 0) {
 2072:         $datatable .= $table;
 2073:     } else {
 2074:         $datatable .= &mt('There are no active Domain Coordinators');
 2075:     }
 2076:     $datatable .='</td></tr>';
 2077:     $$rowtotal += $rows;
 2078:     return $datatable;
 2079: }
 2080: 
 2081: sub print_studentcode {
 2082:     my ($settings,$rowtotal) = @_;
 2083:     my $rownum = 0; 
 2084:     my ($output,%current);
 2085:     my @crstypes = ('official','unofficial','community','textbook');
 2086:     if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2087:         foreach my $type (@crstypes) {
 2088:             $current{$type} = $settings->{'uniquecode'}{$type};
 2089:         }
 2090:     }
 2091:     $output .= '<tr>'.
 2092:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2093:                '<td class="LC_left_item">';
 2094:     foreach my $type (@crstypes) {
 2095:         my $check = ' ';
 2096:         if ($current{$type}) {
 2097:             $check = ' checked="checked" ';
 2098:         }
 2099:         $output .= '<span class="LC_nobreak"><label>'.
 2100:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2101:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2102:     }
 2103:     $output .= '</td></tr>';
 2104:     $$rowtotal ++;
 2105:     return $output;
 2106: }
 2107: 
 2108: sub print_textbookcourses {
 2109:     my ($dom,$type,$settings,$rowtotal) = @_;
 2110:     my $rownum = 0;
 2111:     my $css_class;
 2112:     my $itemcount = 1;
 2113:     my $maxnum = 0;
 2114:     my $bookshash;
 2115:     if (ref($settings) eq 'HASH') {
 2116:         $bookshash = $settings->{$type};
 2117:     }
 2118:     my %ordered;
 2119:     if (ref($bookshash) eq 'HASH') {
 2120:         foreach my $item (keys(%{$bookshash})) {
 2121:             if (ref($bookshash->{$item}) eq 'HASH') {
 2122:                 my $num = $bookshash->{$item}{'order'};
 2123:                 $ordered{$num} = $item;
 2124:             }
 2125:         }
 2126:     }
 2127:     my $confname = $dom.'-domainconfig';
 2128:     my $switchserver = &check_switchserver($dom,$confname);
 2129:     my $maxnum = scalar(keys(%ordered));
 2130:     my $datatable;
 2131:     if (keys(%ordered)) {
 2132:         my @items = sort { $a <=> $b } keys(%ordered);
 2133:         for (my $i=0; $i<@items; $i++) {
 2134:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2135:             my $key = $ordered{$items[$i]};
 2136:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2137:             my $coursetitle = $coursehash{'description'};
 2138:             my ($subject,$title,$author,$image,$imgsrc,$cdom,$cnum);
 2139:             if (ref($bookshash->{$key}) eq 'HASH') {
 2140:                 $subject = $bookshash->{$key}->{'subject'};
 2141:                 $title = $bookshash->{$key}->{'title'};
 2142:                 if ($type eq 'textbooks') {
 2143:                     $author = $bookshash->{$key}->{'author'};
 2144:                     $image = $bookshash->{$key}->{'image'};
 2145:                     if ($image ne '') {
 2146:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2147:                         my $imagethumb = "$path/tn-".$imagefile;
 2148:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2149:                     }
 2150:                 }
 2151:             }
 2152:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2153:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2154:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2155:             for (my $k=0; $k<=$maxnum; $k++) {
 2156:                 my $vpos = $k+1;
 2157:                 my $selstr;
 2158:                 if ($k == $i) {
 2159:                     $selstr = ' selected="selected" ';
 2160:                 }
 2161:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2162:             }
 2163:             $datatable .= '</select>'.('&nbsp;'x2).
 2164:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2165:                 &mt('Delete?').'</label></span></td>'.
 2166:                 '<td colspan="2">'.
 2167:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2168:                 ('&nbsp;'x2).
 2169:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2170:             if ($type eq 'textbooks') {
 2171:                 $datatable .= ('&nbsp;'x2).
 2172:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2173:                               ('&nbsp;'x2).
 2174:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2175:                 if ($image) {
 2176:                     $datatable .= '<span class="LC_nobreak">'.
 2177:                                   $imgsrc.
 2178:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2179:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2180:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2181:                 }
 2182:                 if ($switchserver) {
 2183:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2184:                 } else {
 2185:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2186:                 }
 2187:             }
 2188:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2189:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2190:                           $coursetitle.'</span></td></tr>'."\n";
 2191:             $itemcount ++;
 2192:         }
 2193:     }
 2194:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2195:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2196:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2197:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2198:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2199:     for (my $k=0; $k<$maxnum+1; $k++) {
 2200:         my $vpos = $k+1;
 2201:         my $selstr;
 2202:         if ($k == $maxnum) {
 2203:             $selstr = ' selected="selected" ';
 2204:         }
 2205:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2206:     }
 2207:     $datatable .= '</select>&nbsp;'."\n".
 2208:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2209:                   '<td colspan="2">'.
 2210:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2211:                   ('&nbsp;'x2).
 2212:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2213:                   ('&nbsp;'x2);
 2214:     if ($type eq 'textbooks') {
 2215:         $datatable .= '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2216:                       ('&nbsp;'x2).
 2217:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2218:         if ($switchserver) {
 2219:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2220:         } else {
 2221:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2222:         }
 2223:     }
 2224:     $datatable .= '</span>'."\n".
 2225:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2226:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2227:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2228:                   &Apache::loncommon::selectcourse_link
 2229:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2230:                   '</span></td>'."\n".
 2231:                   '</tr>'."\n";
 2232:     $itemcount ++;
 2233:     return $datatable;
 2234: }
 2235: 
 2236: sub textbookcourses_javascript {
 2237:     my ($settings) = @_;
 2238:     return unless(ref($settings) eq 'HASH');
 2239:     my (%ordered,%total,%jstext);
 2240:     foreach my $type ('textbooks','templates') {
 2241:         $total{$type} = 0;
 2242:         if (ref($settings->{$type}) eq 'HASH') {
 2243:             foreach my $item (keys(%{$settings->{$type}})) {
 2244:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2245:                     my $num = $settings->{$type}->{$item}{'order'};
 2246:                     $ordered{$type}{$num} = $item;
 2247:                 }
 2248:             }
 2249:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2250:         }
 2251:         my @jsarray = ();
 2252:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2253:             push(@jsarray,$ordered{$type}{$item});
 2254:         }
 2255:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2256:     }
 2257:     return <<"ENDSCRIPT";
 2258: <script type="text/javascript">
 2259: // <![CDATA[
 2260: function reorderBooks(form,item,caller) {
 2261:     var changedVal;
 2262: $jstext{'textbooks'};
 2263: $jstext{'templates'};
 2264:     var newpos;
 2265:     var maxh;
 2266:     if (caller == 'textbooks') {  
 2267:         newpos = 'textbooks_addbook_pos';
 2268:         maxh = 1 + $total{'textbooks'};
 2269:     } else {
 2270:         newpos = 'templates_addbook_pos';
 2271:         maxh = 1 + $total{'templates'};
 2272:     }
 2273:     var current = new Array;
 2274:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2275:     if (item == newpos) {
 2276:         changedVal = newitemVal;
 2277:     } else {
 2278:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2279:         current[newitemVal] = newpos;
 2280:     }
 2281:     if (caller == 'textbooks') {
 2282:         for (var i=0; i<textbooks.length; i++) {
 2283:             var elementName = 'textbooks_'+textbooks[i];
 2284:             if (elementName != item) {
 2285:                 if (form.elements[elementName]) {
 2286:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2287:                     current[currVal] = elementName;
 2288:                 }
 2289:             }
 2290:         }
 2291:     }
 2292:     if (caller == 'templates') {
 2293:         for (var i=0; i<templates.length; i++) {
 2294:             var elementName = 'templates_'+templates[i];
 2295:             if (elementName != item) {
 2296:                 if (form.elements[elementName]) {
 2297:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2298:                     current[currVal] = elementName;
 2299:                 }
 2300:             }
 2301:         }
 2302:     }
 2303:     var oldVal;
 2304:     for (var j=0; j<maxh; j++) {
 2305:         if (current[j] == undefined) {
 2306:             oldVal = j;
 2307:         }
 2308:     }
 2309:     if (oldVal < changedVal) {
 2310:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2311:            var elementName = current[k];
 2312:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2313:         }
 2314:     } else {
 2315:         for (var k=changedVal; k<oldVal; k++) {
 2316:             var elementName = current[k];
 2317:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2318:         }
 2319:     }
 2320:     return;
 2321: }
 2322: 
 2323: // ]]>
 2324: </script>
 2325: 
 2326: ENDSCRIPT
 2327: }
 2328: 
 2329: sub print_autoenroll {
 2330:     my ($dom,$settings,$rowtotal) = @_;
 2331:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2332:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2333:     if (ref($settings) eq 'HASH') {
 2334:         if (exists($settings->{'run'})) {
 2335:             if ($settings->{'run'} eq '0') {
 2336:                 $runoff = ' checked="checked" ';
 2337:                 $runon = ' ';
 2338:             } else {
 2339:                 $runon = ' checked="checked" ';
 2340:                 $runoff = ' ';
 2341:             }
 2342:         } else {
 2343:             if ($autorun) {
 2344:                 $runon = ' checked="checked" ';
 2345:                 $runoff = ' ';
 2346:             } else {
 2347:                 $runoff = ' checked="checked" ';
 2348:                 $runon = ' ';
 2349:             }
 2350:         }
 2351:         if (exists($settings->{'co-owners'})) {
 2352:             if ($settings->{'co-owners'} eq '0') {
 2353:                 $coownersoff = ' checked="checked" ';
 2354:                 $coownerson = ' ';
 2355:             } else {
 2356:                 $coownerson = ' checked="checked" ';
 2357:                 $coownersoff = ' ';
 2358:             }
 2359:         } else {
 2360:             $coownersoff = ' checked="checked" ';
 2361:             $coownerson = ' ';
 2362:         }
 2363:         if (exists($settings->{'sender_domain'})) {
 2364:             $defdom = $settings->{'sender_domain'};
 2365:         }
 2366:     } else {
 2367:         if ($autorun) {
 2368:             $runon = ' checked="checked" ';
 2369:             $runoff = ' ';
 2370:         } else {
 2371:             $runoff = ' checked="checked" ';
 2372:             $runon = ' ';
 2373:         }
 2374:     }
 2375:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2376:     my $notif_sender;
 2377:     if (ref($settings) eq 'HASH') {
 2378:         $notif_sender = $settings->{'sender_uname'};
 2379:     }
 2380:     my $datatable='<tr class="LC_odd_row">'.
 2381:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2382:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2383:                   '<input type="radio" name="autoenroll_run"'.
 2384:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2385:                   '<label><input type="radio" name="autoenroll_run"'.
 2386:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2387:                   '</tr><tr>'.
 2388:                   '<td>'.&mt('Notification messages - sender').
 2389:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2390:                   &mt('username').':&nbsp;'.
 2391:                   '<input type="text" name="sender_uname" value="'.
 2392:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2393:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2394:                   '<tr class="LC_odd_row">'.
 2395:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2396:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2397:                   '<input type="radio" name="autoassign_coowners"'.
 2398:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2399:                   '<label><input type="radio" name="autoassign_coowners"'.
 2400:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2401:                   '</tr>';
 2402:     $$rowtotal += 3;
 2403:     return $datatable;
 2404: }
 2405: 
 2406: sub print_autoupdate {
 2407:     my ($position,$dom,$settings,$rowtotal) = @_;
 2408:     my $datatable;
 2409:     if ($position eq 'top') {
 2410:         my $updateon = ' ';
 2411:         my $updateoff = ' checked="checked" ';
 2412:         my $classlistson = ' ';
 2413:         my $classlistsoff = ' checked="checked" ';
 2414:         if (ref($settings) eq 'HASH') {
 2415:             if ($settings->{'run'} eq '1') {
 2416:                 $updateon = $updateoff;
 2417:                 $updateoff = ' ';
 2418:             }
 2419:             if ($settings->{'classlists'} eq '1') {
 2420:                 $classlistson = $classlistsoff;
 2421:                 $classlistsoff = ' ';
 2422:             }
 2423:         }
 2424:         my %title = (
 2425:                    run => 'Auto-update active?',
 2426:                    classlists => 'Update information in classlists?',
 2427:                     );
 2428:         $datatable = '<tr class="LC_odd_row">'. 
 2429:                   '<td>'.&mt($title{'run'}).'</td>'.
 2430:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2431:                   '<input type="radio" name="autoupdate_run"'.
 2432:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2433:                   '<label><input type="radio" name="autoupdate_run"'.
 2434:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2435:                   '</tr><tr>'.
 2436:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2437:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2438:                   '<label><input type="radio" name="classlists"'.
 2439:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2440:                   '<label><input type="radio" name="classlists"'.
 2441:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2442:                   '</tr>';
 2443:         $$rowtotal += 2;
 2444:     } elsif ($position eq 'middle') {
 2445:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2446:         my $numinrow = 3;
 2447:         my $locknamesettings;
 2448:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2449:                                      $dom,$numinrow,$othertitle,
 2450:                                     'lockablenames');
 2451:         $$rowtotal ++;
 2452:     } else {
 2453:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2454:         my @fields = ('lastname','firstname','middlename','generation',
 2455:                       'permanentemail','id');
 2456:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2457:         my $numrows = 0;
 2458:         if (ref($types) eq 'ARRAY') {
 2459:             if (@{$types} > 0) {
 2460:                 $datatable = 
 2461:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2462:                                          \@fields,$types,\$numrows);
 2463:                     $$rowtotal += @{$types}; 
 2464:             }
 2465:         }
 2466:         $datatable .= 
 2467:             &usertype_update_row($settings,{'default' => $othertitle},
 2468:                                  \%fieldtitles,\@fields,['default'],
 2469:                                  \$numrows);
 2470:         $$rowtotal ++;     
 2471:     }
 2472:     return $datatable;
 2473: }
 2474: 
 2475: sub print_autocreate {
 2476:     my ($dom,$settings,$rowtotal) = @_;
 2477:     my (%createon,%createoff,%currhash);
 2478:     my @types = ('xml','req');
 2479:     if (ref($settings) eq 'HASH') {
 2480:         foreach my $item (@types) {
 2481:             $createoff{$item} = ' checked="checked" ';
 2482:             $createon{$item} = ' ';
 2483:             if (exists($settings->{$item})) {
 2484:                 if ($settings->{$item}) {
 2485:                     $createon{$item} = ' checked="checked" ';
 2486:                     $createoff{$item} = ' ';
 2487:                 }
 2488:             }
 2489:         }
 2490:         if ($settings->{'xmldc'} ne '') {
 2491:             $currhash{$settings->{'xmldc'}} = 1;
 2492:         }
 2493:     } else {
 2494:         foreach my $item (@types) {
 2495:             $createoff{$item} = ' checked="checked" ';
 2496:             $createon{$item} = ' ';
 2497:         }
 2498:     }
 2499:     $$rowtotal += 2;
 2500:     my $numinrow = 2;
 2501:     my $datatable='<tr class="LC_odd_row">'.
 2502:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2503:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2504:                   '<input type="radio" name="autocreate_xml"'.
 2505:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2506:                   '<label><input type="radio" name="autocreate_xml"'.
 2507:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2508:                   '</td></tr><tr>'.
 2509:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2510:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2511:                   '<input type="radio" name="autocreate_req"'.
 2512:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2513:                   '<label><input type="radio" name="autocreate_req"'.
 2514:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2515:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2516:                                                    'autocreate_xmldc',%currhash);
 2517:     if ($numdc > 1) {
 2518:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2519:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2520:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2521:     } else {
 2522:         $datatable .= $dctable.'</td></tr>';
 2523:     }
 2524:     $$rowtotal += $rows;
 2525:     return $datatable;
 2526: }
 2527: 
 2528: sub print_directorysrch {
 2529:     my ($dom,$settings,$rowtotal) = @_;
 2530:     my $srchon = ' ';
 2531:     my $srchoff = ' checked="checked" ';
 2532:     my ($exacton,$containson,$beginson);
 2533:     my $localon = ' ';
 2534:     my $localoff = ' checked="checked" ';
 2535:     if (ref($settings) eq 'HASH') {
 2536:         if ($settings->{'available'} eq '1') {
 2537:             $srchon = $srchoff;
 2538:             $srchoff = ' ';
 2539:         }
 2540:         if ($settings->{'localonly'} eq '1') {
 2541:             $localon = $localoff;
 2542:             $localoff = ' ';
 2543:         }
 2544:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2545:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2546:                 if ($type eq 'exact') {
 2547:                     $exacton = ' checked="checked" ';
 2548:                 } elsif ($type eq 'contains') {
 2549:                     $containson = ' checked="checked" ';
 2550:                 } elsif ($type eq 'begins') {
 2551:                     $beginson = ' checked="checked" ';
 2552:                 }
 2553:             }
 2554:         } else {
 2555:             if ($settings->{'searchtypes'} eq 'exact') {
 2556:                 $exacton = ' checked="checked" ';
 2557:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2558:                 $containson = ' checked="checked" ';
 2559:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2560:                 $exacton = ' checked="checked" ';
 2561:                 $containson = ' checked="checked" ';
 2562:             }
 2563:         }
 2564:     }
 2565:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2566:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2567: 
 2568:     my $numinrow = 4;
 2569:     my $cansrchrow = 0;
 2570:     my $datatable='<tr class="LC_odd_row">'.
 2571:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2572:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2573:                   '<input type="radio" name="dirsrch_available"'.
 2574:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2575:                   '<label><input type="radio" name="dirsrch_available"'.
 2576:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2577:                   '</tr><tr>'.
 2578:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2579:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2580:                   '<input type="radio" name="dirsrch_localonly"'.
 2581:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2582:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2583:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2584:                   '</tr>';
 2585:     $$rowtotal += 2;
 2586:     if (ref($usertypes) eq 'HASH') {
 2587:         if (keys(%{$usertypes}) > 0) {
 2588:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2589:                                          $numinrow,$othertitle,'cansearch');
 2590:             $cansrchrow = 1;
 2591:         }
 2592:     }
 2593:     if ($cansrchrow) {
 2594:         $$rowtotal ++;
 2595:         $datatable .= '<tr>';
 2596:     } else {
 2597:         $datatable .= '<tr class="LC_odd_row">';
 2598:     }
 2599:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2600:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2601:     foreach my $title (@{$titleorder}) {
 2602:         if (defined($searchtitles->{$title})) {
 2603:             my $check = ' ';
 2604:             if (ref($settings) eq 'HASH') {
 2605:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2606:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2607:                         $check = ' checked="checked" ';
 2608:                     }
 2609:                 }
 2610:             }
 2611:             $datatable .= '<td class="LC_left_item">'.
 2612:                           '<span class="LC_nobreak"><label>'.
 2613:                           '<input type="checkbox" name="searchby" '.
 2614:                           'value="'.$title.'"'.$check.'/>'.
 2615:                           $searchtitles->{$title}.'</label></span></td>';
 2616:         }
 2617:     }
 2618:     $datatable .= '</tr></table></td></tr>';
 2619:     $$rowtotal ++;
 2620:     if ($cansrchrow) {
 2621:         $datatable .= '<tr class="LC_odd_row">';
 2622:     } else {
 2623:         $datatable .= '<tr>';
 2624:     }
 2625:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2626:                   '<td class="LC_left_item" colspan="2">'.
 2627:                   '<span class="LC_nobreak"><label>'.
 2628:                   '<input type="checkbox" name="searchtypes" '.
 2629:                   $exacton.' value="exact" />'.&mt('Exact match').
 2630:                   '</label>&nbsp;'.
 2631:                   '<label><input type="checkbox" name="searchtypes" '.
 2632:                   $beginson.' value="begins" />'.&mt('Begins with').
 2633:                   '</label>&nbsp;'.
 2634:                   '<label><input type="checkbox" name="searchtypes" '.
 2635:                   $containson.' value="contains" />'.&mt('Contains').
 2636:                   '</label></span></td></tr>';
 2637:     $$rowtotal ++;
 2638:     return $datatable;
 2639: }
 2640: 
 2641: sub print_contacts {
 2642:     my ($dom,$settings,$rowtotal) = @_;
 2643:     my $datatable;
 2644:     my @contacts = ('adminemail','supportemail');
 2645:     my (%checked,%to,%otheremails,%bccemails);
 2646:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2647:                     'requestsmail','updatesmail','idconflictsmail');
 2648:     foreach my $type (@mailings) {
 2649:         $otheremails{$type} = '';
 2650:     }
 2651:     $bccemails{'helpdeskmail'} = '';
 2652:     if (ref($settings) eq 'HASH') {
 2653:         foreach my $item (@contacts) {
 2654:             if (exists($settings->{$item})) {
 2655:                 $to{$item} = $settings->{$item};
 2656:             }
 2657:         }
 2658:         foreach my $type (@mailings) {
 2659:             if (exists($settings->{$type})) {
 2660:                 if (ref($settings->{$type}) eq 'HASH') {
 2661:                     foreach my $item (@contacts) {
 2662:                         if ($settings->{$type}{$item}) {
 2663:                             $checked{$type}{$item} = ' checked="checked" ';
 2664:                         }
 2665:                     }
 2666:                     $otheremails{$type} = $settings->{$type}{'others'};
 2667:                     if ($type eq 'helpdeskmail') {
 2668:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2669:                     }
 2670:                 }
 2671:             } elsif ($type eq 'lonstatusmail') {
 2672:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2673:             }
 2674:         }
 2675:     } else {
 2676:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2677:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2678:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2679:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2680:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2681:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2682:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2683:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2684:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2685:     }
 2686:     my ($titles,$short_titles) = &contact_titles();
 2687:     my $rownum = 0;
 2688:     my $css_class;
 2689:     foreach my $item (@contacts) {
 2690:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2691:         $datatable .= '<tr'.$css_class.'>'. 
 2692:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2693:                   '</span></td><td class="LC_right_item">'.
 2694:                   '<input type="text" name="'.$item.'" value="'.
 2695:                   $to{$item}.'" /></td></tr>';
 2696:         $rownum ++;
 2697:     }
 2698:     foreach my $type (@mailings) {
 2699:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2700:         $datatable .= '<tr'.$css_class.'>'.
 2701:                       '<td><span class="LC_nobreak">'.
 2702:                       $titles->{$type}.': </span></td>'.
 2703:                       '<td class="LC_left_item">'.
 2704:                       '<span class="LC_nobreak">';
 2705:         foreach my $item (@contacts) {
 2706:             $datatable .= '<label>'.
 2707:                           '<input type="checkbox" name="'.$type.'"'.
 2708:                           $checked{$type}{$item}.
 2709:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2710:                           '</label>&nbsp;';
 2711:         }
 2712:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2713:                       '<input type="text" name="'.$type.'_others" '.
 2714:                       'value="'.$otheremails{$type}.'"  />';
 2715:         if ($type eq 'helpdeskmail') {
 2716:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2717:                           '<input type="text" name="'.$type.'_bcc" '.
 2718:                           'value="'.$bccemails{$type}.'"  />';
 2719:         }
 2720:         $datatable .= '</td></tr>'."\n";
 2721:         $rownum ++;
 2722:     }
 2723:     my %choices;
 2724:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2725:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2726:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2727:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2728:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2729:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2730:     my @toggles = ('reporterrors','reportupdates');
 2731:     my %defaultchecked = ('reporterrors'  => 'on',
 2732:                           'reportupdates' => 'on');
 2733:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2734:                                                \%choices,$rownum);
 2735:     $datatable .= $reports;
 2736:     $$rowtotal += $rownum;
 2737:     return $datatable;
 2738: }
 2739: 
 2740: sub print_helpsettings {
 2741:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2742:     my ($datatable,$itemcount);
 2743:     $itemcount = 1;
 2744:     my (%choices,%defaultchecked,@toggles);
 2745:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2746:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2747:                                  &mt('LON-CAPA bug tracker'),600,500));
 2748:     %defaultchecked = ('submitbugs' => 'on');
 2749:     @toggles = ('submitbugs',);
 2750: 
 2751:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2752:                                                  \%choices,$itemcount);
 2753:     return $datatable;
 2754: }
 2755: 
 2756: sub radiobutton_prefs {
 2757:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2758:         $additional) = @_;
 2759:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2760:                    (ref($choices) eq 'HASH'));
 2761: 
 2762:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2763: 
 2764:     foreach my $item (@{$toggles}) {
 2765:         if ($defaultchecked->{$item} eq 'on') {
 2766:             $checkedon{$item} = ' checked="checked" ';
 2767:             $checkedoff{$item} = ' ';
 2768:         } elsif ($defaultchecked->{$item} eq 'off') {
 2769:             $checkedoff{$item} = ' checked="checked" ';
 2770:             $checkedon{$item} = ' ';
 2771:         }
 2772:     }
 2773:     if (ref($settings) eq 'HASH') {
 2774:         foreach my $item (@{$toggles}) {
 2775:             if ($settings->{$item} eq '1') {
 2776:                 $checkedon{$item} =  ' checked="checked" ';
 2777:                 $checkedoff{$item} = ' ';
 2778:             } elsif ($settings->{$item} eq '0') {
 2779:                 $checkedoff{$item} =  ' checked="checked" ';
 2780:                 $checkedon{$item} = ' ';
 2781:             }
 2782:         }
 2783:     }
 2784:     if ($onclick) {
 2785:         $onclick = ' onclick="'.$onclick.'"';
 2786:     }
 2787:     foreach my $item (@{$toggles}) {
 2788:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2789:         $datatable .=
 2790:             '<tr'.$css_class.'><td valign="top">'.
 2791:             '<span class="LC_nobreak">'.$choices->{$item}.
 2792:             '</span></td>'.
 2793:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2794:             '<label><input type="radio" name="'.
 2795:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2796:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2797:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2798:             '</span>'.$additional.
 2799:             '</td>'.
 2800:             '</tr>';
 2801:         $itemcount ++;
 2802:     }
 2803:     return ($datatable,$itemcount);
 2804: }
 2805: 
 2806: sub print_coursedefaults {
 2807:     my ($position,$dom,$settings,$rowtotal) = @_;
 2808:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2809:     my $itemcount = 1;
 2810:     my %choices =  &Apache::lonlocal::texthash (
 2811:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2812:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2813:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2814:         coursecredits        => 'Credits can be specified for courses',
 2815:     );
 2816:     my %staticdefaults = (
 2817:                            anonsurvey_threshold => 10,
 2818:                            uploadquota          => 500,
 2819:                          );
 2820:     if ($position eq 'top') {
 2821:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2822:         @toggles = ('canuse_pdfforms');
 2823:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2824:                                                  \%choices,$itemcount);
 2825:     } else {
 2826:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2827:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2828:             %curruploadquota);
 2829:         my $currusecredits = 0;
 2830:         my @types = ('official','unofficial','community','textbook');
 2831:         if (ref($settings) eq 'HASH') {
 2832:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2833:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2834:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2835:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2836:                 }
 2837:             }
 2838:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2839:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2840:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2841:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2842:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2843:                     ($def_textbook_credits ne '')) {
 2844:                     $currusecredits = 1;
 2845:                 }
 2846:             }
 2847:         }
 2848:         if (!$currdefresponder) {
 2849:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2850:         } elsif ($currdefresponder < 1) {
 2851:             $currdefresponder = 1;
 2852:         }
 2853:         foreach my $type (@types) {
 2854:             if ($curruploadquota{$type} eq '') {
 2855:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2856:             }
 2857:         }
 2858:         $datatable .=
 2859:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2860:                 $choices{'anonsurvey_threshold'}.
 2861:                 '</span></td>'.
 2862:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2863:                 '<input type="text" name="anonsurvey_threshold"'.
 2864:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2865:                 '</td></tr>'."\n";
 2866:         $itemcount ++;
 2867:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2868:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2869:                       $choices{'uploadquota'}.
 2870:                       '</span></td>'.
 2871:                       '<td align="right" class="LC_right_item">'.
 2872:                       '<table><tr>';
 2873:         foreach my $type (@types) {
 2874:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2875:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2876:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2877:         }
 2878:         $datatable .= '</tr></table></td></tr>'."\n";
 2879:         $itemcount ++;
 2880:         my $onclick = "toggleDisplay(this.form,'credits');";
 2881:         my $display = 'none';
 2882:         if ($currusecredits) {
 2883:             $display = 'block';
 2884:         }
 2885:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2886:                          '<span class="LC_nobreak">'.
 2887:                          &mt('Default credits for official courses [_1]',
 2888:                          '<input type="text" name="official_credits" value="'.
 2889:                          $def_official_credits.'" size="3" />').
 2890:                          '</span><br />'.
 2891:                          '<span class="LC_nobreak">'.
 2892:                          &mt('Default credits for unofficial courses [_1]',
 2893:                          '<input type="text" name="unofficial_credits" value="'.
 2894:                          $def_unofficial_credits.'" size="3" />').
 2895:                          '</span><br />'.
 2896:                          '<span class="LC_nobreak">'.
 2897:                          &mt('Default credits for textbook courses [_1]',
 2898:                          '<input type="text" name="textbook_credits" value="'.
 2899:                          $def_textbook_credits.'" size="3" />').
 2900:                          '</span></div>'."\n";
 2901:         %defaultchecked = ('coursecredits' => 'off');
 2902:         @toggles = ('coursecredits');
 2903:         my $current = {
 2904:                         'coursecredits' => $currusecredits,
 2905:                       };
 2906:         (my $table,$itemcount) =
 2907:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2908:                                \%choices,$itemcount,$onclick,$additional);
 2909:         $datatable .= $table;
 2910:         $itemcount ++;
 2911:     }
 2912:     $$rowtotal += $itemcount;
 2913:     return $datatable;
 2914: }
 2915: 
 2916: sub print_selfenrollment {
 2917:     my ($position,$dom,$settings,$rowtotal) = @_;
 2918:     my ($css_class,$datatable);
 2919:     my $itemcount = 1;
 2920:     my @types = ('official','unofficial','community','textbook');
 2921:     if (($position eq 'top') || ($position eq 'middle')) {
 2922:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 2923:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 2924:         my @rows;
 2925:         my $key;
 2926:         if ($position eq 'top') {
 2927:             $key = 'admin'; 
 2928:             if (ref($rowsref) eq 'ARRAY') {
 2929:                 @rows = @{$rowsref};
 2930:             }
 2931:         } elsif ($position eq 'middle') {
 2932:             $key = 'default';
 2933:             @rows = ('types','registered','approval','limit');
 2934:         }
 2935:         foreach my $row (@rows) {
 2936:             if (defined($titlesref->{$row})) {
 2937:                 $itemcount ++;
 2938:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2939:                 $datatable .= '<tr'.$css_class.'>'.
 2940:                               '<td>'.$titlesref->{$row}.'</td>'.
 2941:                               '<td class="LC_left_item">'.
 2942:                               '<table><tr>';
 2943:                 my (%current,%currentcap);
 2944:                 if (ref($settings) eq 'HASH') {
 2945:                     if (ref($settings->{$key}) eq 'HASH') {
 2946:                         foreach my $type (@types) {
 2947:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2948:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 2949:                             }
 2950:                             if (($row eq 'limit') && ($key eq 'default')) {
 2951:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2952:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 2953:                                 }
 2954:                             }
 2955:                         }
 2956:                     }
 2957:                 }
 2958:                 my %roles = (
 2959:                              '0' => &Apache::lonnet::plaintext('dc'),
 2960:                             ); 
 2961:             
 2962:                 foreach my $type (@types) {
 2963:                     unless (($row eq 'registered') && ($key eq 'default')) {
 2964:                         $datatable .= '<th>'.&mt($type).'</th>';
 2965:                     }
 2966:                 }
 2967:                 unless (($row eq 'registered') && ($key eq 'default')) {
 2968:                     $datatable .= '</tr><tr>';
 2969:                 }
 2970:                 foreach my $type (@types) {
 2971:                     if ($type eq 'community') {
 2972:                         $roles{'1'} = &mt('Community personnel');
 2973:                     } else {
 2974:                         $roles{'1'} = &mt('Course personnel');
 2975:                     }
 2976:                     $datatable .= '<td style="vertical-align: top">';
 2977:                     if ($position eq 'top') {
 2978:                         my %checked;
 2979:                         if ($current{$type} eq '0') {
 2980:                             $checked{'0'} = ' checked="checked"';
 2981:                         } else {
 2982:                             $checked{'1'} = ' checked="checked"';
 2983:                         }
 2984:                         foreach my $role ('1','0') {
 2985:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2986:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 2987:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 2988:                                           $roles{$role}.'</label></span> ';
 2989:                         }
 2990:                     } else {
 2991:                         if ($row eq 'types') {
 2992:                             my %checked;
 2993:                             if ($current{$type} =~ /^(all|dom)$/) {
 2994:                                 $checked{$1} = ' checked="checked"';
 2995:                             } else {
 2996:                                 $checked{''} = ' checked="checked"';
 2997:                             }
 2998:                             foreach my $val ('','dom','all') {
 2999:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3000:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3001:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3002:                             }
 3003:                         } elsif ($row eq 'registered') {
 3004:                             my %checked;
 3005:                             if ($current{$type} eq '1') {
 3006:                                 $checked{'1'} = ' checked="checked"';
 3007:                             } else {
 3008:                                 $checked{'0'} = ' checked="checked"';
 3009:                             }
 3010:                             foreach my $val ('0','1') {
 3011:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3012:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3013:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3014:                             }
 3015:                         } elsif ($row eq 'approval') {
 3016:                             my %checked;
 3017:                             if ($current{$type} =~ /^([12])$/) {
 3018:                                 $checked{$1} = ' checked="checked"';
 3019:                             } else {
 3020:                                 $checked{'0'} = ' checked="checked"';
 3021:                             }
 3022:                             for my $val (0..2) {
 3023:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3024:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3025:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3026:                             }
 3027:                         } elsif ($row eq 'limit') {
 3028:                             my %checked;
 3029:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3030:                                 $checked{$1} = ' checked="checked"';
 3031:                             } else {
 3032:                                 $checked{'none'} = ' checked="checked"';
 3033:                             }
 3034:                             my $cap;
 3035:                             if ($currentcap{$type} =~ /^\d+$/) {
 3036:                                 $cap = $currentcap{$type};
 3037:                             }
 3038:                             foreach my $val ('none','allstudents','selfenrolled') {
 3039:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3040:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3041:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3042:                             }
 3043:                             $datatable .= '<br />'.
 3044:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3045:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3046:                                           '</span>'; 
 3047:                         }
 3048:                     }
 3049:                     $datatable .= '</td>';
 3050:                 }
 3051:                 $datatable .= '</tr>';
 3052:             }
 3053:             $datatable .= '</table></td></tr>';
 3054:         }
 3055:     } elsif ($position eq 'bottom') {
 3056:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3057:     }
 3058:     $$rowtotal += $itemcount;
 3059:     return $datatable;
 3060: }
 3061: 
 3062: sub print_validation_rows {
 3063:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3064:     my ($itemsref,$namesref,$fieldsref);
 3065:     if ($caller eq 'selfenroll') { 
 3066:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3067:     } elsif ($caller eq 'requestcourses') {
 3068:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3069:     }
 3070:     my %currvalidation;
 3071:     if (ref($settings) eq 'HASH') {
 3072:         if (ref($settings->{'validation'}) eq 'HASH') {
 3073:             %currvalidation = %{$settings->{'validation'}};
 3074:         }
 3075:     }
 3076:     my $datatable;
 3077:     my $itemcount = 0;
 3078:     foreach my $item (@{$itemsref}) {
 3079:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3080:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3081:                       $namesref->{$item}.
 3082:                       '</span></td>'.
 3083:                       '<td class="LC_left_item">';
 3084:         if (($item eq 'url') || ($item eq 'button')) {
 3085:             $datatable .= '<span class="LC_nobreak">'.
 3086:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3087:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3088:         } elsif ($item eq 'fields') {
 3089:             my @currfields;
 3090:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3091:                 @currfields = @{$currvalidation{$item}};
 3092:             }
 3093:             foreach my $field (@{$fieldsref}) {
 3094:                 my $check = '';
 3095:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3096:                     $check = ' checked="checked"';
 3097:                 }
 3098:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3099:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3100:                               ' value="'.$field.'"'.$check.' />'.$field.
 3101:                               '</label></span> ';
 3102:             }
 3103:         } elsif ($item eq 'markup') {
 3104:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3105:                            $currvalidation{$item}.
 3106:                               '</textarea>';
 3107:         }
 3108:         $datatable .= '</td></tr>'."\n";
 3109:         if (ref($rowtotal)) {
 3110:             $itemcount ++;
 3111:         }
 3112:     }
 3113:     if ($caller eq 'requestcourses') {
 3114:         my %currhash;
 3115:         if (ref($settings->{'validation'}) eq 'HASH') {
 3116:             if ($settings->{'validation'}{'dc'} ne '') {
 3117:                 $currhash{$settings->{'validation'}{'dc'}} = 1;
 3118:             }
 3119:         }
 3120:         my $numinrow = 2;
 3121:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3122:                                                        'validationdc',%currhash);
 3123:         if ($numdc > 1) {
 3124:             $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 3125:                           &mt('Course creation processed as: (choose Dom. Coord.)').
 3126:                           '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3127:         } else {
 3128:             $datatable .= $dctable.'</td></tr>';
 3129:         }
 3130:         $itemcount ++;
 3131:     }
 3132:     if (ref($rowtotal)) {
 3133:         $$rowtotal += $itemcount;
 3134:     }
 3135:     return $datatable;
 3136: }
 3137: 
 3138: sub print_usersessions {
 3139:     my ($position,$dom,$settings,$rowtotal) = @_;
 3140:     my ($css_class,$datatable,%checked,%choices);
 3141:     my (%by_ip,%by_location,@intdoms);
 3142:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3143: 
 3144:     my @alldoms = &Apache::lonnet::all_domains();
 3145:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3146:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3147:     my %altids = &id_for_thisdom(%servers);
 3148:     my $itemcount = 1;
 3149:     if ($position eq 'top') {
 3150:         if (keys(%serverhomes) > 1) {
 3151:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3152:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 3153:         } else {
 3154:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3155:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3156:         }
 3157:     } else {
 3158:         if (keys(%by_location) == 0) {
 3159:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3160:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3161:         } else {
 3162:             my %lt = &usersession_titles();
 3163:             my $numinrow = 5;
 3164:             my $prefix;
 3165:             my @types;
 3166:             if ($position eq 'bottom') {
 3167:                 $prefix = 'remote';
 3168:                 @types = ('version','excludedomain','includedomain');
 3169:             } else {
 3170:                 $prefix = 'hosted';
 3171:                 @types = ('excludedomain','includedomain');
 3172:             }
 3173:             my (%current,%checkedon,%checkedoff);
 3174:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3175:             my @locations = sort(keys(%by_location));
 3176:             foreach my $type (@types) {
 3177:                 $checkedon{$type} = '';
 3178:                 $checkedoff{$type} = ' checked="checked"';
 3179:             }
 3180:             if (ref($settings) eq 'HASH') {
 3181:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3182:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3183:                         $current{$key} = $settings->{$prefix}{$key};
 3184:                         if ($key eq 'version') {
 3185:                             if ($current{$key} ne '') {
 3186:                                 $checkedon{$key} = ' checked="checked"';
 3187:                                 $checkedoff{$key} = '';
 3188:                             }
 3189:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3190:                             $checkedon{$key} = ' checked="checked"';
 3191:                             $checkedoff{$key} = '';
 3192:                         }
 3193:                     }
 3194:                 }
 3195:             }
 3196:             foreach my $type (@types) {
 3197:                 next if ($type ne 'version' && !@locations);
 3198:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3199:                 $datatable .= '<tr'.$css_class.'>
 3200:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3201:                                <span class="LC_nobreak">&nbsp;
 3202:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3203:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3204:                 if ($type eq 'version') {
 3205:                     my $selector = '<select name="'.$prefix.'_version">';
 3206:                     foreach my $version (@lcversions) {
 3207:                         my $selected = '';
 3208:                         if ($current{'version'} eq $version) {
 3209:                             $selected = ' selected="selected"';
 3210:                         }
 3211:                         $selector .= ' <option value="'.$version.'"'.
 3212:                                      $selected.'>'.$version.'</option>';
 3213:                     }
 3214:                     $selector .= '</select> ';
 3215:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3216:                 } else {
 3217:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3218:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3219:                                  ' />'.('&nbsp;'x2).
 3220:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3221:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3222:                                  "\n".
 3223:                                  '</div><div><table>';
 3224:                     my $rem;
 3225:                     for (my $i=0; $i<@locations; $i++) {
 3226:                         my ($showloc,$value,$checkedtype);
 3227:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3228:                             my $ip = $by_location{$locations[$i]}->[0];
 3229:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3230:                                  $value = join(':',@{$by_ip{$ip}});
 3231:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3232:                                 if (ref($current{$type}) eq 'ARRAY') {
 3233:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3234:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3235:                                             $checkedtype = ' checked="checked"';
 3236:                                             last;
 3237:                                         }
 3238:                                     }
 3239:                                 }
 3240:                             }
 3241:                         }
 3242:                         $rem = $i%($numinrow);
 3243:                         if ($rem == 0) {
 3244:                             if ($i > 0) {
 3245:                                 $datatable .= '</tr>';
 3246:                             }
 3247:                             $datatable .= '<tr>';
 3248:                         }
 3249:                         $datatable .= '<td class="LC_left_item">'.
 3250:                                       '<span class="LC_nobreak"><label>'.
 3251:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3252:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3253:                                       '</label></span></td>';
 3254:                     }
 3255:                     $rem = @locations%($numinrow);
 3256:                     my $colsleft = $numinrow - $rem;
 3257:                     if ($colsleft > 1 ) {
 3258:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3259:                                       '&nbsp;</td>';
 3260:                     } elsif ($colsleft == 1) {
 3261:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3262:                     }
 3263:                     $datatable .= '</tr></table>';
 3264:                 }
 3265:                 $datatable .= '</td></tr>';
 3266:                 $itemcount ++;
 3267:             }
 3268:         }
 3269:     }
 3270:     $$rowtotal += $itemcount;
 3271:     return $datatable;
 3272: }
 3273: 
 3274: sub build_location_hashes {
 3275:     my ($intdoms,$by_ip,$by_location) = @_;
 3276:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3277:                   (ref($by_location) eq 'HASH')); 
 3278:     my %iphost = &Apache::lonnet::get_iphost();
 3279:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3280:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3281:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3282:         foreach my $id (@{$iphost{$primary_ip}}) {
 3283:             my $intdom = &Apache::lonnet::internet_dom($id);
 3284:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3285:                 push(@{$intdoms},$intdom);
 3286:             }
 3287:         }
 3288:     }
 3289:     foreach my $ip (keys(%iphost)) {
 3290:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3291:             foreach my $id (@{$iphost{$ip}}) {
 3292:                 my $location = &Apache::lonnet::internet_dom($id);
 3293:                 if ($location) {
 3294:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3295:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3296:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3297:                             push(@{$by_ip->{$ip}},$location);
 3298:                         }
 3299:                     } else {
 3300:                         $by_ip->{$ip} = [$location];
 3301:                     }
 3302:                 }
 3303:             }
 3304:         }
 3305:     }
 3306:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3307:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3308:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3309:             my $first = $by_ip->{$ip}->[0];
 3310:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3311:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3312:                     push(@{$by_location->{$first}},$ip);
 3313:                 }
 3314:             } else {
 3315:                 $by_location->{$first} = [$ip];
 3316:             }
 3317:         }
 3318:     }
 3319:     return;
 3320: }
 3321: 
 3322: sub current_offloads_to {
 3323:     my ($dom,$settings,$servers) = @_;
 3324:     my (%spareid,%otherdomconfigs);
 3325:     if (ref($servers) eq 'HASH') {
 3326:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3327:             my $gotspares;
 3328:             if (ref($settings) eq 'HASH') {
 3329:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3330:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3331:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3332:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3333:                         $gotspares = 1;
 3334:                     }
 3335:                 }
 3336:             }
 3337:             unless ($gotspares) {
 3338:                 my $gotspares;
 3339:                 my $serverhomeID =
 3340:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3341:                 my $serverhomedom =
 3342:                     &Apache::lonnet::host_domain($serverhomeID);
 3343:                 if ($serverhomedom ne $dom) {
 3344:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3345:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3346:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3347:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3348:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3349:                                 $gotspares = 1;
 3350:                             }
 3351:                         }
 3352:                     } else {
 3353:                         $otherdomconfigs{$serverhomedom} =
 3354:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3355:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3356:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3357:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3358:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3359:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3360:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3361:                                         $gotspares = 1;
 3362:                                     }
 3363:                                 }
 3364:                             }
 3365:                         }
 3366:                     }
 3367:                 }
 3368:             }
 3369:             unless ($gotspares) {
 3370:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3371:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3372:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3373:                } else {
 3374:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3375:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3376:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3377:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3378:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3379:                     } else {
 3380:                         my %what = (
 3381:                              spareid => 1,
 3382:                         );
 3383:                         my ($result,$returnhash) = 
 3384:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3385:                         if ($result eq 'ok') { 
 3386:                             if (ref($returnhash) eq 'HASH') {
 3387:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3388:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3389:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3390:                                 }
 3391:                             }
 3392:                         }
 3393:                     }
 3394:                 }
 3395:             }
 3396:         }
 3397:     }
 3398:     return %spareid;
 3399: }
 3400: 
 3401: sub spares_row {
 3402:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3403:     my $css_class;
 3404:     my $numinrow = 4;
 3405:     my $itemcount = 1;
 3406:     my $datatable;
 3407:     my %typetitles = &sparestype_titles();
 3408:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3409:         foreach my $server (sort(keys(%{$servers}))) {
 3410:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3411:             my ($othercontrol,$serverdom);
 3412:             if ($serverhome ne $server) {
 3413:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3414:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3415:             } else {
 3416:                 $serverdom = &Apache::lonnet::host_domain($server);
 3417:                 if ($serverdom ne $dom) {
 3418:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3419:                 }
 3420:             }
 3421:             next unless (ref($spareid->{$server}) eq 'HASH');
 3422:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3423:             $datatable .= '<tr'.$css_class.'>
 3424:                            <td rowspan="2">
 3425:                             <span class="LC_nobreak">'.
 3426:                           &mt('[_1] when busy, offloads to:'
 3427:                               ,'<b>'.$server.'</b>').
 3428:                           "\n";
 3429:             my (%current,%canselect);
 3430:             my @choices = 
 3431:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3432:             foreach my $type ('primary','default') {
 3433:                 if (ref($spareid->{$server}) eq 'HASH') {
 3434:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3435:                         my @spares = @{$spareid->{$server}{$type}};
 3436:                         if (@spares > 0) {
 3437:                             if ($othercontrol) {
 3438:                                 $current{$type} = join(', ',@spares);
 3439:                             } else {
 3440:                                 $current{$type} .= '<table>';
 3441:                                 my $numspares = scalar(@spares);
 3442:                                 for (my $i=0;  $i<@spares; $i++) {
 3443:                                     my $rem = $i%($numinrow);
 3444:                                     if ($rem == 0) {
 3445:                                         if ($i > 0) {
 3446:                                             $current{$type} .= '</tr>';
 3447:                                         }
 3448:                                         $current{$type} .= '<tr>';
 3449:                                     }
 3450:                                     $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;'.
 3451:                                                        $spareid->{$server}{$type}[$i].
 3452:                                                        '</label></td>'."\n";
 3453:                                 }
 3454:                                 my $rem = @spares%($numinrow);
 3455:                                 my $colsleft = $numinrow - $rem;
 3456:                                 if ($colsleft > 1 ) {
 3457:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3458:                                                        '" class="LC_left_item">'.
 3459:                                                        '&nbsp;</td>';
 3460:                                 } elsif ($colsleft == 1) {
 3461:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3462:                                 }
 3463:                                 $current{$type} .= '</tr></table>';
 3464:                             }
 3465:                         }
 3466:                     }
 3467:                     if ($current{$type} eq '') {
 3468:                         $current{$type} = &mt('None specified');
 3469:                     }
 3470:                     if ($othercontrol) {
 3471:                         if ($type eq 'primary') {
 3472:                             $canselect{$type} = $othercontrol;
 3473:                         }
 3474:                     } else {
 3475:                         $canselect{$type} = 
 3476:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3477:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3478:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3479:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3480:                         if (@choices > 0) {
 3481:                             foreach my $lonhost (@choices) {
 3482:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3483:                             }
 3484:                         }
 3485:                         $canselect{$type} .= '</select>'."\n";
 3486:                     }
 3487:                 } else {
 3488:                     $current{$type} = &mt('Could not be determined');
 3489:                     if ($type eq 'primary') {
 3490:                         $canselect{$type} =  $othercontrol;
 3491:                     }
 3492:                 }
 3493:                 if ($type eq 'default') {
 3494:                     $datatable .= '<tr'.$css_class.'>';
 3495:                 }
 3496:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3497:                               '<td>'.$current{$type}.'</td>'."\n".
 3498:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3499:             }
 3500:             $itemcount ++;
 3501:         }
 3502:     }
 3503:     $$rowtotal += $itemcount;
 3504:     return $datatable;
 3505: }
 3506: 
 3507: sub possible_newspares {
 3508:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3509:     my $serverhostname = &Apache::lonnet::hostname($server);
 3510:     my %excluded;
 3511:     if ($serverhostname ne '') {
 3512:         %excluded = (
 3513:                        $serverhostname => 1,
 3514:                     );
 3515:     }
 3516:     if (ref($currspares) eq 'HASH') {
 3517:         foreach my $type (keys(%{$currspares})) {
 3518:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3519:                 if (@{$currspares->{$type}} > 0) {
 3520:                     foreach my $curr (@{$currspares->{$type}}) {
 3521:                         my $hostname = &Apache::lonnet::hostname($curr);
 3522:                         $excluded{$hostname} = 1;
 3523:                     }
 3524:                 }
 3525:             }
 3526:         }
 3527:     }
 3528:     my @choices;
 3529:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3530:         if (keys(%{$serverhomes}) > 1) {
 3531:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3532:                 unless ($excluded{$name}) {
 3533:                     if (exists($altids->{$serverhomes->{$name}})) {
 3534:                         push(@choices,$altids->{$serverhomes->{$name}});
 3535:                     } else {
 3536:                         push(@choices,$serverhomes->{$name});
 3537:                     }
 3538:                 }
 3539:             }
 3540:         }
 3541:     }
 3542:     return sort(@choices);
 3543: }
 3544: 
 3545: sub print_loadbalancing {
 3546:     my ($dom,$settings,$rowtotal) = @_;
 3547:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3548:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3549:     my $numinrow = 1;
 3550:     my $datatable;
 3551:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3552:     my (%currbalancer,%currtargets,%currrules,%existing);
 3553:     if (ref($settings) eq 'HASH') {
 3554:         %existing = %{$settings};
 3555:     }
 3556:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3557:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3558:                                   \%currtargets,\%currrules);
 3559:     } else {
 3560:         return;
 3561:     }
 3562:     my ($othertitle,$usertypes,$types) =
 3563:         &Apache::loncommon::sorted_inst_types($dom);
 3564:     my $rownum = 8;
 3565:     if (ref($types) eq 'ARRAY') {
 3566:         $rownum += scalar(@{$types});
 3567:     }
 3568:     my @css_class = ('LC_odd_row','LC_even_row');
 3569:     my $balnum = 0;
 3570:     my $islast;
 3571:     my (@toshow,$disabledtext);
 3572:     if (keys(%currbalancer) > 0) {
 3573:         @toshow = sort(keys(%currbalancer));
 3574:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3575:             push(@toshow,'');
 3576:         }
 3577:     } else {
 3578:         @toshow = ('');
 3579:         $disabledtext = &mt('No existing load balancer');
 3580:     }
 3581:     foreach my $lonhost (@toshow) {
 3582:         if ($balnum == scalar(@toshow)-1) {
 3583:             $islast = 1;
 3584:         } else {
 3585:             $islast = 0;
 3586:         }
 3587:         my $cssidx = $balnum%2;
 3588:         my $targets_div_style = 'display: none';
 3589:         my $disabled_div_style = 'display: block';
 3590:         my $homedom_div_style = 'display: none';
 3591:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3592:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3593:                       '<p>';
 3594:         if ($lonhost eq '') {
 3595:             $datatable .= '<span class="LC_nobreak">';
 3596:             if (keys(%currbalancer) > 0) {
 3597:                 $datatable .= &mt('Add balancer:');
 3598:             } else {
 3599:                 $datatable .= &mt('Enable balancer:');
 3600:             }
 3601:             $datatable .= '&nbsp;'.
 3602:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3603:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3604:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3605:                           '<option value="" selected="selected">'.&mt('None').
 3606:                           '</option>'."\n";
 3607:             foreach my $server (sort(keys(%servers))) {
 3608:                 next if ($currbalancer{$server});
 3609:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3610:             }
 3611:             $datatable .=
 3612:                 '</select>'."\n".
 3613:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3614:         } else {
 3615:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3616:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3617:                            &mt('Stop balancing').'</label>'.
 3618:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3619:             $targets_div_style = 'display: block';
 3620:             $disabled_div_style = 'display: none';
 3621:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3622:                 $homedom_div_style = 'display: block';
 3623:             }
 3624:         }
 3625:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3626:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3627:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3628:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3629:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3630:         my @sparestypes = ('primary','default');
 3631:         my %typetitles = &sparestype_titles();
 3632:         foreach my $sparetype (@sparestypes) {
 3633:             my $targettable;
 3634:             for (my $i=0; $i<$numspares; $i++) {
 3635:                 my $checked;
 3636:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3637:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3638:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3639:                             $checked = ' checked="checked"';
 3640:                         }
 3641:                     }
 3642:                 }
 3643:                 my ($chkboxval,$disabled);
 3644:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3645:                     $chkboxval = $spares[$i];
 3646:                 }
 3647:                 if (exists($currbalancer{$spares[$i]})) {
 3648:                     $disabled = ' disabled="disabled"';
 3649:                 }
 3650:                 $targettable .=
 3651:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3652:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3653:                     '</span></label></td>';
 3654:                 my $rem = $i%($numinrow);
 3655:                 if ($rem == 0) {
 3656:                     if (($i > 0) && ($i < $numspares-1)) {
 3657:                         $targettable .= '</tr>';
 3658:                     }
 3659:                     if ($i < $numspares-1) {
 3660:                         $targettable .= '<tr>';
 3661:                     }
 3662:                 }
 3663:             }
 3664:             if ($targettable ne '') {
 3665:                 my $rem = $numspares%($numinrow);
 3666:                 my $colsleft = $numinrow - $rem;
 3667:                 if ($colsleft > 1 ) {
 3668:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3669:                                     '&nbsp;</td>';
 3670:                 } elsif ($colsleft == 1) {
 3671:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3672:                 }
 3673:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3674:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3675:             }
 3676:         }
 3677:         $datatable .= '</div></td></tr>'.
 3678:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3679:                                            $othertitle,$usertypes,$types,\%servers,
 3680:                                            \%currbalancer,$lonhost,
 3681:                                            $targets_div_style,$homedom_div_style,
 3682:                                            $css_class[$cssidx],$balnum,$islast);
 3683:         $$rowtotal += $rownum;
 3684:         $balnum ++;
 3685:     }
 3686:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3687:     return $datatable;
 3688: }
 3689: 
 3690: sub get_loadbalancers_config {
 3691:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3692:     return unless ((ref($servers) eq 'HASH') &&
 3693:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3694:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3695:     if (keys(%{$existing}) > 0) {
 3696:         my $oldlonhost;
 3697:         foreach my $key (sort(keys(%{$existing}))) {
 3698:             if ($key eq 'lonhost') {
 3699:                 $oldlonhost = $existing->{'lonhost'};
 3700:                 $currbalancer->{$oldlonhost} = 1;
 3701:             } elsif ($key eq 'targets') {
 3702:                 if ($oldlonhost) {
 3703:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3704:                 }
 3705:             } elsif ($key eq 'rules') {
 3706:                 if ($oldlonhost) {
 3707:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3708:                 }
 3709:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3710:                 $currbalancer->{$key} = 1;
 3711:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3712:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3713:             }
 3714:         }
 3715:     } else {
 3716:         my ($balancerref,$targetsref) =
 3717:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3718:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3719:             foreach my $server (sort(keys(%{$balancerref}))) {
 3720:                 $currbalancer->{$server} = 1;
 3721:                 $currtargets->{$server} = $targetsref->{$server};
 3722:             }
 3723:         }
 3724:     }
 3725:     return;
 3726: }
 3727: 
 3728: sub loadbalancing_rules {
 3729:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3730:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3731:         $css_class,$balnum,$islast) = @_;
 3732:     my $output;
 3733:     my $num = 0;
 3734:     my ($alltypes,$othertypes,$titles) =
 3735:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3736:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3737:         foreach my $type (@{$alltypes}) {
 3738:             $num ++;
 3739:             my $current;
 3740:             if (ref($currrules) eq 'HASH') {
 3741:                 $current = $currrules->{$type};
 3742:             }
 3743:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3744:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3745:                     $current = '';
 3746:                 }
 3747:             }
 3748:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3749:                                              $servers,$currbalancer,$lonhost,$dom,
 3750:                                              $targets_div_style,$homedom_div_style,
 3751:                                              $css_class,$balnum,$num,$islast);
 3752:         }
 3753:     }
 3754:     return $output;
 3755: }
 3756: 
 3757: sub loadbalancing_titles {
 3758:     my ($dom,$intdom,$usertypes,$types) = @_;
 3759:     my %othertypes = (
 3760:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3761:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3762:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3763:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3764:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3765:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3766:                      );
 3767:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3768:     if (ref($types) eq 'ARRAY') {
 3769:         unshift(@alltypes,@{$types},'default');
 3770:     }
 3771:     my %titles;
 3772:     foreach my $type (@alltypes) {
 3773:         if ($type =~ /^_LC_/) {
 3774:             $titles{$type} = $othertypes{$type};
 3775:         } elsif ($type eq 'default') {
 3776:             $titles{$type} = &mt('All users from [_1]',$dom);
 3777:             if (ref($types) eq 'ARRAY') {
 3778:                 if (@{$types} > 0) {
 3779:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3780:                 }
 3781:             }
 3782:         } elsif (ref($usertypes) eq 'HASH') {
 3783:             $titles{$type} = $usertypes->{$type};
 3784:         }
 3785:     }
 3786:     return (\@alltypes,\%othertypes,\%titles);
 3787: }
 3788: 
 3789: sub loadbalance_rule_row {
 3790:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3791:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3792:     my @rulenames;
 3793:     my %ruletitles = &offloadtype_text();
 3794:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3795:         @rulenames = ('balancer','offloadedto');
 3796:     } else {
 3797:         @rulenames = ('default','homeserver');
 3798:         if ($type eq '_LC_external') {
 3799:             push(@rulenames,'externalbalancer');
 3800:         } else {
 3801:             push(@rulenames,'specific');
 3802:         }
 3803:         push(@rulenames,'none');
 3804:     }
 3805:     my $style = $targets_div_style;
 3806:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3807:         $style = $homedom_div_style;
 3808:     }
 3809:     my $space;
 3810:     if ($islast && $num == 1) {
 3811:         $space = '<div display="inline-block">&nbsp;</div>';
 3812:     }
 3813:     my $output =
 3814:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3815:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3816:         '<td valaign="top">'.$space.
 3817:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3818:     for (my $i=0; $i<@rulenames; $i++) {
 3819:         my $rule = $rulenames[$i];
 3820:         my ($checked,$extra);
 3821:         if ($rulenames[$i] eq 'default') {
 3822:             $rule = '';
 3823:         }
 3824:         if ($rulenames[$i] eq 'specific') {
 3825:             if (ref($servers) eq 'HASH') {
 3826:                 my $default;
 3827:                 if (($current ne '') && (exists($servers->{$current}))) {
 3828:                     $checked = ' checked="checked"';
 3829:                 }
 3830:                 unless ($checked) {
 3831:                     $default = ' selected="selected"';
 3832:                 }
 3833:                 $extra =
 3834:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3835:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3836:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3837:                     '<option value=""'.$default.'></option>'."\n";
 3838:                 foreach my $server (sort(keys(%{$servers}))) {
 3839:                     if (ref($currbalancer) eq 'HASH') {
 3840:                         next if (exists($currbalancer->{$server}));
 3841:                     }
 3842:                     my $selected;
 3843:                     if ($server eq $current) {
 3844:                         $selected = ' selected="selected"';
 3845:                     }
 3846:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3847:                 }
 3848:                 $extra .= '</select>';
 3849:             }
 3850:         } elsif ($rule eq $current) {
 3851:             $checked = ' checked="checked"';
 3852:         }
 3853:         $output .= '<span class="LC_nobreak"><label>'.
 3854:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3855:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3856:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3857:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3858:                    '</label>'.$extra.'</span><br />'."\n";
 3859:     }
 3860:     $output .= '</div></td></tr>'."\n";
 3861:     return $output;
 3862: }
 3863: 
 3864: sub offloadtype_text {
 3865:     my %ruletitles = &Apache::lonlocal::texthash (
 3866:            'default'          => 'Offloads to default destinations',
 3867:            'homeserver'       => "Offloads to user's home server",
 3868:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3869:            'specific'         => 'Offloads to specific server',
 3870:            'none'             => 'No offload',
 3871:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3872:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3873:     );
 3874:     return %ruletitles;
 3875: }
 3876: 
 3877: sub sparestype_titles {
 3878:     my %typestitles = &Apache::lonlocal::texthash (
 3879:                           'primary' => 'primary',
 3880:                           'default' => 'default',
 3881:                       );
 3882:     return %typestitles;
 3883: }
 3884: 
 3885: sub contact_titles {
 3886:     my %titles = &Apache::lonlocal::texthash (
 3887:                    'supportemail' => 'Support E-mail address',
 3888:                    'adminemail'   => 'Default Server Admin E-mail address',
 3889:                    'errormail'    => 'Error reports to be e-mailed to',
 3890:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3891:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3892:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3893:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3894:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3895:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3896:                  );
 3897:     my %short_titles = &Apache::lonlocal::texthash (
 3898:                            adminemail   => 'Admin E-mail address',
 3899:                            supportemail => 'Support E-mail',
 3900:                        );   
 3901:     return (\%titles,\%short_titles);
 3902: }
 3903: 
 3904: sub tool_titles {
 3905:     my %titles = &Apache::lonlocal::texthash (
 3906:                      aboutme    => 'Personal web page',
 3907:                      blog       => 'Blog',
 3908:                      webdav     => 'WebDAV',
 3909:                      portfolio  => 'Portfolio',
 3910:                      official   => 'Official courses (with institutional codes)',
 3911:                      unofficial => 'Unofficial courses',
 3912:                      community  => 'Communities',
 3913:                      textbook   => 'Textbook courses',
 3914:                  );
 3915:     return %titles;
 3916: }
 3917: 
 3918: sub courserequest_titles {
 3919:     my %titles = &Apache::lonlocal::texthash (
 3920:                                    official   => 'Official',
 3921:                                    unofficial => 'Unofficial',
 3922:                                    community  => 'Communities',
 3923:                                    textbook   => 'Textbook',
 3924:                                    norequest  => 'Not allowed',
 3925:                                    approval   => 'Approval by Dom. Coord.',
 3926:                                    validate   => 'With validation',
 3927:                                    autolimit  => 'Numerical limit',
 3928:                                    unlimited  => '(blank for unlimited)',
 3929:                  );
 3930:     return %titles;
 3931: }
 3932: 
 3933: sub authorrequest_titles {
 3934:     my %titles = &Apache::lonlocal::texthash (
 3935:                                    norequest  => 'Not allowed',
 3936:                                    approval   => 'Approval by Dom. Coord.',
 3937:                                    automatic  => 'Automatic approval',
 3938:                  );
 3939:     return %titles;
 3940: }
 3941: 
 3942: sub courserequest_conditions {
 3943:     my %conditions = &Apache::lonlocal::texthash (
 3944:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3945:        validate   => '(Processing of request subject to institutional validation).',
 3946:                  );
 3947:     return %conditions;
 3948: }
 3949: 
 3950: 
 3951: sub print_usercreation {
 3952:     my ($position,$dom,$settings,$rowtotal) = @_;
 3953:     my $numinrow = 4;
 3954:     my $datatable;
 3955:     if ($position eq 'top') {
 3956:         $$rowtotal ++;
 3957:         my $rowcount = 0;
 3958:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3959:         if (ref($rules) eq 'HASH') {
 3960:             if (keys(%{$rules}) > 0) {
 3961:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3962:                                                 $ruleorder,$numinrow,$rowcount);
 3963:                 $$rowtotal ++;
 3964:                 $rowcount ++;
 3965:             }
 3966:         }
 3967:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3968:         if (ref($idrules) eq 'HASH') {
 3969:             if (keys(%{$idrules}) > 0) {
 3970:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3971:                                                 $idruleorder,$numinrow,$rowcount);
 3972:                 $$rowtotal ++;
 3973:                 $rowcount ++;
 3974:             }
 3975:         }
 3976:         if ($rowcount == 0) {
 3977:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3978:             $$rowtotal ++;
 3979:             $rowcount ++;
 3980:         }
 3981:     } elsif ($position eq 'middle') {
 3982:         my @creators = ('author','course','requestcrs');
 3983:         my ($rules,$ruleorder) =
 3984:             &Apache::lonnet::inst_userrules($dom,'username');
 3985:         my %lt = &usercreation_types();
 3986:         my %checked;
 3987:         if (ref($settings) eq 'HASH') {
 3988:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3989:                 foreach my $item (@creators) {
 3990:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3991:                 }
 3992:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3993:                 foreach my $item (@creators) {
 3994:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3995:                         $checked{$item} = 'none';
 3996:                     }
 3997:                 }
 3998:             }
 3999:         }
 4000:         my $rownum = 0;
 4001:         foreach my $item (@creators) {
 4002:             $rownum ++;
 4003:             if ($checked{$item} eq '') {
 4004:                 $checked{$item} = 'any';
 4005:             }
 4006:             my $css_class;
 4007:             if ($rownum%2) {
 4008:                 $css_class = '';
 4009:             } else {
 4010:                 $css_class = ' class="LC_odd_row" ';
 4011:             }
 4012:             $datatable .= '<tr'.$css_class.'>'.
 4013:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 4014:                          '</span></td><td align="right">';
 4015:             my @options = ('any');
 4016:             if (ref($rules) eq 'HASH') {
 4017:                 if (keys(%{$rules}) > 0) {
 4018:                     push(@options,('official','unofficial'));
 4019:                 }
 4020:             }
 4021:             push(@options,'none');
 4022:             foreach my $option (@options) {
 4023:                 my $type = 'radio';
 4024:                 my $check = ' ';
 4025:                 if ($checked{$item} eq $option) {
 4026:                     $check = ' checked="checked" ';
 4027:                 } 
 4028:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4029:                               '<input type="'.$type.'" name="can_createuser_'.
 4030:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 4031:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 4032:             }
 4033:             $datatable .= '</td></tr>';
 4034:         }
 4035:     } else {
 4036:         my @contexts = ('author','course','domain');
 4037:         my @authtypes = ('int','krb4','krb5','loc');
 4038:         my %checked;
 4039:         if (ref($settings) eq 'HASH') {
 4040:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 4041:                 foreach my $item (@contexts) {
 4042:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 4043:                         foreach my $auth (@authtypes) {
 4044:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 4045:                                 $checked{$item}{$auth} = ' checked="checked" ';
 4046:                             }
 4047:                         }
 4048:                     }
 4049:                 }
 4050:             }
 4051:         } else {
 4052:             foreach my $item (@contexts) {
 4053:                 foreach my $auth (@authtypes) {
 4054:                     $checked{$item}{$auth} = ' checked="checked" ';
 4055:                 }
 4056:             }
 4057:         }
 4058:         my %title = &context_names();
 4059:         my %authname = &authtype_names();
 4060:         my $rownum = 0;
 4061:         my $css_class; 
 4062:         foreach my $item (@contexts) {
 4063:             if ($rownum%2) {
 4064:                 $css_class = '';
 4065:             } else {
 4066:                 $css_class = ' class="LC_odd_row" ';
 4067:             }
 4068:             $datatable .=   '<tr'.$css_class.'>'.
 4069:                             '<td>'.$title{$item}.
 4070:                             '</td><td class="LC_left_item">'.
 4071:                             '<span class="LC_nobreak">';
 4072:             foreach my $auth (@authtypes) {
 4073:                 $datatable .= '<label>'. 
 4074:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4075:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4076:                               $authname{$auth}.'</label>&nbsp;';
 4077:             }
 4078:             $datatable .= '</span></td></tr>';
 4079:             $rownum ++;
 4080:         }
 4081:         $$rowtotal += $rownum;
 4082:     }
 4083:     return $datatable;
 4084: }
 4085: 
 4086: sub print_selfcreation {
 4087:     my ($position,$dom,$settings,$rowtotal) = @_;
 4088:     my (@selfcreate,$createsettings,$processing,$datatable);
 4089:     if (ref($settings) eq 'HASH') {
 4090:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4091:             $createsettings = $settings->{'cancreate'};
 4092:             if (ref($createsettings) eq 'HASH') {
 4093:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4094:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4095:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4096:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4097:                         @selfcreate = ('email','login','sso');
 4098:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4099:                         @selfcreate = ($createsettings->{'selfcreate'});
 4100:                     }
 4101:                 }
 4102:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4103:                     $processing = $createsettings->{'selfcreateprocessing'};
 4104:                 }
 4105:             }
 4106:         }
 4107:     }
 4108:     my %radiohash;
 4109:     my $numinrow = 4;
 4110:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4111:     if ($position eq 'top') {
 4112:         my %choices = &Apache::lonlocal::texthash (
 4113:                                                       cancreate_login      => 'Institutional Login',
 4114:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4115:                                                   );
 4116:         my @toggles = sort(keys(%choices));
 4117:         my %defaultchecked = (
 4118:                                'cancreate_login' => 'off',
 4119:                                'cancreate_sso'   => 'off',
 4120:                              );
 4121:         my ($onclick,$itemcount);
 4122:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4123:                                                      \%choices,$itemcount,$onclick);
 4124:         $$rowtotal += $itemcount;
 4125:         
 4126:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4127: 
 4128:         if (ref($usertypes) eq 'HASH') {
 4129:             if (keys(%{$usertypes}) > 0) {
 4130:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4131:                                              $dom,$numinrow,$othertitle,
 4132:                                              'statustocreate',$$rowtotal);
 4133:                 $$rowtotal ++;
 4134:             }
 4135:         }
 4136:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 4137:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4138:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 4139:         my $rem;
 4140:         my $numperrow = 2;
 4141:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4142:         $datatable .= '<tr'.$css_class.'>'.
 4143:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 4144:                      '<td class="LC_left_item">'."\n".
 4145:                      '<table><tr><td>'."\n";
 4146:         for (my $i=0; $i<@fields; $i++) {
 4147:             $rem = $i%($numperrow);
 4148:             if ($rem == 0) {
 4149:                 if ($i > 0) {
 4150:                     $datatable .= '</tr>';
 4151:                 }
 4152:                 $datatable .= '<tr>';
 4153:             }
 4154:             my $currval;
 4155:             if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 4156:                 $currval = $createsettings->{'shibenv'}{$fields[$i]};
 4157:             }
 4158:             $datatable .= '<td class="LC_left_item">'.
 4159:                           '<span class="LC_nobreak">'.
 4160:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 4161:                           'value="'.$currval.'" size="10" />&nbsp;'.
 4162:                           $fieldtitles{$fields[$i]}.'</span></td>';
 4163:         }
 4164:         my $colsleft = $numperrow - $rem;
 4165:         if ($colsleft > 1 ) {
 4166:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4167:                          '&nbsp;</td>';
 4168:         } elsif ($colsleft == 1) {
 4169:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4170:         }
 4171:         $datatable .= '</tr></table></td></tr>';
 4172:         $$rowtotal ++;
 4173:     } elsif ($position eq 'middle') {
 4174:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4175:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4176:         $usertypes->{'default'} = $othertitle;
 4177:         if (ref($types) eq 'ARRAY') {
 4178:             push(@{$types},'default');
 4179:             $usertypes->{'default'} = $othertitle;
 4180:             foreach my $status (@{$types}) {
 4181:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4182:                                                        $numinrow,$$rowtotal,$usertypes);
 4183:                 $$rowtotal ++;
 4184:             }
 4185:         }
 4186:     } else {
 4187:         my %choices = &Apache::lonlocal::texthash (
 4188:                                                       cancreate_email => 'E-mail address as username',
 4189:                                                   );
 4190:         my @toggles = sort(keys(%choices));
 4191:         my %defaultchecked = (
 4192:                                'cancreate_email' => 'off',
 4193:                              );
 4194:         my $itemcount = 0;
 4195:         my $display = 'none';
 4196:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 4197:             $display = 'block';
 4198:         }
 4199:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 4200:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 4201:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4202:         my $usertypes = {};
 4203:         my $order = [];
 4204:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 4205:             $usertypes = $domdefaults{'inststatustypes'};
 4206:             $order = $domdefaults{'inststatusguest'};
 4207:         }
 4208:         if (ref($order) eq 'ARRAY') {
 4209:             push(@{$order},'default');
 4210:             if (@{$order} > 1) {
 4211:                 $usertypes->{'default'} = &mt('Other users');
 4212:                 $additional .= '<table><tr>';
 4213:                 foreach my $status (@{$order}) {
 4214:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 4215:                 }
 4216:                 $additional .= '</tr><tr>';
 4217:                 foreach my $status (@{$order}) {
 4218:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 4219:                 }
 4220:                 $additional .= '</tr></table>';
 4221:             } else {
 4222:                 $usertypes->{'default'} = &mt('All users');
 4223:                 $additional .= &email_as_username($rowtotal,$processing);
 4224:             }
 4225:         }
 4226:         $additional .= '</div>'."\n";
 4227: 
 4228:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4229:                                                      \%choices,$$rowtotal,$onclick,$additional);
 4230:         $$rowtotal ++;
 4231:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4232:         $$rowtotal ++;
 4233:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4234:         $numinrow = 1;
 4235:         if (ref($order) eq 'ARRAY') {
 4236:             foreach my $status (@{$order}) {
 4237:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4238:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4239:                 $$rowtotal ++;
 4240:             }
 4241:         }
 4242:         my ($emailrules,$emailruleorder) =
 4243:             &Apache::lonnet::inst_userrules($dom,'email');
 4244:         if (ref($emailrules) eq 'HASH') {
 4245:             if (keys(%{$emailrules}) > 0) {
 4246:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4247:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4248:                 $$rowtotal ++;
 4249:             }
 4250:         }
 4251:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4252:     }
 4253:     return $datatable;
 4254: }
 4255: 
 4256: sub email_as_username {
 4257:     my ($rowtotal,$processing,$type) = @_;
 4258:     my %choices =
 4259:         &Apache::lonlocal::texthash (
 4260:                                       automatic => 'Automatic approval',
 4261:                                       approval  => 'Queued for approval',
 4262:                                     );
 4263:     my $output;
 4264:     foreach my $option ('automatic','approval') {
 4265:         my $checked;
 4266:         if (ref($processing) eq 'HASH') {
 4267:             if ($type eq '') {   
 4268:                 if (!exists($processing->{'default'})) {
 4269:                     if ($option eq 'automatic') {
 4270:                         $checked = ' checked="checked"';
 4271:                     }
 4272:                 } else {
 4273:                     if ($processing->{'default'} eq $option) {
 4274:                         $checked = ' checked="checked"';
 4275:                     }
 4276:                 }
 4277:             } else {
 4278:                 if (!exists($processing->{$type})) {
 4279:                     if ($option eq 'automatic') {
 4280:                         $checked = ' checked="checked"';
 4281:                     }
 4282:                 } else {
 4283:                     if ($processing->{$type} eq $option) {
 4284:                         $checked = ' checked="checked"';
 4285:                     }
 4286:                 }
 4287:             }
 4288:         } elsif ($option eq 'automatic') {
 4289:             $checked = ' checked="checked"'; 
 4290:         }
 4291:         my $name = 'cancreate_emailprocess';
 4292:         if (($type ne '') && ($type ne 'default')) {
 4293:             $name .= '_'.$type;
 4294:         }
 4295:         $output .= '<span class="LC_nobreak"><label>'.
 4296:                    '<input type="radio" name="'.$name.'"'.
 4297:                    $checked.' value="'.$option.'" />'.
 4298:                    $choices{$option}.'</label></span>';
 4299:         if ($type eq '') {
 4300:             $output .= '&nbsp;';
 4301:         } else {
 4302:             $output .= '<br />';
 4303:         }
 4304:     }
 4305:     $$rowtotal ++;
 4306:     return $output;
 4307: }
 4308: 
 4309: sub captcha_choice {
 4310:     my ($context,$settings,$itemcount) = @_;
 4311:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4312:     my %lt = &captcha_phrases();
 4313:     $keyentry = 'hidden';
 4314:     if ($context eq 'cancreate') {
 4315:         $rowname = &mt('CAPTCHA validation');
 4316:     } elsif ($context eq 'login') {
 4317:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4318:     }
 4319:     if (ref($settings) eq 'HASH') {
 4320:         if ($settings->{'captcha'}) {
 4321:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4322:         } else {
 4323:             $checked{'original'} = ' checked="checked"';
 4324:         }
 4325:         if ($settings->{'captcha'} eq 'recaptcha') {
 4326:             $pubtext = $lt{'pub'};
 4327:             $privtext = $lt{'priv'};
 4328:             $keyentry = 'text';
 4329:         }
 4330:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4331:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4332:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4333:         }
 4334:     } else {
 4335:         $checked{'original'} = ' checked="checked"';
 4336:     }
 4337:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4338:     my $output = '<tr'.$css_class.'>'.
 4339:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4340:                  '<table><tr><td>'."\n";
 4341:     foreach my $option ('original','recaptcha','notused') {
 4342:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4343:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4344:                    $lt{$option}.'</label></span>';
 4345:         unless ($option eq 'notused') {
 4346:             $output .= ('&nbsp;'x2)."\n";
 4347:         }
 4348:     }
 4349: #
 4350: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4351: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4352: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4353: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4354: #
 4355:     $output .= '</td></tr>'."\n".
 4356:                '<tr><td>'."\n".
 4357:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4358:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4359:                $currpub.'" size="40" /></span><br />'."\n".
 4360:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4361:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4362:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4363:                '</td></tr>';
 4364:     return $output;
 4365: }
 4366: 
 4367: sub user_formats_row {
 4368:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4369:     my $output;
 4370:     my %text = (
 4371:                    'username' => 'new usernames',
 4372:                    'id'       => 'IDs',
 4373:                    'email'    => 'self-created accounts (e-mail)',
 4374:                );
 4375:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4376:     $output = '<tr '.$css_class.'>'.
 4377:               '<td><span class="LC_nobreak">';
 4378:     if ($type eq 'email') {
 4379:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4380:     } else {
 4381:         $output .= &mt("Format rules to check for $text{$type}: ");
 4382:     }
 4383:     $output .= '</span></td>'.
 4384:                '<td class="LC_left_item" colspan="2"><table>';
 4385:     my $rem;
 4386:     if (ref($ruleorder) eq 'ARRAY') {
 4387:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4388:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4389:                 my $rem = $i%($numinrow);
 4390:                 if ($rem == 0) {
 4391:                     if ($i > 0) {
 4392:                         $output .= '</tr>';
 4393:                     }
 4394:                     $output .= '<tr>';
 4395:                 }
 4396:                 my $check = ' ';
 4397:                 if (ref($settings) eq 'HASH') {
 4398:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4399:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4400:                             $check = ' checked="checked" ';
 4401:                         }
 4402:                     }
 4403:                 }
 4404:                 $output .= '<td class="LC_left_item">'.
 4405:                            '<span class="LC_nobreak"><label>'.
 4406:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4407:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4408:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4409:             }
 4410:         }
 4411:         $rem = @{$ruleorder}%($numinrow);
 4412:     }
 4413:     my $colsleft = $numinrow - $rem;
 4414:     if ($colsleft > 1 ) {
 4415:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4416:                    '&nbsp;</td>';
 4417:     } elsif ($colsleft == 1) {
 4418:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4419:     }
 4420:     $output .= '</tr></table></td></tr>';
 4421:     return $output;
 4422: }
 4423: 
 4424: sub usercreation_types {
 4425:     my %lt = &Apache::lonlocal::texthash (
 4426:                     author     => 'When adding a co-author',
 4427:                     course     => 'When adding a user to a course',
 4428:                     requestcrs => 'When requesting a course',
 4429:                     any        => 'Any',
 4430:                     official   => 'Institutional only ',
 4431:                     unofficial => 'Non-institutional only',
 4432:                     none       => 'None',
 4433:     );
 4434:     return %lt;
 4435: }
 4436: 
 4437: sub selfcreation_types {
 4438:     my %lt = &Apache::lonlocal::texthash (
 4439:                     selfcreate => 'User creates own account',
 4440:                     any        => 'Any',
 4441:                     official   => 'Institutional only ',
 4442:                     unofficial => 'Non-institutional only',
 4443:                     email      => 'E-mail address',
 4444:                     login      => 'Institutional Login',
 4445:                     sso        => 'SSO',
 4446:              );
 4447: }
 4448: 
 4449: sub authtype_names {
 4450:     my %lt = &Apache::lonlocal::texthash(
 4451:                       int    => 'Internal',
 4452:                       krb4   => 'Kerberos 4',
 4453:                       krb5   => 'Kerberos 5',
 4454:                       loc    => 'Local',
 4455:                   );
 4456:     return %lt;
 4457: }
 4458: 
 4459: sub context_names {
 4460:     my %context_title = &Apache::lonlocal::texthash(
 4461:        author => 'Creating users when an Author',
 4462:        course => 'Creating users when in a course',
 4463:        domain => 'Creating users when a Domain Coordinator',
 4464:     );
 4465:     return %context_title;
 4466: }
 4467: 
 4468: sub print_usermodification {
 4469:     my ($position,$dom,$settings,$rowtotal) = @_;
 4470:     my $numinrow = 4;
 4471:     my ($context,$datatable,$rowcount);
 4472:     if ($position eq 'top') {
 4473:         $rowcount = 0;
 4474:         $context = 'author'; 
 4475:         foreach my $role ('ca','aa') {
 4476:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4477:                                                    $numinrow,$rowcount);
 4478:             $$rowtotal ++;
 4479:             $rowcount ++;
 4480:         }
 4481:     } elsif ($position eq 'bottom') {
 4482:         $context = 'course';
 4483:         $rowcount = 0;
 4484:         foreach my $role ('st','ep','ta','in','cr') {
 4485:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4486:                                                    $numinrow,$rowcount);
 4487:             $$rowtotal ++;
 4488:             $rowcount ++;
 4489:         }
 4490:     }
 4491:     return $datatable;
 4492: }
 4493: 
 4494: sub print_defaults {
 4495:     my ($position,$dom,$settings,$rowtotal) = @_;
 4496:     my $rownum = 0;
 4497:     my ($datatable,$css_class);
 4498:     if ($position eq 'top') {
 4499:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4500:                      'datelocale_def','portal_def');
 4501:         my %defaults;
 4502:         if (ref($settings) eq 'HASH') {
 4503:             %defaults = %{$settings};
 4504:         } else {
 4505:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4506:             foreach my $item (@items) {
 4507:                 $defaults{$item} = $domdefaults{$item};
 4508:             }
 4509:         }
 4510:         my $titles = &defaults_titles($dom);
 4511:         foreach my $item (@items) {
 4512:             if ($rownum%2) {
 4513:                 $css_class = '';
 4514:             } else {
 4515:                 $css_class = ' class="LC_odd_row" ';
 4516:             }
 4517:             $datatable .= '<tr'.$css_class.'>'.
 4518:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4519:                           '</span></td><td class="LC_right_item" colspan="3">';
 4520:             if ($item eq 'auth_def') {
 4521:                 my @authtypes = ('internal','krb4','krb5','localauth');
 4522:                 my %shortauth = (
 4523:                                  internal => 'int',
 4524:                                  krb4 => 'krb4',
 4525:                                  krb5 => 'krb5',
 4526:                                  localauth  => 'loc'
 4527:                                 );
 4528:                 my %authnames = &authtype_names();
 4529:                 foreach my $auth (@authtypes) {
 4530:                     my $checked = ' ';
 4531:                     if ($defaults{$item} eq $auth) {
 4532:                         $checked = ' checked="checked" ';
 4533:                     }
 4534:                     $datatable .= '<label><input type="radio" name="'.$item.
 4535:                                   '" value="'.$auth.'"'.$checked.'/>'.
 4536:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4537:                 }
 4538:             } elsif ($item eq 'timezone_def') {
 4539:                 my $includeempty = 1;
 4540:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4541:             } elsif ($item eq 'datelocale_def') {
 4542:                 my $includeempty = 1;
 4543:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4544:             } elsif ($item eq 'lang_def') {
 4545:                 my %langchoices = &get_languages_hash();
 4546:                 $langchoices{''} = 'No language preference';
 4547:                 %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4548:                 $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4549:                                                               \%langchoices);
 4550:             } else {
 4551:                 my $size;
 4552:                 if ($item eq 'portal_def') {
 4553:                     $size = ' size="25"';
 4554:                 }
 4555:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4556:                               $defaults{$item}.'"'.$size.' />';
 4557:             }
 4558:             $datatable .= '</td></tr>';
 4559:             $rownum ++;
 4560:         }
 4561:     } else {
 4562:         my (%defaults);
 4563:         if (ref($settings) eq 'HASH') {
 4564:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 4565:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 4566:                 my $maxnum = @{$settings->{'inststatusorder'}};
 4567:                 for (my $i=0; $i<$maxnum; $i++) {
 4568:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 4569:                     my $item = $settings->{'inststatusorder'}->[$i];
 4570:                     my $title = $settings->{'inststatustypes'}->{$item};
 4571:                     my $guestok;
 4572:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 4573:                         $guestok = 1;
 4574:                     }
 4575:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 4576:                     $datatable .= '<tr'.$css_class.'>'.
 4577:                                   '<td><span class="LC_nobreak">'.
 4578:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 4579:                     for (my $k=0; $k<=$maxnum; $k++) {
 4580:                         my $vpos = $k+1;
 4581:                         my $selstr;
 4582:                         if ($k == $i) {
 4583:                             $selstr = ' selected="selected" ';
 4584:                         }
 4585:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4586:                     }
 4587:                     my ($checkedon,$checkedoff);
 4588:                     $checkedoff = ' checked="checked"';
 4589:                     if ($guestok) {
 4590:                         $checkedon = $checkedoff;
 4591:                         $checkedoff = ''; 
 4592:                     }
 4593:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 4594:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 4595:                                   &mt('delete').'</span></td>'.
 4596:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 4597:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 4598:                                   '</span></td>'.
 4599:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4600:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 4601:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 4602:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 4603:                                   &mt('No').'</label></span></td></tr>';
 4604:                 }
 4605:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 4606:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 4607:                 $datatable .= '<tr '.$css_class.'>'.
 4608:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 4609:                 for (my $k=0; $k<=$maxnum; $k++) {
 4610:                     my $vpos = $k+1;
 4611:                     my $selstr;
 4612:                     if ($k == $maxnum) {
 4613:                         $selstr = ' selected="selected" ';
 4614:                     }
 4615:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4616:                 }
 4617:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 4618:                               '<input type="text" size="10" name="addinststatus" value="" /></span>'.
 4619:                               '&nbsp;'.&mt('(new)').
 4620:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 4621:                               &mt('Name displayed:').
 4622:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 4623:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 4624:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 4625:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 4626:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 4627:                               &mt('No').'</label></span></td></tr>';
 4628:                               '</tr>'."\n";
 4629:                 $rownum ++;
 4630:             }
 4631:         }
 4632:     }
 4633:     $$rowtotal += $rownum;
 4634:     return $datatable;
 4635: }
 4636: 
 4637: sub get_languages_hash {
 4638:     my %langchoices;
 4639:     foreach my $id (&Apache::loncommon::languageids()) {
 4640:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4641:         if ($code ne '') {
 4642:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4643:         }
 4644:     }
 4645:     return %langchoices;
 4646: }
 4647: 
 4648: sub defaults_titles {
 4649:     my ($dom) = @_;
 4650:     my %titles = &Apache::lonlocal::texthash (
 4651:                    'auth_def'      => 'Default authentication type',
 4652:                    'auth_arg_def'  => 'Default authentication argument',
 4653:                    'lang_def'      => 'Default language',
 4654:                    'timezone_def'  => 'Default timezone',
 4655:                    'datelocale_def' => 'Default locale for dates',
 4656:                    'portal_def'     => 'Portal/Default URL',
 4657:                  );
 4658:     if ($dom) {
 4659:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4660:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4661:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4662:         $protocol = 'http' if ($protocol ne 'https');
 4663:         if ($uint_dom) {
 4664:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4665:                                          $uint_dom);
 4666:         }
 4667:     }
 4668:     return (\%titles);
 4669: }
 4670: 
 4671: sub print_scantronformat {
 4672:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4673:     my $itemcount = 1;
 4674:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4675:         %confhash);
 4676:     my $switchserver = &check_switchserver($dom,$confname);
 4677:     my %lt = &Apache::lonlocal::texthash (
 4678:                 default => 'Default bubblesheet format file error',
 4679:                 custom  => 'Custom bubblesheet format file error',
 4680:              );
 4681:     my %scantronfiles = (
 4682:         default => 'default.tab',
 4683:         custom => 'custom.tab',
 4684:     );
 4685:     foreach my $key (keys(%scantronfiles)) {
 4686:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4687:                               .$scantronfiles{$key};
 4688:     }
 4689:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4690:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4691:         if (!$switchserver) {
 4692:             my $servadm = $r->dir_config('lonAdmEMail');
 4693:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4694:             if ($configuserok eq 'ok') {
 4695:                 if ($author_ok eq 'ok') {
 4696:                     my %legacyfile = (
 4697:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4698:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4699:                     );
 4700:                     my %md5chk;
 4701:                     foreach my $type (keys(%legacyfile)) {
 4702:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4703:                         chomp($md5chk{$type});
 4704:                     }
 4705:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4706:                         foreach my $type (keys(%legacyfile)) {
 4707:                             ($scantronurls{$type},my $error) = 
 4708:                                 &legacy_scantronformat($r,$dom,$confname,
 4709:                                                  $type,$legacyfile{$type},
 4710:                                                  $scantronurls{$type},
 4711:                                                  $scantronfiles{$type});
 4712:                             if ($error ne '') {
 4713:                                 $error{$type} = $error;
 4714:                             }
 4715:                         }
 4716:                         if (keys(%error) == 0) {
 4717:                             $is_custom = 1;
 4718:                             $confhash{'scantron'}{'scantronformat'} = 
 4719:                                 $scantronurls{'custom'};
 4720:                             my $putresult = 
 4721:                                 &Apache::lonnet::put_dom('configuration',
 4722:                                                          \%confhash,$dom);
 4723:                             if ($putresult ne 'ok') {
 4724:                                 $error{'custom'} = 
 4725:                                     '<span class="LC_error">'.
 4726:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4727:                             }
 4728:                         }
 4729:                     } else {
 4730:                         ($scantronurls{'default'},my $error) =
 4731:                             &legacy_scantronformat($r,$dom,$confname,
 4732:                                           'default',$legacyfile{'default'},
 4733:                                           $scantronurls{'default'},
 4734:                                           $scantronfiles{'default'});
 4735:                         if ($error eq '') {
 4736:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4737:                             my $putresult =
 4738:                                 &Apache::lonnet::put_dom('configuration',
 4739:                                                          \%confhash,$dom);
 4740:                             if ($putresult ne 'ok') {
 4741:                                 $error{'default'} =
 4742:                                     '<span class="LC_error">'.
 4743:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4744:                             }
 4745:                         } else {
 4746:                             $error{'default'} = $error;
 4747:                         }
 4748:                     }
 4749:                 }
 4750:             }
 4751:         } else {
 4752:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4753:         }
 4754:     }
 4755:     if (ref($settings) eq 'HASH') {
 4756:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4757:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4758:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4759:                 $scantronurl = '';
 4760:             } else {
 4761:                 $scantronurl = $settings->{'scantronformat'};
 4762:             }
 4763:             $is_custom = 1;
 4764:         } else {
 4765:             $scantronurl = $scantronurls{'default'};
 4766:         }
 4767:     } else {
 4768:         if ($is_custom) {
 4769:             $scantronurl = $scantronurls{'custom'};
 4770:         } else {
 4771:             $scantronurl = $scantronurls{'default'};
 4772:         }
 4773:     }
 4774:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4775:     $datatable .= '<tr'.$css_class.'>';
 4776:     if (!$is_custom) {
 4777:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4778:                       '<span class="LC_nobreak">';
 4779:         if ($scantronurl) {
 4780:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4781:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4782:         } else {
 4783:             $datatable = &mt('File unavailable for display');
 4784:         }
 4785:         $datatable .= '</span></td>';
 4786:         if (keys(%error) == 0) { 
 4787:             $datatable .= '<td valign="bottom">';
 4788:             if (!$switchserver) {
 4789:                 $datatable .= &mt('Upload:').'<br />';
 4790:             }
 4791:         } else {
 4792:             my $errorstr;
 4793:             foreach my $key (sort(keys(%error))) {
 4794:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4795:             }
 4796:             $datatable .= '<td>'.$errorstr;
 4797:         }
 4798:     } else {
 4799:         if (keys(%error) > 0) {
 4800:             my $errorstr;
 4801:             foreach my $key (sort(keys(%error))) {
 4802:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4803:             } 
 4804:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4805:         } elsif ($scantronurl) {
 4806:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4807:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4808:             $datatable .= '<td><span class="LC_nobreak">'.
 4809:                           $link.
 4810:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4811:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4812:                           '<td><span class="LC_nobreak">&nbsp;'.
 4813:                           &mt('Replace:').'</span><br />';
 4814:         }
 4815:     }
 4816:     if (keys(%error) == 0) {
 4817:         if ($switchserver) {
 4818:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4819:         } else {
 4820:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4821:                          '<input type="file" name="scantronformat" /></span>';
 4822:         }
 4823:     }
 4824:     $datatable .= '</td></tr>';
 4825:     $$rowtotal ++;
 4826:     return $datatable;
 4827: }
 4828: 
 4829: sub legacy_scantronformat {
 4830:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4831:     my ($url,$error);
 4832:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4833:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4834:         (my $result,$url) =
 4835:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4836:                          '','',$newfile);
 4837:         if ($result ne 'ok') {
 4838:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4839:         }
 4840:     }
 4841:     return ($url,$error);
 4842: }
 4843: 
 4844: sub print_coursecategories {
 4845:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4846:     my $datatable;
 4847:     if ($position eq 'top') {
 4848:         my (%checked);
 4849:         my @catitems = ('unauth','auth');
 4850:         my @cattypes = ('std','domonly','codesrch','none');
 4851:         $checked{'unauth'} = 'std';
 4852:         $checked{'auth'} = 'std';
 4853:         if (ref($settings) eq 'HASH') {
 4854:             foreach my $type (@cattypes) {
 4855:                 if ($type eq $settings->{'unauth'}) {
 4856:                     $checked{'unauth'} = $type;
 4857:                 }
 4858:                 if ($type eq $settings->{'auth'}) {
 4859:                     $checked{'auth'} = $type;
 4860:                 }
 4861:             }
 4862:         }
 4863:         my %lt = &Apache::lonlocal::texthash (
 4864:                                                unauth   => 'Catalog type for unauthenticated users',
 4865:                                                auth     => 'Catalog type for authenticated users',
 4866:                                                none     => 'No catalog',
 4867:                                                std      => 'Standard catalog',
 4868:                                                domonly  => 'Domain-only catalog',
 4869:                                                codesrch => "Code search form",
 4870:                                              );
 4871:        my $itemcount = 0;
 4872:        foreach my $item (@catitems) {
 4873:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 4874:            $datatable .= '<tr '.$css_class.'>'.
 4875:                          '<td>'.$lt{$item}.'</td>'.
 4876:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 4877:            foreach my $type (@cattypes) {
 4878:                my $ischecked;
 4879:                if ($checked{$item} eq $type) {
 4880:                    $ischecked=' checked="checked"';
 4881:                }
 4882:                $datatable .= '<label>'.
 4883:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 4884:                              ' />'.$lt{$type}.'</label>&nbsp;';
 4885:            }
 4886:            $datatable .= '</td></tr>';
 4887:            $itemcount ++;
 4888:         }
 4889:         $$rowtotal += $itemcount;
 4890:     } elsif ($position eq 'middle') {
 4891:         my $toggle_cats_crs = ' ';
 4892:         my $toggle_cats_dom = ' checked="checked" ';
 4893:         my $can_cat_crs = ' ';
 4894:         my $can_cat_dom = ' checked="checked" ';
 4895:         my $toggle_catscomm_comm = ' ';
 4896:         my $toggle_catscomm_dom = ' checked="checked" ';
 4897:         my $can_catcomm_comm = ' ';
 4898:         my $can_catcomm_dom = ' checked="checked" ';
 4899: 
 4900:         if (ref($settings) eq 'HASH') {
 4901:             if ($settings->{'togglecats'} eq 'crs') {
 4902:                 $toggle_cats_crs = $toggle_cats_dom;
 4903:                 $toggle_cats_dom = ' ';
 4904:             }
 4905:             if ($settings->{'categorize'} eq 'crs') {
 4906:                 $can_cat_crs = $can_cat_dom;
 4907:                 $can_cat_dom = ' ';
 4908:             }
 4909:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4910:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4911:                 $toggle_catscomm_dom = ' ';
 4912:             }
 4913:             if ($settings->{'categorizecomm'} eq 'comm') {
 4914:                 $can_catcomm_comm = $can_catcomm_dom;
 4915:                 $can_catcomm_dom = ' ';
 4916:             }
 4917:         }
 4918:         my %title = &Apache::lonlocal::texthash (
 4919:                      togglecats     => 'Show/Hide a course in catalog',
 4920:                      togglecatscomm => 'Show/Hide a community in catalog',
 4921:                      categorize     => 'Assign a category to a course',
 4922:                      categorizecomm => 'Assign a category to a community',
 4923:                     );
 4924:         my %level = &Apache::lonlocal::texthash (
 4925:                      dom  => 'Set in Domain',
 4926:                      crs  => 'Set in Course',
 4927:                      comm => 'Set in Community',
 4928:                     );
 4929:         $datatable = '<tr class="LC_odd_row">'.
 4930:                   '<td>'.$title{'togglecats'}.'</td>'.
 4931:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4932:                   '<input type="radio" name="togglecats"'.
 4933:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4934:                   '<label><input type="radio" name="togglecats"'.
 4935:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4936:                   '</tr><tr>'.
 4937:                   '<td>'.$title{'categorize'}.'</td>'.
 4938:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4939:                   '<label><input type="radio" name="categorize"'.
 4940:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4941:                   '<label><input type="radio" name="categorize"'.
 4942:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4943:                   '</tr><tr class="LC_odd_row">'.
 4944:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4945:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4946:                   '<input type="radio" name="togglecatscomm"'.
 4947:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4948:                   '<label><input type="radio" name="togglecatscomm"'.
 4949:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4950:                   '</tr><tr>'.
 4951:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4952:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4953:                   '<label><input type="radio" name="categorizecomm"'.
 4954:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4955:                   '<label><input type="radio" name="categorizecomm"'.
 4956:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4957:                   '</tr>';
 4958:         $$rowtotal += 4;
 4959:     } else {
 4960:         my $css_class;
 4961:         my $itemcount = 1;
 4962:         my $cathash; 
 4963:         if (ref($settings) eq 'HASH') {
 4964:             $cathash = $settings->{'cats'};
 4965:         }
 4966:         if (ref($cathash) eq 'HASH') {
 4967:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4968:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4969:                                                    \%allitems,\%idx,\@jsarray);
 4970:             my $maxdepth = scalar(@cats);
 4971:             my $colattrib = '';
 4972:             if ($maxdepth > 2) {
 4973:                 $colattrib = ' colspan="2" ';
 4974:             }
 4975:             my @path;
 4976:             if (@cats > 0) {
 4977:                 if (ref($cats[0]) eq 'ARRAY') {
 4978:                     my $numtop = @{$cats[0]};
 4979:                     my $maxnum = $numtop;
 4980:                     my %default_names = (
 4981:                           instcode    => &mt('Official courses'),
 4982:                           communities => &mt('Communities'),
 4983:                     );
 4984: 
 4985:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4986:                         ($cathash->{'instcode::0'} eq '') ||
 4987:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4988:                         ($cathash->{'communities::0'} eq '')) {
 4989:                         $maxnum ++;
 4990:                     }
 4991:                     my $lastidx;
 4992:                     for (my $i=0; $i<$numtop; $i++) {
 4993:                         my $parent = $cats[0][$i];
 4994:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4995:                         my $item = &escape($parent).'::0';
 4996:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4997:                         $lastidx = $idx{$item};
 4998:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4999:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 5000:                         for (my $k=0; $k<=$maxnum; $k++) {
 5001:                             my $vpos = $k+1;
 5002:                             my $selstr;
 5003:                             if ($k == $i) {
 5004:                                 $selstr = ' selected="selected" ';
 5005:                             }
 5006:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5007:                         }
 5008:                         $datatable .= '</select></span></td><td>';
 5009:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 5010:                             $datatable .=  '<span class="LC_nobreak">'
 5011:                                            .$default_names{$parent}.'</span>';
 5012:                             if ($parent eq 'instcode') {
 5013:                                 $datatable .= '<br /><span class="LC_nobreak">('
 5014:                                               .&mt('with institutional codes')
 5015:                                               .')</span></td><td'.$colattrib.'>';
 5016:                             } else {
 5017:                                 $datatable .= '<table><tr><td>';
 5018:                             }
 5019:                             $datatable .= '<span class="LC_nobreak">'
 5020:                                           .'<label><input type="radio" name="'
 5021:                                           .$parent.'" value="1" checked="checked" />'
 5022:                                           .&mt('Display').'</label>';
 5023:                             if ($parent eq 'instcode') {
 5024:                                 $datatable .= '&nbsp;';
 5025:                             } else {
 5026:                                 $datatable .= '</span></td></tr><tr><td>'
 5027:                                               .'<span class="LC_nobreak">';
 5028:                             }
 5029:                             $datatable .= '<label><input type="radio" name="'
 5030:                                           .$parent.'" value="0" />'
 5031:                                           .&mt('Do not display').'</label></span>';
 5032:                             if ($parent eq 'communities') {
 5033:                                 $datatable .= '</td></tr></table>';
 5034:                             }
 5035:                             $datatable .= '</td>';
 5036:                         } else {
 5037:                             $datatable .= $parent
 5038:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 5039:                                           .'<input type="checkbox" name="deletecategory" '
 5040:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 5041:                         }
 5042:                         my $depth = 1;
 5043:                         push(@path,$parent);
 5044:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 5045:                         pop(@path);
 5046:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 5047:                         $itemcount ++;
 5048:                     }
 5049:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5050:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 5051:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 5052:                     for (my $k=0; $k<=$maxnum; $k++) {
 5053:                         my $vpos = $k+1;
 5054:                         my $selstr;
 5055:                         if ($k == $numtop) {
 5056:                             $selstr = ' selected="selected" ';
 5057:                         }
 5058:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5059:                     }
 5060:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 5061:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 5062:                                   .'</tr>'."\n";
 5063:                     $itemcount ++;
 5064:                     foreach my $default ('instcode','communities') {
 5065:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 5066:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5067:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 5068:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 5069:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5070:                             for (my $k=0; $k<=$maxnum; $k++) {
 5071:                                 my $vpos = $k+1;
 5072:                                 my $selstr;
 5073:                                 if ($k == $maxnum) {
 5074:                                     $selstr = ' selected="selected" ';
 5075:                                 }
 5076:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5077:                             }
 5078:                             $datatable .= '</select></span></td>'.
 5079:                                           '<td><span class="LC_nobreak">'.
 5080:                                           $default_names{$default}.'</span>';
 5081:                             if ($default eq 'instcode') {
 5082:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5083:                                               .&mt('with institutional codes').')</span>';
 5084:                             }
 5085:                             $datatable .= '</td>'
 5086:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5087:                                           .&mt('Display').'</label>&nbsp;'
 5088:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5089:                                           .&mt('Do not display').'</label></span></td></tr>';
 5090:                         }
 5091:                     }
 5092:                 }
 5093:             } else {
 5094:                 $datatable .= &initialize_categories($itemcount);
 5095:             }
 5096:         } else {
 5097:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 5098:                           .&initialize_categories($itemcount);
 5099:         }
 5100:         $$rowtotal += $itemcount;
 5101:     }
 5102:     return $datatable;
 5103: }
 5104: 
 5105: sub print_serverstatuses {
 5106:     my ($dom,$settings,$rowtotal) = @_;
 5107:     my $datatable;
 5108:     my @pages = &serverstatus_pages();
 5109:     my (%namedaccess,%machineaccess);
 5110:     foreach my $type (@pages) {
 5111:         $namedaccess{$type} = '';
 5112:         $machineaccess{$type}= '';
 5113:     }
 5114:     if (ref($settings) eq 'HASH') {
 5115:         foreach my $type (@pages) {
 5116:             if (exists($settings->{$type})) {
 5117:                 if (ref($settings->{$type}) eq 'HASH') {
 5118:                     foreach my $key (keys(%{$settings->{$type}})) {
 5119:                         if ($key eq 'namedusers') {
 5120:                             $namedaccess{$type} = $settings->{$type}->{$key};
 5121:                         } elsif ($key eq 'machines') {
 5122:                             $machineaccess{$type} = $settings->{$type}->{$key};
 5123:                         }
 5124:                     }
 5125:                 }
 5126:             }
 5127:         }
 5128:     }
 5129:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 5130:     my $rownum = 0;
 5131:     my $css_class;
 5132:     foreach my $type (@pages) {
 5133:         $rownum ++;
 5134:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 5135:         $datatable .= '<tr'.$css_class.'>'.
 5136:                       '<td><span class="LC_nobreak">'.
 5137:                       $titles->{$type}.'</span></td>'.
 5138:                       '<td class="LC_left_item">'.
 5139:                       '<input type="text" name="'.$type.'_namedusers" '.
 5140:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 5141:                       '<td class="LC_right_item">'.
 5142:                       '<span class="LC_nobreak">'.
 5143:                       '<input type="text" name="'.$type.'_machines" '.
 5144:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 5145:                       '</td></tr>'."\n";
 5146:     }
 5147:     $$rowtotal += $rownum;
 5148:     return $datatable;
 5149: }
 5150: 
 5151: sub serverstatus_pages {
 5152:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 5153:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 5154:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 5155:             'uniquecodes','diskusage');
 5156: }
 5157: 
 5158: sub defaults_javascript {
 5159:     my ($settings) = @_;
 5160:     my ($output,$jstext); 
 5161:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5162:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 5163:         if ($maxnum eq '') {
 5164:             $maxnum = 0;
 5165:         }
 5166:         $maxnum ++;
 5167:         $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 5168:         return <<"ENDSCRIPT";
 5169: <script type="text/javascript">
 5170: // <![CDATA[
 5171: function reorderTypes(form,caller) {
 5172:     var changedVal;
 5173: $jstext 
 5174:     var newpos = 'addinststatus_pos';
 5175:     var current = new Array;
 5176:     var maxh = $maxnum;
 5177:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5178:     var oldVal;
 5179:     if (caller == newpos) {
 5180:         changedVal = newitemVal;
 5181:     } else {
 5182:         var curritem = 'inststatus_pos_'+caller;
 5183:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 5184:         current[newitemVal] = newpos;
 5185:     }
 5186:     for (var i=0; i<inststatuses.length; i++) {
 5187:         if (inststatuses[i] != caller) {
 5188:             var elementName = 'inststatus_pos_'+inststatuses[i];
 5189:             if (form.elements[elementName]) {
 5190:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5191:                 current[currVal] = elementName;
 5192:             }
 5193:         }
 5194:     }
 5195:     for (var j=0; j<maxh; j++) {
 5196:         if (current[j] == undefined) {
 5197:             oldVal = j;
 5198:         }
 5199:     }
 5200:     if (oldVal < changedVal) {
 5201:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5202:            var elementName = current[k];
 5203:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5204:         }
 5205:     } else {
 5206:         for (var k=changedVal; k<oldVal; k++) {
 5207:             var elementName = current[k];
 5208:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5209:         }
 5210:     }
 5211:     return;
 5212: }
 5213: 
 5214: // ]]>
 5215: </script>
 5216: 
 5217: ENDSCRIPT
 5218:     }
 5219: }
 5220: 
 5221: sub coursecategories_javascript {
 5222:     my ($settings) = @_;
 5223:     my ($output,$jstext,$cathash);
 5224:     if (ref($settings) eq 'HASH') {
 5225:         $cathash = $settings->{'cats'};
 5226:     }
 5227:     if (ref($cathash) eq 'HASH') {
 5228:         my (@cats,@jsarray,%idx);
 5229:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 5230:         if (@jsarray > 0) {
 5231:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 5232:             for (my $i=0; $i<@jsarray; $i++) {
 5233:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 5234:                     my $catstr = join('","',@{$jsarray[$i]});
 5235:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 5236:                 }
 5237:             }
 5238:         }
 5239:     } else {
 5240:         $jstext  = '    var categories = Array(1);'."\n".
 5241:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 5242:     }
 5243:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 5244:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 5245:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category.'); 
 5246:     $output = <<"ENDSCRIPT";
 5247: <script type="text/javascript">
 5248: // <![CDATA[
 5249: function reorderCats(form,parent,item,idx) {
 5250:     var changedVal;
 5251: $jstext
 5252:     var newpos = 'addcategory_pos';
 5253:     if (parent == '') {
 5254:         var has_instcode = 0;
 5255:         var maxtop = categories[idx].length;
 5256:         for (var j=0; j<maxtop; j++) {
 5257:             if (categories[idx][j] == 'instcode::0') {
 5258:                 has_instcode == 1;
 5259:             }
 5260:         }
 5261:         if (has_instcode == 0) {
 5262:             categories[idx][maxtop] = 'instcode_pos';
 5263:         }
 5264:     } else {
 5265:         newpos += '_'+parent;
 5266:     }
 5267:     var maxh = 1 + categories[idx].length;
 5268:     var current = new Array;
 5269:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5270:     if (item == newpos) {
 5271:         changedVal = newitemVal;
 5272:     } else {
 5273:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5274:         current[newitemVal] = newpos;
 5275:     }
 5276:     for (var i=0; i<categories[idx].length; i++) {
 5277:         var elementName = categories[idx][i];
 5278:         if (elementName != item) {
 5279:             if (form.elements[elementName]) {
 5280:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5281:                 current[currVal] = elementName;
 5282:             }
 5283:         }
 5284:     }
 5285:     var oldVal;
 5286:     for (var j=0; j<maxh; j++) {
 5287:         if (current[j] == undefined) {
 5288:             oldVal = j;
 5289:         }
 5290:     }
 5291:     if (oldVal < changedVal) {
 5292:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5293:            var elementName = current[k];
 5294:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5295:         }
 5296:     } else {
 5297:         for (var k=changedVal; k<oldVal; k++) {
 5298:             var elementName = current[k];
 5299:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5300:         }
 5301:     }
 5302:     return;
 5303: }
 5304: 
 5305: function categoryCheck(form) {
 5306:     if (form.elements['addcategory_name'].value == 'instcode') {
 5307:         alert('$instcode_reserved\\n$choose_again');
 5308:         return false;
 5309:     }
 5310:     if (form.elements['addcategory_name'].value == 'communities') {
 5311:         alert('$communities_reserved\\n$choose_again');
 5312:         return false;
 5313:     }
 5314:     return true;
 5315: }
 5316: 
 5317: // ]]>
 5318: </script>
 5319: 
 5320: ENDSCRIPT
 5321:     return $output;
 5322: }
 5323: 
 5324: sub initialize_categories {
 5325:     my ($itemcount) = @_;
 5326:     my ($datatable,$css_class,$chgstr);
 5327:     my %default_names = (
 5328:                       instcode    => 'Official courses (with institutional codes)',
 5329:                       communities => 'Communities',
 5330:                         );
 5331:     my $select0 = ' selected="selected"';
 5332:     my $select1 = '';
 5333:     foreach my $default ('instcode','communities') {
 5334:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5335:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 5336:         if ($default eq 'communities') {
 5337:             $select1 = $select0;
 5338:             $select0 = '';
 5339:         }
 5340:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5341:                      .'<select name="'.$default.'_pos">'
 5342:                      .'<option value="0"'.$select0.'>1</option>'
 5343:                      .'<option value="1"'.$select1.'>2</option>'
 5344:                      .'<option value="2">3</option></select>&nbsp;'
 5345:                      .$default_names{$default}
 5346:                      .'</span></td><td><span class="LC_nobreak">'
 5347:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5348:                      .&mt('Display').'</label>&nbsp;<label>'
 5349:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5350:                  .'</label></span></td></tr>';
 5351:         $itemcount ++;
 5352:     }
 5353:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5354:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5355:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5356:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5357:                   .'<option value="0">1</option>'
 5358:                   .'<option value="1">2</option>'
 5359:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5360:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5361:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5362:     return $datatable;
 5363: }
 5364: 
 5365: sub build_category_rows {
 5366:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5367:     my ($text,$name,$item,$chgstr);
 5368:     if (ref($cats) eq 'ARRAY') {
 5369:         my $maxdepth = scalar(@{$cats});
 5370:         if (ref($cats->[$depth]) eq 'HASH') {
 5371:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5372:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5373:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5374:                 $text .= '<td><table class="LC_data_table">';
 5375:                 my ($idxnum,$parent_name,$parent_item);
 5376:                 my $higher = $depth - 1;
 5377:                 if ($higher == 0) {
 5378:                     $parent_name = &escape($parent).'::'.$higher;
 5379:                 } else {
 5380:                     if (ref($path) eq 'ARRAY') {
 5381:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5382:                     }
 5383:                 }
 5384:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5385:                 for (my $j=0; $j<=$numchildren; $j++) {
 5386:                     if ($j < $numchildren) {
 5387:                         $name = $cats->[$depth]{$parent}[$j];
 5388:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5389:                         $idxnum = $idx->{$item};
 5390:                     } else {
 5391:                         $name = $parent_name;
 5392:                         $item = $parent_item;
 5393:                     }
 5394:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5395:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5396:                     for (my $i=0; $i<=$numchildren; $i++) {
 5397:                         my $vpos = $i+1;
 5398:                         my $selstr;
 5399:                         if ($j == $i) {
 5400:                             $selstr = ' selected="selected" ';
 5401:                         }
 5402:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5403:                     }
 5404:                     $text .= '</select>&nbsp;';
 5405:                     if ($j < $numchildren) {
 5406:                         my $deeper = $depth+1;
 5407:                         $text .= $name.'&nbsp;'
 5408:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5409:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5410:                         if(ref($path) eq 'ARRAY') {
 5411:                             push(@{$path},$name);
 5412:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5413:                             pop(@{$path});
 5414:                         }
 5415:                     } else {
 5416:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5417:                         if ($j == $numchildren) {
 5418:                             $text .= $name;
 5419:                         } else {
 5420:                             $text .= $item;
 5421:                         }
 5422:                         $text .= '" value="" />';
 5423:                     }
 5424:                     $text .= '</td></tr>';
 5425:                 }
 5426:                 $text .= '</table></td>';
 5427:             } else {
 5428:                 my $higher = $depth-1;
 5429:                 if ($higher == 0) {
 5430:                     $name = &escape($parent).'::'.$higher;
 5431:                 } else {
 5432:                     if (ref($path) eq 'ARRAY') {
 5433:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5434:                     }
 5435:                 }
 5436:                 my $colspan;
 5437:                 if ($parent ne 'instcode') {
 5438:                     $colspan = $maxdepth - $depth - 1;
 5439:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5440:                 }
 5441:             }
 5442:         }
 5443:     }
 5444:     return $text;
 5445: }
 5446: 
 5447: sub modifiable_userdata_row {
 5448:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5449:     my ($role,$rolename,$statustype);
 5450:     $role = $item;
 5451:     if ($context eq 'cancreate') {
 5452:         if ($item =~ /^emailusername_(.+)$/) {
 5453:             $statustype = $1;
 5454:             $role = 'emailusername';
 5455:             if (ref($usertypes) eq 'HASH') {
 5456:                 if ($usertypes->{$statustype}) {
 5457:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5458:                 } else {
 5459:                     $rolename = &mt('Data provided by user');
 5460:                 }
 5461:             }
 5462:         }
 5463:     } elsif ($context eq 'selfcreate') {
 5464:         if (ref($usertypes) eq 'HASH') {
 5465:             $rolename = $usertypes->{$role};
 5466:         } else {
 5467:             $rolename = $role;
 5468:         }
 5469:     } else {
 5470:         if ($role eq 'cr') {
 5471:             $rolename = &mt('Custom role');
 5472:         } else {
 5473:             $rolename = &Apache::lonnet::plaintext($role);
 5474:         }
 5475:     }
 5476:     my (@fields,%fieldtitles);
 5477:     if (ref($fieldsref) eq 'ARRAY') {
 5478:         @fields = @{$fieldsref};
 5479:     } else {
 5480:         @fields = ('lastname','firstname','middlename','generation',
 5481:                    'permanentemail','id');
 5482:     }
 5483:     if ((ref($titlesref) eq 'HASH')) {
 5484:         %fieldtitles = %{$titlesref};
 5485:     } else {
 5486:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5487:     }
 5488:     my $output;
 5489:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5490:     $output = '<tr '.$css_class.'>'.
 5491:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5492:               '<td class="LC_left_item" colspan="2"><table>';
 5493:     my $rem;
 5494:     my %checks;
 5495:     if (ref($settings) eq 'HASH') {
 5496:         if (ref($settings->{$context}) eq 'HASH') {
 5497:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5498:                 my $hashref = $settings->{$context}->{$role};
 5499:                 if ($role eq 'emailusername') {
 5500:                     if ($statustype) {
 5501:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5502:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5503:                             if (ref($hashref) eq 'HASH') { 
 5504:                                 foreach my $field (@fields) {
 5505:                                     if ($hashref->{$field}) {
 5506:                                         $checks{$field} = $hashref->{$field};
 5507:                                     }
 5508:                                 }
 5509:                             }
 5510:                         }
 5511:                     }
 5512:                 } else {
 5513:                     if (ref($hashref) eq 'HASH') {
 5514:                         foreach my $field (@fields) {
 5515:                             if ($hashref->{$field}) {
 5516:                                 $checks{$field} = ' checked="checked" ';
 5517:                             }
 5518:                         }
 5519:                     }
 5520:                 }
 5521:             }
 5522:         }
 5523:     }
 5524:      
 5525:     for (my $i=0; $i<@fields; $i++) {
 5526:         my $rem = $i%($numinrow);
 5527:         if ($rem == 0) {
 5528:             if ($i > 0) {
 5529:                 $output .= '</tr>';
 5530:             }
 5531:             $output .= '<tr>';
 5532:         }
 5533:         my $check = ' ';
 5534:         unless ($role eq 'emailusername') {
 5535:             if (exists($checks{$fields[$i]})) {
 5536:                 $check = $checks{$fields[$i]}
 5537:             } else {
 5538:                 if ($role eq 'st') {
 5539:                     if (ref($settings) ne 'HASH') {
 5540:                         $check = ' checked="checked" '; 
 5541:                     }
 5542:                 }
 5543:             }
 5544:         }
 5545:         $output .= '<td class="LC_left_item">'.
 5546:                    '<span class="LC_nobreak">';
 5547:         if ($role eq 'emailusername') {
 5548:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5549:                 $checks{$fields[$i]} = 'omit';
 5550:             }
 5551:             foreach my $option ('required','optional','omit') {
 5552:                 my $checked='';
 5553:                 if ($checks{$fields[$i]} eq $option) {
 5554:                     $checked='checked="checked" ';
 5555:                 }
 5556:                 $output .= '<label>'.
 5557:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5558:                            &mt($option).'</label>'.('&nbsp;' x2);
 5559:             }
 5560:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5561:         } else {
 5562:             $output .= '<label>'.
 5563:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5564:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5565:                        '</label>';
 5566:         }
 5567:         $output .= '</span></td>';
 5568:         $rem = @fields%($numinrow);
 5569:     }
 5570:     my $colsleft = $numinrow - $rem;
 5571:     if ($colsleft > 1 ) {
 5572:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5573:                    '&nbsp;</td>';
 5574:     } elsif ($colsleft == 1) {
 5575:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5576:     }
 5577:     $output .= '</tr></table></td></tr>';
 5578:     return $output;
 5579: }
 5580: 
 5581: sub insttypes_row {
 5582:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5583:     my %lt = &Apache::lonlocal::texthash (
 5584:                       cansearch => 'Users allowed to search',
 5585:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5586:                       lockablenames => 'User preference to lock name',
 5587:              );
 5588:     my $showdom;
 5589:     if ($context eq 'cansearch') {
 5590:         $showdom = ' ('.$dom.')';
 5591:     }
 5592:     my $class = 'LC_left_item';
 5593:     if ($context eq 'statustocreate') {
 5594:         $class = 'LC_right_item';
 5595:     }
 5596:     my $css_class = ' class="LC_odd_row"';
 5597:     if ($rownum ne '') { 
 5598:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5599:     }
 5600:     my $output = '<tr'.$css_class.'>'.
 5601:                  '<td>'.$lt{$context}.$showdom.
 5602:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5603:     my $rem;
 5604:     if (ref($types) eq 'ARRAY') {
 5605:         for (my $i=0; $i<@{$types}; $i++) {
 5606:             if (defined($usertypes->{$types->[$i]})) {
 5607:                 my $rem = $i%($numinrow);
 5608:                 if ($rem == 0) {
 5609:                     if ($i > 0) {
 5610:                         $output .= '</tr>';
 5611:                     }
 5612:                     $output .= '<tr>';
 5613:                 }
 5614:                 my $check = ' ';
 5615:                 if (ref($settings) eq 'HASH') {
 5616:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5617:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5618:                             $check = ' checked="checked" ';
 5619:                         }
 5620:                     } elsif ($context eq 'statustocreate') {
 5621:                         $check = ' checked="checked" ';
 5622:                     }
 5623:                 }
 5624:                 $output .= '<td class="LC_left_item">'.
 5625:                            '<span class="LC_nobreak"><label>'.
 5626:                            '<input type="checkbox" name="'.$context.'" '.
 5627:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5628:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5629:             }
 5630:         }
 5631:         $rem = @{$types}%($numinrow);
 5632:     }
 5633:     my $colsleft = $numinrow - $rem;
 5634:     if (($rem == 0) && (@{$types} > 0)) {
 5635:         $output .= '<tr>';
 5636:     }
 5637:     if ($colsleft > 1) {
 5638:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5639:     } else {
 5640:         $output .= '<td class="LC_left_item">';
 5641:     }
 5642:     my $defcheck = ' ';
 5643:     if (ref($settings) eq 'HASH') {  
 5644:         if (ref($settings->{$context}) eq 'ARRAY') {
 5645:             if (grep(/^default$/,@{$settings->{$context}})) {
 5646:                 $defcheck = ' checked="checked" ';
 5647:             }
 5648:         } elsif ($context eq 'statustocreate') {
 5649:             $defcheck = ' checked="checked" ';
 5650:         }
 5651:     }
 5652:     $output .= '<span class="LC_nobreak"><label>'.
 5653:                '<input type="checkbox" name="'.$context.'" '.
 5654:                'value="default"'.$defcheck.'/>'.
 5655:                $othertitle.'</label></span></td>'.
 5656:                '</tr></table></td></tr>';
 5657:     return $output;
 5658: }
 5659: 
 5660: sub sorted_searchtitles {
 5661:     my %searchtitles = &Apache::lonlocal::texthash(
 5662:                          'uname' => 'username',
 5663:                          'lastname' => 'last name',
 5664:                          'lastfirst' => 'last name, first name',
 5665:                      );
 5666:     my @titleorder = ('uname','lastname','lastfirst');
 5667:     return (\%searchtitles,\@titleorder);
 5668: }
 5669: 
 5670: sub sorted_searchtypes {
 5671:     my %srchtypes_desc = (
 5672:                            exact    => 'is exact match',
 5673:                            contains => 'contains ..',
 5674:                            begins   => 'begins with ..',
 5675:                          );
 5676:     my @srchtypeorder = ('exact','begins','contains');
 5677:     return (\%srchtypes_desc,\@srchtypeorder);
 5678: }
 5679: 
 5680: sub usertype_update_row {
 5681:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5682:     my $datatable;
 5683:     my $numinrow = 4;
 5684:     foreach my $type (@{$types}) {
 5685:         if (defined($usertypes->{$type})) {
 5686:             $$rownums ++;
 5687:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5688:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5689:                           '</td><td class="LC_left_item"><table>';
 5690:             for (my $i=0; $i<@{$fields}; $i++) {
 5691:                 my $rem = $i%($numinrow);
 5692:                 if ($rem == 0) {
 5693:                     if ($i > 0) {
 5694:                         $datatable .= '</tr>';
 5695:                     }
 5696:                     $datatable .= '<tr>';
 5697:                 }
 5698:                 my $check = ' ';
 5699:                 if (ref($settings) eq 'HASH') {
 5700:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5701:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5702:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5703:                                 $check = ' checked="checked" ';
 5704:                             }
 5705:                         }
 5706:                     }
 5707:                 }
 5708: 
 5709:                 if ($i == @{$fields}-1) {
 5710:                     my $colsleft = $numinrow - $rem;
 5711:                     if ($colsleft > 1) {
 5712:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5713:                     } else {
 5714:                         $datatable .= '<td>';
 5715:                     }
 5716:                 } else {
 5717:                     $datatable .= '<td>';
 5718:                 }
 5719:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5720:                               '<input type="checkbox" name="updateable_'.$type.
 5721:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5722:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5723:             }
 5724:             $datatable .= '</tr></table></td></tr>';
 5725:         }
 5726:     }
 5727:     return $datatable;
 5728: }
 5729: 
 5730: sub modify_login {
 5731:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5732:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5733:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5734:     %title = ( coursecatalog => 'Display course catalog',
 5735:                adminmail => 'Display administrator E-mail address',
 5736:                helpdesk  => 'Display "Contact Helpdesk" link',
 5737:                newuser => 'Link for visitors to create a user account',
 5738:                loginheader => 'Log-in box header');
 5739:     @offon = ('off','on');
 5740:     if (ref($domconfig{login}) eq 'HASH') {
 5741:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5742:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5743:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5744:             }
 5745:         }
 5746:     }
 5747:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5748:                                            \%domconfig,\%loginhash);
 5749:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5750:     foreach my $item (@toggles) {
 5751:         $loginhash{login}{$item} = $env{'form.'.$item};
 5752:     }
 5753:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5754:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5755:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5756:                                          \%loginhash);
 5757:     }
 5758: 
 5759:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5760:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5761:     if (keys(%servers) > 1) {
 5762:         foreach my $lonhost (keys(%servers)) {
 5763:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5764:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5765:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5766:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5767:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5768:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5769:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5770:                         $changes{'loginvia'}{$lonhost} = 1;
 5771:                     } else {
 5772:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5773:                         $changes{'loginvia'}{$lonhost} = 1;
 5774:                     }
 5775:                 } else {
 5776:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5777:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5778:                         $changes{'loginvia'}{$lonhost} = 1;
 5779:                     }
 5780:                 }
 5781:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5782:                     foreach my $item (@loginvia_attribs) {
 5783:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5784:                     }
 5785:                 } else {
 5786:                     foreach my $item (@loginvia_attribs) {
 5787:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5788:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5789:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5790:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5791:                                 $new = '/';
 5792:                             }
 5793:                         }
 5794:                         if (($item eq 'custompath') && 
 5795:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5796:                             $new = '';
 5797:                         }
 5798:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5799:                             $changes{'loginvia'}{$lonhost} = 1;
 5800:                         }
 5801:                         if ($item eq 'exempt') {
 5802:                             $new =~ s/^\s+//;
 5803:                             $new =~ s/\s+$//;
 5804:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5805:                             my @okips;
 5806:                             foreach my $ip (@poss_ips) {
 5807:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5808:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5809:                                         push(@okips,$ip); 
 5810:                                     }
 5811:                                 }
 5812:                             }
 5813:                             if (@okips > 0) {
 5814:                                 $new = join(',',@okips); 
 5815:                             } else {
 5816:                                 $new = ''; 
 5817:                             }
 5818:                         }
 5819:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5820:                     }
 5821:                 }
 5822:             } else {
 5823:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5824:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5825:                     $changes{'loginvia'}{$lonhost} = 1;
 5826:                     foreach my $item (@loginvia_attribs) {
 5827:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5828:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5829:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5830:                                 $new = '/';
 5831:                             }
 5832:                         }
 5833:                         if (($item eq 'custompath') && 
 5834:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5835:                             $new = '';
 5836:                         }
 5837:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5838:                     }
 5839:                 }
 5840:             }
 5841:         }
 5842:     }
 5843: 
 5844:     my $servadm = $r->dir_config('lonAdmEMail');
 5845:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5846:     if (ref($domconfig{'login'}) eq 'HASH') {
 5847:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5848:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5849:                 if ($lang eq 'nolang') {
 5850:                     push(@currlangs,$lang);
 5851:                 } elsif (defined($langchoices{$lang})) {
 5852:                     push(@currlangs,$lang);
 5853:                 } else {
 5854:                     next;
 5855:                 }
 5856:             }
 5857:         }
 5858:     }
 5859:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5860:     if (@currlangs > 0) {
 5861:         foreach my $lang (@currlangs) {
 5862:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5863:                 $changes{'helpurl'}{$lang} = 1;
 5864:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5865:                 $changes{'helpurl'}{$lang} = 1;
 5866:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5867:                 push(@newlangs,$lang);
 5868:             } else {
 5869:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5870:             }
 5871:         }
 5872:     }
 5873:     unless (grep(/^nolang$/,@currlangs)) {
 5874:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5875:             $changes{'helpurl'}{'nolang'} = 1;
 5876:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5877:             push(@newlangs,'nolang');
 5878:         }
 5879:     }
 5880:     if ($env{'form.loginhelpurl_add_lang'}) {
 5881:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5882:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5883:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5884:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5885:         }
 5886:     }
 5887:     if ((@newlangs > 0) || ($addedfile)) {
 5888:         my $error;
 5889:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5890:         if ($configuserok eq 'ok') {
 5891:             if ($switchserver) {
 5892:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5893:             } elsif ($author_ok eq 'ok') {
 5894:                 my @allnew = @newlangs;
 5895:                 if ($addedfile ne '') {
 5896:                     push(@allnew,$addedfile);
 5897:                 }
 5898:                 foreach my $lang (@allnew) {
 5899:                     my $formelem = 'loginhelpurl_'.$lang;
 5900:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5901:                         $formelem = 'loginhelpurl_add_file';
 5902:                     }
 5903:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5904:                                                                "help/$lang",'','',$newfile{$lang});
 5905:                     if ($result eq 'ok') {
 5906:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5907:                         $changes{'helpurl'}{$lang} = 1;
 5908:                     } else {
 5909:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5910:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5911:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5912:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5913: 
 5914:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5915:                         }
 5916:                     }
 5917:                 }
 5918:             } else {
 5919:                 $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);
 5920:             }
 5921:         } else {
 5922:             $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);
 5923:         }
 5924:         if ($error) {
 5925:             &Apache::lonnet::logthis($error);
 5926:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5927:         }
 5928:     }
 5929:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5930: 
 5931:     my $defaulthelpfile = '/adm/loginproblems.html';
 5932:     my $defaulttext = &mt('Default in use');
 5933: 
 5934:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5935:                                              $dom);
 5936:     if ($putresult eq 'ok') {
 5937:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5938:         my %defaultchecked = (
 5939:                     'coursecatalog' => 'on',
 5940:                     'helpdesk'      => 'on',
 5941:                     'adminmail'     => 'off',
 5942:                     'newuser'       => 'off',
 5943:         );
 5944:         if (ref($domconfig{'login'}) eq 'HASH') {
 5945:             foreach my $item (@toggles) {
 5946:                 if ($defaultchecked{$item} eq 'on') { 
 5947:                     if (($domconfig{'login'}{$item} eq '0') &&
 5948:                         ($env{'form.'.$item} eq '1')) {
 5949:                         $changes{$item} = 1;
 5950:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5951:                               $domconfig{'login'}{$item} eq '1') &&
 5952:                              ($env{'form.'.$item} eq '0')) {
 5953:                         $changes{$item} = 1;
 5954:                     }
 5955:                 } elsif ($defaultchecked{$item} eq 'off') {
 5956:                     if (($domconfig{'login'}{$item} eq '1') &&
 5957:                         ($env{'form.'.$item} eq '0')) {
 5958:                         $changes{$item} = 1;
 5959:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5960:                               $domconfig{'login'}{$item} eq '0') &&
 5961:                              ($env{'form.'.$item} eq '1')) {
 5962:                         $changes{$item} = 1;
 5963:                     }
 5964:                 }
 5965:             }
 5966:         }
 5967:         if (keys(%changes) > 0 || $colchgtext) {
 5968:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5969:             if (ref($lastactref) eq 'HASH') {
 5970:                 $lastactref->{'domainconfig'} = 1;
 5971:             }
 5972:             $resulttext = &mt('Changes made:').'<ul>';
 5973:             foreach my $item (sort(keys(%changes))) {
 5974:                 if ($item eq 'loginvia') {
 5975:                     if (ref($changes{$item}) eq 'HASH') {
 5976:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5977:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5978:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5979:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5980:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5981:                                     $protocol = 'http' if ($protocol ne 'https');
 5982:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5983: 
 5984:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5985:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5986:                                     } else {
 5987:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5988:                                     }
 5989:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5990:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5991:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5992:                                     }
 5993:                                     $resulttext .= '</li>';
 5994:                                 } else {
 5995:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5996:                                 }
 5997:                             } else {
 5998:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5999:                             }
 6000:                         }
 6001:                         $resulttext .= '</ul></li>';
 6002:                     }
 6003:                 } elsif ($item eq 'helpurl') {
 6004:                     if (ref($changes{$item}) eq 'HASH') {
 6005:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 6006:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 6007:                                 my ($chg,$link);
 6008:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 6009:                                 if ($lang eq 'nolang') {
 6010:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 6011:                                 } else {
 6012:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 6013:                                 }
 6014:                                 $resulttext .= '<li>'.$chg.'</li>';
 6015:                             } else {
 6016:                                 my $chg;
 6017:                                 if ($lang eq 'nolang') {
 6018:                                     $chg = &mt('custom log-in help file for no preferred language');
 6019:                                 } else {
 6020:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 6021:                                 }
 6022:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 6023:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 6024:                                                       '?inhibitmenu=yes',$chg,600,500).
 6025:                                                '</li>';
 6026:                             }
 6027:                         }
 6028:                     }
 6029:                 } elsif ($item eq 'captcha') {
 6030:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6031:                         my $chgtxt;
 6032:                         if ($loginhash{'login'}{$item} eq 'notused') {
 6033:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 6034:                         } else {
 6035:                             my %captchas = &captcha_phrases();
 6036:                             if ($captchas{$loginhash{'login'}{$item}}) {
 6037:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 6038:                             } else {
 6039:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 6040:                             }
 6041:                         }
 6042:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6043:                     }
 6044:                 } elsif ($item eq 'recaptchakeys') {
 6045:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6046:                         my ($privkey,$pubkey);
 6047:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 6048:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 6049:                             $privkey = $loginhash{'login'}{$item}{'private'};
 6050:                         }
 6051:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 6052:                         if (!$pubkey) {
 6053:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 6054:                         } else {
 6055:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6056:                         }
 6057:                         if (!$privkey) {
 6058:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 6059:                         } else {
 6060:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6061:                         }
 6062:                         $chgtxt .= '</ul>';
 6063:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6064:                     }
 6065:                 } else {
 6066:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 6067:                 }
 6068:             }
 6069:             $resulttext .= $colchgtext.'</ul>';
 6070:         } else {
 6071:             $resulttext = &mt('No changes made to log-in page settings');
 6072:         }
 6073:     } else {
 6074:         $resulttext = '<span class="LC_error">'.
 6075: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6076:     }
 6077:     if ($errors) {
 6078:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 6079:                        $errors.'</ul>';
 6080:     }
 6081:     return $resulttext;
 6082: }
 6083: 
 6084: sub color_font_choices {
 6085:     my %choices =
 6086:         &Apache::lonlocal::texthash (
 6087:             img => "Header",
 6088:             bgs => "Background colors",
 6089:             links => "Link colors",
 6090:             images => "Images",
 6091:             font => "Font color",
 6092:             fontmenu => "Font menu",
 6093:             pgbg => "Page",
 6094:             tabbg => "Header",
 6095:             sidebg => "Border",
 6096:             link => "Link",
 6097:             alink => "Active link",
 6098:             vlink => "Visited link",
 6099:         );
 6100:     return %choices;
 6101: }
 6102: 
 6103: sub modify_rolecolors {
 6104:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 6105:     my ($resulttext,%rolehash);
 6106:     $rolehash{'rolecolors'} = {};
 6107:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 6108:         if ($domconfig{'rolecolors'} eq '') {
 6109:             $domconfig{'rolecolors'} = {};
 6110:         }
 6111:     }
 6112:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 6113:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 6114:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 6115:                                              $dom);
 6116:     if ($putresult eq 'ok') {
 6117:         if (keys(%changes) > 0) {
 6118:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6119:             if (ref($lastactref) eq 'HASH') {
 6120:                 $lastactref->{'domainconfig'} = 1;
 6121:             }
 6122:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 6123:                                              $rolehash{'rolecolors'});
 6124:         } else {
 6125:             $resulttext = &mt('No changes made to default color schemes');
 6126:         }
 6127:     } else {
 6128:         $resulttext = '<span class="LC_error">'.
 6129: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6130:     }
 6131:     if ($errors) {
 6132:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6133:                        $errors.'</ul>';
 6134:     }
 6135:     return $resulttext;
 6136: }
 6137: 
 6138: sub modify_colors {
 6139:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 6140:     my (%changes,%choices);
 6141:     my @bgs;
 6142:     my @links = ('link','alink','vlink');
 6143:     my @logintext;
 6144:     my @images;
 6145:     my $servadm = $r->dir_config('lonAdmEMail');
 6146:     my $errors;
 6147:     my %defaults;
 6148:     foreach my $role (@{$roles}) {
 6149:         if ($role eq 'login') {
 6150:             %choices = &login_choices();
 6151:             @logintext = ('textcol','bgcol');
 6152:         } else {
 6153:             %choices = &color_font_choices();
 6154:         }
 6155:         if ($role eq 'login') {
 6156:             @images = ('img','logo','domlogo','login');
 6157:             @bgs = ('pgbg','mainbg','sidebg');
 6158:         } else {
 6159:             @images = ('img');
 6160:             @bgs = ('pgbg','tabbg','sidebg');
 6161:         }
 6162:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 6163:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 6164:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 6165:         }
 6166:         if ($role eq 'login') {
 6167:             foreach my $item (@logintext) {
 6168:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6169:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6170:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6171:                 }
 6172:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 6173:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6174:                 }
 6175:             }
 6176:         } else {
 6177:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 6178:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 6179:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 6180:             }
 6181:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 6182:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 6183:             }
 6184:         }
 6185:         foreach my $item (@bgs) {
 6186:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6187:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6188:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6189:             }
 6190:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 6191:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6192:             }
 6193:         }
 6194:         foreach my $item (@links) {
 6195:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6196:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6197:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6198:             }
 6199:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 6200:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6201:             }
 6202:         }
 6203:         my ($configuserok,$author_ok,$switchserver) = 
 6204:             &config_check($dom,$confname,$servadm);
 6205:         my ($width,$height) = &thumb_dimensions();
 6206:         if (ref($domconfig->{$role}) ne 'HASH') {
 6207:             $domconfig->{$role} = {};
 6208:         }
 6209:         foreach my $img (@images) {
 6210:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 6211:                 if (defined($env{'form.login_showlogo_'.$img})) {
 6212:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 6213:                 } else { 
 6214:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 6215:                 }
 6216:             } 
 6217: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 6218: 		 && !defined($domconfig->{$role}{$img})
 6219: 		 && !$env{'form.'.$role.'_del_'.$img}
 6220: 		 && $env{'form.'.$role.'_import_'.$img}) {
 6221: 		# import the old configured image from the .tab setting
 6222: 		# if they haven't provided a new one 
 6223: 		$domconfig->{$role}{$img} = 
 6224: 		    $env{'form.'.$role.'_import_'.$img};
 6225: 	    }
 6226:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 6227:                 my $error;
 6228:                 if ($configuserok eq 'ok') {
 6229:                     if ($switchserver) {
 6230:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 6231:                     } else {
 6232:                         if ($author_ok eq 'ok') {
 6233:                             my ($result,$logourl) = 
 6234:                                 &publishlogo($r,'upload',$role.'_'.$img,
 6235:                                            $dom,$confname,$img,$width,$height);
 6236:                             if ($result eq 'ok') {
 6237:                                 $confhash->{$role}{$img} = $logourl;
 6238:                                 $changes{$role}{'images'}{$img} = 1;
 6239:                             } else {
 6240:                                 $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);
 6241:                             }
 6242:                         } else {
 6243:                             $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);
 6244:                         }
 6245:                     }
 6246:                 } else {
 6247:                     $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);
 6248:                 }
 6249:                 if ($error) {
 6250:                     &Apache::lonnet::logthis($error);
 6251:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6252:                 }
 6253:             } elsif ($domconfig->{$role}{$img} ne '') {
 6254:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 6255:                     my $error;
 6256:                     if ($configuserok eq 'ok') {
 6257: # is confname an author?
 6258:                         if ($switchserver eq '') {
 6259:                             if ($author_ok eq 'ok') {
 6260:                                 my ($result,$logourl) = 
 6261:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 6262:                                             $dom,$confname,$img,$width,$height);
 6263:                                 if ($result eq 'ok') {
 6264:                                     $confhash->{$role}{$img} = $logourl;
 6265: 				    $changes{$role}{'images'}{$img} = 1;
 6266:                                 }
 6267:                             }
 6268:                         }
 6269:                     }
 6270:                 }
 6271:             }
 6272:         }
 6273:         if (ref($domconfig) eq 'HASH') {
 6274:             if (ref($domconfig->{$role}) eq 'HASH') {
 6275:                 foreach my $img (@images) {
 6276:                     if ($domconfig->{$role}{$img} ne '') {
 6277:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6278:                             $confhash->{$role}{$img} = '';
 6279:                             $changes{$role}{'images'}{$img} = 1;
 6280:                         } else {
 6281:                             if ($confhash->{$role}{$img} eq '') {
 6282:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 6283:                             }
 6284:                         }
 6285:                     } else {
 6286:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6287:                             $confhash->{$role}{$img} = '';
 6288:                             $changes{$role}{'images'}{$img} = 1;
 6289:                         } 
 6290:                     }
 6291:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 6292:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 6293:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 6294:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 6295:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 6296:                             }
 6297:                         } else {
 6298:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6299:                                 $changes{$role}{'showlogo'}{$img} = 1;
 6300:                             }
 6301:                         }
 6302:                     }
 6303:                 }
 6304:                 if ($domconfig->{$role}{'font'} ne '') {
 6305:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 6306:                         $changes{$role}{'font'} = 1;
 6307:                     }
 6308:                 } else {
 6309:                     if ($confhash->{$role}{'font'}) {
 6310:                         $changes{$role}{'font'} = 1;
 6311:                     }
 6312:                 }
 6313:                 if ($role ne 'login') {
 6314:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 6315:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 6316:                             $changes{$role}{'fontmenu'} = 1;
 6317:                         }
 6318:                     } else {
 6319:                         if ($confhash->{$role}{'fontmenu'}) {
 6320:                             $changes{$role}{'fontmenu'} = 1;
 6321:                         }
 6322:                     }
 6323:                 }
 6324:                 foreach my $item (@bgs) {
 6325:                     if ($domconfig->{$role}{$item} ne '') {
 6326:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6327:                             $changes{$role}{'bgs'}{$item} = 1;
 6328:                         } 
 6329:                     } else {
 6330:                         if ($confhash->{$role}{$item}) {
 6331:                             $changes{$role}{'bgs'}{$item} = 1;
 6332:                         }
 6333:                     }
 6334:                 }
 6335:                 foreach my $item (@links) {
 6336:                     if ($domconfig->{$role}{$item} ne '') {
 6337:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6338:                             $changes{$role}{'links'}{$item} = 1;
 6339:                         }
 6340:                     } else {
 6341:                         if ($confhash->{$role}{$item}) {
 6342:                             $changes{$role}{'links'}{$item} = 1;
 6343:                         }
 6344:                     }
 6345:                 }
 6346:                 foreach my $item (@logintext) {
 6347:                     if ($domconfig->{$role}{$item} ne '') {
 6348:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6349:                             $changes{$role}{'logintext'}{$item} = 1;
 6350:                         }
 6351:                     } else {
 6352:                         if ($confhash->{$role}{$item}) {
 6353:                             $changes{$role}{'logintext'}{$item} = 1;
 6354:                         }
 6355:                     }
 6356:                 }
 6357:             } else {
 6358:                 &default_change_checker($role,\@images,\@links,\@bgs,
 6359:                                         \@logintext,$confhash,\%changes); 
 6360:             }
 6361:         } else {
 6362:             &default_change_checker($role,\@images,\@links,\@bgs,
 6363:                                     \@logintext,$confhash,\%changes); 
 6364:         }
 6365:     }
 6366:     return ($errors,%changes);
 6367: }
 6368: 
 6369: sub config_check {
 6370:     my ($dom,$confname,$servadm) = @_;
 6371:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6372:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6373:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6374:                                                    $confname,$servadm);
 6375:     if ($configuserok eq 'ok') {
 6376:         $switchserver = &check_switchserver($dom,$confname);
 6377:         if ($switchserver eq '') {
 6378:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6379:         }
 6380:     }
 6381:     return ($configuserok,$author_ok,$switchserver);
 6382: }
 6383: 
 6384: sub default_change_checker {
 6385:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6386:     foreach my $item (@{$links}) {
 6387:         if ($confhash->{$role}{$item}) {
 6388:             $changes->{$role}{'links'}{$item} = 1;
 6389:         }
 6390:     }
 6391:     foreach my $item (@{$bgs}) {
 6392:         if ($confhash->{$role}{$item}) {
 6393:             $changes->{$role}{'bgs'}{$item} = 1;
 6394:         }
 6395:     }
 6396:     foreach my $item (@{$logintext}) {
 6397:         if ($confhash->{$role}{$item}) {
 6398:             $changes->{$role}{'logintext'}{$item} = 1;
 6399:         }
 6400:     }
 6401:     foreach my $img (@{$images}) {
 6402:         if ($env{'form.'.$role.'_del_'.$img}) {
 6403:             $confhash->{$role}{$img} = '';
 6404:             $changes->{$role}{'images'}{$img} = 1;
 6405:         }
 6406:         if ($role eq 'login') {
 6407:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6408:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6409:             }
 6410:         }
 6411:     }
 6412:     if ($confhash->{$role}{'font'}) {
 6413:         $changes->{$role}{'font'} = 1;
 6414:     }
 6415: }
 6416: 
 6417: sub display_colorchgs {
 6418:     my ($dom,$changes,$roles,$confhash) = @_;
 6419:     my (%choices,$resulttext);
 6420:     if (!grep(/^login$/,@{$roles})) {
 6421:         $resulttext = &mt('Changes made:').'<br />';
 6422:     }
 6423:     foreach my $role (@{$roles}) {
 6424:         if ($role eq 'login') {
 6425:             %choices = &login_choices();
 6426:         } else {
 6427:             %choices = &color_font_choices();
 6428:         }
 6429:         if (ref($changes->{$role}) eq 'HASH') {
 6430:             if ($role ne 'login') {
 6431:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6432:             }
 6433:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6434:                 if ($role ne 'login') {
 6435:                     $resulttext .= '<ul>';
 6436:                 }
 6437:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6438:                     if ($role ne 'login') {
 6439:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6440:                     }
 6441:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6442:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6443:                             if ($confhash->{$role}{$key}{$item}) {
 6444:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6445:                             } else {
 6446:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6447:                             }
 6448:                         } elsif ($confhash->{$role}{$item} eq '') {
 6449:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6450:                         } else {
 6451:                             my $newitem = $confhash->{$role}{$item};
 6452:                             if ($key eq 'images') {
 6453:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6454:                             }
 6455:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6456:                         }
 6457:                     }
 6458:                     if ($role ne 'login') {
 6459:                         $resulttext .= '</ul></li>';
 6460:                     }
 6461:                 } else {
 6462:                     if ($confhash->{$role}{$key} eq '') {
 6463:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6464:                     } else {
 6465:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6466:                     }
 6467:                 }
 6468:                 if ($role ne 'login') {
 6469:                     $resulttext .= '</ul>';
 6470:                 }
 6471:             }
 6472:         }
 6473:     }
 6474:     return $resulttext;
 6475: }
 6476: 
 6477: sub thumb_dimensions {
 6478:     return ('200','50');
 6479: }
 6480: 
 6481: sub check_dimensions {
 6482:     my ($inputfile) = @_;
 6483:     my ($fullwidth,$fullheight);
 6484:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6485:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6486:             my $imageinfo = <PIPE>;
 6487:             if (!close(PIPE)) {
 6488:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6489:             }
 6490:             chomp($imageinfo);
 6491:             my ($fullsize) = 
 6492:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6493:             if ($fullsize) {
 6494:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6495:             }
 6496:         }
 6497:     }
 6498:     return ($fullwidth,$fullheight);
 6499: }
 6500: 
 6501: sub check_configuser {
 6502:     my ($uhome,$dom,$confname,$servadm) = @_;
 6503:     my ($configuserok,%currroles);
 6504:     if ($uhome eq 'no_host') {
 6505:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6506:         my $configpass = &LONCAPA::Enrollment::create_password();
 6507:         $configuserok = 
 6508:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6509:                              $configpass,'','','','','',undef,$servadm);
 6510:     } else {
 6511:         $configuserok = 'ok';
 6512:         %currroles = 
 6513:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6514:     }
 6515:     return ($configuserok,%currroles);
 6516: }
 6517: 
 6518: sub check_authorstatus {
 6519:     my ($dom,$confname,%currroles) = @_;
 6520:     my $author_ok;
 6521:     if (!$currroles{':'.$dom.':au'}) {
 6522:         my $start = time;
 6523:         my $end = 0;
 6524:         $author_ok = 
 6525:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6526:                                         'au',$end,$start,'','','domconfig');
 6527:     } else {
 6528:         $author_ok = 'ok';
 6529:     }
 6530:     return $author_ok;
 6531: }
 6532: 
 6533: sub publishlogo {
 6534:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6535:     my ($output,$fname,$logourl);
 6536:     if ($action eq 'upload') {
 6537:         $fname=$env{'form.'.$formname.'.filename'};
 6538:         chop($env{'form.'.$formname});
 6539:     } else {
 6540:         ($fname) = ($formname =~ /([^\/]+)$/);
 6541:     }
 6542:     if ($savefileas ne '') {
 6543:         $fname = $savefileas;
 6544:     }
 6545:     $fname=&Apache::lonnet::clean_filename($fname);
 6546: # See if there is anything left
 6547:     unless ($fname) { return ('error: no uploaded file'); }
 6548:     $fname="$subdir/$fname";
 6549:     my $docroot=$r->dir_config('lonDocRoot');
 6550:     my $filepath="$docroot/priv";
 6551:     my $relpath = "$dom/$confname";
 6552:     my ($fnamepath,$file,$fetchthumb);
 6553:     $file=$fname;
 6554:     if ($fname=~m|/|) {
 6555:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6556:     }
 6557:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6558:     my $count;
 6559:     for ($count=5;$count<=$#parts;$count++) {
 6560:         $filepath.="/$parts[$count]";
 6561:         if ((-e $filepath)!=1) {
 6562:             mkdir($filepath,02770);
 6563:         }
 6564:     }
 6565:     # Check for bad extension and disallow upload
 6566:     if ($file=~/\.(\w+)$/ &&
 6567:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6568:         $output = 
 6569:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6570:     } elsif ($file=~/\.(\w+)$/ &&
 6571:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6572:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6573:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6574:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6575:     } elsif (-d "$filepath/$file") {
 6576:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6577:     } else {
 6578:         my $source = $filepath.'/'.$file;
 6579:         my $logfile;
 6580:         if (!open($logfile,">>$source".'.log')) {
 6581:             return (&mt('No write permission to Authoring Space'));
 6582:         }
 6583:         print $logfile
 6584: "\n================= Publish ".localtime()." ================\n".
 6585: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6586: # Save the file
 6587:         if (!open(FH,'>'.$source)) {
 6588:             &Apache::lonnet::logthis('Failed to create '.$source);
 6589:             return (&mt('Failed to create file'));
 6590:         }
 6591:         if ($action eq 'upload') {
 6592:             if (!print FH ($env{'form.'.$formname})) {
 6593:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6594:                 return (&mt('Failed to write file'));
 6595:             }
 6596:         } else {
 6597:             my $original = &Apache::lonnet::filelocation('',$formname);
 6598:             if(!copy($original,$source)) {
 6599:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6600:                 return (&mt('Failed to write file'));
 6601:             }
 6602:         }
 6603:         close(FH);
 6604:         chmod(0660, $source); # Permissions to rw-rw---.
 6605: 
 6606:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6607:         my $copyfile=$targetdir.'/'.$file;
 6608: 
 6609:         my @parts=split(/\//,$targetdir);
 6610:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6611:         for (my $count=5;$count<=$#parts;$count++) {
 6612:             $path.="/$parts[$count]";
 6613:             if (!-e $path) {
 6614:                 print $logfile "\nCreating directory ".$path;
 6615:                 mkdir($path,02770);
 6616:             }
 6617:         }
 6618:         my $versionresult;
 6619:         if (-e $copyfile) {
 6620:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6621:         } else {
 6622:             $versionresult = 'ok';
 6623:         }
 6624:         if ($versionresult eq 'ok') {
 6625:             if (copy($source,$copyfile)) {
 6626:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6627:                 $output = 'ok';
 6628:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6629:                 push(@{$modified_urls},[$copyfile,$source]);
 6630:                 my $metaoutput = 
 6631:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6632:                 unless ($registered_cleanup) {
 6633:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6634:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6635:                     $registered_cleanup=1;
 6636:                 }
 6637:             } else {
 6638:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6639:                 $output = &mt('Failed to copy file to RES space').", $!";
 6640:             }
 6641:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6642:                 my $inputfile = $filepath.'/'.$file;
 6643:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6644:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6645:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6646:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6647:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6648:                         system("convert -sample $thumbsize $inputfile $outfile");
 6649:                         chmod(0660, $filepath.'/tn-'.$file);
 6650:                         if (-e $outfile) {
 6651:                             my $copyfile=$targetdir.'/tn-'.$file;
 6652:                             if (copy($outfile,$copyfile)) {
 6653:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6654:                                 my $thumb_metaoutput = 
 6655:                                     &write_metadata($dom,$confname,$formname,
 6656:                                                     $targetdir,'tn-'.$file,$logfile);
 6657:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6658:                                 unless ($registered_cleanup) {
 6659:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6660:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6661:                                     $registered_cleanup=1;
 6662:                                 }
 6663:                             } else {
 6664:                                 print $logfile "\nUnable to write ".$copyfile.
 6665:                                                ':'.$!."\n";
 6666:                             }
 6667:                         }
 6668:                     }
 6669:                 }
 6670:             }
 6671:         } else {
 6672:             $output = $versionresult;
 6673:         }
 6674:     }
 6675:     return ($output,$logourl);
 6676: }
 6677: 
 6678: sub logo_versioning {
 6679:     my ($targetdir,$file,$logfile) = @_;
 6680:     my $target = $targetdir.'/'.$file;
 6681:     my ($maxversion,$fn,$extn,$output);
 6682:     $maxversion = 0;
 6683:     if ($file =~ /^(.+)\.(\w+)$/) {
 6684:         $fn=$1;
 6685:         $extn=$2;
 6686:     }
 6687:     opendir(DIR,$targetdir);
 6688:     while (my $filename=readdir(DIR)) {
 6689:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6690:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6691:         }
 6692:     }
 6693:     $maxversion++;
 6694:     print $logfile "\nCreating old version ".$maxversion."\n";
 6695:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6696:     if (copy($target,$copyfile)) {
 6697:         print $logfile "Copied old target to ".$copyfile."\n";
 6698:         $copyfile=$copyfile.'.meta';
 6699:         if (copy($target.'.meta',$copyfile)) {
 6700:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6701:             $output = 'ok';
 6702:         } else {
 6703:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6704:             $output = &mt('Failed to copy old meta').", $!, ";
 6705:         }
 6706:     } else {
 6707:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6708:         $output = &mt('Failed to copy old target').", $!, ";
 6709:     }
 6710:     return $output;
 6711: }
 6712: 
 6713: sub write_metadata {
 6714:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6715:     my (%metadatafields,%metadatakeys,$output);
 6716:     $metadatafields{'title'}=$formname;
 6717:     $metadatafields{'creationdate'}=time;
 6718:     $metadatafields{'lastrevisiondate'}=time;
 6719:     $metadatafields{'copyright'}='public';
 6720:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6721:                                          $env{'user.domain'};
 6722:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6723:     $metadatafields{'domain'}=$dom;
 6724:     {
 6725:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6726:         my $mfh;
 6727:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6728:             foreach (sort(keys(%metadatafields))) {
 6729:                 unless ($_=~/\./) {
 6730:                     my $unikey=$_;
 6731:                     $unikey=~/^([A-Za-z]+)/;
 6732:                     my $tag=$1;
 6733:                     $tag=~tr/A-Z/a-z/;
 6734:                     print $mfh "\n\<$tag";
 6735:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6736:                         my $value=$metadatafields{$unikey.'.'.$_};
 6737:                         $value=~s/\"/\'\'/g;
 6738:                         print $mfh ' '.$_.'="'.$value.'"';
 6739:                     }
 6740:                     print $mfh '>'.
 6741:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6742:                             .'</'.$tag.'>';
 6743:                 }
 6744:             }
 6745:             $output = 'ok';
 6746:             print $logfile "\nWrote metadata";
 6747:             close($mfh);
 6748:         } else {
 6749:             print $logfile "\nFailed to open metadata file";
 6750:             $output = &mt('Could not write metadata');
 6751:         }
 6752:     }
 6753:     return $output;
 6754: }
 6755: 
 6756: sub notifysubscribed {
 6757:     foreach my $targetsource (@{$modified_urls}){
 6758:         next unless (ref($targetsource) eq 'ARRAY');
 6759:         my ($target,$source)=@{$targetsource};
 6760:         if ($source ne '') {
 6761:             if (open(my $logfh,'>>'.$source.'.log')) {
 6762:                 print $logfh "\nCleanup phase: Notifications\n";
 6763:                 my @subscribed=&subscribed_hosts($target);
 6764:                 foreach my $subhost (@subscribed) {
 6765:                     print $logfh "\nNotifying host ".$subhost.':';
 6766:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6767:                     print $logfh $reply;
 6768:                 }
 6769:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6770:                 foreach my $subhost (@subscribedmeta) {
 6771:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6772:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6773:                                                         $subhost);
 6774:                     print $logfh $reply;
 6775:                 }
 6776:                 print $logfh "\n============ Done ============\n";
 6777:                 close($logfh);
 6778:             }
 6779:         }
 6780:     }
 6781:     return OK;
 6782: }
 6783: 
 6784: sub subscribed_hosts {
 6785:     my ($target) = @_;
 6786:     my @subscribed;
 6787:     if (open(my $fh,"<$target.subscription")) {
 6788:         while (my $subline=<$fh>) {
 6789:             if ($subline =~ /^($match_lonid):/) {
 6790:                 my $host = $1;
 6791:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6792:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6793:                         push(@subscribed,$host);
 6794:                     }
 6795:                 }
 6796:             }
 6797:         }
 6798:     }
 6799:     return @subscribed;
 6800: }
 6801: 
 6802: sub check_switchserver {
 6803:     my ($dom,$confname) = @_;
 6804:     my ($allowed,$switchserver);
 6805:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6806:     if ($home eq 'no_host') {
 6807:         $home = &Apache::lonnet::domain($dom,'primary');
 6808:     }
 6809:     my @ids=&Apache::lonnet::current_machine_ids();
 6810:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6811:     if (!$allowed) {
 6812: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6813:     }
 6814:     return $switchserver;
 6815: }
 6816: 
 6817: sub modify_quotas {
 6818:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6819:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6820:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6821:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 6822:         $validationfieldsref);
 6823:     if ($action eq 'quotas') {
 6824:         $context = 'tools'; 
 6825:     } else {
 6826:         $context = $action;
 6827:     }
 6828:     if ($context eq 'requestcourses') {
 6829:         @usertools = ('official','unofficial','community','textbook');
 6830:         @options =('norequest','approval','validate','autolimit');
 6831:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6832:         %titles = &courserequest_titles();
 6833:         $toolregexp = join('|',@usertools);
 6834:         %conditions = &courserequest_conditions();
 6835:         $confname = $dom.'-domainconfig';
 6836:         my $servadm = $r->dir_config('lonAdmEMail');
 6837:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6838:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 6839:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6840:     } elsif ($context eq 'requestauthor') {
 6841:         @usertools = ('author');
 6842:         %titles = &authorrequest_titles();
 6843:     } else {
 6844:         @usertools = ('aboutme','blog','webdav','portfolio');
 6845:         %titles = &tool_titles();
 6846:     }
 6847:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6848:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6849:     foreach my $key (keys(%env)) {
 6850:         if ($context eq 'requestcourses') {
 6851:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6852:                 my $item = $1;
 6853:                 my $type = $2;
 6854:                 if ($type =~ /^limit_(.+)/) {
 6855:                     $limithash{$item}{$1} = $env{$key};
 6856:                 } else {
 6857:                     $confhash{$item}{$type} = $env{$key};
 6858:                 }
 6859:             }
 6860:         } elsif ($context eq 'requestauthor') {
 6861:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6862:                 $confhash{$1} = $env{$key};
 6863:             }
 6864:         } else {
 6865:             if ($key =~ /^form\.quota_(.+)$/) {
 6866:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6867:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6868:                 $confhash{'authorquota'}{$1} = $env{$key};
 6869:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6870:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6871:             }
 6872:         }
 6873:     }
 6874:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6875:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6876:         @approvalnotify = sort(@approvalnotify);
 6877:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6878:         my @crstypes = ('official','unofficial','community','textbook');
 6879:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6880:         foreach my $type (@hasuniquecode) {
 6881:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6882:                 $confhash{'uniquecode'}{$type} = 1;
 6883:             }
 6884:         }
 6885:         my (%newbook,%allpos);
 6886:         if ($context eq 'requestcourses') {
 6887:             foreach my $type ('textbooks','templates') {
 6888:                 @{$allpos{$type}} = (); 
 6889:                 my $invalid;
 6890:                 if ($type eq 'textbooks') {
 6891:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 6892:                 } else {
 6893:                     $invalid = &mt('Invalid LON-CAPA course for template');
 6894:                 }
 6895:                 if ($env{'form.'.$type.'_addbook'}) {
 6896:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 6897:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 6898:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 6899:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 6900:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 6901:                         } else {
 6902:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 6903:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 6904:                             $position =~ s/\D+//g;
 6905:                             if ($position ne '') {
 6906:                                 $allpos{$type}[$position] = $newbook{$type};
 6907:                             }
 6908:                         }
 6909:                     } else {
 6910:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 6911:                     }
 6912:                 }
 6913:             } 
 6914:         }
 6915:         if (ref($domconfig{$action}) eq 'HASH') {
 6916:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6917:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6918:                     $changes{'notify'}{'approval'} = 1;
 6919:                 }
 6920:             } else {
 6921:                 if ($confhash{'notify'}{'approval'}) {
 6922:                     $changes{'notify'}{'approval'} = 1;
 6923:                 }
 6924:             }
 6925:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6926:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6927:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6928:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6929:                             $changes{'uniquecode'} = 1;
 6930:                         }
 6931:                     }
 6932:                     unless ($changes{'uniquecode'}) {
 6933:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6934:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6935:                                 $changes{'uniquecode'} = 1;
 6936:                             }
 6937:                         }
 6938:                     }
 6939:                } else {
 6940:                    $changes{'uniquecode'} = 1;
 6941:                }
 6942:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6943:                 $changes{'uniquecode'} = 1;
 6944:             }
 6945:             if ($context eq 'requestcourses') {
 6946:                 foreach my $type ('textbooks','templates') {
 6947:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 6948:                         my %deletions;
 6949:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 6950:                         if (@todelete) {
 6951:                             map { $deletions{$_} = 1; } @todelete;
 6952:                         }
 6953:                         my %imgdeletions;
 6954:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 6955:                         if (@todeleteimages) {
 6956:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 6957:                         }
 6958:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 6959:                         for (my $i=0; $i<=$maxnum; $i++) {
 6960:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 6961:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 6962:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 6963:                                 if ($deletions{$key}) {
 6964:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 6965:                                         #FIXME need to obsolete item in RES space
 6966:                                     }
 6967:                                     next;
 6968:                                 } else {
 6969:                                     my $newpos = $env{'form.'.$itemid};
 6970:                                     $newpos =~ s/\D+//g;
 6971:                                     foreach my $item ('subject','title','author') {
 6972:                                         next if (($item eq 'author') && ($type eq 'templates'));
 6973:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 6974:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 6975:                                             $changes{$type}{$key} = 1;
 6976:                                         }
 6977:                                     }
 6978:                                     $allpos{$type}[$newpos] = $key;
 6979:                                 }
 6980:                                 if ($imgdeletions{$key}) {
 6981:                                     $changes{$type}{$key} = 1;
 6982:                                     #FIXME need to obsolete item in RES space
 6983:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 6984:                                     my ($cdom,$cnum) = split(/_/,$key);
 6985:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 6986:                                                                                   $cdom,$cnum,$type,$configuserok,
 6987:                                                                                   $switchserver,$author_ok);
 6988:                                     if ($imgurl) {
 6989:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 6990:                                         $changes{$type}{$key} = 1; 
 6991:                                     }
 6992:                                     if ($error) {
 6993:                                         &Apache::lonnet::logthis($error);
 6994:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6995:                                     } 
 6996:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 6997:                                     $confhash{$type}{$key}{'image'} = 
 6998:                                         $domconfig{$action}{$type}{$key}{'image'};
 6999:                                 }
 7000:                             }
 7001:                         }
 7002:                     }
 7003:                 }
 7004:             }
 7005:         } else {
 7006:             if ($confhash{'notify'}{'approval'}) {
 7007:                 $changes{'notify'}{'approval'} = 1;
 7008:             }
 7009:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 7010:                 $changes{'uniquecode'} = 1;
 7011:             }
 7012:         }
 7013:         if ($context eq 'requestcourses') {
 7014:             foreach my $type ('textbooks','templates') {
 7015:                 if ($newbook{$type}) {
 7016:                     $changes{$type}{$newbook{$type}} = 1;
 7017:                     foreach my $item ('subject','title','author') {
 7018:                         next if (($item eq 'author') && ($type eq 'template'));
 7019:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 7020:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 7021:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 7022:                         }
 7023:                     }
 7024:                     if ($type eq 'textbooks') {
 7025:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 7026:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 7027:                             my ($imageurl,$error) =
 7028:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 7029:                                                         $configuserok,$switchserver,$author_ok);
 7030:                             if ($imageurl) {
 7031:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 7032:                             }
 7033:                             if ($error) {
 7034:                                 &Apache::lonnet::logthis($error);
 7035:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7036:                             }
 7037:                         }
 7038:                     }
 7039:                 }
 7040:                 if (@{$allpos{$type}} > 0) {
 7041:                     my $idx = 0;
 7042:                     foreach my $item (@{$allpos{$type}}) {
 7043:                         if ($item ne '') {
 7044:                             $confhash{$type}{$item}{'order'} = $idx;
 7045:                             if (ref($domconfig{$action}) eq 'HASH') {
 7046:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7047:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 7048:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 7049:                                             $changes{$type}{$item} = 1;
 7050:                                         }
 7051:                                     }
 7052:                                 }
 7053:                             }
 7054:                             $idx ++;
 7055:                         }
 7056:                     }
 7057:                 }
 7058:             }
 7059:             if (ref($validationitemsref) eq 'ARRAY') {
 7060:                 foreach my $item (@{$validationitemsref}) {
 7061:                     if ($item eq 'fields') {
 7062:                         my @changed;
 7063:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 7064:                         if (@{$confhash{'validation'}{$item}} > 0) {
 7065:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 7066:                         }
 7067:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7068:                             if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 7069:                                 @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 7070:                                                                               $domconfig{'requestcourses'}{'validation'}{$item});
 7071:                             } else {
 7072:                                 @changed = @{$confhash{'validation'}{$item}};
 7073:                             }
 7074:                         } else {
 7075:                             @changed = @{$confhash{'validation'}{$item}};
 7076:                         }
 7077:                         if (@changed) {
 7078:                             if ($confhash{'validation'}{$item}) {
 7079:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 7080:                             } else {
 7081:                                 $changes{'validation'}{$item} = &mt('None');
 7082:                             }
 7083:                         }
 7084:                     } else {
 7085:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 7086:                         if ($item eq 'markup') {
 7087:                             if ($env{'form.requestcourses_validation_'.$item}) {
 7088:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 7089:                             }
 7090:                         }
 7091:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7092:                             if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 7093:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7094:                             }
 7095:                         } else {
 7096:                             if ($confhash{'validation'}{$item} ne '') {
 7097:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7098:                             }
 7099:                         }
 7100:                     }
 7101:                 }
 7102:             }
 7103:             if ($env{'form.validationdc'}) {
 7104:                 my $newval = $env{'form.validationdc'};
 7105:                 my %domcoords = &get_active_dcs($dom);
 7106:                 if (exists($domcoords{$newval})) {
 7107:                     $confhash{'validation'}{'dc'} = $newval;
 7108:                 }
 7109:             }
 7110:             if (ref($confhash{'validation'}) eq 'HASH') {
 7111:                 if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7112:                     if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7113:                         unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7114:                             if ($confhash{'validation'}{'dc'} eq '') {
 7115:                                 $changes{'validation'}{'dc'} = &mt('None');
 7116:                             } else {
 7117:                                 $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7118:                             }
 7119:                         }
 7120:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 7121:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7122:                     }
 7123:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 7124:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7125:                 }
 7126:             } elsif (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7127:                 if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7128:                     $changes{'validation'}{'dc'} = &mt('None');
 7129:                 }
 7130:             }
 7131:         }
 7132:     } else {
 7133:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 7134:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 7135:     }
 7136:     foreach my $item (@usertools) {
 7137:         foreach my $type (@{$types},'default','_LC_adv') {
 7138:             my $unset; 
 7139:             if ($context eq 'requestcourses') {
 7140:                 $unset = '0';
 7141:                 if ($type eq '_LC_adv') {
 7142:                     $unset = '';
 7143:                 }
 7144:                 if ($confhash{$item}{$type} eq 'autolimit') {
 7145:                     $confhash{$item}{$type} .= '=';
 7146:                     unless ($limithash{$item}{$type} =~ /\D/) {
 7147:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 7148:                     }
 7149:                 }
 7150:             } elsif ($context eq 'requestauthor') {
 7151:                 $unset = '0';
 7152:                 if ($type eq '_LC_adv') {
 7153:                     $unset = '';
 7154:                 }
 7155:             } else {
 7156:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 7157:                     $confhash{$item}{$type} = 1;
 7158:                 } else {
 7159:                     $confhash{$item}{$type} = 0;
 7160:                 }
 7161:             }
 7162:             if (ref($domconfig{$action}) eq 'HASH') {
 7163:                 if ($action eq 'requestauthor') {
 7164:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 7165:                         $changes{$type} = 1;
 7166:                     }
 7167:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 7168:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 7169:                         $changes{$item}{$type} = 1;
 7170:                     }
 7171:                 } else {
 7172:                     if ($context eq 'requestcourses') {
 7173:                         if ($confhash{$item}{$type} ne $unset) {
 7174:                             $changes{$item}{$type} = 1;
 7175:                         }
 7176:                     } else {
 7177:                         if (!$confhash{$item}{$type}) {
 7178:                             $changes{$item}{$type} = 1;
 7179:                         }
 7180:                     }
 7181:                 }
 7182:             } else {
 7183:                 if ($context eq 'requestcourses') {
 7184:                     if ($confhash{$item}{$type} ne $unset) {
 7185:                         $changes{$item}{$type} = 1;
 7186:                     }
 7187:                 } elsif ($context eq 'requestauthor') {
 7188:                     if ($confhash{$type} ne $unset) {
 7189:                         $changes{$type} = 1;
 7190:                     }
 7191:                 } else {
 7192:                     if (!$confhash{$item}{$type}) {
 7193:                         $changes{$item}{$type} = 1;
 7194:                     }
 7195:                 }
 7196:             }
 7197:         }
 7198:     }
 7199:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7200:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 7201:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7202:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 7203:                     if (exists($confhash{'defaultquota'}{$key})) {
 7204:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 7205:                             $changes{'defaultquota'}{$key} = 1;
 7206:                         }
 7207:                     } else {
 7208:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 7209:                     }
 7210:                 }
 7211:             } else {
 7212:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 7213:                     if (exists($confhash{'defaultquota'}{$key})) {
 7214:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 7215:                             $changes{'defaultquota'}{$key} = 1;
 7216:                         }
 7217:                     } else {
 7218:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 7219:                     }
 7220:                 }
 7221:             }
 7222:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7223:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 7224:                     if (exists($confhash{'authorquota'}{$key})) {
 7225:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 7226:                             $changes{'authorquota'}{$key} = 1;
 7227:                         }
 7228:                     } else {
 7229:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 7230:                     }
 7231:                 }
 7232:             }
 7233:         }
 7234:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 7235:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 7236:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7237:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7238:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 7239:                             $changes{'defaultquota'}{$key} = 1;
 7240:                         }
 7241:                     } else {
 7242:                         if (!exists($domconfig{'quotas'}{$key})) {
 7243:                             $changes{'defaultquota'}{$key} = 1;
 7244:                         }
 7245:                     }
 7246:                 } else {
 7247:                     $changes{'defaultquota'}{$key} = 1;
 7248:                 }
 7249:             }
 7250:         }
 7251:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 7252:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 7253:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7254:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7255:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 7256:                             $changes{'authorquota'}{$key} = 1;
 7257:                         }
 7258:                     } else {
 7259:                         $changes{'authorquota'}{$key} = 1;
 7260:                     }
 7261:                 } else {
 7262:                     $changes{'authorquota'}{$key} = 1;
 7263:                 }
 7264:             }
 7265:         }
 7266:     }
 7267: 
 7268:     if ($context eq 'requestauthor') {
 7269:         $domdefaults{'requestauthor'} = \%confhash;
 7270:     } else {
 7271:         foreach my $key (keys(%confhash)) {
 7272:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 7273:                 $domdefaults{$key} = $confhash{$key};
 7274:             }
 7275:         }
 7276:     }
 7277: 
 7278:     my %quotahash = (
 7279:                       $action => { %confhash }
 7280:                     );
 7281:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 7282:                                              $dom);
 7283:     if ($putresult eq 'ok') {
 7284:         if (keys(%changes) > 0) {
 7285:             my $cachetime = 24*60*60;
 7286:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7287:             if (ref($lastactref) eq 'HASH') {
 7288:                 $lastactref->{'domdefaults'} = 1;
 7289:             }
 7290:             $resulttext = &mt('Changes made:').'<ul>';
 7291:             unless (($context eq 'requestcourses') ||
 7292:                     ($context eq 'requestauthor')) {
 7293:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 7294:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 7295:                     foreach my $type (@{$types},'default') {
 7296:                         if (defined($changes{'defaultquota'}{$type})) {
 7297:                             my $typetitle = $usertypes->{$type};
 7298:                             if ($type eq 'default') {
 7299:                                 $typetitle = $othertitle;
 7300:                             }
 7301:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 7302:                         }
 7303:                     }
 7304:                     $resulttext .= '</ul></li>';
 7305:                 }
 7306:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 7307:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 7308:                     foreach my $type (@{$types},'default') {
 7309:                         if (defined($changes{'authorquota'}{$type})) {
 7310:                             my $typetitle = $usertypes->{$type};
 7311:                             if ($type eq 'default') {
 7312:                                 $typetitle = $othertitle;
 7313:                             }
 7314:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 7315:                         }
 7316:                     }
 7317:                     $resulttext .= '</ul></li>';
 7318:                 }
 7319:             }
 7320:             my %newenv;
 7321:             foreach my $item (@usertools) {
 7322:                 my (%haschgs,%inconf);
 7323:                 if ($context eq 'requestauthor') {
 7324:                     %haschgs = %changes;
 7325:                     %inconf = %confhash;
 7326:                 } else {
 7327:                     if (ref($changes{$item}) eq 'HASH') {
 7328:                         %haschgs = %{$changes{$item}};
 7329:                     }
 7330:                     if (ref($confhash{$item}) eq 'HASH') {
 7331:                         %inconf = %{$confhash{$item}};
 7332:                     }
 7333:                 }
 7334:                 if (keys(%haschgs) > 0) {
 7335:                     my $newacc = 
 7336:                         &Apache::lonnet::usertools_access($env{'user.name'},
 7337:                                                           $env{'user.domain'},
 7338:                                                           $item,'reload',$context);
 7339:                     if (($context eq 'requestcourses') ||
 7340:                         ($context eq 'requestauthor')) {
 7341:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 7342:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 7343:                         }
 7344:                     } else {
 7345:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 7346:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 7347:                         }
 7348:                     }
 7349:                     unless ($context eq 'requestauthor') {
 7350:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 7351:                     }
 7352:                     foreach my $type (@{$types},'default','_LC_adv') {
 7353:                         if ($haschgs{$type}) {
 7354:                             my $typetitle = $usertypes->{$type};
 7355:                             if ($type eq 'default') {
 7356:                                 $typetitle = $othertitle;
 7357:                             } elsif ($type eq '_LC_adv') {
 7358:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 7359:                             }
 7360:                             if ($inconf{$type}) {
 7361:                                 if ($context eq 'requestcourses') {
 7362:                                     my $cond;
 7363:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 7364:                                         if ($1 eq '') {
 7365:                                             $cond = &mt('(Automatic processing of any request).');
 7366:                                         } else {
 7367:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 7368:                                         }
 7369:                                     } else { 
 7370:                                         $cond = $conditions{$inconf{$type}};
 7371:                                     }
 7372:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 7373:                                 } elsif ($context eq 'requestauthor') {
 7374:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 7375:                                                              $titles{$inconf{$type}},$typetitle);
 7376: 
 7377:                                 } else {
 7378:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7379:                                 }
 7380:                             } else {
 7381:                                 if ($type eq '_LC_adv') {
 7382:                                     if ($inconf{$type} eq '0') {
 7383:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7384:                                     } else { 
 7385:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7386:                                     }
 7387:                                 } else {
 7388:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7389:                                 }
 7390:                             }
 7391:                         }
 7392:                     }
 7393:                     unless ($context eq 'requestauthor') {
 7394:                         $resulttext .= '</ul></li>';
 7395:                     }
 7396:                 }
 7397:             }
 7398:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7399:                 if (ref($changes{'notify'}) eq 'HASH') {
 7400:                     if ($changes{'notify'}{'approval'}) {
 7401:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7402:                             if ($confhash{'notify'}{'approval'}) {
 7403:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7404:                             } else {
 7405:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7406:                             }
 7407:                         }
 7408:                     }
 7409:                 }
 7410:             }
 7411:             if ($action eq 'requestcourses') {
 7412:                 my @offon = ('off','on');
 7413:                 if ($changes{'uniquecode'}) {
 7414:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7415:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7416:                         $resulttext .= '<li>'.
 7417:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7418:                                        '</li>';
 7419:                     } else {
 7420:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7421:                                        '</li>';
 7422:                     }
 7423:                 }
 7424:                 foreach my $type ('textbooks','templates') {
 7425:                     if (ref($changes{$type}) eq 'HASH') {
 7426:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 7427:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 7428:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 7429:                             my $coursetitle = $coursehash{'description'};
 7430:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 7431:                             $resulttext .= '<li>';
 7432:                             foreach my $item ('subject','title','author') {
 7433:                                 next if (($item eq 'author') && ($type eq 'templates'));
 7434:                                 my $name = $item.':';
 7435:                                 $name =~ s/^(\w)/\U$1/;
 7436:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 7437:                             }
 7438:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7439:                             if ($type eq 'textbooks') {
 7440:                                 if ($confhash{$type}{$key}{'image'}) {
 7441:                                     $resulttext .= ' '.&mt('Image: [_1]',
 7442:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 7443:                                                    ' alt="Textbook cover" />').'<br />';
 7444:                                 }
 7445:                             }
 7446:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7447:                         }
 7448:                         $resulttext .= '</ul></li>';
 7449:                     }
 7450:                 }
 7451:                 if (ref($changes{'validation'}) eq 'HASH') {
 7452:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7453:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7454:                         foreach my $item (@{$validationitemsref}) {
 7455:                             if (exists($changes{'validation'}{$item})) {
 7456:                                 if ($item eq 'markup') {
 7457:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7458:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7459:                                 } else {
 7460:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7461:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7462:                                 }
 7463:                             }
 7464:                         }
 7465:                         if (exists($changes{'validation'}{'dc'})) {
 7466:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7467:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7468:                         }
 7469:                     }
 7470:                 }
 7471:             }
 7472:             $resulttext .= '</ul>';
 7473:             if (keys(%newenv)) {
 7474:                 &Apache::lonnet::appenv(\%newenv);
 7475:             }
 7476:         } else {
 7477:             if ($context eq 'requestcourses') {
 7478:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7479:             } elsif ($context eq 'requestauthor') {
 7480:                 $resulttext = &mt('No changes made to rights to request author space.');
 7481:             } else {
 7482:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7483:             }
 7484:         }
 7485:     } else {
 7486:         $resulttext = '<span class="LC_error">'.
 7487: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7488:     }
 7489:     if ($errors) {
 7490:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7491:                        '<ul>'.$errors.'</ul></p>';
 7492:     }
 7493:     return $resulttext;
 7494: }
 7495: 
 7496: sub process_textbook_image {
 7497:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 7498:     my $filename = $env{'form.'.$caller.'.filename'};
 7499:     my ($error,$url);
 7500:     my ($width,$height) = (50,50);
 7501:     if ($configuserok eq 'ok') {
 7502:         if ($switchserver) {
 7503:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7504:                          $switchserver);
 7505:         } elsif ($author_ok eq 'ok') {
 7506:             my ($result,$imageurl) =
 7507:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7508:                              "$type/$dom/$cnum/cover",$width,$height);
 7509:             if ($result eq 'ok') {
 7510:                 $url = $imageurl;
 7511:             } else {
 7512:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7513:             }
 7514:         } else {
 7515:             $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);
 7516:         }
 7517:     } else {
 7518:         $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);
 7519:     }
 7520:     return ($url,$error);
 7521: }
 7522: 
 7523: sub modify_autoenroll {
 7524:     my ($dom,$lastactref,%domconfig) = @_;
 7525:     my ($resulttext,%changes);
 7526:     my %currautoenroll;
 7527:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7528:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7529:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7530:         }
 7531:     }
 7532:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7533:     my %title = ( run => 'Auto-enrollment active',
 7534:                   sender => 'Sender for notification messages',
 7535:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7536:     my @offon = ('off','on');
 7537:     my $sender_uname = $env{'form.sender_uname'};
 7538:     my $sender_domain = $env{'form.sender_domain'};
 7539:     if ($sender_domain eq '') {
 7540:         $sender_uname = '';
 7541:     } elsif ($sender_uname eq '') {
 7542:         $sender_domain = '';
 7543:     }
 7544:     my $coowners = $env{'form.autoassign_coowners'};
 7545:     my %autoenrollhash =  (
 7546:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7547:                                        'sender_uname' => $sender_uname,
 7548:                                        'sender_domain' => $sender_domain,
 7549:                                        'co-owners' => $coowners,
 7550:                                 }
 7551:                      );
 7552:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7553:                                              $dom);
 7554:     if ($putresult eq 'ok') {
 7555:         if (exists($currautoenroll{'run'})) {
 7556:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7557:                  $changes{'run'} = 1;
 7558:              }
 7559:         } elsif ($autorun) {
 7560:             if ($env{'form.autoenroll_run'} ne '1') {
 7561:                  $changes{'run'} = 1;
 7562:             }
 7563:         }
 7564:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7565:             $changes{'sender'} = 1;
 7566:         }
 7567:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7568:             $changes{'sender'} = 1;
 7569:         }
 7570:         if ($currautoenroll{'co-owners'} ne '') {
 7571:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7572:                 $changes{'coowners'} = 1;
 7573:             }
 7574:         } elsif ($coowners) {
 7575:             $changes{'coowners'} = 1;
 7576:         }      
 7577:         if (keys(%changes) > 0) {
 7578:             $resulttext = &mt('Changes made:').'<ul>';
 7579:             if ($changes{'run'}) {
 7580:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7581:             }
 7582:             if ($changes{'sender'}) {
 7583:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7584:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7585:                 } else {
 7586:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7587:                 }
 7588:             }
 7589:             if ($changes{'coowners'}) {
 7590:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7591:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7592:                 if (ref($lastactref) eq 'HASH') {
 7593:                     $lastactref->{'domainconfig'} = 1;
 7594:                 }
 7595:             }
 7596:             $resulttext .= '</ul>';
 7597:         } else {
 7598:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7599:         }
 7600:     } else {
 7601:         $resulttext = '<span class="LC_error">'.
 7602: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7603:     }
 7604:     return $resulttext;
 7605: }
 7606: 
 7607: sub modify_autoupdate {
 7608:     my ($dom,%domconfig) = @_;
 7609:     my ($resulttext,%currautoupdate,%fields,%changes);
 7610:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7611:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7612:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7613:         }
 7614:     }
 7615:     my @offon = ('off','on');
 7616:     my %title = &Apache::lonlocal::texthash (
 7617:                    run => 'Auto-update:',
 7618:                    classlists => 'Updates to user information in classlists?'
 7619:                 );
 7620:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7621:     my %fieldtitles = &Apache::lonlocal::texthash (
 7622:                         id => 'Student/Employee ID',
 7623:                         permanentemail => 'E-mail address',
 7624:                         lastname => 'Last Name',
 7625:                         firstname => 'First Name',
 7626:                         middlename => 'Middle Name',
 7627:                         generation => 'Generation',
 7628:                       );
 7629:     $othertitle = &mt('All users');
 7630:     if (keys(%{$usertypes}) >  0) {
 7631:         $othertitle = &mt('Other users');
 7632:     }
 7633:     foreach my $key (keys(%env)) {
 7634:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7635:             my ($usertype,$item) = ($1,$2);
 7636:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7637:                 if ($usertype eq 'default') {   
 7638:                     push(@{$fields{$1}},$2);
 7639:                 } elsif (ref($types) eq 'ARRAY') {
 7640:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7641:                         push(@{$fields{$1}},$2);
 7642:                     }
 7643:                 }
 7644:             }
 7645:         }
 7646:     }
 7647:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 7648:     @lockablenames = sort(@lockablenames);
 7649:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 7650:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7651:         if (@changed) {
 7652:             $changes{'lockablenames'} = 1;
 7653:         }
 7654:     } else {
 7655:         if (@lockablenames) {
 7656:             $changes{'lockablenames'} = 1;
 7657:         }
 7658:     }
 7659:     my %updatehash = (
 7660:                       autoupdate => { run => $env{'form.autoupdate_run'},
 7661:                                       classlists => $env{'form.classlists'},
 7662:                                       fields => {%fields},
 7663:                                       lockablenames => \@lockablenames,
 7664:                                     }
 7665:                      );
 7666:     foreach my $key (keys(%currautoupdate)) {
 7667:         if (($key eq 'run') || ($key eq 'classlists')) {
 7668:             if (exists($updatehash{autoupdate}{$key})) {
 7669:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 7670:                     $changes{$key} = 1;
 7671:                 }
 7672:             }
 7673:         } elsif ($key eq 'fields') {
 7674:             if (ref($currautoupdate{$key}) eq 'HASH') {
 7675:                 foreach my $item (@{$types},'default') {
 7676:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 7677:                         my $change = 0;
 7678:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 7679:                             if (!exists($fields{$item})) {
 7680:                                 $change = 1;
 7681:                                 last;
 7682:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 7683:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 7684:                                     $change = 1;
 7685:                                     last;
 7686:                                 }
 7687:                             }
 7688:                         }
 7689:                         if ($change) {
 7690:                             push(@{$changes{$key}},$item);
 7691:                         }
 7692:                     } 
 7693:                 }
 7694:             }
 7695:         } elsif ($key eq 'lockablenames') {
 7696:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 7697:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7698:                 if (@changed) {
 7699:                     $changes{'lockablenames'} = 1;
 7700:                 }
 7701:             } else {
 7702:                 if (@lockablenames) {
 7703:                     $changes{'lockablenames'} = 1;
 7704:                 }
 7705:             }
 7706:         }
 7707:     }
 7708:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 7709:         if (@lockablenames) {
 7710:             $changes{'lockablenames'} = 1;
 7711:         }
 7712:     }
 7713:     foreach my $item (@{$types},'default') {
 7714:         if (defined($fields{$item})) {
 7715:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 7716:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 7717:                     my $change = 0;
 7718:                     if (ref($fields{$item}) eq 'ARRAY') {
 7719:                         foreach my $type (@{$fields{$item}}) {
 7720:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 7721:                                 $change = 1;
 7722:                                 last;
 7723:                             }
 7724:                         }
 7725:                     }
 7726:                     if ($change) {
 7727:                         push(@{$changes{'fields'}},$item);
 7728:                     }
 7729:                 } else {
 7730:                     push(@{$changes{'fields'}},$item);
 7731:                 }
 7732:             } else {
 7733:                 push(@{$changes{'fields'}},$item);
 7734:             }
 7735:         }
 7736:     }
 7737:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7738:                                              $dom);
 7739:     if ($putresult eq 'ok') {
 7740:         if (keys(%changes) > 0) {
 7741:             $resulttext = &mt('Changes made:').'<ul>';
 7742:             foreach my $key (sort(keys(%changes))) {
 7743:                 if ($key eq 'lockablenames') {
 7744:                     $resulttext .= '<li>';
 7745:                     if (@lockablenames) {
 7746:                         $usertypes->{'default'} = $othertitle;
 7747:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7748:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7749:                     } else {
 7750:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7751:                     }
 7752:                     $resulttext .= '</li>';
 7753:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7754:                     foreach my $item (@{$changes{$key}}) {
 7755:                         my @newvalues;
 7756:                         foreach my $type (@{$fields{$item}}) {
 7757:                             push(@newvalues,$fieldtitles{$type});
 7758:                         }
 7759:                         my $newvaluestr;
 7760:                         if (@newvalues > 0) {
 7761:                             $newvaluestr = join(', ',@newvalues);
 7762:                         } else {
 7763:                             $newvaluestr = &mt('none');
 7764:                         }
 7765:                         if ($item eq 'default') {
 7766:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7767:                         } else {
 7768:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7769:                         }
 7770:                     }
 7771:                 } else {
 7772:                     my $newvalue;
 7773:                     if ($key eq 'run') {
 7774:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7775:                     } else {
 7776:                         $newvalue = $offon[$env{'form.'.$key}];
 7777:                     }
 7778:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7779:                 }
 7780:             }
 7781:             $resulttext .= '</ul>';
 7782:         } else {
 7783:             $resulttext = &mt('No changes made to autoupdates');
 7784:         }
 7785:     } else {
 7786:         $resulttext = '<span class="LC_error">'.
 7787: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7788:     }
 7789:     return $resulttext;
 7790: }
 7791: 
 7792: sub modify_autocreate {
 7793:     my ($dom,%domconfig) = @_;
 7794:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7795:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7796:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7797:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7798:         }
 7799:     }
 7800:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7801:                  req => 'Auto-creation of validated requests for official courses',
 7802:                  xmldc => 'Identity of course creator of courses from XML files',
 7803:                );
 7804:     my @types = ('xml','req');
 7805:     foreach my $item (@types) {
 7806:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7807:         $newvals{$item} =~ s/\D//g;
 7808:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7809:     }
 7810:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7811:     my %domcoords = &get_active_dcs($dom);
 7812:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7813:         $newvals{'xmldc'} = '';
 7814:     } 
 7815:     %autocreatehash =  (
 7816:                         autocreate => { xml => $newvals{'xml'},
 7817:                                         req => $newvals{'req'},
 7818:                                       }
 7819:                        );
 7820:     if ($newvals{'xmldc'} ne '') {
 7821:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7822:     }
 7823:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7824:                                              $dom);
 7825:     if ($putresult eq 'ok') {
 7826:         my @items = @types;
 7827:         if ($newvals{'xml'}) {
 7828:             push(@items,'xmldc');
 7829:         }
 7830:         foreach my $item (@items) {
 7831:             if (exists($currautocreate{$item})) {
 7832:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7833:                     $changes{$item} = 1;
 7834:                 }
 7835:             } elsif ($newvals{$item}) {
 7836:                 $changes{$item} = 1;
 7837:             }
 7838:         }
 7839:         if (keys(%changes) > 0) {
 7840:             my @offon = ('off','on'); 
 7841:             $resulttext = &mt('Changes made:').'<ul>';
 7842:             foreach my $item (@types) {
 7843:                 if ($changes{$item}) {
 7844:                     my $newtxt = $offon[$newvals{$item}];
 7845:                     $resulttext .= '<li>'.
 7846:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7847:                                        '<b>','</b>').
 7848:                                    '</li>';
 7849:                 }
 7850:             }
 7851:             if ($changes{'xmldc'}) {
 7852:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7853:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7854:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7855:             }
 7856:             $resulttext .= '</ul>';
 7857:         } else {
 7858:             $resulttext = &mt('No changes made to auto-creation settings');
 7859:         }
 7860:     } else {
 7861:         $resulttext = '<span class="LC_error">'.
 7862:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7863:     }
 7864:     return $resulttext;
 7865: }
 7866: 
 7867: sub modify_directorysrch {
 7868:     my ($dom,%domconfig) = @_;
 7869:     my ($resulttext,%changes);
 7870:     my %currdirsrch;
 7871:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7872:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7873:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7874:         }
 7875:     }
 7876:     my %title = ( available => 'Directory search available',
 7877:                   localonly => 'Other domains can search',
 7878:                   searchby => 'Search types',
 7879:                   searchtypes => 'Search latitude');
 7880:     my @offon = ('off','on');
 7881:     my @otherdoms = ('Yes','No');
 7882: 
 7883:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7884:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7885:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7886: 
 7887:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7888:     if (keys(%{$usertypes}) == 0) {
 7889:         @cansearch = ('default');
 7890:     } else {
 7891:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7892:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7893:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7894:                     push(@{$changes{'cansearch'}},$type);
 7895:                 }
 7896:             }
 7897:             foreach my $type (@cansearch) {
 7898:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7899:                     push(@{$changes{'cansearch'}},$type);
 7900:                 }
 7901:             }
 7902:         } else {
 7903:             push(@{$changes{'cansearch'}},@cansearch);
 7904:         }
 7905:     }
 7906: 
 7907:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7908:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7909:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7910:                 push(@{$changes{'searchby'}},$by);
 7911:             }
 7912:         }
 7913:         foreach my $by (@searchby) {
 7914:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7915:                 push(@{$changes{'searchby'}},$by);
 7916:             }
 7917:         }
 7918:     } else {
 7919:         push(@{$changes{'searchby'}},@searchby);
 7920:     }
 7921: 
 7922:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7923:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7924:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7925:                 push(@{$changes{'searchtypes'}},$type);
 7926:             }
 7927:         }
 7928:         foreach my $type (@searchtypes) {
 7929:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7930:                 push(@{$changes{'searchtypes'}},$type);
 7931:             }
 7932:         }
 7933:     } else {
 7934:         if (exists($currdirsrch{'searchtypes'})) {
 7935:             foreach my $type (@searchtypes) {  
 7936:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7937:                     push(@{$changes{'searchtypes'}},$type);
 7938:                 }
 7939:             }
 7940:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7941:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7942:             }   
 7943:         } else {
 7944:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7945:         }
 7946:     }
 7947: 
 7948:     my %dirsrch_hash =  (
 7949:             directorysrch => { available => $env{'form.dirsrch_available'},
 7950:                                cansearch => \@cansearch,
 7951:                                localonly => $env{'form.dirsrch_localonly'},
 7952:                                searchby => \@searchby,
 7953:                                searchtypes => \@searchtypes,
 7954:                              }
 7955:             );
 7956:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7957:                                              $dom);
 7958:     if ($putresult eq 'ok') {
 7959:         if (exists($currdirsrch{'available'})) {
 7960:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7961:                  $changes{'available'} = 1;
 7962:              }
 7963:         } else {
 7964:             if ($env{'form.dirsrch_available'} eq '1') {
 7965:                 $changes{'available'} = 1;
 7966:             }
 7967:         }
 7968:         if (exists($currdirsrch{'localonly'})) {
 7969:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7970:                  $changes{'localonly'} = 1;
 7971:              }
 7972:         } else {
 7973:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7974:                 $changes{'localonly'} = 1;
 7975:             }
 7976:         }
 7977:         if (keys(%changes) > 0) {
 7978:             $resulttext = &mt('Changes made:').'<ul>';
 7979:             if ($changes{'available'}) {
 7980:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7981:             }
 7982:             if ($changes{'localonly'}) {
 7983:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7984:             }
 7985: 
 7986:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7987:                 my $chgtext;
 7988:                 if (ref($usertypes) eq 'HASH') {
 7989:                     if (keys(%{$usertypes}) > 0) {
 7990:                         foreach my $type (@{$types}) {
 7991:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7992:                                 $chgtext .= $usertypes->{$type}.'; ';
 7993:                             }
 7994:                         }
 7995:                         if (grep(/^default$/,@cansearch)) {
 7996:                             $chgtext .= $othertitle;
 7997:                         } else {
 7998:                             $chgtext =~ s/\; $//;
 7999:                         }
 8000:                         $resulttext .=
 8001:                             '<li>'.
 8002:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 8003:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 8004:                             '</li>';
 8005:                     }
 8006:                 }
 8007:             }
 8008:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 8009:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 8010:                 my $chgtext;
 8011:                 foreach my $type (@{$titleorder}) {
 8012:                     if (grep(/^\Q$type\E$/,@searchby)) {
 8013:                         if (defined($searchtitles->{$type})) {
 8014:                             $chgtext .= $searchtitles->{$type}.'; ';
 8015:                         }
 8016:                     }
 8017:                 }
 8018:                 $chgtext =~ s/\; $//;
 8019:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 8020:             }
 8021:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 8022:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 8023:                 my $chgtext;
 8024:                 foreach my $type (@{$srchtypeorder}) {
 8025:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 8026:                         if (defined($srchtypes_desc->{$type})) {
 8027:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 8028:                         }
 8029:                     }
 8030:                 }
 8031:                 $chgtext =~ s/\; $//;
 8032:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 8033:             }
 8034:             $resulttext .= '</ul>';
 8035:         } else {
 8036:             $resulttext = &mt('No changes made to institution directory search settings');
 8037:         }
 8038:     } else {
 8039:         $resulttext = '<span class="LC_error">'.
 8040:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 8041:     }
 8042:     return $resulttext;
 8043: }
 8044: 
 8045: sub modify_contacts {
 8046:     my ($dom,$lastactref,%domconfig) = @_;
 8047:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 8048:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 8049:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 8050:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 8051:         }
 8052:     }
 8053:     my (%others,%to,%bcc);
 8054:     my @contacts = ('supportemail','adminemail');
 8055:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 8056:                     'requestsmail','updatesmail','idconflictsmail');
 8057:     my @toggles = ('reporterrors','reportupdates');
 8058:     foreach my $type (@mailings) {
 8059:         @{$newsetting{$type}} = 
 8060:             &Apache::loncommon::get_env_multiple('form.'.$type);
 8061:         foreach my $item (@contacts) {
 8062:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 8063:                 $contacts_hash{contacts}{$type}{$item} = 1;
 8064:             } else {
 8065:                 $contacts_hash{contacts}{$type}{$item} = 0;
 8066:             }
 8067:         }  
 8068:         $others{$type} = $env{'form.'.$type.'_others'};
 8069:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 8070:         if ($type eq 'helpdeskmail') {
 8071:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 8072:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 8073:         }
 8074:     }
 8075:     foreach my $item (@contacts) {
 8076:         $to{$item} = $env{'form.'.$item};
 8077:         $contacts_hash{'contacts'}{$item} = $to{$item};
 8078:     }
 8079:     foreach my $item (@toggles) {
 8080:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 8081:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 8082:         }
 8083:     }
 8084:     if (keys(%currsetting) > 0) {
 8085:         foreach my $item (@contacts) {
 8086:             if ($to{$item} ne $currsetting{$item}) {
 8087:                 $changes{$item} = 1;
 8088:             }
 8089:         }
 8090:         foreach my $type (@mailings) {
 8091:             foreach my $item (@contacts) {
 8092:                 if (ref($currsetting{$type}) eq 'HASH') {
 8093:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 8094:                         push(@{$changes{$type}},$item);
 8095:                     }
 8096:                 } else {
 8097:                     push(@{$changes{$type}},@{$newsetting{$type}});
 8098:                 }
 8099:             }
 8100:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 8101:                 push(@{$changes{$type}},'others');
 8102:             }
 8103:             if ($type eq 'helpdeskmail') {   
 8104:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 8105:                     push(@{$changes{$type}},'bcc'); 
 8106:                 }
 8107:             }
 8108:         }
 8109:     } else {
 8110:         my %default;
 8111:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 8112:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 8113:         $default{'errormail'} = 'adminemail';
 8114:         $default{'packagesmail'} = 'adminemail';
 8115:         $default{'helpdeskmail'} = 'supportemail';
 8116:         $default{'lonstatusmail'} = 'adminemail';
 8117:         $default{'requestsmail'} = 'adminemail';
 8118:         $default{'updatesmail'} = 'adminemail';
 8119:         foreach my $item (@contacts) {
 8120:            if ($to{$item} ne $default{$item}) {
 8121:               $changes{$item} = 1;
 8122:            }
 8123:         }
 8124:         foreach my $type (@mailings) {
 8125:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 8126:                
 8127:                 push(@{$changes{$type}},@{$newsetting{$type}});
 8128:             }
 8129:             if ($others{$type} ne '') {
 8130:                 push(@{$changes{$type}},'others');
 8131:             }
 8132:             if ($type eq 'helpdeskmail') {
 8133:                 if ($bcc{$type} ne '') {
 8134:                     push(@{$changes{$type}},'bcc');
 8135:                 }
 8136:             }
 8137:         }
 8138:     }
 8139:     foreach my $item (@toggles) {
 8140:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 8141:             $changes{$item} = 1;
 8142:         } elsif ((!$env{'form.'.$item}) &&
 8143:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 8144:             $changes{$item} = 1;
 8145:         }
 8146:     }
 8147:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 8148:                                              $dom);
 8149:     if ($putresult eq 'ok') {
 8150:         if (keys(%changes) > 0) {
 8151:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8152:             if (ref($lastactref) eq 'HASH') {
 8153:                 $lastactref->{'domainconfig'} = 1;
 8154:             }
 8155:             my ($titles,$short_titles)  = &contact_titles();
 8156:             $resulttext = &mt('Changes made:').'<ul>';
 8157:             foreach my $item (@contacts) {
 8158:                 if ($changes{$item}) {
 8159:                     $resulttext .= '<li>'.$titles->{$item}.
 8160:                                     &mt(' set to: ').
 8161:                                     '<span class="LC_cusr_emph">'.
 8162:                                     $to{$item}.'</span></li>';
 8163:                 }
 8164:             }
 8165:             foreach my $type (@mailings) {
 8166:                 if (ref($changes{$type}) eq 'ARRAY') {
 8167:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 8168:                     my @text;
 8169:                     foreach my $item (@{$newsetting{$type}}) {
 8170:                         push(@text,$short_titles->{$item});
 8171:                     }
 8172:                     if ($others{$type} ne '') {
 8173:                         push(@text,$others{$type});
 8174:                     }
 8175:                     $resulttext .= '<span class="LC_cusr_emph">'.
 8176:                                    join(', ',@text).'</span>';
 8177:                     if ($type eq 'helpdeskmail') {
 8178:                         if ($bcc{$type} ne '') {
 8179:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 8180:                         }
 8181:                     }
 8182:                     $resulttext .= '</li>';
 8183:                 }
 8184:             }
 8185:             my @offon = ('off','on');
 8186:             if ($changes{'reporterrors'}) {
 8187:                 $resulttext .= '<li>'.
 8188:                                &mt('E-mail error reports to [_1] set to "'.
 8189:                                    $offon[$env{'form.reporterrors'}].'".',
 8190:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8191:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 8192:                                '</li>';
 8193:             }
 8194:             if ($changes{'reportupdates'}) {
 8195:                 $resulttext .= '<li>'.
 8196:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 8197:                                     $offon[$env{'form.reportupdates'}].'".',
 8198:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8199:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 8200:                                 '</li>';
 8201:             }
 8202:             $resulttext .= '</ul>';
 8203:         } else {
 8204:             $resulttext = &mt('No changes made to contact information');
 8205:         }
 8206:     } else {
 8207:         $resulttext = '<span class="LC_error">'.
 8208:             &mt('An error occurred: [_1].',$putresult).'</span>';
 8209:     }
 8210:     return $resulttext;
 8211: }
 8212: 
 8213: sub modify_usercreation {
 8214:     my ($dom,%domconfig) = @_;
 8215:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 8216:     my $warningmsg;
 8217:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8218:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8219:             if ($key eq 'cancreate') {
 8220:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8221:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8222:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8223:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 8224:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8225:                         } else {
 8226:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8227:                         }
 8228:                     }
 8229:                 }
 8230:             } elsif ($key eq 'email_rule') {
 8231:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8232:             } else {
 8233:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8234:             }
 8235:         }
 8236:     }
 8237:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 8238:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 8239:     my @contexts = ('author','course','requestcrs');
 8240:     foreach my $item(@contexts) {
 8241:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 8242:     }
 8243:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8244:         foreach my $item (@contexts) {
 8245:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 8246:                 push(@{$changes{'cancreate'}},$item);
 8247:             }
 8248:         }
 8249:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 8250:         foreach my $item (@contexts) {
 8251:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 8252:                 if ($cancreate{$item} ne 'any') {
 8253:                     push(@{$changes{'cancreate'}},$item);
 8254:                 }
 8255:             } else {
 8256:                 if ($cancreate{$item} ne 'none') {
 8257:                     push(@{$changes{'cancreate'}},$item);
 8258:                 }
 8259:             }
 8260:         }
 8261:     } else {
 8262:         foreach my $item (@contexts)  {
 8263:             push(@{$changes{'cancreate'}},$item);
 8264:         }
 8265:     }
 8266: 
 8267:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 8268:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 8269:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 8270:                 push(@{$changes{'username_rule'}},$type);
 8271:             }
 8272:         }
 8273:         foreach my $type (@username_rule) {
 8274:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 8275:                 push(@{$changes{'username_rule'}},$type);
 8276:             }
 8277:         }
 8278:     } else {
 8279:         push(@{$changes{'username_rule'}},@username_rule);
 8280:     }
 8281: 
 8282:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 8283:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 8284:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 8285:                 push(@{$changes{'id_rule'}},$type);
 8286:             }
 8287:         }
 8288:         foreach my $type (@id_rule) {
 8289:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 8290:                 push(@{$changes{'id_rule'}},$type);
 8291:             }
 8292:         }
 8293:     } else {
 8294:         push(@{$changes{'id_rule'}},@id_rule);
 8295:     }
 8296: 
 8297:     my @authen_contexts = ('author','course','domain');
 8298:     my @authtypes = ('int','krb4','krb5','loc');
 8299:     my %authhash;
 8300:     foreach my $item (@authen_contexts) {
 8301:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 8302:         foreach my $auth (@authtypes) {
 8303:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 8304:                 $authhash{$item}{$auth} = 1;
 8305:             } else {
 8306:                 $authhash{$item}{$auth} = 0;
 8307:             }
 8308:         }
 8309:     }
 8310:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 8311:         foreach my $item (@authen_contexts) {
 8312:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 8313:                 foreach my $auth (@authtypes) {
 8314:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 8315:                         push(@{$changes{'authtypes'}},$item);
 8316:                         last;
 8317:                     }
 8318:                 }
 8319:             }
 8320:         }
 8321:     } else {
 8322:         foreach my $item (@authen_contexts) {
 8323:             push(@{$changes{'authtypes'}},$item);
 8324:         }
 8325:     }
 8326: 
 8327:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 8328:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 8329:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 8330:     $save_usercreate{'id_rule'} = \@id_rule;
 8331:     $save_usercreate{'username_rule'} = \@username_rule,
 8332:     $save_usercreate{'authtypes'} = \%authhash;
 8333: 
 8334:     my %usercreation_hash =  (
 8335:         usercreation     => \%save_usercreate,
 8336:     );
 8337: 
 8338:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 8339:                                              $dom);
 8340: 
 8341:     if ($putresult eq 'ok') {
 8342:         if (keys(%changes) > 0) {
 8343:             $resulttext = &mt('Changes made:').'<ul>';
 8344:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8345:                 my %lt = &usercreation_types();
 8346:                 foreach my $type (@{$changes{'cancreate'}}) {
 8347:                     my $chgtext = $lt{$type}.', ';
 8348:                     if ($cancreate{$type} eq 'none') {
 8349:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 8350:                     } elsif ($cancreate{$type} eq 'any') {
 8351:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 8352:                     } elsif ($cancreate{$type} eq 'official') {
 8353:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 8354:                     } elsif ($cancreate{$type} eq 'unofficial') {
 8355:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 8356:                     }
 8357:                     $resulttext .= '<li>'.$chgtext.'</li>';
 8358:                 }
 8359:             }
 8360:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 8361:                 my ($rules,$ruleorder) = 
 8362:                     &Apache::lonnet::inst_userrules($dom,'username');
 8363:                 my $chgtext = '<ul>';
 8364:                 foreach my $type (@username_rule) {
 8365:                     if (ref($rules->{$type}) eq 'HASH') {
 8366:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 8367:                     }
 8368:                 }
 8369:                 $chgtext .= '</ul>';
 8370:                 if (@username_rule > 0) {
 8371:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 8372:                 } else {
 8373:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 8374:                 }
 8375:             }
 8376:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 8377:                 my ($idrules,$idruleorder) = 
 8378:                     &Apache::lonnet::inst_userrules($dom,'id');
 8379:                 my $chgtext = '<ul>';
 8380:                 foreach my $type (@id_rule) {
 8381:                     if (ref($idrules->{$type}) eq 'HASH') {
 8382:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8383:                     }
 8384:                 }
 8385:                 $chgtext .= '</ul>';
 8386:                 if (@id_rule > 0) {
 8387:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8388:                 } else {
 8389:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8390:                 }
 8391:             }
 8392:             my %authname = &authtype_names();
 8393:             my %context_title = &context_names();
 8394:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8395:                 my $chgtext = '<ul>';
 8396:                 foreach my $type (@{$changes{'authtypes'}}) {
 8397:                     my @allowed;
 8398:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8399:                     foreach my $auth (@authtypes) {
 8400:                         if ($authhash{$type}{$auth}) {
 8401:                             push(@allowed,$authname{$auth});
 8402:                         }
 8403:                     }
 8404:                     if (@allowed > 0) {
 8405:                         $chgtext .= join(', ',@allowed).'</li>';
 8406:                     } else {
 8407:                         $chgtext .= &mt('none').'</li>';
 8408:                     }
 8409:                 }
 8410:                 $chgtext .= '</ul>';
 8411:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8412:                 $resulttext .= '</li>';
 8413:             }
 8414:             $resulttext .= '</ul>';
 8415:         } else {
 8416:             $resulttext = &mt('No changes made to user creation settings');
 8417:         }
 8418:     } else {
 8419:         $resulttext = '<span class="LC_error">'.
 8420:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8421:     }
 8422:     if ($warningmsg ne '') {
 8423:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8424:     }
 8425:     return $resulttext;
 8426: }
 8427: 
 8428: sub modify_selfcreation {
 8429:     my ($dom,%domconfig) = @_;
 8430:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8431:     my (%save_usercreate,%save_usermodify);
 8432:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8433:     if (ref($types) eq 'ARRAY') {
 8434:         $usertypes->{'default'} = $othertitle;
 8435:         push(@{$types},'default');
 8436:     }
 8437: #
 8438: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8439: #
 8440:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8441:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8442:             if ($key eq 'cancreate') {
 8443:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8444:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8445:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8446:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8447:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 8448:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 8449:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8450:                         } else {
 8451:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8452:                         }
 8453:                     }
 8454:                 }
 8455:             } elsif ($key eq 'email_rule') {
 8456:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8457:             } else {
 8458:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8459:             }
 8460:         }
 8461:     }
 8462: #
 8463: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8464: #
 8465:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8466:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8467:             if ($key eq 'selfcreate') {
 8468:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8469:             } else {
 8470:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8471:             }
 8472:         }
 8473:     }
 8474: 
 8475:     my @contexts = ('selfcreate');
 8476:     @{$cancreate{'selfcreate'}} = ();
 8477:     %{$cancreate{'emailusername'}} = ();
 8478:     @{$cancreate{'statustocreate'}} = ();
 8479:     %{$cancreate{'selfcreateprocessing'}} = ();
 8480:     %{$cancreate{'shibenv'}} = ();
 8481:     my %selfcreatetypes = (
 8482:                              sso   => 'users authenticated by institutional single sign on',
 8483:                              login => 'users authenticated by institutional log-in',
 8484:                              email => 'users who provide a valid e-mail address for use as username',
 8485:                           );
 8486: #
 8487: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8488: # is permitted.
 8489: #
 8490: 
 8491:     my @statuses;
 8492:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 8493:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 8494:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 8495:         }
 8496:     }
 8497:     push(@statuses,'default');
 8498: 
 8499:     foreach my $item ('login','sso','email') {
 8500:         if ($item eq 'email') {
 8501:             if ($env{'form.cancreate_email'}) {
 8502:                 push(@{$cancreate{'selfcreate'}},'email');
 8503:                 push(@contexts,'selfcreateprocessing');
 8504:                 foreach my $type (@statuses) {
 8505:                     if ($type eq 'default') {
 8506:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 8507:                     } else { 
 8508:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 8509:                     }
 8510:                 }
 8511:             }
 8512:         } else {
 8513:             if ($env{'form.cancreate_'.$item}) {
 8514:                 push(@{$cancreate{'selfcreate'}},$item);
 8515:             }
 8516:         }
 8517:     }
 8518:     my (@email_rule,%userinfo,%savecaptcha);
 8519:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8520: #
 8521: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8522: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8523: #
 8524: 
 8525:     if ($env{'form.cancreate_email'} eq 'email') {
 8526:         push(@contexts,'emailusername');
 8527:         if (ref($types) eq 'ARRAY') {
 8528:             foreach my $type (@{$types}) {
 8529:                 if (ref($infofields) eq 'ARRAY') {
 8530:                     foreach my $field (@{$infofields}) {
 8531:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8532:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8533:                         }
 8534:                     }
 8535:                 }
 8536:             }
 8537:         }
 8538: #
 8539: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8540: # queued requests for self-creation of account using e-mail address as username
 8541: #
 8542: 
 8543:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8544:         @approvalnotify = sort(@approvalnotify);
 8545:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8546:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8547:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8548:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8549:                     push(@{$changes{'cancreate'}},'notify');
 8550:                 }
 8551:             } else {
 8552:                 if ($cancreate{'notify'}{'approval'}) {
 8553:                     push(@{$changes{'cancreate'}},'notify');
 8554:                 }
 8555:             }
 8556:         } elsif ($cancreate{'notify'}{'approval'}) {
 8557:             push(@{$changes{'cancreate'}},'notify');
 8558:         }
 8559: 
 8560: #
 8561: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8562: #
 8563:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8564:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8565:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8566:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8567:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8568:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8569:                         push(@{$changes{'email_rule'}},$type);
 8570:                     }
 8571:                 }
 8572:             }
 8573:             if (@email_rule > 0) {
 8574:                 foreach my $type (@email_rule) {
 8575:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8576:                         push(@{$changes{'email_rule'}},$type);
 8577:                     }
 8578:                 }
 8579:             }
 8580:         } elsif (@email_rule > 0) {
 8581:             push(@{$changes{'email_rule'}},@email_rule);
 8582:         }
 8583:     }
 8584: #  
 8585: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 8586: # institutional log-in.
 8587: #
 8588:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8589:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8590:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8591:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8592:             $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.').' '.
 8593:                           &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.');
 8594:         }
 8595:     }
 8596:     my @fields = ('lastname','firstname','middlename','generation',
 8597:                   'permanentemail','id');
 8598:     my @shibfields = (@fields,'inststatus');
 8599:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8600: #
 8601: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8602: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8603: # may self-create accounts 
 8604: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8605: # which the user may supply, if institutional data is unavailable.
 8606: #
 8607:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8608:         if (ref($types) eq 'ARRAY') {
 8609:             if (@{$types} > 1) {
 8610:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8611:                 push(@contexts,'statustocreate');
 8612:             } else {
 8613:                 undef($cancreate{'statustocreate'});
 8614:             } 
 8615:             foreach my $type (@{$types}) {
 8616:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8617:                 foreach my $field (@fields) {
 8618:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8619:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8620:                     } else {
 8621:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8622:                     }
 8623:                 }
 8624:             }
 8625:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8626:                 foreach my $type (@{$types}) {
 8627:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8628:                         foreach my $field (@fields) {
 8629:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8630:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8631:                                 push(@{$changes{'selfcreate'}},$type);
 8632:                                 last;
 8633:                             }
 8634:                         }
 8635:                     }
 8636:                 }
 8637:             } else {
 8638:                 foreach my $type (@{$types}) {
 8639:                     push(@{$changes{'selfcreate'}},$type);
 8640:                 }
 8641:             }
 8642:         }
 8643:         foreach my $field (@shibfields) {
 8644:             if ($env{'form.shibenv_'.$field} ne '') {
 8645:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 8646:             }
 8647:         }
 8648:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8649:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 8650:                 foreach my $field (@shibfields) {
 8651:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 8652:                         push(@{$changes{'cancreate'}},'shibenv');
 8653:                     }
 8654:                 }
 8655:             } else {
 8656:                 foreach my $field (@shibfields) {
 8657:                     if ($env{'form.shibenv_'.$field}) {
 8658:                         push(@{$changes{'cancreate'}},'shibenv');
 8659:                         last;
 8660:                     }
 8661:                 }
 8662:             }
 8663:         }
 8664:     }
 8665:     foreach my $item (@contexts) {
 8666:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 8667:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 8668:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 8669:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 8670:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8671:                             push(@{$changes{'cancreate'}},$item);
 8672:                         }
 8673:                     }
 8674:                 }
 8675:             }
 8676:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8677:                 foreach my $type (@{$cancreate{$item}}) {
 8678:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 8679:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8680:                             push(@{$changes{'cancreate'}},$item);
 8681:                         }
 8682:                     }
 8683:                 }
 8684:             }
 8685:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 8686:             if (ref($cancreate{$item}) eq 'HASH') {
 8687:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 8688:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 8689:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 8690:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 8691:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8692:                                     push(@{$changes{'cancreate'}},$item);
 8693:                                 }
 8694:                             }
 8695:                         }
 8696:                     } elsif ($item eq 'selfcreateprocessing') {
 8697:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 8698:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8699:                                 push(@{$changes{'cancreate'}},$item);
 8700:                             }
 8701:                         }
 8702:                     } else {
 8703:                         if (!$cancreate{$item}{$curr}) {
 8704:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8705:                                 push(@{$changes{'cancreate'}},$item);
 8706:                             }
 8707:                         }
 8708:                     }
 8709:                 }
 8710:                 foreach my $field (keys(%{$cancreate{$item}})) {
 8711:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 8712:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 8713:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 8714:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 8715:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8716:                                         push(@{$changes{'cancreate'}},$item);
 8717:                                     }
 8718:                                 }
 8719:                             } else {
 8720:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8721:                                     push(@{$changes{'cancreate'}},$item);
 8722:                                 }
 8723:                             }
 8724:                         }
 8725:                     } elsif ($item eq 'selfcreateprocessing') {
 8726:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 8727:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8728:                                 push(@{$changes{'cancreate'}},$item);
 8729:                             }
 8730:                         }
 8731:                     } else {
 8732:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 8733:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8734:                                 push(@{$changes{'cancreate'}},$item);
 8735:                             }
 8736:                         }
 8737:                     }
 8738:                 }
 8739:             }
 8740:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 8741:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8742:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 8743:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8744:                         push(@{$changes{'cancreate'}},$item);
 8745:                     }
 8746:                 }
 8747:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 8748:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 8749:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8750:                         push(@{$changes{'cancreate'}},$item);
 8751:                     }
 8752:                 }
 8753:             }
 8754:         } elsif ($item eq 'emailusername') {
 8755:             if (ref($cancreate{$item}) eq 'HASH') {
 8756:                 foreach my $type (keys(%{$cancreate{$item}})) {
 8757:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 8758:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 8759:                             if ($cancreate{$item}{$type}{$field}) {
 8760:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8761:                                     push(@{$changes{'cancreate'}},$item);
 8762:                                 }
 8763:                                 last;
 8764:                             }
 8765:                         }
 8766:                     }
 8767:                 }
 8768:             }
 8769:         }
 8770:     }
 8771: #
 8772: # Populate %save_usercreate hash with updates to self-creation configuration.
 8773: #
 8774:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 8775:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 8776:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 8777:     if (ref($cancreate{'notify'}) eq 'HASH') {
 8778:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 8779:     }
 8780:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
 8781:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
 8782:     }
 8783:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8784:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 8785:     }
 8786:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
 8787:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
 8788:     }
 8789:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 8790:     $save_usercreate{'emailrule'} = \@email_rule;
 8791: 
 8792:     my %userconfig_hash = (
 8793:             usercreation     => \%save_usercreate,
 8794:             usermodification => \%save_usermodify,
 8795:     );
 8796:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8797:                                              $dom);
 8798: #
 8799: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8800: #
 8801:     if ($putresult eq 'ok') {
 8802:         if (keys(%changes) > 0) {
 8803:             $resulttext = &mt('Changes made:').'<ul>';
 8804:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8805:                 my %lt = &selfcreation_types();
 8806:                 foreach my $type (@{$changes{'cancreate'}}) {
 8807:                     my $chgtext;
 8808:                     if ($type eq 'selfcreate') {
 8809:                         if (@{$cancreate{$type}} == 0) {
 8810:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 8811:                         } else {
 8812:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 8813:                                         '<ul>';
 8814:                             foreach my $case (@{$cancreate{$type}}) {
 8815:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 8816:                             }
 8817:                             $chgtext .= '</ul>';
 8818:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 8819:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 8820:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8821:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 8822:                                             $chgtext .= '<br />'.
 8823:                                                         '<span class="LC_warning">'.
 8824:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8825:                                                         '</span>';
 8826:                                         }
 8827:                                     }
 8828:                                 }
 8829:                             }
 8830:                         }
 8831:                     } elsif ($type eq 'shibenv') {
 8832:                         if (keys(%{$cancreate{$type}}) == 0) {
 8833:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
 8834:                         } else {
 8835:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
 8836:                                         '<ul>';
 8837:                             foreach my $field (@shibfields) {
 8838:                                 next if ($cancreate{$type}{$field} eq '');
 8839:                                 if ($field eq 'inststatus') {
 8840:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
 8841:                                 } else {
 8842:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
 8843:                                 }
 8844:                             }
 8845:                             $chgtext .= '</ul>';
 8846:                         }  
 8847:                     } elsif ($type eq 'statustocreate') {
 8848:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 8849:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 8850:                             if (@{$cancreate{'selfcreate'}} > 0) {
 8851:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 8852:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 8853:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 8854:                                         $chgtext .= '<br />'.
 8855:                                                     '<span class="LC_warning">'.
 8856:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8857:                                                     '</span>';
 8858:                                     }
 8859:                                 } elsif (ref($usertypes) eq 'HASH') {
 8860:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8861:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 8862:                                     } else {
 8863:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 8864:                                     }
 8865:                                     $chgtext .= '<ul>';
 8866:                                     foreach my $case (@{$cancreate{$type}}) {
 8867:                                         if ($case eq 'default') {
 8868:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 8869:                                         } else {
 8870:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 8871:                                         }
 8872:                                     }
 8873:                                     $chgtext .= '</ul>';
 8874:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8875:                                         $chgtext .= '<br /><span class="LC_warning">'.
 8876:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 8877:                                                     '</span>';
 8878:                                     }
 8879:                                 }
 8880:                             } else {
 8881:                                 if (@{$cancreate{$type}} == 0) {
 8882:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8883:                                 } else {
 8884:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8885:                                 }
 8886:                             }
 8887:                         }
 8888:                     } elsif ($type eq 'selfcreateprocessing') {
 8889:                         my %choices = &Apache::lonlocal::texthash (
 8890:                                                                     automatic => 'Automatic approval',
 8891:                                                                     approval  => 'Queued for approval',
 8892:                                                                   );
 8893:                         if (@statuses > 1) {
 8894:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
 8895:                                         '<ul>';
 8896:                            foreach my $type (@statuses) {
 8897:                                if ($type eq 'default') {
 8898:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8899:                                } else {
 8900:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 8901:                                }
 8902:                            }
 8903:                            $chgtext .= '</ul>';
 8904:                         } else {
 8905:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
 8906:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
 8907:                         }
 8908:                     } elsif ($type eq 'captcha') {
 8909:                         if ($savecaptcha{$type} eq 'notused') {
 8910:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8911:                         } else {
 8912:                             my %captchas = &captcha_phrases();
 8913:                             if ($captchas{$savecaptcha{$type}}) {
 8914:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8915:                             } else {
 8916:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8917:                             }
 8918:                         }
 8919:                     } elsif ($type eq 'recaptchakeys') {
 8920:                         my ($privkey,$pubkey);
 8921:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8922:                             $pubkey = $savecaptcha{$type}{'public'};
 8923:                             $privkey = $savecaptcha{$type}{'private'};
 8924:                         }
 8925:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8926:                         if (!$pubkey) {
 8927:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8928:                         } else {
 8929:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8930:                         }
 8931:                         if (!$privkey) {
 8932:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8933:                         } else {
 8934:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8935:                         }
 8936:                         $chgtext .= '</ul>';
 8937:                     } elsif ($type eq 'emailusername') {
 8938:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8939:                             if (ref($types) eq 'ARRAY') {
 8940:                                 foreach my $type (@{$types}) {
 8941:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 8942:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 8943:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',$usertypes->{$type}).
 8944:                                                     '<ul>';
 8945:                                             foreach my $field (@{$infofields}) {
 8946:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 8947:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8948:                                                 }
 8949:                                             }
 8950:                                         }
 8951:                                         $chgtext .= '</ul>';
 8952:                                     } else {
 8953:                                         $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 />';
 8954:                                     }
 8955:                                 }
 8956:                             }
 8957:                         }
 8958:                     } elsif ($type eq 'notify') {
 8959:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8960:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8961:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8962:                                 if ($cancreate{'notify'}{'approval'}) {
 8963:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8964:                                 }
 8965:                             }
 8966:                         }
 8967:                     }
 8968:                     if ($chgtext) {
 8969:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8970:                     }
 8971:                 }
 8972:             }
 8973:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8974:                 my ($emailrules,$emailruleorder) =
 8975:                     &Apache::lonnet::inst_userrules($dom,'email');
 8976:                 my $chgtext = '<ul>';
 8977:                 foreach my $type (@email_rule) {
 8978:                     if (ref($emailrules->{$type}) eq 'HASH') {
 8979:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 8980:                     }
 8981:                 }
 8982:                 $chgtext .= '</ul>';
 8983:                 if (@email_rule > 0) {
 8984:                     $resulttext .= '<li>'.
 8985:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 8986:                                        $chgtext.
 8987:                                    '</li>';
 8988:                 } else {
 8989:                     $resulttext .= '<li>'.
 8990:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 8991:                                    '</li>';
 8992:                 }
 8993:             }
 8994:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 8995:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 8996:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8997:                 foreach my $type (@{$changes{'selfcreate'}}) {
 8998:                     my $typename = $type;
 8999:                     if (ref($usertypes) eq 'HASH') {
 9000:                         if ($usertypes->{$type} ne '') {
 9001:                             $typename = $usertypes->{$type};
 9002:                         }
 9003:                     }
 9004:                     my @modifiable;
 9005:                     $resulttext .= '<li>'.
 9006:                                     &mt('Self-creation of account by users with status: [_1]',
 9007:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 9008:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 9009:                     foreach my $field (@fields) {
 9010:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 9011:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 9012:                         }
 9013:                     }
 9014:                     if (@modifiable > 0) {
 9015:                         $resulttext .= join(', ',@modifiable);
 9016:                     } else {
 9017:                         $resulttext .= &mt('none');
 9018:                     }
 9019:                     $resulttext .= '</li>';
 9020:                 }
 9021:                 $resulttext .= '</ul></li>';
 9022:             }
 9023:             $resulttext .= '</ul>';
 9024:         } else {
 9025:             $resulttext = &mt('No changes made to self-creation settings');
 9026:         }
 9027:     } else {
 9028:         $resulttext = '<span class="LC_error">'.
 9029:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9030:     }
 9031:     if ($warningmsg ne '') {
 9032:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9033:     }
 9034:     return $resulttext;
 9035: }
 9036: 
 9037: sub process_captcha {
 9038:     my ($container,$changes,$newsettings,$current) = @_;
 9039:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 9040:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 9041:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 9042:         $newsettings->{'captcha'} = 'original';
 9043:     }
 9044:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 9045:         if ($container eq 'cancreate') {
 9046:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9047:                 push(@{$changes->{'cancreate'}},'captcha');
 9048:             } elsif (!defined($changes->{'cancreate'})) {
 9049:                 $changes->{'cancreate'} = ['captcha'];
 9050:             }
 9051:         } else {
 9052:             $changes->{'captcha'} = 1;
 9053:         }
 9054:     }
 9055:     my ($newpub,$newpriv,$currpub,$currpriv);
 9056:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 9057:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 9058:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 9059:         $newpub =~ s/\W//g;
 9060:         $newpriv =~ s/\W//g;
 9061:         $newsettings->{'recaptchakeys'} = {
 9062:                                              public  => $newpub,
 9063:                                              private => $newpriv,
 9064:                                           };
 9065:     }
 9066:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 9067:         $currpub = $current->{'recaptchakeys'}{'public'};
 9068:         $currpriv = $current->{'recaptchakeys'}{'private'};
 9069:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 9070:             $newsettings->{'recaptchakeys'} = {
 9071:                                                  public  => '',
 9072:                                                  private => '',
 9073:                                               }
 9074:         }
 9075:     }
 9076:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 9077:         if ($container eq 'cancreate') {
 9078:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9079:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 9080:             } elsif (!defined($changes->{'cancreate'})) {
 9081:                 $changes->{'cancreate'} = ['recaptchakeys'];
 9082:             }
 9083:         } else {
 9084:             $changes->{'recaptchakeys'} = 1;
 9085:         }
 9086:     }
 9087:     return;
 9088: }
 9089: 
 9090: sub modify_usermodification {
 9091:     my ($dom,%domconfig) = @_;
 9092:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 9093:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9094:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9095:             if ($key eq 'selfcreate') {
 9096:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 9097:             } else {  
 9098:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 9099:             }
 9100:         }
 9101:     }
 9102:     my @contexts = ('author','course');
 9103:     my %context_title = (
 9104:                            author => 'In author context',
 9105:                            course => 'In course context',
 9106:                         );
 9107:     my @fields = ('lastname','firstname','middlename','generation',
 9108:                   'permanentemail','id');
 9109:     my %roles = (
 9110:                   author => ['ca','aa'],
 9111:                   course => ['st','ep','ta','in','cr'],
 9112:                 );
 9113:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9114:     foreach my $context (@contexts) {
 9115:         foreach my $role (@{$roles{$context}}) {
 9116:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 9117:             foreach my $item (@fields) {
 9118:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 9119:                     $modifyhash{$context}{$role}{$item} = 1;
 9120:                 } else {
 9121:                     $modifyhash{$context}{$role}{$item} = 0;
 9122:                 }
 9123:             }
 9124:         }
 9125:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 9126:             foreach my $role (@{$roles{$context}}) {
 9127:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 9128:                     foreach my $field (@fields) {
 9129:                         if ($modifyhash{$context}{$role}{$field} ne 
 9130:                                 $curr_usermodification{$context}{$role}{$field}) {
 9131:                             push(@{$changes{$context}},$role);
 9132:                             last;
 9133:                         }
 9134:                     }
 9135:                 }
 9136:             }
 9137:         } else {
 9138:             foreach my $context (@contexts) {
 9139:                 foreach my $role (@{$roles{$context}}) {
 9140:                     push(@{$changes{$context}},$role);
 9141:                 }
 9142:             }
 9143:         }
 9144:     }
 9145:     my %usermodification_hash =  (
 9146:                                    usermodification => \%modifyhash,
 9147:                                  );
 9148:     my $putresult = &Apache::lonnet::put_dom('configuration',
 9149:                                              \%usermodification_hash,$dom);
 9150:     if ($putresult eq 'ok') {
 9151:         if (keys(%changes) > 0) {
 9152:             $resulttext = &mt('Changes made: ').'<ul>';
 9153:             foreach my $context (@contexts) {
 9154:                 if (ref($changes{$context}) eq 'ARRAY') {
 9155:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 9156:                     if (ref($changes{$context}) eq 'ARRAY') {
 9157:                         foreach my $role (@{$changes{$context}}) {
 9158:                             my $rolename;
 9159:                             if ($role eq 'cr') {
 9160:                                 $rolename = &mt('Custom');
 9161:                             } else {
 9162:                                 $rolename = &Apache::lonnet::plaintext($role);
 9163:                             }
 9164:                             my @modifiable;
 9165:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 9166:                             foreach my $field (@fields) {
 9167:                                 if ($modifyhash{$context}{$role}{$field}) {
 9168:                                     push(@modifiable,$fieldtitles{$field});
 9169:                                 }
 9170:                             }
 9171:                             if (@modifiable > 0) {
 9172:                                 $resulttext .= join(', ',@modifiable);
 9173:                             } else {
 9174:                                 $resulttext .= &mt('none'); 
 9175:                             }
 9176:                             $resulttext .= '</li>';
 9177:                         }
 9178:                         $resulttext .= '</ul></li>';
 9179:                     }
 9180:                 }
 9181:             }
 9182:             $resulttext .= '</ul>';
 9183:         } else {
 9184:             $resulttext = &mt('No changes made to user modification settings');
 9185:         }
 9186:     } else {
 9187:         $resulttext = '<span class="LC_error">'.
 9188:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9189:     }
 9190:     return $resulttext;
 9191: }
 9192: 
 9193: sub modify_defaults {
 9194:     my ($dom,$lastactref,%domconfig) = @_;
 9195:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 9196:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9197:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 9198:     my @authtypes = ('internal','krb4','krb5','localauth');
 9199:     foreach my $item (@items) {
 9200:         $newvalues{$item} = $env{'form.'.$item};
 9201:         if ($item eq 'auth_def') {
 9202:             if ($newvalues{$item} ne '') {
 9203:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 9204:                     push(@errors,$item);
 9205:                 }
 9206:             }
 9207:         } elsif ($item eq 'lang_def') {
 9208:             if ($newvalues{$item} ne '') {
 9209:                 if ($newvalues{$item} =~ /^(\w+)/) {
 9210:                     my $langcode = $1;
 9211:                     if ($langcode ne 'x_chef') {
 9212:                         if (code2language($langcode) eq '') {
 9213:                             push(@errors,$item);
 9214:                         }
 9215:                     }
 9216:                 } else {
 9217:                     push(@errors,$item);
 9218:                 }
 9219:             }
 9220:         } elsif ($item eq 'timezone_def') {
 9221:             if ($newvalues{$item} ne '') {
 9222:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 9223:                     push(@errors,$item);   
 9224:                 }
 9225:             }
 9226:         } elsif ($item eq 'datelocale_def') {
 9227:             if ($newvalues{$item} ne '') {
 9228:                 my @datelocale_ids = DateTime::Locale->ids();
 9229:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 9230:                     push(@errors,$item);
 9231:                 }
 9232:             }
 9233:         } elsif ($item eq 'portal_def') {
 9234:             if ($newvalues{$item} ne '') {
 9235:                 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])\/?$/) {
 9236:                     push(@errors,$item);
 9237:                 }
 9238:             }
 9239:         }
 9240:         if (grep(/^\Q$item\E$/,@errors)) {
 9241:             $newvalues{$item} = $domdefaults{$item};
 9242:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 9243:             $changes{$item} = 1;
 9244:         }
 9245:         $domdefaults{$item} = $newvalues{$item};
 9246:     }
 9247:     my %defaults_hash = (
 9248:                          defaults => \%newvalues,
 9249:                         );
 9250:     my $title = &defaults_titles();
 9251: 
 9252:     my $currinststatus;
 9253:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9254:         $currinststatus = $domconfig{'inststatus'};
 9255:     } else {
 9256:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9257:         $currinststatus = {
 9258:                              inststatustypes => $usertypes,
 9259:                              inststatusorder => $types,
 9260:                              inststatusguest => [],
 9261:                           };
 9262:     }
 9263:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
 9264:     my @allpos;
 9265:     my %guests;
 9266:     my %alltypes;
 9267:     my ($currtitles,$currguests,$currorder);
 9268:     if (ref($currinststatus) eq 'HASH') {
 9269:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
 9270:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
 9271:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
 9272:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
 9273:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
 9274:                     }
 9275:                 }
 9276:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
 9277:                     my $position = $env{'form.inststatus_pos_'.$type};
 9278:                     $position =~ s/\D+//g;
 9279:                     $allpos[$position] = $type;
 9280:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
 9281:                     $alltypes{$type} =~ s/`//g;
 9282:                     if ($env{'form.inststatus_guest_'.$type}) {
 9283:                         $guests{$type} = 1;
 9284:                     }
 9285:                 }
 9286:             }
 9287:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
 9288:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
 9289:             }
 9290:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
 9291:             $currtitles =~ s/,$//;
 9292:         }
 9293:     }
 9294:     if ($env{'form.addinststatus'}) {
 9295:         my $newtype = $env{'form.addinststatus'};
 9296:         $newtype =~ s/\W//g;
 9297:         unless (exists($alltypes{$newtype})) {
 9298:             if ($env{'form.addinststatus_guest'}) {
 9299:                 $guests{$newtype} = 1;
 9300:             }
 9301:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
 9302:             $alltypes{$newtype} =~ s/`//g; 
 9303:             my $position = $env{'form.addinststatus_pos'};
 9304:             $position =~ s/\D+//g;
 9305:             if ($position ne '') {
 9306:                 $allpos[$position] = $newtype;
 9307:             }
 9308:         }
 9309:     }
 9310:     my (@orderedstatus,@orderedguests);
 9311:     foreach my $type (@allpos) {
 9312:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
 9313:             push(@orderedstatus,$type);
 9314:             if ($guests{$type}) {
 9315:                 push(@orderedguests,$type);
 9316:             }
 9317:         }
 9318:     }
 9319:     foreach my $type (keys(%alltypes)) {
 9320:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
 9321:             delete($alltypes{$type});
 9322:         }
 9323:     }
 9324:     $defaults_hash{'inststatus'} = {
 9325:                                      inststatustypes => \%alltypes,
 9326:                                      inststatusorder => \@orderedstatus,
 9327:                                      inststatusguest => \@orderedguests,
 9328:                                    };
 9329:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
 9330:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 9331:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
 9332:         }
 9333:     }
 9334:     if ($currorder ne join(',',@orderedstatus)) {
 9335:         $changes{'inststatus'}{'inststatusorder'} = 1;
 9336:     }
 9337:     if ($currguests ne join(',',@orderedguests)) {
 9338:         $changes{'inststatus'}{'inststatusguest'} = 1;
 9339:     }
 9340:     my $newtitles;
 9341:     foreach my $item (@orderedstatus) {
 9342:         $newtitles .= $alltypes{$item}.',';
 9343:     }
 9344:     $newtitles =~ s/,$//;
 9345:     if ($currtitles ne $newtitles) {
 9346:         $changes{'inststatus'}{'inststatustypes'} = 1;
 9347:     }
 9348:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 9349:                                              $dom);
 9350:     if ($putresult eq 'ok') {
 9351:         if (keys(%changes) > 0) {
 9352:             $resulttext = &mt('Changes made:').'<ul>';
 9353:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 9354:             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";
 9355:             foreach my $item (sort(keys(%changes))) {
 9356:                 if ($item eq 'inststatus') {
 9357:                     if (ref($changes{'inststatus'}) eq 'HASH') {
 9358:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
 9359:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
 9360:                             foreach my $type (@orderedstatus) { 
 9361:                                 $resulttext .= $alltypes{$type}.', ';
 9362:                             }
 9363:                             $resulttext =~ s/, $//;
 9364:                             $resulttext .= '</li>';
 9365:                         }
 9366:                         if ($changes{'inststatus'}{'inststatusguest'}) {
 9367:                             $resulttext .= '<li>'; 
 9368:                             if (@orderedguests) {
 9369:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
 9370:                                 foreach my $type (@orderedguests) {
 9371:                                     $resulttext .= $alltypes{$type}.', ';
 9372:                                 }
 9373:                                 $resulttext =~ s/, $//;
 9374:                             } else {
 9375:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
 9376:                             }
 9377:                             $resulttext .= '</li>';
 9378:                         }
 9379:                     }
 9380:                 } else {
 9381:                     my $value = $env{'form.'.$item};
 9382:                     if ($value eq '') {
 9383:                         $value = &mt('none');
 9384:                     } elsif ($item eq 'auth_def') {
 9385:                         my %authnames = &authtype_names();
 9386:                         my %shortauth = (
 9387:                                           internal   => 'int',
 9388:                                           krb4       => 'krb4',
 9389:                                           krb5       => 'krb5',
 9390:                                           localauth  => 'loc',
 9391:                         );
 9392:                         $value = $authnames{$shortauth{$value}};
 9393:                     }
 9394:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 9395:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
 9396:                 }
 9397:             }
 9398:             $resulttext .= '</ul>';
 9399:             $mailmsgtext .= "\n";
 9400:             my $cachetime = 24*60*60;
 9401:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9402:             if (ref($lastactref) eq 'HASH') {
 9403:                 $lastactref->{'domdefaults'} = 1;
 9404:             }
 9405:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 9406:                 my $notify = 1;
 9407:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 9408:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 9409:                         $notify = 0;
 9410:                     }
 9411:                 }
 9412:                 if ($notify) {
 9413:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 9414:                                                "LON-CAPA Domain Settings Change - $dom",
 9415:                                                $mailmsgtext);
 9416:                 }
 9417:             }
 9418:         } else {
 9419:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 9420:         }
 9421:     } else {
 9422:         $resulttext = '<span class="LC_error">'.
 9423:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9424:     }
 9425:     if (@errors > 0) {
 9426:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 9427:         foreach my $item (@errors) {
 9428:             $resulttext .= ' "'.$title->{$item}.'",';
 9429:         }
 9430:         $resulttext =~ s/,$//;
 9431:     }
 9432:     return $resulttext;
 9433: }
 9434: 
 9435: sub modify_scantron {
 9436:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9437:     my ($resulttext,%confhash,%changes,$errors);
 9438:     my $custom = 'custom.tab';
 9439:     my $default = 'default.tab';
 9440:     my $servadm = $r->dir_config('lonAdmEMail');
 9441:     my ($configuserok,$author_ok,$switchserver) = 
 9442:         &config_check($dom,$confname,$servadm);
 9443:     if ($env{'form.scantronformat.filename'} ne '') {
 9444:         my $error;
 9445:         if ($configuserok eq 'ok') {
 9446:             if ($switchserver) {
 9447:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 9448:             } else {
 9449:                 if ($author_ok eq 'ok') {
 9450:                     my ($result,$scantronurl) =
 9451:                         &publishlogo($r,'upload','scantronformat',$dom,
 9452:                                      $confname,'scantron','','',$custom);
 9453:                     if ($result eq 'ok') {
 9454:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 9455:                         $changes{'scantronformat'} = 1;
 9456:                     } else {
 9457:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 9458:                     }
 9459:                 } else {
 9460:                     $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);
 9461:                 }
 9462:             }
 9463:         } else {
 9464:             $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);
 9465:         }
 9466:         if ($error) {
 9467:             &Apache::lonnet::logthis($error);
 9468:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9469:         }
 9470:     }
 9471:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 9472:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 9473:             if ($env{'form.scantronformat_del'}) {
 9474:                 $confhash{'scantron'}{'scantronformat'} = '';
 9475:                 $changes{'scantronformat'} = 1;
 9476:             }
 9477:         }
 9478:     }
 9479:     if (keys(%confhash) > 0) {
 9480:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 9481:                                                  $dom);
 9482:         if ($putresult eq 'ok') {
 9483:             if (keys(%changes) > 0) {
 9484:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 9485:                     $resulttext = &mt('Changes made:').'<ul>';
 9486:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 9487:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 9488:                     } else {
 9489:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 9490:                     }
 9491:                     $resulttext .= '</ul>';
 9492:                 } else {
 9493:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 9494:                 }
 9495:                 $resulttext .= '</ul>';
 9496:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9497:                 if (ref($lastactref) eq 'HASH') {
 9498:                     $lastactref->{'domainconfig'} = 1;
 9499:                 }
 9500:             } else {
 9501:                 $resulttext = &mt('No changes made to bubblesheet format file');
 9502:             }
 9503:         } else {
 9504:             $resulttext = '<span class="LC_error">'.
 9505:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 9506:         }
 9507:     } else {
 9508:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 9509:     }
 9510:     if ($errors) {
 9511:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9512:                        $errors.'</ul>';
 9513:     }
 9514:     return $resulttext;
 9515: }
 9516: 
 9517: sub modify_coursecategories {
 9518:     my ($dom,$lastactref,%domconfig) = @_;
 9519:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 9520:         $cathash);
 9521:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 9522:     my @catitems = ('unauth','auth');
 9523:     my @cattypes = ('std','domonly','codesrch','none');
 9524:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9525:         $cathash = $domconfig{'coursecategories'}{'cats'};
 9526:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 9527:             $changes{'togglecats'} = 1;
 9528:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 9529:         }
 9530:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 9531:             $changes{'categorize'} = 1;
 9532:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 9533:         }
 9534:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 9535:             $changes{'togglecatscomm'} = 1;
 9536:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 9537:         }
 9538:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 9539:             $changes{'categorizecomm'} = 1;
 9540:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 9541:         }
 9542:         foreach my $item (@catitems) {
 9543:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9544:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
 9545:                     $changes{$item} = 1;
 9546:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9547:                 }
 9548:             }
 9549:         }
 9550:     } else {
 9551:         $changes{'togglecats'} = 1;
 9552:         $changes{'categorize'} = 1;
 9553:         $changes{'togglecatscomm'} = 1;
 9554:         $changes{'categorizecomm'} = 1;
 9555:         $domconfig{'coursecategories'} = {
 9556:                                              togglecats => $env{'form.togglecats'},
 9557:                                              categorize => $env{'form.categorize'},
 9558:                                              togglecatscomm => $env{'form.togglecatscomm'},
 9559:                                              categorizecomm => $env{'form.categorizecomm'},
 9560:                                          };
 9561:         foreach my $item (@catitems) {
 9562:             if ($env{'form.coursecat_'.$item} ne 'std') {
 9563:                 $changes{$item} = 1;
 9564:             }
 9565:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9566:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9567:             }
 9568:         }
 9569:     }
 9570:     if (ref($cathash) eq 'HASH') {
 9571:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 9572:             push (@deletecategory,'instcode::0');
 9573:         }
 9574:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 9575:             push(@deletecategory,'communities::0');
 9576:         }
 9577:     }
 9578:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 9579:     if (ref($cathash) eq 'HASH') {
 9580:         if (@deletecategory > 0) {
 9581:             #FIXME Need to remove category from all courses using a deleted category 
 9582:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 9583:             foreach my $item (@deletecategory) {
 9584:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 9585:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 9586:                     $deletions{$item} = 1;
 9587:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 9588:                 }
 9589:             }
 9590:         }
 9591:         foreach my $item (keys(%{$cathash})) {
 9592:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9593:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 9594:                 $reorderings{$item} = 1;
 9595:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 9596:             }
 9597:             if ($env{'form.addcategory_name_'.$item} ne '') {
 9598:                 my $newcat = $env{'form.addcategory_name_'.$item};
 9599:                 my $newdepth = $depth+1;
 9600:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9601:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 9602:                 $adds{$newitem} = 1; 
 9603:             }
 9604:             if ($env{'form.subcat_'.$item} ne '') {
 9605:                 my $newcat = $env{'form.subcat_'.$item};
 9606:                 my $newdepth = $depth+1;
 9607:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9608:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 9609:                 $adds{$newitem} = 1;
 9610:             }
 9611:         }
 9612:     }
 9613:     if ($env{'form.instcode'} eq '1') {
 9614:         if (ref($cathash) eq 'HASH') {
 9615:             my $newitem = 'instcode::0';
 9616:             if ($cathash->{$newitem} eq '') {  
 9617:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9618:                 $adds{$newitem} = 1;
 9619:             }
 9620:         } else {
 9621:             my $newitem = 'instcode::0';
 9622:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9623:             $adds{$newitem} = 1;
 9624:         }
 9625:     }
 9626:     if ($env{'form.communities'} eq '1') {
 9627:         if (ref($cathash) eq 'HASH') {
 9628:             my $newitem = 'communities::0';
 9629:             if ($cathash->{$newitem} eq '') {
 9630:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9631:                 $adds{$newitem} = 1;
 9632:             }
 9633:         } else {
 9634:             my $newitem = 'communities::0';
 9635:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9636:             $adds{$newitem} = 1;
 9637:         }
 9638:     }
 9639:     if ($env{'form.addcategory_name'} ne '') {
 9640:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 9641:             ($env{'form.addcategory_name'} ne 'communities')) {
 9642:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 9643:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 9644:             $adds{$newitem} = 1;
 9645:         }
 9646:     }
 9647:     my $putresult;
 9648:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9649:         if (keys(%deletions) > 0) {
 9650:             foreach my $key (keys(%deletions)) {
 9651:                 if ($predelallitems{$key} ne '') {
 9652:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 9653:                 }
 9654:             }
 9655:         }
 9656:         my (@chkcats,@chktrails,%chkallitems);
 9657:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 9658:         if (ref($chkcats[0]) eq 'ARRAY') {
 9659:             my $depth = 0;
 9660:             my $chg = 0;
 9661:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 9662:                 my $name = $chkcats[0][$i];
 9663:                 my $item;
 9664:                 if ($name eq '') {
 9665:                     $chg ++;
 9666:                 } else {
 9667:                     $item = &escape($name).'::0';
 9668:                     if ($chg) {
 9669:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 9670:                     }
 9671:                     $depth ++; 
 9672:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 9673:                     $depth --;
 9674:                 }
 9675:             }
 9676:         }
 9677:     }
 9678:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9679:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 9680:         if ($putresult eq 'ok') {
 9681:             my %title = (
 9682:                          togglecats     => 'Show/Hide a course in catalog',
 9683:                          categorize     => 'Assign a category to a course',
 9684:                          togglecatscomm => 'Show/Hide a community in catalog',
 9685:                          categorizecomm => 'Assign a category to a community',
 9686:                         );
 9687:             my %level = (
 9688:                          dom  => 'set in Domain ("Modify Course/Community")',
 9689:                          crs  => 'set in Course ("Course Configuration")',
 9690:                          comm => 'set in Community ("Community Configuration")',
 9691:                          none     => 'No catalog',
 9692:                          std      => 'Standard catalog',
 9693:                          domonly  => 'Domain-only catalog',
 9694:                          codesrch => 'Code search form',
 9695:                         );
 9696:             $resulttext = &mt('Changes made:').'<ul>';
 9697:             if ($changes{'togglecats'}) {
 9698:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 9699:             }
 9700:             if ($changes{'categorize'}) {
 9701:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 9702:             }
 9703:             if ($changes{'togglecatscomm'}) {
 9704:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 9705:             }
 9706:             if ($changes{'categorizecomm'}) {
 9707:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 9708:             }
 9709:             if ($changes{'unauth'}) {
 9710:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
 9711:             }
 9712:             if ($changes{'auth'}) {
 9713:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
 9714:             }
 9715:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9716:                 my $cathash;
 9717:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9718:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 9719:                 } else {
 9720:                     $cathash = {};
 9721:                 } 
 9722:                 my (@cats,@trails,%allitems);
 9723:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 9724:                 if (keys(%deletions) > 0) {
 9725:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 9726:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 9727:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 9728:                     }
 9729:                     $resulttext .= '</ul></li>';
 9730:                 }
 9731:                 if (keys(%reorderings) > 0) {
 9732:                     my %sort_by_trail;
 9733:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 9734:                     foreach my $key (keys(%reorderings)) {
 9735:                         if ($allitems{$key} ne '') {
 9736:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9737:                         }
 9738:                     }
 9739:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9740:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9741:                     }
 9742:                     $resulttext .= '</ul></li>';
 9743:                 }
 9744:                 if (keys(%adds) > 0) {
 9745:                     my %sort_by_trail;
 9746:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 9747:                     foreach my $key (keys(%adds)) {
 9748:                         if ($allitems{$key} ne '') {
 9749:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9750:                         }
 9751:                     }
 9752:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9753:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9754:                     }
 9755:                     $resulttext .= '</ul></li>';
 9756:                 }
 9757:             }
 9758:             $resulttext .= '</ul>';
 9759:             if ($changes{'unauth'} || $changes{'auth'}) {
 9760:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9761:                 if (ref($lastactref) eq 'HASH') {
 9762:                     $lastactref->{'domainconfig'} = 1;
 9763:                 }
 9764:             }
 9765:         } else {
 9766:             $resulttext = '<span class="LC_error">'.
 9767:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9768:         }
 9769:     } else {
 9770:         $resulttext = &mt('No changes made to course and community categories');
 9771:     }
 9772:     return $resulttext;
 9773: }
 9774: 
 9775: sub modify_serverstatuses {
 9776:     my ($dom,%domconfig) = @_;
 9777:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 9778:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 9779:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 9780:     }
 9781:     my @pages = &serverstatus_pages();
 9782:     foreach my $type (@pages) {
 9783:         $newserverstatus{$type}{'namedusers'} = '';
 9784:         $newserverstatus{$type}{'machines'} = '';
 9785:         if (defined($env{'form.'.$type.'_namedusers'})) {
 9786:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 9787:             my @okusers;
 9788:             foreach my $user (@users) {
 9789:                 my ($uname,$udom) = split(/:/,$user);
 9790:                 if (($udom =~ /^$match_domain$/) &&   
 9791:                     (&Apache::lonnet::domain($udom)) &&
 9792:                     ($uname =~ /^$match_username$/)) {
 9793:                     if (!grep(/^\Q$user\E/,@okusers)) {
 9794:                         push(@okusers,$user);
 9795:                     }
 9796:                 }
 9797:             }
 9798:             if (@okusers > 0) {
 9799:                  @okusers = sort(@okusers);
 9800:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 9801:             }
 9802:         }
 9803:         if (defined($env{'form.'.$type.'_machines'})) {
 9804:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 9805:             my @okmachines;
 9806:             foreach my $ip (@machines) {
 9807:                 my @parts = split(/\./,$ip);
 9808:                 next if (@parts < 4);
 9809:                 my $badip = 0;
 9810:                 for (my $i=0; $i<4; $i++) {
 9811:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 9812:                         $badip = 1;
 9813:                         last;
 9814:                     }
 9815:                 }
 9816:                 if (!$badip) {
 9817:                     push(@okmachines,$ip);     
 9818:                 }
 9819:             }
 9820:             @okmachines = sort(@okmachines);
 9821:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 9822:         }
 9823:     }
 9824:     my %serverstatushash =  (
 9825:                                 serverstatuses => \%newserverstatus,
 9826:                             );
 9827:     foreach my $type (@pages) {
 9828:         foreach my $setting ('namedusers','machines') {
 9829:             my (@current,@new);
 9830:             if (ref($currserverstatus{$type}) eq 'HASH') {
 9831:                 if ($currserverstatus{$type}{$setting} ne '') { 
 9832:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 9833:                 }
 9834:             }
 9835:             if ($newserverstatus{$type}{$setting} ne '') {
 9836:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 9837:             }
 9838:             if (@current > 0) {
 9839:                 if (@new > 0) {
 9840:                     foreach my $item (@current) {
 9841:                         if (!grep(/^\Q$item\E$/,@new)) {
 9842:                             $changes{$type}{$setting} = 1;
 9843:                             last;
 9844:                         }
 9845:                     }
 9846:                     foreach my $item (@new) {
 9847:                         if (!grep(/^\Q$item\E$/,@current)) {
 9848:                             $changes{$type}{$setting} = 1;
 9849:                             last;
 9850:                         }
 9851:                     }
 9852:                 } else {
 9853:                     $changes{$type}{$setting} = 1;
 9854:                 }
 9855:             } elsif (@new > 0) {
 9856:                 $changes{$type}{$setting} = 1;
 9857:             }
 9858:         }
 9859:     }
 9860:     if (keys(%changes) > 0) {
 9861:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9862:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9863:                                                  \%serverstatushash,$dom);
 9864:         if ($putresult eq 'ok') {
 9865:             $resulttext .= &mt('Changes made:').'<ul>';
 9866:             foreach my $type (@pages) {
 9867:                 if (ref($changes{$type}) eq 'HASH') {
 9868:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 9869:                     if ($changes{$type}{'namedusers'}) {
 9870:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 9871:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 9872:                         } else {
 9873:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 9874:                         }
 9875:                     }
 9876:                     if ($changes{$type}{'machines'}) {
 9877:                         if ($newserverstatus{$type}{'machines'} eq '') {
 9878:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 9879:                         } else {
 9880:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 9881:                         }
 9882: 
 9883:                     }
 9884:                     $resulttext .= '</ul></li>';
 9885:                 }
 9886:             }
 9887:             $resulttext .= '</ul>';
 9888:         } else {
 9889:             $resulttext = '<span class="LC_error">'.
 9890:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 9891: 
 9892:         }
 9893:     } else {
 9894:         $resulttext = &mt('No changes made to access to server status pages');
 9895:     }
 9896:     return $resulttext;
 9897: }
 9898: 
 9899: sub modify_helpsettings {
 9900:     my ($r,$dom,$confname,%domconfig) = @_;
 9901:     my ($resulttext,$errors,%changes,%helphash);
 9902:     my %defaultchecked = ('submitbugs' => 'on');
 9903:     my @offon = ('off','on');
 9904:     my @toggles = ('submitbugs');
 9905:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 9906:         foreach my $item (@toggles) {
 9907:             if ($defaultchecked{$item} eq 'on') { 
 9908:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9909:                     if ($env{'form.'.$item} eq '0') {
 9910:                         $changes{$item} = 1;
 9911:                     }
 9912:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9913:                     $changes{$item} = 1;
 9914:                 }
 9915:             } elsif ($defaultchecked{$item} eq 'off') {
 9916:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9917:                     if ($env{'form.'.$item} eq '1') {
 9918:                         $changes{$item} = 1;
 9919:                     }
 9920:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9921:                     $changes{$item} = 1;
 9922:                 }
 9923:             }
 9924:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 9925:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 9926:             }
 9927:         }
 9928:     }
 9929:     my $putresult;
 9930:     if (keys(%changes) > 0) {
 9931:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 9932:         if ($putresult eq 'ok') {
 9933:             $resulttext = &mt('Changes made:').'<ul>';
 9934:             foreach my $item (sort(keys(%changes))) {
 9935:                 if ($item eq 'submitbugs') {
 9936:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 9937:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 9938:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 9939:                 }
 9940:             }
 9941:             $resulttext .= '</ul>';
 9942:         } else {
 9943:             $resulttext = &mt('No changes made to help settings');
 9944:             $errors .= '<li><span class="LC_error">'.
 9945:                        &mt('An error occurred storing the settings: [_1]',
 9946:                            $putresult).'</span></li>';
 9947:         }
 9948:     }
 9949:     if ($errors) {
 9950:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9951:                        $errors.'</ul>';
 9952:     }
 9953:     return $resulttext;
 9954: }
 9955: 
 9956: sub modify_coursedefaults {
 9957:     my ($dom,$lastactref,%domconfig) = @_;
 9958:     my ($resulttext,$errors,%changes,%defaultshash);
 9959:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 9960:     my @toggles = ('canuse_pdfforms');
 9961:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 9962:                    'uploadquota_community','uploadquota_textbook');
 9963:     my @types = ('official','unofficial','community','textbook');
 9964:     my %staticdefaults = (
 9965:                            anonsurvey_threshold => 10,
 9966:                            uploadquota          => 500,
 9967:                          );
 9968: 
 9969:     $defaultshash{'coursedefaults'} = {};
 9970: 
 9971:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 9972:         if ($domconfig{'coursedefaults'} eq '') {
 9973:             $domconfig{'coursedefaults'} = {};
 9974:         }
 9975:     }
 9976: 
 9977:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 9978:         foreach my $item (@toggles) {
 9979:             if ($defaultchecked{$item} eq 'on') {
 9980:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9981:                     ($env{'form.'.$item} eq '0')) {
 9982:                     $changes{$item} = 1;
 9983:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9984:                     $changes{$item} = 1;
 9985:                 }
 9986:             } elsif ($defaultchecked{$item} eq 'off') {
 9987:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9988:                     ($env{'form.'.$item} eq '1')) {
 9989:                     $changes{$item} = 1;
 9990:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9991:                     $changes{$item} = 1;
 9992:                 }
 9993:             }
 9994:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 9995:         }
 9996:         foreach my $item (@numbers) {
 9997:             my ($currdef,$newdef);
 9998:             $newdef = $env{'form.'.$item};
 9999:             if ($item eq 'anonsurvey_threshold') {
10000:                 $currdef = $domconfig{'coursedefaults'}{$item};
10001:                 $newdef =~ s/\D//g;
10002:                 if ($newdef eq '' || $newdef < 1) {
10003:                     $newdef = 1;
10004:                 }
10005:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
10006:             } else {
10007:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
10008:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10009:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
10010:                 }
10011:                 $newdef =~ s/[^\w.\-]//g;
10012:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
10013:             }
10014:             if ($currdef ne $newdef) {
10015:                 my $staticdef;
10016:                 if ($item eq 'anonsurvey_threshold') {
10017:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
10018:                         $changes{$item} = 1;
10019:                     }
10020:                 } else {
10021:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
10022:                         $changes{'uploadquota'} = 1;
10023:                     }
10024:                 }
10025:             }
10026:         }
10027: 
10028:         my $officialcreds = $env{'form.official_credits'};
10029:         $officialcreds =~ s/[^\d.]+//g;
10030:         my $unofficialcreds = $env{'form.unofficial_credits'};
10031:         $unofficialcreds =~ s/[^\d.]+//g;
10032:         my $textbookcreds = $env{'form.textbook_credits'};
10033:         $textbookcreds =~ s/[^\d.]+//g;
10034:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
10035:                 ($env{'form.coursecredits'} eq '1')) {
10036:                 $changes{'coursecredits'} = 1;
10037:         } else {
10038:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
10039:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
10040:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
10041:                 $changes{'coursecredits'} = 1;
10042:             }
10043:         }
10044:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
10045:             official   => $officialcreds,
10046:             unofficial => $unofficialcreds,
10047:             textbook   => $textbookcreds,
10048:         }
10049:     }
10050:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10051:                                              $dom);
10052:     if ($putresult eq 'ok') {
10053:         if (keys(%changes) > 0) {
10054:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10055:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || 
10056:                 ($changes{'uploadquota'})) { 
10057:                 if ($changes{'canuse_pdfforms'}) {
10058:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
10059:                 }
10060:                 if ($changes{'coursecredits'}) {
10061:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10062:                         $domdefaults{'officialcredits'} =
10063:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
10064:                         $domdefaults{'unofficialcredits'} =
10065:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
10066:                         $domdefaults{'textbookcredits'} =
10067:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
10068:                     }
10069:                 }
10070:                 if ($changes{'uploadquota'}) {
10071:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10072:                         foreach my $type (@types) {
10073:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
10074:                         }
10075:                     }
10076:                 }
10077:                 my $cachetime = 24*60*60;
10078:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10079:                 if (ref($lastactref) eq 'HASH') {
10080:                     $lastactref->{'domdefaults'} = 1;
10081:                 }
10082:             }
10083:             $resulttext = &mt('Changes made:').'<ul>';
10084:             foreach my $item (sort(keys(%changes))) {
10085:                 if ($item eq 'canuse_pdfforms') {
10086:                     if ($env{'form.'.$item} eq '1') {
10087:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
10088:                     } else {
10089:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
10090:                     }
10091:                 } elsif ($item eq 'anonsurvey_threshold') {
10092:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
10093:                 } elsif ($item eq 'uploadquota') {
10094:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10095:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
10096:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
10097:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
10098:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
10099: 
10100:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
10101:                                        '</ul>'.
10102:                                        '</li>';
10103:                     } else {
10104:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
10105:                     }
10106:                 } elsif ($item eq 'coursecredits') {
10107:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10108:                         if (($domdefaults{'officialcredits'} eq '') &&
10109:                             ($domdefaults{'unofficialcredits'} eq '') &&
10110:                             ($domdefaults{'textbookcredits'} eq '')) {
10111:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10112:                         } else {
10113:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
10114:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
10115:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
10116:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
10117:                                            '</ul>'.
10118:                                            '</li>';
10119:                         }
10120:                     } else {
10121:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10122:                     }
10123:                 }
10124:             }
10125:             $resulttext .= '</ul>';
10126:         } else {
10127:             $resulttext = &mt('No changes made to course defaults');
10128:         }
10129:     } else {
10130:         $resulttext = '<span class="LC_error">'.
10131:             &mt('An error occurred: [_1]',$putresult).'</span>';
10132:     }
10133:     return $resulttext;
10134: }
10135: 
10136: sub modify_selfenrollment {
10137:     my ($dom,$lastactref,%domconfig) = @_;
10138:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
10139:     my @types = ('official','unofficial','community','textbook');
10140:     my %titles = &tool_titles();
10141:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
10142:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
10143:     $ordered{'default'} = ['types','registered','approval','limit'];
10144: 
10145:     my (%roles,%shown,%toplevel);
10146:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
10147: 
10148:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
10149:         if ($domconfig{'selfenrollment'} eq '') {
10150:             $domconfig{'selfenrollment'} = {};
10151:         }
10152:     }
10153:     %toplevel = (
10154:                   admin      => 'Configuration Rights',
10155:                   default    => 'Default settings',
10156:                   validation => 'Validation of self-enrollment requests',
10157:                 );
10158:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
10159: 
10160:     if (ref($ordered{'admin'}) eq 'ARRAY') {
10161:         foreach my $item (@{$ordered{'admin'}}) {
10162:             foreach my $type (@types) {
10163:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
10164:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
10165:                 } else {
10166:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
10167:                 }
10168:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
10169:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
10170:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
10171:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
10172:                             push(@{$changes{'admin'}{$type}},$item);
10173:                         }
10174:                     } else {
10175:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
10176:                             push(@{$changes{'admin'}{$type}},$item);
10177:                         }
10178:                     }
10179:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
10180:                     push(@{$changes{'admin'}{$type}},$item);
10181:                 }
10182:             }
10183:         }
10184:     }
10185: 
10186:     foreach my $item (@{$ordered{'default'}}) {
10187:         foreach my $type (@types) {
10188:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
10189:             if ($item eq 'types') {
10190:                 unless (($value eq 'all') || ($value eq 'dom')) {
10191:                     $value = '';
10192:                 }
10193:             } elsif ($item eq 'registered') {
10194:                 unless ($value eq '1') {
10195:                     $value = 0;
10196:                 }
10197:             } elsif ($item eq 'approval') {
10198:                 unless ($value =~ /^[012]$/) {
10199:                     $value = 0;
10200:                 }
10201:             } else {
10202:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10203:                     $value = 'none';
10204:                 }
10205:             }
10206:             $selfenrollhash{'default'}{$type}{$item} = $value;
10207:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
10208:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10209:                     if ($selfenrollhash{'default'}{$type}{$item} ne
10210:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
10211:                          push(@{$changes{'default'}{$type}},$item);
10212:                     }
10213:                 } else {
10214:                     push(@{$changes{'default'}{$type}},$item);
10215:                 }
10216:             } else {
10217:                 push(@{$changes{'default'}{$type}},$item);
10218:             }
10219:             if ($item eq 'limit') {
10220:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10221:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
10222:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
10223:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
10224:                     }
10225:                 } else {
10226:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
10227:                 }
10228:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10229:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
10230:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
10231:                          push(@{$changes{'default'}{$type}},'cap');
10232:                     }
10233:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
10234:                     push(@{$changes{'default'}{$type}},'cap');
10235:                 }
10236:             }
10237:         }
10238:     }
10239: 
10240:     foreach my $item (@{$itemsref}) {
10241:         if ($item eq 'fields') {
10242:             my @changed;
10243:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
10244:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
10245:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
10246:             }
10247:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10248:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
10249:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
10250:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
10251:                 } else {
10252:                     @changed = @{$selfenrollhash{'validation'}{$item}};
10253:                 }
10254:             } else {
10255:                 @changed = @{$selfenrollhash{'validation'}{$item}};
10256:             }
10257:             if (@changed) {
10258:                 if ($selfenrollhash{'validation'}{$item}) { 
10259:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
10260:                 } else {
10261:                     $changes{'validation'}{$item} = &mt('None');
10262:                 }
10263:             }
10264:         } else {
10265:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
10266:             if ($item eq 'markup') {
10267:                if ($env{'form.selfenroll_validation_'.$item}) {
10268:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10269:                }
10270:             }
10271:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10272:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
10273:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
10274:                 }
10275:             }
10276:         }
10277:     }
10278: 
10279:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
10280:                                              $dom);
10281:     if ($putresult eq 'ok') {
10282:         if (keys(%changes) > 0) {
10283:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10284:             $resulttext = &mt('Changes made:').'<ul>';
10285:             foreach my $key ('admin','default','validation') {
10286:                 if (ref($changes{$key}) eq 'HASH') {
10287:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
10288:                     if ($key eq 'validation') {
10289:                         foreach my $item (@{$itemsref}) {
10290:                             if (exists($changes{$key}{$item})) {
10291:                                 if ($item eq 'markup') {
10292:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10293:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
10294:                                 } else {  
10295:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10296:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
10297:                                 }
10298:                             }
10299:                         }
10300:                     } else {
10301:                         foreach my $type (@types) {
10302:                             if ($type eq 'community') {
10303:                                 $roles{'1'} = &mt('Community personnel');
10304:                             } else {
10305:                                 $roles{'1'} = &mt('Course personnel');
10306:                             }
10307:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
10308:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
10309:                                     if ($key eq 'admin') {
10310:                                         my @mgrdc = ();
10311:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10312:                                             foreach my $item (@{$ordered{'admin'}}) {
10313:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
10314:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
10315:                                                         push(@mgrdc,$item);
10316:                                                     }
10317:                                                 }
10318:                                             }
10319:                                             if (@mgrdc) {
10320:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
10321:                                             } else {
10322:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
10323:                                             }
10324:                                         }
10325:                                     } else {
10326:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10327:                                             foreach my $item (@{$ordered{$key}}) {
10328:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10329:                                                     $domdefaults{$type.'selfenroll'.$item} =
10330:                                                         $selfenrollhash{$key}{$type}{$item};
10331:                                                 }
10332:                                             }
10333:                                         }
10334:                                     }
10335:                                 }
10336:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
10337:                                 foreach my $item (@{$ordered{$key}}) {
10338:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10339:                                         $resulttext .= '<li>';
10340:                                         if ($key eq 'admin') {
10341:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
10342:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
10343:                                         } else {
10344:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
10345:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
10346:                                         }
10347:                                         $resulttext .= '</li>';
10348:                                     }
10349:                                 }
10350:                                 $resulttext .= '</ul></li>';
10351:                             }
10352:                         }
10353:                         $resulttext .= '</ul></li>'; 
10354:                     }
10355:                 }
10356:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
10357:                     my $cachetime = 24*60*60;
10358:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10359:                     if (ref($lastactref) eq 'HASH') {
10360:                         $lastactref->{'domdefaults'} = 1;
10361:                     }
10362:                 }
10363:             }
10364:             $resulttext .= '</ul>';
10365:         } else {
10366:             $resulttext = &mt('No changes made to self-enrollment settings');
10367:         }
10368:     } else {
10369:         $resulttext = '<span class="LC_error">'.
10370:             &mt('An error occurred: [_1]',$putresult).'</span>';
10371:     }
10372:     return $resulttext;
10373: }
10374: 
10375: sub modify_usersessions {
10376:     my ($dom,$lastactref,%domconfig) = @_;
10377:     my @hostingtypes = ('version','excludedomain','includedomain');
10378:     my @offloadtypes = ('primary','default');
10379:     my %types = (
10380:                   remote => \@hostingtypes,
10381:                   hosted => \@hostingtypes,
10382:                   spares => \@offloadtypes,
10383:                 );
10384:     my @prefixes = ('remote','hosted','spares');
10385:     my @lcversions = &Apache::lonnet::all_loncaparevs();
10386:     my (%by_ip,%by_location,@intdoms);
10387:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
10388:     my @locations = sort(keys(%by_location));
10389:     my (%defaultshash,%changes);
10390:     foreach my $prefix (@prefixes) {
10391:         $defaultshash{'usersessions'}{$prefix} = {};
10392:     }
10393:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10394:     my $resulttext;
10395:     my %iphost = &Apache::lonnet::get_iphost();
10396:     foreach my $prefix (@prefixes) {
10397:         next if ($prefix eq 'spares');
10398:         foreach my $type (@{$types{$prefix}}) {
10399:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
10400:             if ($type eq 'version') {
10401:                 my $value = $env{'form.'.$prefix.'_'.$type};
10402:                 my $okvalue;
10403:                 if ($value ne '') {
10404:                     if (grep(/^\Q$value\E$/,@lcversions)) {
10405:                         $okvalue = $value;
10406:                     }
10407:                 }
10408:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10409:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10410:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
10411:                             if ($inuse == 0) {
10412:                                 $changes{$prefix}{$type} = 1;
10413:                             } else {
10414:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
10415:                                     $changes{$prefix}{$type} = 1;
10416:                                 }
10417:                                 if ($okvalue ne '') {
10418:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10419:                                 } 
10420:                             }
10421:                         } else {
10422:                             if (($inuse == 1) && ($okvalue ne '')) {
10423:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10424:                                 $changes{$prefix}{$type} = 1;
10425:                             }
10426:                         }
10427:                     } else {
10428:                         if (($inuse == 1) && ($okvalue ne '')) {
10429:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10430:                             $changes{$prefix}{$type} = 1;
10431:                         }
10432:                     }
10433:                 } else {
10434:                     if (($inuse == 1) && ($okvalue ne '')) {
10435:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10436:                         $changes{$prefix}{$type} = 1;
10437:                     }
10438:                 }
10439:             } else {
10440:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
10441:                 my @okvals;
10442:                 foreach my $val (@vals) {
10443:                     if ($val =~ /:/) {
10444:                         my @items = split(/:/,$val);
10445:                         foreach my $item (@items) {
10446:                             if (ref($by_location{$item}) eq 'ARRAY') {
10447:                                 push(@okvals,$item);
10448:                             }
10449:                         }
10450:                     } else {
10451:                         if (ref($by_location{$val}) eq 'ARRAY') {
10452:                             push(@okvals,$val);
10453:                         }
10454:                     }
10455:                 }
10456:                 @okvals = sort(@okvals);
10457:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10458:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10459:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10460:                             if ($inuse == 0) {
10461:                                 $changes{$prefix}{$type} = 1; 
10462:                             } else {
10463:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10464:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
10465:                                 if (@changed > 0) {
10466:                                     $changes{$prefix}{$type} = 1;
10467:                                 }
10468:                             }
10469:                         } else {
10470:                             if ($inuse == 1) {
10471:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10472:                                 $changes{$prefix}{$type} = 1;
10473:                             }
10474:                         } 
10475:                     } else {
10476:                         if ($inuse == 1) {
10477:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10478:                             $changes{$prefix}{$type} = 1;
10479:                         }
10480:                     }
10481:                 } else {
10482:                     if ($inuse == 1) {
10483:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10484:                         $changes{$prefix}{$type} = 1;
10485:                     }
10486:                 }
10487:             }
10488:         }
10489:     }
10490: 
10491:     my @alldoms = &Apache::lonnet::all_domains();
10492:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10493:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
10494:     my $savespares;
10495: 
10496:     foreach my $lonhost (sort(keys(%servers))) {
10497:         my $serverhomeID =
10498:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
10499:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
10500:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
10501:         my %spareschg;
10502:         foreach my $type (@{$types{'spares'}}) {
10503:             my @okspares;
10504:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
10505:             foreach my $server (@checked) {
10506:                 if (&Apache::lonnet::hostname($server) ne '') {
10507:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
10508:                         unless (grep(/^\Q$server\E$/,@okspares)) {
10509:                             push(@okspares,$server);
10510:                         }
10511:                     }
10512:                 }
10513:             }
10514:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
10515:             my $newspare;
10516:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
10517:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
10518:                     $newspare = $new;
10519:                 }
10520:             }
10521:             my @spares;
10522:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
10523:                 @spares = sort(@okspares,$newspare);
10524:             } else {
10525:                 @spares = sort(@okspares);
10526:             }
10527:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
10528:             if (ref($spareid{$lonhost}) eq 'HASH') {
10529:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
10530:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
10531:                     if (@diffs > 0) {
10532:                         $spareschg{$type} = 1;
10533:                     }
10534:                 }
10535:             }
10536:         }
10537:         if (keys(%spareschg) > 0) {
10538:             $changes{'spares'}{$lonhost} = \%spareschg;
10539:         }
10540:     }
10541: 
10542:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
10543:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
10544:             if (ref($changes{'spares'}) eq 'HASH') {
10545:                 if (keys(%{$changes{'spares'}}) > 0) {
10546:                     $savespares = 1;
10547:                 }
10548:             }
10549:         } else {
10550:             $savespares = 1;
10551:         }
10552:     }
10553: 
10554:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
10555:     if ((keys(%changes) > 0) || ($savespares)) {
10556:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10557:                                                  $dom);
10558:         if ($putresult eq 'ok') {
10559:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10560:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
10561:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
10562:                 }
10563:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
10564:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
10565:                 }
10566:             }
10567:             my $cachetime = 24*60*60;
10568:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10569:             if (ref($lastactref) eq 'HASH') {
10570:                 $lastactref->{'domdefaults'} = 1;
10571:             }
10572:             if (keys(%changes) > 0) {
10573:                 my %lt = &usersession_titles();
10574:                 $resulttext = &mt('Changes made:').'<ul>';
10575:                 foreach my $prefix (@prefixes) {
10576:                     if (ref($changes{$prefix}) eq 'HASH') {
10577:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
10578:                         if ($prefix eq 'spares') {
10579:                             if (ref($changes{$prefix}) eq 'HASH') {
10580:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
10581:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
10582:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
10583:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
10584:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
10585:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
10586:                                         foreach my $type (@{$types{$prefix}}) {
10587:                                             if ($changes{$prefix}{$lonhost}{$type}) {
10588:                                                 my $offloadto = &mt('None');
10589:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
10590:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
10591:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
10592:                                                     }
10593:                                                 }
10594:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
10595:                                             }
10596:                                         }
10597:                                     }
10598:                                     $resulttext .= '</li>';
10599:                                 }
10600:                             }
10601:                         } else {
10602:                             foreach my $type (@{$types{$prefix}}) {
10603:                                 if (defined($changes{$prefix}{$type})) {
10604:                                     my $newvalue;
10605:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
10606:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
10607:                                             if ($type eq 'version') {
10608:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
10609:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10610:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
10611:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
10612:                                                 }
10613:                                             }
10614:                                         }
10615:                                     }
10616:                                     if ($newvalue eq '') {
10617:                                         if ($type eq 'version') {
10618:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
10619:                                         } else {
10620:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
10621:                                         }
10622:                                     } else {
10623:                                         if ($type eq 'version') {
10624:                                             $newvalue .= ' '.&mt('(or later)'); 
10625:                                         }
10626:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
10627:                                     }
10628:                                 }
10629:                             }
10630:                         }
10631:                         $resulttext .= '</ul>';
10632:                     }
10633:                 }
10634:                 $resulttext .= '</ul>';
10635:             } else {
10636:                 $resulttext = $nochgmsg;
10637:             }
10638:         } else {
10639:             $resulttext = '<span class="LC_error">'.
10640:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10641:         }
10642:     } else {
10643:         $resulttext = $nochgmsg;
10644:     }
10645:     return $resulttext;
10646: }
10647: 
10648: sub modify_loadbalancing {
10649:     my ($dom,%domconfig) = @_;
10650:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
10651:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
10652:     my ($othertitle,$usertypes,$types) =
10653:         &Apache::loncommon::sorted_inst_types($dom);
10654:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10655:     my @sparestypes = ('primary','default');
10656:     my %typetitles = &sparestype_titles();
10657:     my $resulttext;
10658:     my (%currbalancer,%currtargets,%currrules,%existing);
10659:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10660:         %existing = %{$domconfig{'loadbalancing'}};
10661:     }
10662:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
10663:                               \%currtargets,\%currrules);
10664:     my ($saveloadbalancing,%defaultshash,%changes);
10665:     my ($alltypes,$othertypes,$titles) =
10666:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
10667:     my %ruletitles = &offloadtype_text();
10668:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
10669:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
10670:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
10671:         if ($balancer eq '') {
10672:             next;
10673:         }
10674:         if (!exists($servers{$balancer})) {
10675:             if (exists($currbalancer{$balancer})) {
10676:                 push(@{$changes{'delete'}},$balancer);
10677:             }
10678:             next;
10679:         }
10680:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
10681:             push(@{$changes{'delete'}},$balancer);
10682:             next;
10683:         }
10684:         if (!exists($currbalancer{$balancer})) {
10685:             push(@{$changes{'add'}},$balancer);
10686:         }
10687:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
10688:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
10689:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
10690:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10691:             $saveloadbalancing = 1;
10692:         }
10693:         foreach my $sparetype (@sparestypes) {
10694:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
10695:             my @offloadto;
10696:             foreach my $target (@targets) {
10697:                 if (($servers{$target}) && ($target ne $balancer)) {
10698:                     if ($sparetype eq 'default') {
10699:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
10700:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
10701:                         }
10702:                     }
10703:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
10704:                         push(@offloadto,$target);
10705:                     }
10706:                 }
10707:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
10708:             }
10709:         }
10710:         if (ref($currtargets{$balancer}) eq 'HASH') {
10711:             foreach my $sparetype (@sparestypes) {
10712:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
10713:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
10714:                     if (@targetdiffs > 0) {
10715:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10716:                     }
10717:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10718:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10719:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10720:                     }
10721:                 }
10722:             }
10723:         } else {
10724:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
10725:                 foreach my $sparetype (@sparestypes) {
10726:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10727:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10728:                             $changes{'curr'}{$balancer}{'targets'} = 1;
10729:                         }
10730:                     }
10731:                 }
10732:             }
10733:         }
10734:         my $ishomedom;
10735:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
10736:             $ishomedom = 1;
10737:         }
10738:         if (ref($alltypes) eq 'ARRAY') {
10739:             foreach my $type (@{$alltypes}) {
10740:                 my $rule;
10741:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
10742:                          (!$ishomedom)) {
10743:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
10744:                 }
10745:                 if ($rule eq 'specific') {
10746:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
10747:                 }
10748:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
10749:                 if (ref($currrules{$balancer}) eq 'HASH') {
10750:                     if ($rule ne $currrules{$balancer}{$type}) {
10751:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10752:                     }
10753:                 } elsif ($rule ne '') {
10754:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10755:                 }
10756:             }
10757:         }
10758:     }
10759:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
10760:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
10761:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
10762:             $defaultshash{'loadbalancing'} = {};
10763:         }
10764:         my $putresult = &Apache::lonnet::put_dom('configuration',
10765:                                                  \%defaultshash,$dom);
10766:         if ($putresult eq 'ok') {
10767:             if (keys(%changes) > 0) {
10768:                 if (ref($changes{'delete'}) eq 'ARRAY') {
10769:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
10770:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
10771:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10772:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10773:                     }
10774:                 }
10775:                 if (ref($changes{'add'}) eq 'ARRAY') {
10776:                     foreach my $balancer (sort(@{$changes{'add'}})) {
10777:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
10778:                     }
10779:                 }
10780:                 if (ref($changes{'curr'}) eq 'HASH') {
10781:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
10782:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
10783:                             if ($changes{'curr'}{$balancer}{'targets'}) {
10784:                                 my %offloadstr;
10785:                                 foreach my $sparetype (@sparestypes) {
10786:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10787:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10788:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10789:                                         }
10790:                                     }
10791:                                 }
10792:                                 if (keys(%offloadstr) == 0) {
10793:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
10794:                                 } else {
10795:                                     my $showoffload;
10796:                                     foreach my $sparetype (@sparestypes) {
10797:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
10798:                                         if (defined($offloadstr{$sparetype})) {
10799:                                             $showoffload .= $offloadstr{$sparetype};
10800:                                         } else {
10801:                                             $showoffload .= &mt('None');
10802:                                         }
10803:                                         $showoffload .= ('&nbsp;'x3);
10804:                                     }
10805:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
10806:                                 }
10807:                             }
10808:                         }
10809:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
10810:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
10811:                                 foreach my $type (@{$alltypes}) {
10812:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
10813:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
10814:                                         my $balancetext;
10815:                                         if ($rule eq '') {
10816:                                             $balancetext =  $ruletitles{'default'};
10817:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
10818:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
10819:                                             $balancetext =  $ruletitles{$rule};
10820:                                         } else {
10821:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
10822:                                         }
10823:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
10824:                                     }
10825:                                 }
10826:                             }
10827:                         }
10828:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10829:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10830:                     }
10831:                 }
10832:                 if ($resulttext ne '') {
10833:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
10834:                 } else {
10835:                     $resulttext = $nochgmsg;
10836:                 }
10837:             } else {
10838:                 $resulttext = $nochgmsg;
10839:             }
10840:         } else {
10841:             $resulttext = '<span class="LC_error">'.
10842:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10843:         }
10844:     } else {
10845:         $resulttext = $nochgmsg;
10846:     }
10847:     return $resulttext;
10848: }
10849: 
10850: sub recurse_check {
10851:     my ($chkcats,$categories,$depth,$name) = @_;
10852:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
10853:         my $chg = 0;
10854:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
10855:             my $category = $chkcats->[$depth]{$name}[$j];
10856:             my $item;
10857:             if ($category eq '') {
10858:                 $chg ++;
10859:             } else {
10860:                 my $deeper = $depth + 1;
10861:                 $item = &escape($category).':'.&escape($name).':'.$depth;
10862:                 if ($chg) {
10863:                     $categories->{$item} -= $chg;
10864:                 }
10865:                 &recurse_check($chkcats,$categories,$deeper,$category);
10866:                 $deeper --;
10867:             }
10868:         }
10869:     }
10870:     return;
10871: }
10872: 
10873: sub recurse_cat_deletes {
10874:     my ($item,$coursecategories,$deletions) = @_;
10875:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10876:     my $subdepth = $depth + 1;
10877:     if (ref($coursecategories) eq 'HASH') {
10878:         foreach my $subitem (keys(%{$coursecategories})) {
10879:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
10880:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
10881:                 delete($coursecategories->{$subitem});
10882:                 $deletions->{$subitem} = 1;
10883:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
10884:             }
10885:         }
10886:     }
10887:     return;
10888: }
10889: 
10890: sub get_active_dcs {
10891:     my ($dom) = @_;
10892:     my $now = time;
10893:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
10894:     my %domcoords;
10895:     my $numdcs = 0;
10896:     foreach my $server (keys(%dompersonnel)) {
10897:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
10898:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
10899:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
10900:         }
10901:     }
10902:     return %domcoords;
10903: }
10904: 
10905: sub active_dc_picker {
10906:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
10907:     my %domcoords = &get_active_dcs($dom);
10908:     my @domcoord = keys(%domcoords);
10909:     if (keys(%currhash)) {
10910:         foreach my $dc (keys(%currhash)) {
10911:             unless (exists($domcoords{$dc})) {
10912:                 push(@domcoord,$dc);
10913:             }
10914:         }
10915:     }
10916:     @domcoord = sort(@domcoord);
10917:     my $numdcs = scalar(@domcoord);
10918:     my $rows = 0;
10919:     my $table;
10920:     if ($numdcs > 1) {
10921:         $table = '<table>';
10922:         for (my $i=0; $i<@domcoord; $i++) {
10923:             my $rem = $i%($numinrow);
10924:             if ($rem == 0) {
10925:                 if ($i > 0) {
10926:                     $table .= '</tr>';
10927:                 }
10928:                 $table .= '<tr>';
10929:                 $rows ++;
10930:             }
10931:             my $check = '';
10932:             if ($inputtype eq 'radio') {
10933:                 if (keys(%currhash) == 0) {
10934:                     if (!$i) {
10935:                         $check = ' checked="checked"';
10936:                     }
10937:                 } elsif (exists($currhash{$domcoord[$i]})) {
10938:                     $check = ' checked="checked"';
10939:                 }
10940:             } else {
10941:                 if (exists($currhash{$domcoord[$i]})) {
10942:                     $check = ' checked="checked"';
10943:                 }
10944:             }
10945:             if ($i == @domcoord - 1) {
10946:                 my $colsleft = $numinrow - $rem;
10947:                 if ($colsleft > 1) {
10948:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
10949:                 } else {
10950:                     $table .= '<td class="LC_left_item">';
10951:                 }
10952:             } else {
10953:                 $table .= '<td class="LC_left_item">';
10954:             }
10955:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
10956:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10957:             $table .= '<span class="LC_nobreak"><label>'.
10958:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
10959:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
10960:             if ($user ne $dcname.':'.$dcdom) {
10961:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10962:             }
10963:             $table .= '</label></span></td>';
10964:         }
10965:         $table .= '</tr></table>';
10966:     } elsif ($numdcs == 1) {
10967:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
10968:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10969:         if ($inputtype eq 'radio') {
10970:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
10971:             if ($user ne $dcname.':'.$dcdom) {
10972:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10973:             }
10974:         } else {
10975:             my $check;
10976:             if (exists($currhash{$domcoord[0]})) {
10977:                 $check = ' checked="checked"';
10978:             }
10979:             $table .= '<span class="LC_nobreak"><label>'.
10980:                       '<input type="checkbox" name="'.$name.'" '.
10981:                       'value="'.$domcoord[0].'"'.$check.' />'.$user;
10982:             if ($user ne $dcname.':'.$dcdom) {
10983:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10984:             }
10985:             $table .= '</label></span>';
10986:             $rows ++;
10987:         }
10988:     }
10989:     return ($numdcs,$table,$rows);
10990: }
10991: 
10992: sub usersession_titles {
10993:     return &Apache::lonlocal::texthash(
10994:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
10995:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
10996:                spares => 'Servers offloaded to, when busy',
10997:                version => 'LON-CAPA version requirement',
10998:                excludedomain => 'Allow all, but exclude specific domains',
10999:                includedomain => 'Deny all, but include specific domains',
11000:                primary => 'Primary (checked first)',
11001:                default => 'Default',
11002:            );
11003: }
11004: 
11005: sub id_for_thisdom {
11006:     my (%servers) = @_;
11007:     my %altids;
11008:     foreach my $server (keys(%servers)) {
11009:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
11010:         if ($serverhome ne $server) {
11011:             $altids{$serverhome} = $server;
11012:         }
11013:     }
11014:     return %altids;
11015: }
11016: 
11017: sub count_servers {
11018:     my ($currbalancer,%servers) = @_;
11019:     my (@spares,$numspares);
11020:     foreach my $lonhost (sort(keys(%servers))) {
11021:         next if ($currbalancer eq $lonhost);
11022:         push(@spares,$lonhost);
11023:     }
11024:     if ($currbalancer) {
11025:         $numspares = scalar(@spares);
11026:     } else {
11027:         $numspares = scalar(@spares) - 1;
11028:     }
11029:     return ($numspares,@spares);
11030: }
11031: 
11032: sub lonbalance_targets_js {
11033:     my ($dom,$types,$servers,$settings) = @_;
11034:     my $select = &mt('Select');
11035:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
11036:     if (ref($servers) eq 'HASH') {
11037:         $alltargets = join("','",sort(keys(%{$servers})));
11038:         my @homedoms;
11039:         foreach my $server (sort(keys(%{$servers}))) {
11040:             if (&Apache::lonnet::host_domain($server) eq $dom) {
11041:                 push(@homedoms,'1');
11042:             } else {
11043:                 push(@homedoms,'0');
11044:             }
11045:         }
11046:         $allishome = join("','",@homedoms);
11047:     }
11048:     if (ref($types) eq 'ARRAY') {
11049:         if (@{$types} > 0) {
11050:             @alltypes = @{$types};
11051:         }
11052:     }
11053:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
11054:     $allinsttypes = join("','",@alltypes);
11055:     my (%currbalancer,%currtargets,%currrules,%existing);
11056:     if (ref($settings) eq 'HASH') {
11057:         %existing = %{$settings};
11058:     }
11059:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
11060:                               \%currtargets,\%currrules);
11061:     my $balancers = join("','",sort(keys(%currbalancer)));
11062:     return <<"END";
11063: 
11064: <script type="text/javascript">
11065: // <![CDATA[
11066: 
11067: currBalancers = new Array('$balancers');
11068: 
11069: function toggleTargets(balnum) {
11070:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11071:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
11072:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
11073:     var prevbalancer = prevhostitem.value;
11074:     var baltotal = document.getElementById('loadbalancing_total').value;
11075:     prevhostitem.value = balancer;
11076:     if (prevbalancer != '') {
11077:         var prevIdx = currBalancers.indexOf(prevbalancer);
11078:         if (prevIdx != -1) {
11079:             currBalancers.splice(prevIdx,1);
11080:         }
11081:     }
11082:     if (balancer == '') {
11083:         hideSpares(balnum);
11084:     } else {
11085:         var currIdx = currBalancers.indexOf(balancer);
11086:         if (currIdx == -1) {
11087:             currBalancers.push(balancer);
11088:         }
11089:         var homedoms = new Array('$allishome');
11090:         var ishomedom = homedoms[lonhostitem.selectedIndex];
11091:         showSpares(balancer,ishomedom,balnum);
11092:     }
11093:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
11094:     return;
11095: }
11096: 
11097: function showSpares(balancer,ishomedom,balnum) {
11098:     var alltargets = new Array('$alltargets');
11099:     var insttypes = new Array('$allinsttypes');
11100:     var offloadtypes = new Array('primary','default');
11101: 
11102:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
11103:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
11104:  
11105:     for (var i=0; i<offloadtypes.length; i++) {
11106:         var count = 0;
11107:         for (var j=0; j<alltargets.length; j++) {
11108:             if (alltargets[j] != balancer) {
11109:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
11110:                 item.value = alltargets[j];
11111:                 item.style.textAlign='left';
11112:                 item.style.textFace='normal';
11113:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
11114:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
11115:                     item.disabled = '';
11116:                 } else {
11117:                     item.disabled = 'disabled';
11118:                     item.checked = false;
11119:                 }
11120:                 count ++;
11121:             }
11122:         }
11123:     }
11124:     for (var k=0; k<insttypes.length; k++) {
11125:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
11126:             if (ishomedom == 1) {
11127:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11128:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11129:             } else {
11130:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11131:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11132:             }
11133:         } else {
11134:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11135:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11136:         }
11137:         if ((insttypes[k] != '_LC_external') && 
11138:             ((insttypes[k] != '_LC_internetdom') ||
11139:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
11140:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
11141:             item.options.length = 0;
11142:             item.options[0] = new Option("","",true,true);
11143:             var idx = 0;
11144:             for (var m=0; m<alltargets.length; m++) {
11145:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
11146:                     idx ++;
11147:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11148:                 }
11149:             }
11150:         }
11151:     }
11152:     return;
11153: }
11154: 
11155: function hideSpares(balnum) {
11156:     var alltargets = new Array('$alltargets');
11157:     var insttypes = new Array('$allinsttypes');
11158:     var offloadtypes = new Array('primary','default');
11159: 
11160:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
11161:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
11162: 
11163:     var total = alltargets.length - 1;
11164:     for (var i=0; i<offloadtypes; i++) {
11165:         for (var j=0; j<total; j++) {
11166:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
11167:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
11168:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
11169:         }
11170:     }
11171:     for (var k=0; k<insttypes.length; k++) {
11172:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11173:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11174:         if (insttypes[k] != '_LC_external') {
11175:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
11176:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
11177:         }
11178:     }
11179:     return;
11180: }
11181: 
11182: function checkOffloads(item,balnum,type) {
11183:     var alltargets = new Array('$alltargets');
11184:     var offloadtypes = new Array('primary','default');
11185:     if (item.checked) {
11186:         var total = alltargets.length - 1;
11187:         var other;
11188:         if (type == offloadtypes[0]) {
11189:             other = offloadtypes[1];
11190:         } else {
11191:             other = offloadtypes[0];
11192:         }
11193:         for (var i=0; i<total; i++) {
11194:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
11195:             if (server == item.value) {
11196:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
11197:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
11198:                 }
11199:             }
11200:         }
11201:     }
11202:     return;
11203: }
11204: 
11205: function singleServerToggle(balnum,type) {
11206:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
11207:     if (offloadtoSelIdx == 0) {
11208:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
11209:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11210: 
11211:     } else {
11212:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
11213:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11214:     }
11215:     return;
11216: }
11217: 
11218: function balanceruleChange(formname,balnum,type) {
11219:     if (type == '_LC_external') {
11220:         return;
11221:     }
11222:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
11223:     for (var i=0; i<typesRules.length; i++) {
11224:         if (formname.elements[typesRules[i]].checked) {
11225:             if (formname.elements[typesRules[i]].value != 'specific') {
11226:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
11227:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11228:             } else {
11229:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11230:             }
11231:         }
11232:     }
11233:     return;
11234: }
11235: 
11236: function balancerDeleteChange(balnum) {
11237:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11238:     var baltotal = document.getElementById('loadbalancing_total').value;
11239:     var addtarget;
11240:     var removetarget;
11241:     var action = 'delete';
11242:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
11243:         var lonhost = hostitem.value;
11244:         var currIdx = currBalancers.indexOf(lonhost);
11245:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
11246:             if (currIdx != -1) {
11247:                 currBalancers.splice(currIdx,1);
11248:             }
11249:             addtarget = lonhost;
11250:         } else {
11251:             if (currIdx == -1) {
11252:                 currBalancers.push(lonhost);
11253:             }
11254:             removetarget = lonhost;
11255:             action = 'undelete';
11256:         }
11257:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
11258:     }
11259:     return;
11260: }
11261: 
11262: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
11263:     if (baltotal > 1) {
11264:         var offloadtypes = new Array('primary','default');
11265:         var alltargets = new Array('$alltargets');
11266:         var insttypes = new Array('$allinsttypes');
11267:         for (var i=0; i<baltotal; i++) {
11268:             if (i != balnum) {
11269:                 for (var j=0; j<offloadtypes.length; j++) {
11270:                     var total = alltargets.length - 1;
11271:                     for (var k=0; k<total; k++) {
11272:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
11273:                         var server = serveritem.value;
11274:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
11275:                             if (server == addtarget) {
11276:                                 serveritem.disabled = '';
11277:                             }
11278:                         }
11279:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11280:                             if (server == removetarget) {
11281:                                 serveritem.disabled = 'disabled';
11282:                                 serveritem.checked = false;
11283:                             }
11284:                         }
11285:                     }
11286:                 }
11287:                 for (var j=0; j<insttypes.length; j++) {
11288:                     if (insttypes[j] != '_LC_external') {
11289:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
11290:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
11291:                             var currSel = singleserver.selectedIndex;
11292:                             var currVal = singleserver.options[currSel].value;
11293:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
11294:                                 var numoptions = singleserver.options.length;
11295:                                 var needsnew = 1;
11296:                                 for (var k=0; k<numoptions; k++) {
11297:                                     if (singleserver.options[k] == addtarget) {
11298:                                         needsnew = 0;
11299:                                         break;
11300:                                     }
11301:                                 }
11302:                                 if (needsnew == 1) {
11303:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
11304:                                 }
11305:                             }
11306:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11307:                                 singleserver.options.length = 0;
11308:                                 if ((currVal) && (currVal != removetarget)) {
11309:                                     singleserver.options[0] = new Option("","",false,false);
11310:                                 } else {
11311:                                     singleserver.options[0] = new Option("","",true,true);
11312:                                 }
11313:                                 var idx = 0;
11314:                                 for (var m=0; m<alltargets.length; m++) {
11315:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
11316:                                         idx ++;
11317:                                         if (currVal == alltargets[m]) {
11318:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
11319:                                         } else {
11320:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11321:                                         }
11322:                                     }
11323:                                 }
11324:                             }
11325:                         }
11326:                     }
11327:                 }
11328:             }
11329:         }
11330:     }
11331:     return;
11332: }
11333: 
11334: // ]]>
11335: </script>
11336: 
11337: END
11338: }
11339: 
11340: sub new_spares_js {
11341:     my @sparestypes = ('primary','default');
11342:     my $types = join("','",@sparestypes);
11343:     my $select = &mt('Select');
11344:     return <<"END";
11345: 
11346: <script type="text/javascript">
11347: // <![CDATA[
11348: 
11349: function updateNewSpares(formname,lonhost) {
11350:     var types = new Array('$types');
11351:     var include = new Array();
11352:     var exclude = new Array();
11353:     for (var i=0; i<types.length; i++) {
11354:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
11355:         for (var j=0; j<spareboxes.length; j++) {
11356:             if (formname.elements[spareboxes[j]].checked) {
11357:                 exclude.push(formname.elements[spareboxes[j]].value);
11358:             } else {
11359:                 include.push(formname.elements[spareboxes[j]].value);
11360:             }
11361:         }
11362:     }
11363:     for (var i=0; i<types.length; i++) {
11364:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
11365:         var selIdx = newSpare.selectedIndex;
11366:         var currnew = newSpare.options[selIdx].value;
11367:         var okSpares = new Array();
11368:         for (var j=0; j<newSpare.options.length; j++) {
11369:             var possible = newSpare.options[j].value;
11370:             if (possible != '') {
11371:                 if (exclude.indexOf(possible) == -1) {
11372:                     okSpares.push(possible);
11373:                 } else {
11374:                     if (currnew == possible) {
11375:                         selIdx = 0;
11376:                     }
11377:                 }
11378:             }
11379:         }
11380:         for (var k=0; k<include.length; k++) {
11381:             if (okSpares.indexOf(include[k]) == -1) {
11382:                 okSpares.push(include[k]);
11383:             }
11384:         }
11385:         okSpares.sort();
11386:         newSpare.options.length = 0;
11387:         if (selIdx == 0) {
11388:             newSpare.options[0] = new Option("$select","",true,true);
11389:         } else {
11390:             newSpare.options[0] = new Option("$select","",false,false);
11391:         }
11392:         for (var m=0; m<okSpares.length; m++) {
11393:             var idx = m+1;
11394:             var selThis = 0;
11395:             if (selIdx != 0) {
11396:                 if (okSpares[m] == currnew) {
11397:                     selThis = 1;
11398:                 }
11399:             }
11400:             if (selThis == 1) {
11401:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
11402:             } else {
11403:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
11404:             }
11405:         }
11406:     }
11407:     return;
11408: }
11409: 
11410: function checkNewSpares(lonhost,type) {
11411:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
11412:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
11413:     if (chosen != '') { 
11414:         var othertype;
11415:         var othernewSpare;
11416:         if (type == 'primary') {
11417:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
11418:         }
11419:         if (type == 'default') {
11420:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
11421:         }
11422:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
11423:             othernewSpare.selectedIndex = 0;
11424:         }
11425:     }
11426:     return;
11427: }
11428: 
11429: // ]]>
11430: </script>
11431: 
11432: END
11433: 
11434: }
11435: 
11436: sub common_domprefs_js {
11437:     return <<"END";
11438: 
11439: <script type="text/javascript">
11440: // <![CDATA[
11441: 
11442: function getIndicesByName(formname,item) {
11443:     var group = new Array();
11444:     for (var i=0;i<formname.elements.length;i++) {
11445:         if (formname.elements[i].name == item) {
11446:             group.push(formname.elements[i].id);
11447:         }
11448:     }
11449:     return group;
11450: }
11451: 
11452: // ]]>
11453: </script>
11454: 
11455: END
11456: 
11457: }
11458: 
11459: sub recaptcha_js {
11460:     my %lt = &captcha_phrases();
11461:     return <<"END";
11462: 
11463: <script type="text/javascript">
11464: // <![CDATA[
11465: 
11466: function updateCaptcha(caller,context) {
11467:     var privitem;
11468:     var pubitem;
11469:     var privtext;
11470:     var pubtext;
11471:     if (document.getElementById(context+'_recaptchapub')) {
11472:         pubitem = document.getElementById(context+'_recaptchapub');
11473:     } else {
11474:         return;
11475:     }
11476:     if (document.getElementById(context+'_recaptchapriv')) {
11477:         privitem = document.getElementById(context+'_recaptchapriv');
11478:     } else {
11479:         return;
11480:     }
11481:     if (document.getElementById(context+'_recaptchapubtxt')) {
11482:         pubtext = document.getElementById(context+'_recaptchapubtxt');
11483:     } else {
11484:         return;
11485:     }
11486:     if (document.getElementById(context+'_recaptchaprivtxt')) {
11487:         privtext = document.getElementById(context+'_recaptchaprivtxt');
11488:     } else {
11489:         return;
11490:     }
11491:     if (caller.checked) {
11492:         if (caller.value == 'recaptcha') {
11493:             pubitem.type = 'text';
11494:             privitem.type = 'text';
11495:             pubitem.size = '40';
11496:             privitem.size = '40';
11497:             pubtext.innerHTML = "$lt{'pub'}";
11498:             privtext.innerHTML = "$lt{'priv'}";
11499:         } else {
11500:             pubitem.type = 'hidden';
11501:             privitem.type = 'hidden';
11502:             pubtext.innerHTML = '';
11503:             privtext.innerHTML = '';
11504:         }
11505:     }
11506:     return;
11507: }
11508: 
11509: // ]]>
11510: </script>
11511: 
11512: END
11513: 
11514: }
11515: 
11516: sub toggle_display_js {
11517:     return <<"END";
11518: 
11519: <script type="text/javascript">
11520: // <![CDATA[
11521: 
11522: function toggleDisplay(domForm,caller) {
11523:     if (document.getElementById(caller)) {
11524:         var divitem = document.getElementById(caller);
11525:         var optionsElement = domForm.coursecredits;
11526:         if (caller == 'emailoptions') {
11527:             optionsElement = domForm.cancreate_email; 
11528:         }
11529:         if (optionsElement.length) {
11530:             var currval;
11531:             for (var i=0; i<optionsElement.length; i++) {
11532:                 if (optionsElement[i].checked) {
11533:                    currval = optionsElement[i].value;
11534:                 }
11535:             }
11536:             if (currval == 1) {
11537:                 divitem.style.display = 'block';
11538:             } else {
11539:                 divitem.style.display = 'none';
11540:             }
11541:         }
11542:     }
11543:     return;
11544: }
11545: 
11546: // ]]>
11547: </script>
11548: 
11549: END
11550: 
11551: }
11552: 
11553: sub captcha_phrases {
11554:     return &Apache::lonlocal::texthash (
11555:                  priv => 'Private key',
11556:                  pub  => 'Public key',
11557:                  original  => 'original (CAPTCHA)',
11558:                  recaptcha => 'successor (ReCAPTCHA)',
11559:                  notused   => 'unused',
11560:     );
11561: }
11562: 
11563: sub devalidate_remote_domconfs {
11564:     my ($dom,$cachekeys) = @_;
11565:     return unless (ref($cachekeys) eq 'HASH');
11566:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11567:     my %thismachine;
11568:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
11569:     my @posscached = ('domainconfig','domdefaults');
11570:     if (keys(%servers) > 1) {
11571:         foreach my $server (keys(%servers)) {
11572:             next if ($thismachine{$server});
11573:             my @cached;
11574:             foreach my $name (@posscached) {
11575:                 if ($cachekeys->{$name}) {
11576:                     push(@cached,&escape($name).':'.&escape($dom));
11577:                 }
11578:             }
11579:             if (@cached) {
11580:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
11581:             }
11582:         }
11583:     }
11584:     return;
11585: }
11586: 
11587: 1;

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