File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.37: download - view: text, annotated - select for diffs
Sat Apr 5 23:22:19 2014 UTC (10 years, 2 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  - Backport 1.230, 1.231, 1.232, 1.233.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.37 2014/04/05 23:22:19 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 LONCAPA qw(:DEFAULT :match);
  169: use LONCAPA::Enrollment;
  170: use LONCAPA::lonauthcgi();
  171: use File::Copy;
  172: use Locale::Language;
  173: use DateTime::TimeZone;
  174: use DateTime::Locale;
  175: 
  176: my $registered_cleanup;
  177: my $modified_urls;
  178: 
  179: sub handler {
  180:     my $r=shift;
  181:     if ($r->header_only) {
  182:         &Apache::loncommon::content_type($r,'text/html');
  183:         $r->send_http_header;
  184:         return OK;
  185:     }
  186: 
  187:     my $context = 'domain';
  188:     my $dom = $env{'request.role.domain'};
  189:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  190:     if (&Apache::lonnet::allowed('mau',$dom)) {
  191:         &Apache::loncommon::content_type($r,'text/html');
  192:         $r->send_http_header;
  193:     } else {
  194:         $env{'user.error.msg'}=
  195:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  196:         return HTTP_NOT_ACCEPTABLE;
  197:     }
  198: 
  199:     $registered_cleanup=0;
  200:     @{$modified_urls}=();
  201: 
  202:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  203:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  204:                                             ['phase','actions']);
  205:     my $phase = 'pickactions';
  206:     if ( exists($env{'form.phase'}) ) {
  207:         $phase = $env{'form.phase'};
  208:     }
  209:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  210:     my %domconfig =
  211:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  212:                 'quotas','autoenroll','autoupdate','autocreate',
  213:                 'directorysrch','usercreation','usermodification',
  214:                 'contacts','defaults','scantron','coursecategories',
  215:                 'serverstatuses','requestcourses','coursedefaults',
  216:                 'usersessions','loadbalancing',requestauthor',
  217:                 'selfenrollment'],$dom);
  218:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  219:                        'autoupdate','autocreate','directorysrch','contacts',
  220:                        'usercreation','selfcreation','usermodification','scantron',
  221:                        'requestcourses','requestauthor','coursecategories',
  222:                        'serverstatuses','coursedefaults','selfenrollment',
  223:                        'usersessions');
  224:     my %existing;
  225:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  226:         %existing = %{$domconfig{'loadbalancing'}};
  227:     }
  228:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  229:         push(@prefs_order,'loadbalancing');
  230:     }
  231:     my %prefs = (
  232:         'rolecolors' =>
  233:                    { text => 'Default color schemes',
  234:                      help => 'Domain_Configuration_Color_Schemes',
  235:                      header => [{col1 => 'Student Settings',
  236:                                  col2 => '',},
  237:                                 {col1 => 'Coordinator Settings',
  238:                                  col2 => '',},
  239:                                 {col1 => 'Author Settings',
  240:                                  col2 => '',},
  241:                                 {col1 => 'Administrator Settings',
  242:                                  col2 => '',}],
  243:                       print => \&print_rolecolors,
  244:                       modify => \&modify_rolecolors,
  245:                     },
  246:         'login' =>
  247:                     { text => 'Log-in page options',
  248:                       help => 'Domain_Configuration_Login_Page',
  249:                       header => [{col1 => 'Log-in Page Items',
  250:                                   col2 => '',},
  251:                                  {col1 => 'Log-in Help',
  252:                                   col2 => 'Value'}],
  253:                       print => \&print_login,
  254:                       modify => \&modify_login,
  255:                     },
  256:         'defaults' => 
  257:                     { text => 'Default authentication/language/timezone/portal',
  258:                       help => 'Domain_Configuration_LangTZAuth',
  259:                       header => [{col1 => 'Setting',
  260:                                   col2 => 'Value'}],
  261:                       print => \&print_defaults,
  262:                       modify => \&modify_defaults,
  263:                     },
  264:         'quotas' => 
  265:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  266:                       help => 'Domain_Configuration_Quotas',
  267:                       header => [{col1 => 'User affiliation',
  268:                                   col2 => 'Available tools',
  269:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  270:                       print => \&print_quotas,
  271:                       modify => \&modify_quotas,
  272:                     },
  273:         'autoenroll' =>
  274:                    { text => 'Auto-enrollment settings',
  275:                      help => 'Domain_Configuration_Auto_Enrollment',
  276:                      header => [{col1 => 'Configuration setting',
  277:                                  col2 => 'Value(s)'}],
  278:                      print => \&print_autoenroll,
  279:                      modify => \&modify_autoenroll,
  280:                    },
  281:         'autoupdate' => 
  282:                    { text => 'Auto-update settings',
  283:                      help => 'Domain_Configuration_Auto_Updates',
  284:                      header => [{col1 => 'Setting',
  285:                                  col2 => 'Value',},
  286:                                 {col1 => 'Setting',
  287:                                  col2 => 'Affiliation'},
  288:                                 {col1 => 'User population',
  289:                                  col2 => 'Updatable user data'}],
  290:                      print => \&print_autoupdate,
  291:                      modify => \&modify_autoupdate,
  292:                   },
  293:         'autocreate' => 
  294:                   { text => 'Auto-course creation settings',
  295:                      help => 'Domain_Configuration_Auto_Creation',
  296:                      header => [{col1 => 'Configuration Setting',
  297:                                  col2 => 'Value',}],
  298:                      print => \&print_autocreate,
  299:                      modify => \&modify_autocreate,
  300:                   },
  301:         'directorysrch' => 
  302:                   { text => 'Institutional directory searches',
  303:                     help => 'Domain_Configuration_InstDirectory_Search',
  304:                     header => [{col1 => 'Setting',
  305:                                 col2 => 'Value',}],
  306:                     print => \&print_directorysrch,
  307:                     modify => \&modify_directorysrch,
  308:                   },
  309:         'contacts' =>
  310:                   { text => 'Contact Information',
  311:                     help => 'Domain_Configuration_Contact_Info',
  312:                     header => [{col1 => 'Setting',
  313:                                 col2 => 'Value',}],
  314:                     print => \&print_contacts,
  315:                     modify => \&modify_contacts,
  316:                   },
  317:         'usercreation' => 
  318:                   { text => 'User creation',
  319:                     help => 'Domain_Configuration_User_Creation',
  320:                     header => [{col1 => 'Format rule type',
  321:                                 col2 => 'Format rules in force'},
  322:                                {col1 => 'User account creation',
  323:                                 col2 => 'Usernames which may be created',},
  324:                                {col1 => 'Context',
  325:                                 col2 => 'Assignable authentication types'}],
  326:                     print => \&print_usercreation,
  327:                     modify => \&modify_usercreation,
  328:                   },
  329:         'selfcreation' => 
  330:                   { text => 'Users self-creating accounts',
  331:                     help => 'Domain_Configuration_Self_Creation', 
  332:                     header => [{col1 => 'Self-creation with institutional username',
  333:                                 col2 => 'Enabled?'},
  334:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  335:                                 col2 => 'Information user can enter'},
  336:                                {col1 => 'Self-creation with e-mail as username',
  337:                                 col2 => 'Settings'}],
  338:                     print => \&print_selfcreation,
  339:                     modify => \&modify_selfcreation,
  340:                   },
  341:         'usermodification' =>
  342:                   { text => 'User modification',
  343:                     help => 'Domain_Configuration_User_Modification',
  344:                     header => [{col1 => 'Target user has role',
  345:                                 col2 => 'User information updatable in author context'},
  346:                                {col1 => 'Target user has role',
  347:                                 col2 => 'User information updatable in course context'}],
  348:                     print => \&print_usermodification,
  349:                     modify => \&modify_usermodification,
  350:                   },
  351:         'scantron' =>
  352:                   { text => 'Bubblesheet format file',
  353:                     help => 'Domain_Configuration_Scantron_Format',
  354:                     header => [ {col1 => 'Item',
  355:                                  col2 => '',
  356:                               }],
  357:                     print => \&print_scantron,
  358:                     modify => \&modify_scantron,
  359:                   },
  360:         'requestcourses' => 
  361:                  {text => 'Request creation of courses',
  362:                   help => 'Domain_Configuration_Request_Courses',
  363:                   header => [{col1 => 'User affiliation',
  364:                               col2 => 'Availability/Processing of requests',},
  365:                              {col1 => 'Setting',
  366:                               col2 => 'Value'},
  367:                              {col1 => 'Available textbooks',
  368:                               col2 => ''}],
  369:                   print => \&print_quotas,
  370:                   modify => \&modify_quotas,
  371:                  },
  372:         'requestauthor' =>
  373:                  {text => 'Request Authoring Space',
  374:                   help => 'Domain_Configuration_Request_Author',
  375:                   header => [{col1 => 'User affiliation',
  376:                               col2 => 'Availability/Processing of requests',},
  377:                              {col1 => 'Setting',
  378:                               col2 => 'Value'}],
  379:                   print => \&print_quotas,
  380:                   modify => \&modify_quotas,
  381:                  },
  382:         'coursecategories' =>
  383:                   { text => 'Cataloging of courses/communities',
  384:                     help => 'Domain_Configuration_Cataloging_Courses',
  385:                     header => [{col1 => 'Category settings',
  386:                                 col2 => '',},
  387:                                {col1 => 'Categories',
  388:                                 col2 => '',
  389:                                }],
  390:                     print => \&print_coursecategories,
  391:                     modify => \&modify_coursecategories,
  392:                   },
  393:         'serverstatuses' =>
  394:                  {text   => 'Access to server status pages',
  395:                   help   => 'Domain_Configuration_Server_Status',
  396:                   header => [{col1 => 'Status Page',
  397:                               col2 => 'Other named users',
  398:                               col3 => 'Specific IPs',
  399:                             }],
  400:                   print => \&print_serverstatuses,
  401:                   modify => \&modify_serverstatuses,
  402:                  },
  403:         'coursedefaults' =>
  404:                  {text => 'Course/Community defaults',
  405:                   help => 'Domain_Configuration_Course_Defaults',
  406:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  407:                               col2 => 'Value',},
  408:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  409:                               col2 => 'Value',},],
  410:                   print => \&print_coursedefaults,
  411:                   modify => \&modify_coursedefaults,
  412:                  },
  413:         'selfenrollment' =>
  414:                  {text   => 'Self-enrollment in Course/Community',
  415:                   help   => 'Domain_Configuration_Selfenrollment',
  416:                   header => [{col1 => 'Configuration Rights',
  417:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  418:                              {col1 => 'Defaults',
  419:                               col2 => 'Value'},
  420:                              {col1 => 'Self-enrollment validation (optional)',
  421:                               col2 => 'Value'},],
  422:                   print => \&print_selfenrollment,
  423:                   modify => \&modify_selfenrollment,
  424:                  },
  425:         'usersessions' =>
  426:                  {text  => 'User session hosting/offloading',
  427:                   help  => 'Domain_Configuration_User_Sessions',
  428:                   header => [{col1 => 'Domain server',
  429:                               col2 => 'Servers to offload sessions to when busy'},
  430:                              {col1 => 'Hosting of users from other domains',
  431:                               col2 => 'Rules'},
  432:                              {col1 => "Hosting domain's own users elsewhere",
  433:                               col2 => 'Rules'}],
  434:                   print => \&print_usersessions,
  435:                   modify => \&modify_usersessions,
  436:                  },
  437:          'loadbalancing' =>
  438:                  {text  => 'Dedicated Load Balancer(s)',
  439:                   help  => 'Domain_Configuration_Load_Balancing',
  440:                   header => [{col1 => 'Balancers',
  441:                               col2 => 'Default destinations',
  442:                               col3 => 'User affiliation',
  443:                               col4 => 'Overrides'},
  444:                             ],
  445:                   print => \&print_loadbalancing,
  446:                   modify => \&modify_loadbalancing,
  447:                  },
  448:     );
  449:     if (keys(%servers) > 1) {
  450:         $prefs{'login'}  = { text   => 'Log-in page options',
  451:                              help   => 'Domain_Configuration_Login_Page',
  452:                             header => [{col1 => 'Log-in Service',
  453:                                         col2 => 'Server Setting',},
  454:                                        {col1 => 'Log-in Page Items',
  455:                                         col2 => ''},
  456:                                        {col1 => 'Log-in Help',
  457:                                         col2 => 'Value'}],
  458:                             print => \&print_login,
  459:                             modify => \&modify_login,
  460:                            };
  461:     }
  462: 
  463:     my @roles = ('student','coordinator','author','admin');
  464:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  465:     &Apache::lonhtmlcommon::add_breadcrumb
  466:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  467:       text=>"Settings to display/modify"});
  468:     my $confname = $dom.'-domainconfig';
  469: 
  470:     if ($phase eq 'process') {
  471:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  472:                                                               \%prefs,\%domconfig,$confname,\@roles);
  473:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  474:             $r->rflush();
  475:             &devalidate_remote_domconfs($dom,$result);
  476:         }
  477:     } elsif ($phase eq 'display') {
  478:         my $js = &recaptcha_js().
  479:                  &credits_js();
  480:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  481:             my ($othertitle,$usertypes,$types) =
  482:                 &Apache::loncommon::sorted_inst_types($dom);
  483:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  484:                                           $domconfig{'loadbalancing'}).
  485:                    &new_spares_js().
  486:                    &common_domprefs_js().
  487:                    &Apache::loncommon::javascript_array_indexof();
  488:         }
  489:         if (grep(/^requestcourses$/,@actions)) {
  490:             my $javascript_validations;
  491:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  492:             $js .= <<END;
  493: <script type="text/javascript">
  494: $javascript_validations
  495: </script>
  496: $coursebrowserjs
  497: END
  498:         }
  499:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  500:     } else {
  501: # check if domconfig user exists for the domain.
  502:         my $servadm = $r->dir_config('lonAdmEMail');
  503:         my ($configuserok,$author_ok,$switchserver) =
  504:             &config_check($dom,$confname,$servadm);
  505:         unless ($configuserok eq 'ok') {
  506:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  507:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  508:                           $confname).
  509:                       '<br />'
  510:             );
  511:             if ($switchserver) {
  512:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  513:                           '<br />'.
  514:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  515:                           '<br />'.
  516:                           &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).
  517:                           '<br />'.
  518:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  519:                 );
  520:             } else {
  521:                 $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.').
  522:                           '<br />'.
  523:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  524:                 );
  525:             }
  526:             $r->print(&Apache::loncommon::end_page());
  527:             return OK;
  528:         }
  529:         if (keys(%domconfig) == 0) {
  530:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  531:             my @ids=&Apache::lonnet::current_machine_ids();
  532:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  533:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  534:                 my @loginimages = ('img','logo','domlogo','login');
  535:                 my $custom_img_count = 0;
  536:                 foreach my $img (@loginimages) {
  537:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  538:                         $custom_img_count ++;
  539:                     }
  540:                 }
  541:                 foreach my $role (@roles) {
  542:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  543:                         $custom_img_count ++;
  544:                     }
  545:                 }
  546:                 if ($custom_img_count > 0) {
  547:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  548:                     my $switch_server = &check_switchserver($dom,$confname);
  549:                     $r->print(
  550:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  551:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  552:     &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 />'.
  553:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  554:                     if ($switch_server) {
  555:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  556:                     }
  557:                     $r->print(&Apache::loncommon::end_page());
  558:                     return OK;
  559:                 }
  560:             }
  561:         }
  562:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  563:     }
  564:     return OK;
  565: }
  566: 
  567: sub process_changes {
  568:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  569:     my %domconfig;
  570:     if (ref($values) eq 'HASH') {
  571:         %domconfig = %{$values};
  572:     }
  573:     my $output;
  574:     if ($action eq 'login') {
  575:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  576:     } elsif ($action eq 'rolecolors') {
  577:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  578:                                      $lastactref,%domconfig);
  579:     } elsif ($action eq 'quotas') {
  580:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  581:     } elsif ($action eq 'autoenroll') {
  582:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  583:     } elsif ($action eq 'autoupdate') {
  584:         $output = &modify_autoupdate($dom,%domconfig);
  585:     } elsif ($action eq 'autocreate') {
  586:         $output = &modify_autocreate($dom,%domconfig);
  587:     } elsif ($action eq 'directorysrch') {
  588:         $output = &modify_directorysrch($dom,%domconfig);
  589:     } elsif ($action eq 'usercreation') {
  590:         $output = &modify_usercreation($dom,%domconfig);
  591:     } elsif ($action eq 'selfcreation') {
  592:         $output = &modify_selfcreation($dom,%domconfig);
  593:     } elsif ($action eq 'usermodification') {
  594:         $output = &modify_usermodification($dom,%domconfig);
  595:     } elsif ($action eq 'contacts') {
  596:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  597:     } elsif ($action eq 'defaults') {
  598:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  599:     } elsif ($action eq 'scantron') {
  600:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  601:     } elsif ($action eq 'coursecategories') {
  602:         $output = &modify_coursecategories($dom,%domconfig);
  603:     } elsif ($action eq 'serverstatuses') {
  604:         $output = &modify_serverstatuses($dom,%domconfig);
  605:     } elsif ($action eq 'requestcourses') {
  606:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  607:     } elsif ($action eq 'requestauthor') {
  608:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  609:     } elsif ($action eq 'coursedefaults') {
  610:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  611:     } elsif ($action eq 'selfenrollment') {
  612:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  613:     } elsif ($action eq 'usersessions') {
  614:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  615:     } elsif ($action eq 'loadbalancing') {
  616:         $output = &modify_loadbalancing($dom,%domconfig);
  617:     }
  618:     return $output;
  619: }
  620: 
  621: sub print_config_box {
  622:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  623:     my $rowtotal = 0;
  624:     my $output;
  625:     if ($action eq 'coursecategories') {
  626:         $output = &coursecategories_javascript($settings);
  627:     }
  628:     $output .= 
  629:          '<table class="LC_nested_outer">
  630:           <tr>
  631:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  632:            &mt($item->{text}).'&nbsp;'.
  633:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  634:           '</tr>';
  635:     $rowtotal ++;
  636:     my $numheaders = 1;
  637:     if (ref($item->{'header'}) eq 'ARRAY') {
  638:         $numheaders = scalar(@{$item->{'header'}});
  639:     }
  640:     if ($numheaders > 1) {
  641:         my $colspan = '';
  642:         my $rightcolspan = '';
  643:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  644:             (($action eq 'login') && ($numheaders < 3))) {
  645:             $colspan = ' colspan="2"';
  646:         }
  647:         if ($action eq 'usersessions') {
  648:             $rightcolspan = ' colspan="3"'; 
  649:         }
  650:         $output .= '
  651:           <tr>
  652:            <td>
  653:             <table class="LC_nested">
  654:              <tr class="LC_info_row">
  655:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  656:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  657:              </tr>';
  658:         $rowtotal ++;
  659:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  660:             ($action eq 'usermodification') || ($action eq 'selfenrollment') ||
  661:             ($action eq 'usersessions')) {
  662:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  663:         } elsif ($action eq 'coursecategories') {
  664:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  665:         } elsif ($action eq 'login') {
  666:             if ($numheaders == 3) {
  667:                 $colspan = ' colspan="2"';
  668:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  669:             } else {
  670:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  671:             }
  672:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  673:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  674:         } elsif ($action eq 'rolecolors') {
  675:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  676:         }
  677:         $output .= '
  678:            </table>
  679:           </td>
  680:          </tr>
  681:          <tr>
  682:            <td>
  683:             <table class="LC_nested">
  684:              <tr class="LC_info_row">
  685:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  686:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  687:              </tr>';
  688:             $rowtotal ++;
  689:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  690:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  691:             ($action eq 'usersessions')) {
  692:             $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal).'
  693:            </table>
  694:           </td>
  695:          </tr>
  696:          <tr>
  697:            <td>
  698:             <table class="LC_nested">
  699:              <tr class="LC_info_row">
  700:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  701:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  702:              </tr>'."\n".
  703:             $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  704:             $rowtotal ++;
  705:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults')) {
  706:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  707:         } elsif ($action eq 'coursecategories') {
  708:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  709:         } elsif ($action eq 'login') {
  710:             if ($numheaders == 3) {
  711:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  712:            </table>
  713:           </td>
  714:          </tr>
  715:          <tr>
  716:            <td>
  717:             <table class="LC_nested">
  718:              <tr class="LC_info_row">
  719:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  720:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  721:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  722:                 $rowtotal ++;
  723:             } else {
  724:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  725:             }
  726:         } elsif ($action eq 'requestcourses') {
  727:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal).
  728:                        &print_studentcode($settings,\$rowtotal).'
  729:            </table>
  730:           </td>
  731:          </tr>
  732:          <tr>
  733:            <td>
  734:             <table class="LC_nested">
  735:              <tr class="LC_info_row">
  736:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  737:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  738:                        &print_textbookcourses($dom,$settings,\$rowtotal);
  739:         } elsif ($action eq 'requestauthor') {
  740:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  741:         } elsif ($action eq 'rolecolors') {
  742:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  743:            </table>
  744:           </td>
  745:          </tr>
  746:          <tr>
  747:            <td>
  748:             <table class="LC_nested">
  749:              <tr class="LC_info_row">
  750:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  751:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  752:               <td class="LC_right_item" valign="top">'.
  753:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  754:              </tr>'.
  755:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  756:            </table>
  757:           </td>
  758:          </tr>
  759:          <tr>
  760:            <td>
  761:             <table class="LC_nested">
  762:              <tr class="LC_info_row">
  763:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  764:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  765:              </tr>'.
  766:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  767:             $rowtotal += 2;
  768:         }
  769:     } else {
  770:         $output .= '
  771:           <tr>
  772:            <td>
  773:             <table class="LC_nested">
  774:              <tr class="LC_info_row">';
  775:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  776:             $output .= '  
  777:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  778:         } elsif ($action eq 'serverstatuses') {
  779:             $output .= '
  780:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  781:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  782: 
  783:         } else {
  784:             $output .= '
  785:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  786:         }
  787:         if (defined($item->{'header'}->[0]->{'col3'})) {
  788:             $output .= '<td class="LC_left_item" valign="top">'.
  789:                        &mt($item->{'header'}->[0]->{'col2'});
  790:             if ($action eq 'serverstatuses') {
  791:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  792:             } 
  793:         } else {
  794:             $output .= '<td class="LC_right_item" valign="top">'.
  795:                        &mt($item->{'header'}->[0]->{'col2'});
  796:         }
  797:         $output .= '</td>';
  798:         if ($item->{'header'}->[0]->{'col3'}) {
  799:             if (defined($item->{'header'}->[0]->{'col4'})) {
  800:                 $output .= '<td class="LC_left_item" valign="top">'.
  801:                             &mt($item->{'header'}->[0]->{'col3'});
  802:             } else {
  803:                 $output .= '<td class="LC_right_item" valign="top">'.
  804:                            &mt($item->{'header'}->[0]->{'col3'});
  805:             }
  806:             if ($action eq 'serverstatuses') {
  807:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  808:             }
  809:             $output .= '</td>';
  810:         }
  811:         if ($item->{'header'}->[0]->{'col4'}) {
  812:             $output .= '<td class="LC_right_item" valign="top">'.
  813:                        &mt($item->{'header'}->[0]->{'col4'});
  814:         }
  815:         $output .= '</tr>';
  816:         $rowtotal ++;
  817:         if ($action eq 'quotas') {
  818:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  819:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  820:                  ($action eq 'contacts') || ($action eq 'defaults') || ($action eq 'serverstatuses') ||
  821:                  ($action eq 'loadbalancing')) {
  822:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  823:         } elsif ($action eq 'scantron') {
  824:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  825:         } elsif ($action eq 'helpsettings') {
  826:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  827:         }
  828:     }
  829:     $output .= '
  830:    </table>
  831:   </td>
  832:  </tr>
  833: </table><br />';
  834:     return ($output,$rowtotal);
  835: }
  836: 
  837: sub print_login {
  838:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  839:     my ($css_class,$datatable);
  840:     my %choices = &login_choices();
  841: 
  842:     if ($caller eq 'service') {
  843:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  844:         my $choice = $choices{'disallowlogin'};
  845:         $css_class = ' class="LC_odd_row"';
  846:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  847:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  848:                       '<th>'.$choices{'server'}.'</th>'.
  849:                       '<th>'.$choices{'serverpath'}.'</th>'.
  850:                       '<th>'.$choices{'custompath'}.'</th>'.
  851:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  852:         my %disallowed;
  853:         if (ref($settings) eq 'HASH') {
  854:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  855:                %disallowed = %{$settings->{'loginvia'}};
  856:             }
  857:         }
  858:         foreach my $lonhost (sort(keys(%servers))) {
  859:             my $direct = 'selected="selected"';
  860:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  861:                 if ($disallowed{$lonhost}{'server'} ne '') {
  862:                     $direct = '';
  863:                 }
  864:             }
  865:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  866:                           '<td><select name="'.$lonhost.'_server">'.
  867:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  868:                           '</option>';
  869:             foreach my $hostid (sort(keys(%servers))) {
  870:                 next if ($servers{$hostid} eq $servers{$lonhost});
  871:                 my $selected = '';
  872:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  873:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  874:                         $selected = 'selected="selected"';
  875:                     }
  876:                 }
  877:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  878:                               $servers{$hostid}.'</option>';
  879:             }
  880:             $datatable .= '</select></td>'.
  881:                           '<td><select name="'.$lonhost.'_serverpath">';
  882:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  883:                 my $pathname = $path;
  884:                 if ($path eq 'custom') {
  885:                     $pathname = &mt('Custom Path').' ->';
  886:                 }
  887:                 my $selected = '';
  888:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  889:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  890:                         $selected = 'selected="selected"';
  891:                     }
  892:                 } elsif ($path eq '') {
  893:                     $selected = 'selected="selected"';
  894:                 }
  895:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  896:             }
  897:             $datatable .= '</select></td>';
  898:             my ($custom,$exempt);
  899:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  900:                 $custom = $disallowed{$lonhost}{'custompath'};
  901:                 $exempt = $disallowed{$lonhost}{'exempt'};
  902:             }
  903:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  904:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  905:                           '</tr>';
  906:         }
  907:         $datatable .= '</table></td></tr>';
  908:         return $datatable;
  909:     } elsif ($caller eq 'page') {
  910:         my %defaultchecked = ( 
  911:                                'coursecatalog' => 'on',
  912:                                'helpdesk'      => 'on',
  913:                                'adminmail'     => 'off',
  914:                                'newuser'       => 'off',
  915:                              );
  916:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  917:         my (%checkedon,%checkedoff);
  918:         foreach my $item (@toggles) {
  919:             if ($defaultchecked{$item} eq 'on') { 
  920:                 $checkedon{$item} = ' checked="checked" ';
  921:                 $checkedoff{$item} = ' ';
  922:             } elsif ($defaultchecked{$item} eq 'off') {
  923:                 $checkedoff{$item} = ' checked="checked" ';
  924:                 $checkedon{$item} = ' ';
  925:             }
  926:         }
  927:         my @images = ('img','logo','domlogo','login');
  928:         my @logintext = ('textcol','bgcol');
  929:         my @bgs = ('pgbg','mainbg','sidebg');
  930:         my @links = ('link','alink','vlink');
  931:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  932:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  933:         my (%is_custom,%designs);
  934:         my %defaults = (
  935:                        font => $defaultdesign{'login.font'},
  936:                        );
  937:         foreach my $item (@images) {
  938:             $defaults{$item} = $defaultdesign{'login.'.$item};
  939:             $defaults{'showlogo'}{$item} = 1;
  940:         }
  941:         foreach my $item (@bgs) {
  942:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  943:         }
  944:         foreach my $item (@logintext) {
  945:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  946:         }
  947:         foreach my $item (@links) {
  948:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  949:         }
  950:         if (ref($settings) eq 'HASH') {
  951:             foreach my $item (@toggles) {
  952:                 if ($settings->{$item} eq '1') {
  953:                     $checkedon{$item} =  ' checked="checked" ';
  954:                     $checkedoff{$item} = ' ';
  955:                 } elsif ($settings->{$item} eq '0') {
  956:                     $checkedoff{$item} =  ' checked="checked" ';
  957:                     $checkedon{$item} = ' ';
  958:                 }
  959:             }
  960:             foreach my $item (@images) {
  961:                 if (defined($settings->{$item})) {
  962:                     $designs{$item} = $settings->{$item};
  963:                     $is_custom{$item} = 1;
  964:                 }
  965:                 if (defined($settings->{'showlogo'}{$item})) {
  966:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  967:                 }
  968:             }
  969:             foreach my $item (@logintext) {
  970:                 if ($settings->{$item} ne '') {
  971:                     $designs{'logintext'}{$item} = $settings->{$item};
  972:                     $is_custom{$item} = 1;
  973:                 }
  974:             }
  975:             if ($settings->{'font'} ne '') {
  976:                 $designs{'font'} = $settings->{'font'};
  977:                 $is_custom{'font'} = 1;
  978:             }
  979:             foreach my $item (@bgs) {
  980:                 if ($settings->{$item} ne '') {
  981:                     $designs{'bgs'}{$item} = $settings->{$item};
  982:                     $is_custom{$item} = 1;
  983:                 }
  984:             }
  985:             foreach my $item (@links) {
  986:                 if ($settings->{$item} ne '') {
  987:                     $designs{'links'}{$item} = $settings->{$item};
  988:                     $is_custom{$item} = 1;
  989:                 }
  990:             }
  991:         } else {
  992:             if ($designhash{$dom.'.login.font'} ne '') {
  993:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  994:                 $is_custom{'font'} = 1;
  995:             }
  996:             foreach my $item (@images) {
  997:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  998:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  999:                     $is_custom{$item} = 1;
 1000:                 }
 1001:             }
 1002:             foreach my $item (@bgs) {
 1003:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1004:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1005:                     $is_custom{$item} = 1;
 1006:                 }
 1007:             }
 1008:             foreach my $item (@links) {
 1009:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1010:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1011:                     $is_custom{$item} = 1;
 1012:                 }
 1013:             }
 1014:         }
 1015:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1016:                                                       logo => 'Institution Logo',
 1017:                                                       domlogo => 'Domain Logo',
 1018:                                                       login => 'Login box');
 1019:         my $itemcount = 1;
 1020:         foreach my $item (@toggles) {
 1021:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1022:             $datatable .=  
 1023:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1024:                 '</td><td>'.
 1025:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1026:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1027:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1028:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1029:                 '</tr>';
 1030:             $itemcount ++;
 1031:         }
 1032:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1033:         $datatable .= '</tr></table></td></tr>';
 1034:     } elsif ($caller eq 'help') {
 1035:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1036:         my $switchserver = &check_switchserver($dom,$confname);
 1037:         my $itemcount = 1;
 1038:         $defaulturl = '/adm/loginproblems.html';
 1039:         $defaulttype = 'default';
 1040:         %lt = &Apache::lonlocal::texthash (
 1041:                      del     => 'Delete?',
 1042:                      rep     => 'Replace:',
 1043:                      upl     => 'Upload:',
 1044:                      default => 'Default',
 1045:                      custom  => 'Custom',
 1046:                                              );
 1047:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1048:         my @currlangs;
 1049:         if (ref($settings) eq 'HASH') {
 1050:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1051:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1052:                     next if ($settings->{'helpurl'}{$key} eq '');
 1053:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1054:                     $type{$key} = 'custom';
 1055:                     unless ($key eq 'nolang') {
 1056:                         push(@currlangs,$key);
 1057:                     }
 1058:                 }
 1059:             } elsif ($settings->{'helpurl'} ne '') {
 1060:                 $type{'nolang'} = 'custom';
 1061:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1062:             }
 1063:         }
 1064:         foreach my $lang ('nolang',sort(@currlangs)) {
 1065:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1066:             $datatable .= '<tr'.$css_class.'>';
 1067:             if ($url{$lang} eq '') {
 1068:                 $url{$lang} = $defaulturl;
 1069:             }
 1070:             if ($type{$lang} eq '') {
 1071:                 $type{$lang} = $defaulttype;
 1072:             }
 1073:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1074:             if ($lang eq 'nolang') {
 1075:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1076:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1077:             } else {
 1078:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1079:                                   $langchoices{$lang},
 1080:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1081:             }
 1082:             $datatable .= '</span></td>'."\n".
 1083:                           '<td class="LC_left_item">';
 1084:             if ($type{$lang} eq 'custom') {
 1085:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1086:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1087:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1088:             } else {
 1089:                 $datatable .= $lt{'upl'};
 1090:             }
 1091:             $datatable .='<br />';
 1092:             if ($switchserver) {
 1093:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1094:             } else {
 1095:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1096:             }
 1097:             $datatable .= '</td></tr>';
 1098:             $itemcount ++;
 1099:         }
 1100:         my @addlangs;
 1101:         foreach my $lang (sort(keys(%langchoices))) {
 1102:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1103:             push(@addlangs,$lang);
 1104:         }
 1105:         if (@addlangs > 0) {
 1106:             my %toadd;
 1107:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1108:             $toadd{''} = &mt('Select');
 1109:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1110:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1111:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1112:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1113:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1114:             if ($switchserver) {
 1115:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1116:             } else {
 1117:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1118:             }
 1119:             $datatable .= '</td></tr>';
 1120:             $itemcount ++;
 1121:         }
 1122:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1123:     }
 1124:     return $datatable;
 1125: }
 1126: 
 1127: sub login_choices {
 1128:     my %choices =
 1129:         &Apache::lonlocal::texthash (
 1130:             coursecatalog => 'Display Course/Community Catalog link?',
 1131:             adminmail     => "Display Administrator's E-mail Address?",
 1132:             helpdesk      => 'Display "Contact Helpdesk" link',
 1133:             disallowlogin => "Login page requests redirected",
 1134:             hostid        => "Server",
 1135:             server        => "Redirect to:",
 1136:             serverpath    => "Path",
 1137:             custompath    => "Custom", 
 1138:             exempt        => "Exempt IP(s)",
 1139:             directlogin   => "No redirect",
 1140:             newuser       => "Link to create a user account",
 1141:             img           => "Header",
 1142:             logo          => "Main Logo",
 1143:             domlogo       => "Domain Logo",
 1144:             login         => "Log-in Header", 
 1145:             textcol       => "Text color",
 1146:             bgcol         => "Box color",
 1147:             bgs           => "Background colors",
 1148:             links         => "Link colors",
 1149:             font          => "Font color",
 1150:             pgbg          => "Header",
 1151:             mainbg        => "Page",
 1152:             sidebg        => "Login box",
 1153:             link          => "Link",
 1154:             alink         => "Active link",
 1155:             vlink         => "Visited link",
 1156:         );
 1157:     return %choices;
 1158: }
 1159: 
 1160: sub print_rolecolors {
 1161:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1162:     my %choices = &color_font_choices();
 1163:     my @bgs = ('pgbg','tabbg','sidebg');
 1164:     my @links = ('link','alink','vlink');
 1165:     my @images = ('img');
 1166:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1167:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1168:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1169:     my (%is_custom,%designs);
 1170:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1171:     if (ref($settings) eq 'HASH') {
 1172:         if (ref($settings->{$role}) eq 'HASH') {
 1173:             if ($settings->{$role}->{'img'} ne '') {
 1174:                 $designs{'img'} = $settings->{$role}->{'img'};
 1175:                 $is_custom{'img'} = 1;
 1176:             }
 1177:             if ($settings->{$role}->{'font'} ne '') {
 1178:                 $designs{'font'} = $settings->{$role}->{'font'};
 1179:                 $is_custom{'font'} = 1;
 1180:             }
 1181:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1182:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1183:                 $is_custom{'fontmenu'} = 1;
 1184:             }
 1185:             foreach my $item (@bgs) {
 1186:                 if ($settings->{$role}->{$item} ne '') {
 1187:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1188:                     $is_custom{$item} = 1;
 1189:                 }
 1190:             }
 1191:             foreach my $item (@links) {
 1192:                 if ($settings->{$role}->{$item} ne '') {
 1193:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1194:                     $is_custom{$item} = 1;
 1195:                 }
 1196:             }
 1197:         }
 1198:     } else {
 1199:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1200:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1201:             $is_custom{'img'} = 1;
 1202:         }
 1203:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1204:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1205:             $is_custom{'fontmenu'} = 1; 
 1206:         }
 1207:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1208:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1209:             $is_custom{'font'} = 1;
 1210:         }
 1211:         foreach my $item (@bgs) {
 1212:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1213:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1214:                 $is_custom{$item} = 1;
 1215:             
 1216:             }
 1217:         }
 1218:         foreach my $item (@links) {
 1219:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1220:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1221:                 $is_custom{$item} = 1;
 1222:             }
 1223:         }
 1224:     }
 1225:     my $itemcount = 1;
 1226:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1227:     $datatable .= '</tr></table></td></tr>';
 1228:     return $datatable;
 1229: }
 1230: 
 1231: sub role_defaults {
 1232:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1233:     my %defaults;
 1234:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1235:         return %defaults;
 1236:     }
 1237:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1238:     if ($role eq 'login') {
 1239:         %defaults = (
 1240:                        font => $defaultdesign{$role.'.font'},
 1241:                     );
 1242:         if (ref($logintext) eq 'ARRAY') {
 1243:             foreach my $item (@{$logintext}) {
 1244:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1245:             }
 1246:         }
 1247:         foreach my $item (@{$images}) {
 1248:             $defaults{'showlogo'}{$item} = 1;
 1249:         }
 1250:     } else {
 1251:         %defaults = (
 1252:                        img => $defaultdesign{$role.'.img'},
 1253:                        font => $defaultdesign{$role.'.font'},
 1254:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1255:                     );
 1256:     }
 1257:     foreach my $item (@{$bgs}) {
 1258:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1259:     }
 1260:     foreach my $item (@{$links}) {
 1261:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1262:     }
 1263:     foreach my $item (@{$images}) {
 1264:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1265:     }
 1266:     return %defaults;
 1267: }
 1268: 
 1269: sub display_color_options {
 1270:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1271:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1272:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1273:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1274:     my $datatable = '<tr'.$css_class.'>'.
 1275:         '<td>'.$choices->{'font'}.'</td>';
 1276:     if (!$is_custom->{'font'}) {
 1277:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1278:     } else {
 1279:         $datatable .= '<td>&nbsp;</td>';
 1280:     }
 1281:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1282: 
 1283:     $datatable .= '<td><span class="LC_nobreak">'.
 1284:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1285:                   ' value="'.$current_color.'" />&nbsp;'.
 1286:                   '&nbsp;</td></tr>';
 1287:     unless ($role eq 'login') { 
 1288:         $datatable .= '<tr'.$css_class.'>'.
 1289:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1290:         if (!$is_custom->{'fontmenu'}) {
 1291:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1292:         } else {
 1293:             $datatable .= '<td>&nbsp;</td>';
 1294:         }
 1295: 	$current_color = $designs->{'fontmenu'} ?
 1296: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1297:         $datatable .= '<td><span class="LC_nobreak">'.
 1298:                       '<input class="colorchooser" type="text" size="10" name="'
 1299: 		      .$role.'_fontmenu"'.
 1300:                       ' value="'.$current_color.'" />&nbsp;'.
 1301:                       '&nbsp;</td></tr>';
 1302:     }
 1303:     my $switchserver = &check_switchserver($dom,$confname);
 1304:     foreach my $img (@{$images}) {
 1305: 	$itemcount ++;
 1306:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1307:         $datatable .= '<tr'.$css_class.'>'.
 1308:                       '<td>'.$choices->{$img};
 1309:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1310:         if ($role eq 'login') {
 1311:             if ($img eq 'login') {
 1312:                 $login_hdr_pick =
 1313:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1314:                 $logincolors =
 1315:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1316:                                        $designs,$defaults);
 1317:             } elsif ($img ne 'domlogo') {
 1318:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1319:             }
 1320:         }
 1321:         $datatable .= '</td>';
 1322:         if ($designs->{$img} ne '') {
 1323:             $imgfile = $designs->{$img};
 1324: 	    $img_import = ($imgfile =~ m{^/adm/});
 1325:         } else {
 1326:             $imgfile = $defaults->{$img};
 1327:         }
 1328:         if ($imgfile) {
 1329:             my ($showfile,$fullsize);
 1330:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1331:                 my $urldir = $1;
 1332:                 my $filename = $2;
 1333:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1334:                 if (@info) {
 1335:                     my $thumbfile = 'tn-'.$filename;
 1336:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1337:                     if (@thumb) {
 1338:                         $showfile = $urldir.'/'.$thumbfile;
 1339:                     } else {
 1340:                         $showfile = $imgfile;
 1341:                     }
 1342:                 } else {
 1343:                     $showfile = '';
 1344:                 }
 1345:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1346:                 $showfile = $imgfile;
 1347:                 my $imgdir = $1;
 1348:                 my $filename = $2;
 1349:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1350:                     $showfile = "/$imgdir/tn-".$filename;
 1351:                 } else {
 1352:                     my $input = $londocroot.$imgfile;
 1353:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1354:                     if (!-e $output) {
 1355:                         my ($width,$height) = &thumb_dimensions();
 1356:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1357:                         if ($fullwidth ne '' && $fullheight ne '') {
 1358:                             if ($fullwidth > $width && $fullheight > $height) { 
 1359:                                 my $size = $width.'x'.$height;
 1360:                                 system("convert -sample $size $input $output");
 1361:                                 $showfile = "/$imgdir/tn-".$filename;
 1362:                             }
 1363:                         }
 1364:                     }
 1365:                 }
 1366:             }
 1367:             if ($showfile) {
 1368:                 if ($showfile =~ m{^/(adm|res)/}) {
 1369:                     if ($showfile =~ m{^/res/}) {
 1370:                         my $local_showfile =
 1371:                             &Apache::lonnet::filelocation('',$showfile);
 1372:                         &Apache::lonnet::repcopy($local_showfile);
 1373:                     }
 1374:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1375:                 }
 1376:                 if ($imgfile) {
 1377:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1378:                         if ($imgfile =~ m{^/res/}) {
 1379:                             my $local_imgfile =
 1380:                                 &Apache::lonnet::filelocation('',$imgfile);
 1381:                             &Apache::lonnet::repcopy($local_imgfile);
 1382:                         }
 1383:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1384:                     } else {
 1385:                         $fullsize = $imgfile;
 1386:                     }
 1387:                 }
 1388:                 $datatable .= '<td>';
 1389:                 if ($img eq 'login') {
 1390:                     $datatable .= $login_hdr_pick;
 1391:                 } 
 1392:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1393:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1394:             } else {
 1395:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1396:                               &mt('Upload:').'<br />';
 1397:             }
 1398:         } else {
 1399:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1400:                           &mt('Upload:').'<br />';
 1401:         }
 1402:         if ($switchserver) {
 1403:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1404:         } else {
 1405:             if ($img ne 'login') { # suppress file selection for Log-in header
 1406:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1407:             }
 1408:         }
 1409:         $datatable .= '</td></tr>';
 1410:     }
 1411:     $itemcount ++;
 1412:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1413:     $datatable .= '<tr'.$css_class.'>'.
 1414:                   '<td>'.$choices->{'bgs'}.'</td>';
 1415:     my $bgs_def;
 1416:     foreach my $item (@{$bgs}) {
 1417:         if (!$is_custom->{$item}) {
 1418:             $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>';
 1419:         }
 1420:     }
 1421:     if ($bgs_def) {
 1422:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1423:     } else {
 1424:         $datatable .= '<td>&nbsp;</td>';
 1425:     }
 1426:     $datatable .= '<td class="LC_right_item">'.
 1427:                   '<table border="0"><tr>';
 1428: 
 1429:     foreach my $item (@{$bgs}) {
 1430:         $datatable .= '<td align="center">'.$choices->{$item};
 1431: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1432:         if ($designs->{'bgs'}{$item}) {
 1433:             $datatable .= '&nbsp;';
 1434:         }
 1435:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1436:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1437:     }
 1438:     $datatable .= '</tr></table></td></tr>';
 1439:     $itemcount ++;
 1440:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1441:     $datatable .= '<tr'.$css_class.'>'.
 1442:                   '<td>'.$choices->{'links'}.'</td>';
 1443:     my $links_def;
 1444:     foreach my $item (@{$links}) {
 1445:         if (!$is_custom->{$item}) {
 1446:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1447:         }
 1448:     }
 1449:     if ($links_def) {
 1450:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1451:     } else {
 1452:         $datatable .= '<td>&nbsp;</td>';
 1453:     }
 1454:     $datatable .= '<td class="LC_right_item">'.
 1455:                   '<table border="0"><tr>';
 1456:     foreach my $item (@{$links}) {
 1457: 	my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1458:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1459:         if ($designs->{'links'}{$item}) {
 1460:             $datatable.='&nbsp;';
 1461:         }
 1462:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1463:                       '" /></td>';
 1464:     }
 1465:     $$rowtotal += $itemcount;
 1466:     return $datatable;
 1467: }
 1468: 
 1469: sub logo_display_options {
 1470:     my ($img,$defaults,$designs) = @_;
 1471:     my $checkedon;
 1472:     if (ref($defaults) eq 'HASH') {
 1473:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1474:             if ($defaults->{'showlogo'}{$img}) {
 1475:                 $checkedon = 'checked="checked" ';     
 1476:             }
 1477:         } 
 1478:     }
 1479:     if (ref($designs) eq 'HASH') {
 1480:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1481:             if (defined($designs->{'showlogo'}{$img})) {
 1482:                 if ($designs->{'showlogo'}{$img} == 0) {
 1483:                     $checkedon = '';
 1484:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1485:                     $checkedon = 'checked="checked" ';
 1486:                 }
 1487:             }
 1488:         }
 1489:     }
 1490:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1491:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1492:            &mt('show').'</label>'."\n";
 1493: }
 1494: 
 1495: sub login_header_options  {
 1496:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1497:     my $output = '';
 1498:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1499:         $output .= &mt('Text default(s):').'<br />';
 1500:         if (!$is_custom->{'textcol'}) {
 1501:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1502:                        '&nbsp;&nbsp;&nbsp;';
 1503:         }
 1504:         if (!$is_custom->{'bgcol'}) {
 1505:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1506:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1507:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1508:         }
 1509:         $output .= '<br />';
 1510:     }
 1511:     $output .='<br />';
 1512:     return $output;
 1513: }
 1514: 
 1515: sub login_text_colors {
 1516:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1517:     my $color_menu = '<table border="0"><tr>';
 1518:     foreach my $item (@{$logintext}) {
 1519:         $color_menu .= '<td align="center">'.$choices->{$item};
 1520:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1521:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1522:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1523:     }
 1524:     $color_menu .= '</tr></table><br />';
 1525:     return $color_menu;
 1526: }
 1527: 
 1528: sub image_changes {
 1529:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1530:     my $output;
 1531:     if ($img eq 'login') {
 1532:             # suppress image for Log-in header
 1533:     } elsif (!$is_custom) {
 1534:         if ($img ne 'domlogo') {
 1535:             $output .= &mt('Default image:').'<br />';
 1536:         } else {
 1537:             $output .= &mt('Default in use:').'<br />';
 1538:         }
 1539:     }
 1540:     if ($img eq 'login') { # suppress image for Log-in header
 1541:         $output .= '<td>'.$logincolors;
 1542:     } else {
 1543:         if ($img_import) {
 1544:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1545:         }
 1546:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1547:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1548:         if ($is_custom) {
 1549:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1550:                        '<input type="checkbox" name="'.
 1551:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1552:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1553:         } else {
 1554:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1555:         }
 1556:     }
 1557:     return $output;
 1558: }
 1559: 
 1560: sub print_quotas {
 1561:     my ($dom,$settings,$rowtotal,$action) = @_;
 1562:     my $context;
 1563:     if ($action eq 'quotas') {
 1564:         $context = 'tools';
 1565:     } else {
 1566:         $context = $action;
 1567:     }
 1568:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1569:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1570:     my $typecount = 0;
 1571:     my ($css_class,%titles);
 1572:     if ($context eq 'requestcourses') {
 1573:         @usertools = ('official','unofficial','community','textbook');
 1574:         @options =('norequest','approval','validate','autolimit');
 1575:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1576:         %titles = &courserequest_titles();
 1577:     } elsif ($context eq 'requestauthor') {
 1578:         @usertools = ('author');
 1579:         @options = ('norequest','approval','automatic');
 1580:         %titles = &authorrequest_titles();
 1581:     } else {
 1582:         @usertools = ('aboutme','blog','webdav','portfolio');
 1583:         %titles = &tool_titles();
 1584:     }
 1585:     if (ref($types) eq 'ARRAY') {
 1586:         foreach my $type (@{$types}) {
 1587:             my ($currdefquota,$currauthorquota);
 1588:             unless (($context eq 'requestcourses') ||
 1589:                     ($context eq 'requestauthor')) {
 1590:                 if (ref($settings) eq 'HASH') {
 1591:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1592:                         $currdefquota = $settings->{defaultquota}->{$type};
 1593:                     } else {
 1594:                         $currdefquota = $settings->{$type};
 1595:                     }
 1596:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1597:                         $currauthorquota = $settings->{authorquota}->{$type};
 1598:                     }
 1599:                 }
 1600:             }
 1601:             if (defined($usertypes->{$type})) {
 1602:                 $typecount ++;
 1603:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1604:                 $datatable .= '<tr'.$css_class.'>'.
 1605:                               '<td>'.$usertypes->{$type}.'</td>'.
 1606:                               '<td class="LC_left_item">';
 1607:                 if ($context eq 'requestcourses') {
 1608:                     $datatable .= '<table><tr>';
 1609:                 }
 1610:                 my %cell;  
 1611:                 foreach my $item (@usertools) {
 1612:                     if ($context eq 'requestcourses') {
 1613:                         my ($curroption,$currlimit);
 1614:                         if (ref($settings) eq 'HASH') {
 1615:                             if (ref($settings->{$item}) eq 'HASH') {
 1616:                                 $curroption = $settings->{$item}->{$type};
 1617:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1618:                                     $currlimit = $1; 
 1619:                                 }
 1620:                             }
 1621:                         }
 1622:                         if (!$curroption) {
 1623:                             $curroption = 'norequest';
 1624:                         }
 1625:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1626:                         foreach my $option (@options) {
 1627:                             my $val = $option;
 1628:                             if ($option eq 'norequest') {
 1629:                                 $val = 0;  
 1630:                             }
 1631:                             if ($option eq 'validate') {
 1632:                                 my $canvalidate = 0;
 1633:                                 if (ref($validations{$item}) eq 'HASH') { 
 1634:                                     if ($validations{$item}{$type}) {
 1635:                                         $canvalidate = 1;
 1636:                                     }
 1637:                                 }
 1638:                                 next if (!$canvalidate);
 1639:                             }
 1640:                             my $checked = '';
 1641:                             if ($option eq $curroption) {
 1642:                                 $checked = ' checked="checked"';
 1643:                             } elsif ($option eq 'autolimit') {
 1644:                                 if ($curroption =~ /^autolimit/) {
 1645:                                     $checked = ' checked="checked"';
 1646:                                 }                       
 1647:                             } 
 1648:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1649:                                   '<input type="radio" name="crsreq_'.$item.
 1650:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1651:                                   $titles{$option}.'</label>';
 1652:                             if ($option eq 'autolimit') {
 1653:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1654:                                                 $item.'_limit_'.$type.'" size="1" '.
 1655:                                                 'value="'.$currlimit.'" />';
 1656:                             }
 1657:                             $cell{$item} .= '</span> ';
 1658:                             if ($option eq 'autolimit') {
 1659:                                 $cell{$item} .= $titles{'unlimited'};
 1660:                             }
 1661:                         }
 1662:                     } elsif ($context eq 'requestauthor') {
 1663:                         my $curroption;
 1664:                         if (ref($settings) eq 'HASH') {
 1665:                             $curroption = $settings->{$type};
 1666:                         }
 1667:                         if (!$curroption) {
 1668:                             $curroption = 'norequest';
 1669:                         }
 1670:                         foreach my $option (@options) {
 1671:                             my $val = $option;
 1672:                             if ($option eq 'norequest') {
 1673:                                 $val = 0;
 1674:                             }
 1675:                             my $checked = '';
 1676:                             if ($option eq $curroption) {
 1677:                                 $checked = ' checked="checked"';
 1678:                             }
 1679:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1680:                                   '<input type="radio" name="authorreq_'.$type.
 1681:                                   '" value="'.$val.'"'.$checked.' />'.
 1682:                                   $titles{$option}.'</label></span>&nbsp; ';
 1683:                         }
 1684:                     } else {
 1685:                         my $checked = 'checked="checked" ';
 1686:                         if (ref($settings) eq 'HASH') {
 1687:                             if (ref($settings->{$item}) eq 'HASH') {
 1688:                                 if ($settings->{$item}->{$type} == 0) {
 1689:                                     $checked = '';
 1690:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1691:                                     $checked =  'checked="checked" ';
 1692:                                 }
 1693:                             }
 1694:                         }
 1695:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1696:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1697:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1698:                                       '</label></span>&nbsp; ';
 1699:                     }
 1700:                 }
 1701:                 if ($context eq 'requestcourses') {
 1702:                     $datatable .= '</tr><tr>';
 1703:                     foreach my $item (@usertools) {
 1704:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1705:                     }
 1706:                     $datatable .= '</tr></table>';
 1707:                 }
 1708:                 $datatable .= '</td>';
 1709:                 unless (($context eq 'requestcourses') ||
 1710:                         ($context eq 'requestauthor')) {
 1711:                     $datatable .= 
 1712:                               '<td class="LC_right_item">'.
 1713:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1714:                               '<input type="text" name="quota_'.$type.
 1715:                               '" value="'.$currdefquota.
 1716:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1717:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1718:                               '<input type="text" name="authorquota_'.$type.
 1719:                               '" value="'.$currauthorquota.
 1720:                               '" size="5" /></span></td>';
 1721:                 }
 1722:                 $datatable .= '</tr>';
 1723:             }
 1724:         }
 1725:     }
 1726:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1727:         $defaultquota = '20';
 1728:         $authorquota = '500';
 1729:         if (ref($settings) eq 'HASH') {
 1730:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1731:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1732:             } elsif (defined($settings->{'default'})) {
 1733:                 $defaultquota = $settings->{'default'};
 1734:             }
 1735:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1736:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1737:             }
 1738:         }
 1739:     }
 1740:     $typecount ++;
 1741:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1742:     $datatable .= '<tr'.$css_class.'>'.
 1743:                   '<td>'.$othertitle.'</td>'.
 1744:                   '<td class="LC_left_item">';
 1745:     if ($context eq 'requestcourses') {
 1746:         $datatable .= '<table><tr>';
 1747:     }
 1748:     my %defcell;
 1749:     foreach my $item (@usertools) {
 1750:         if ($context eq 'requestcourses') {
 1751:             my ($curroption,$currlimit);
 1752:             if (ref($settings) eq 'HASH') {
 1753:                 if (ref($settings->{$item}) eq 'HASH') {
 1754:                     $curroption = $settings->{$item}->{'default'};
 1755:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1756:                         $currlimit = $1;
 1757:                     }
 1758:                 }
 1759:             }
 1760:             if (!$curroption) {
 1761:                 $curroption = 'norequest';
 1762:             }
 1763:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1764:             foreach my $option (@options) {
 1765:                 my $val = $option;
 1766:                 if ($option eq 'norequest') {
 1767:                     $val = 0;
 1768:                 }
 1769:                 if ($option eq 'validate') {
 1770:                     my $canvalidate = 0;
 1771:                     if (ref($validations{$item}) eq 'HASH') {
 1772:                         if ($validations{$item}{'default'}) {
 1773:                             $canvalidate = 1;
 1774:                         }
 1775:                     }
 1776:                     next if (!$canvalidate);
 1777:                 }
 1778:                 my $checked = '';
 1779:                 if ($option eq $curroption) {
 1780:                     $checked = ' checked="checked"';
 1781:                 } elsif ($option eq 'autolimit') {
 1782:                     if ($curroption =~ /^autolimit/) {
 1783:                         $checked = ' checked="checked"';
 1784:                     }
 1785:                 }
 1786:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1787:                                   '<input type="radio" name="crsreq_'.$item.
 1788:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1789:                                   $titles{$option}.'</label>';
 1790:                 if ($option eq 'autolimit') {
 1791:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1792:                                        $item.'_limit_default" size="1" '.
 1793:                                        'value="'.$currlimit.'" />';
 1794:                 }
 1795:                 $defcell{$item} .= '</span> ';
 1796:                 if ($option eq 'autolimit') {
 1797:                     $defcell{$item} .= $titles{'unlimited'};
 1798:                 }
 1799:             }
 1800:         } elsif ($context eq 'requestauthor') {
 1801:             my $curroption;
 1802:             if (ref($settings) eq 'HASH') {
 1803:                 $curroption = $settings->{'default'};
 1804:             }
 1805:             if (!$curroption) {
 1806:                 $curroption = 'norequest';
 1807:             }
 1808:             foreach my $option (@options) {
 1809:                 my $val = $option;
 1810:                 if ($option eq 'norequest') {
 1811:                     $val = 0;
 1812:                 }
 1813:                 my $checked = '';
 1814:                 if ($option eq $curroption) {
 1815:                     $checked = ' checked="checked"';
 1816:                 }
 1817:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1818:                               '<input type="radio" name="authorreq_default"'.
 1819:                               ' value="'.$val.'"'.$checked.' />'.
 1820:                               $titles{$option}.'</label></span>&nbsp; ';
 1821:             }
 1822:         } else {
 1823:             my $checked = 'checked="checked" ';
 1824:             if (ref($settings) eq 'HASH') {
 1825:                 if (ref($settings->{$item}) eq 'HASH') {
 1826:                     if ($settings->{$item}->{'default'} == 0) {
 1827:                         $checked = '';
 1828:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1829:                         $checked = 'checked="checked" ';
 1830:                     }
 1831:                 }
 1832:             }
 1833:             $datatable .= '<span class="LC_nobreak"><label>'.
 1834:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1835:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1836:                           '</label></span>&nbsp; ';
 1837:         }
 1838:     }
 1839:     if ($context eq 'requestcourses') {
 1840:         $datatable .= '</tr><tr>';
 1841:         foreach my $item (@usertools) {
 1842:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1843:         }
 1844:         $datatable .= '</tr></table>';
 1845:     }
 1846:     $datatable .= '</td>';
 1847:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1848:         $datatable .= '<td class="LC_right_item">'.
 1849:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1850:                       '<input type="text" name="defaultquota" value="'.
 1851:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1852:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1853:                       '<input type="text" name="authorquota" value="'.
 1854:                       $authorquota.'" size="5" /></span></td>';
 1855:     }
 1856:     $datatable .= '</tr>';
 1857:     $typecount ++;
 1858:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1859:     $datatable .= '<tr'.$css_class.'>'.
 1860:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1861:     if ($context eq 'requestcourses') {
 1862:         $datatable .= &mt('(overrides affiliation, if set)').
 1863:                       '</td>'.
 1864:                       '<td class="LC_left_item">'.
 1865:                       '<table><tr>';
 1866:     } else {
 1867:         $datatable .= &mt('(overrides affiliation, if checked)').
 1868:                       '</td>'.
 1869:                       '<td class="LC_left_item" colspan="2">'.
 1870:                       '<br />';
 1871:     }
 1872:     my %advcell;
 1873:     foreach my $item (@usertools) {
 1874:         if ($context eq 'requestcourses') {
 1875:             my ($curroption,$currlimit);
 1876:             if (ref($settings) eq 'HASH') {
 1877:                 if (ref($settings->{$item}) eq 'HASH') {
 1878:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1879:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1880:                         $currlimit = $1;
 1881:                     }
 1882:                 }
 1883:             }
 1884:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1885:             my $checked = '';
 1886:             if ($curroption eq '') {
 1887:                 $checked = ' checked="checked"';
 1888:             }
 1889:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1890:                                '<input type="radio" name="crsreq_'.$item.
 1891:                                '__LC_adv" value=""'.$checked.' />'.
 1892:                                &mt('No override set').'</label></span>&nbsp; ';
 1893:             foreach my $option (@options) {
 1894:                 my $val = $option;
 1895:                 if ($option eq 'norequest') {
 1896:                     $val = 0;
 1897:                 }
 1898:                 if ($option eq 'validate') {
 1899:                     my $canvalidate = 0;
 1900:                     if (ref($validations{$item}) eq 'HASH') {
 1901:                         if ($validations{$item}{'_LC_adv'}) {
 1902:                             $canvalidate = 1;
 1903:                         }
 1904:                     }
 1905:                     next if (!$canvalidate);
 1906:                 }
 1907:                 my $checked = '';
 1908:                 if ($val eq $curroption) {
 1909:                     $checked = ' checked="checked"';
 1910:                 } elsif ($option eq 'autolimit') {
 1911:                     if ($curroption =~ /^autolimit/) {
 1912:                         $checked = ' checked="checked"';
 1913:                     }
 1914:                 }
 1915:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1916:                                   '<input type="radio" name="crsreq_'.$item.
 1917:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1918:                                   $titles{$option}.'</label>';
 1919:                 if ($option eq 'autolimit') {
 1920:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1921:                                        $item.'_limit__LC_adv" size="1" '.
 1922:                                        'value="'.$currlimit.'" />';
 1923:                 }
 1924:                 $advcell{$item} .= '</span> ';
 1925:                 if ($option eq 'autolimit') {
 1926:                     $advcell{$item} .= $titles{'unlimited'};
 1927:                 }
 1928:             }
 1929:         } elsif ($context eq 'requestauthor') {
 1930:             my $curroption;
 1931:             if (ref($settings) eq 'HASH') {
 1932:                 $curroption = $settings->{'_LC_adv'};
 1933:             }
 1934:             my $checked = '';
 1935:             if ($curroption eq '') {
 1936:                 $checked = ' checked="checked"';
 1937:             }
 1938:             $datatable .= '<span class="LC_nobreak"><label>'.
 1939:                           '<input type="radio" name="authorreq__LC_adv"'.
 1940:                           ' value=""'.$checked.' />'.
 1941:                           &mt('No override set').'</label></span>&nbsp; ';
 1942:             foreach my $option (@options) {
 1943:                 my $val = $option;
 1944:                 if ($option eq 'norequest') {
 1945:                     $val = 0;
 1946:                 }
 1947:                 my $checked = '';
 1948:                 if ($val eq $curroption) {
 1949:                     $checked = ' checked="checked"';
 1950:                 }
 1951:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1952:                               '<input type="radio" name="authorreq__LC_adv"'.
 1953:                               ' value="'.$val.'"'.$checked.' />'.
 1954:                               $titles{$option}.'</label></span>&nbsp; ';
 1955:             }
 1956:         } else {
 1957:             my $checked = 'checked="checked" ';
 1958:             if (ref($settings) eq 'HASH') {
 1959:                 if (ref($settings->{$item}) eq 'HASH') {
 1960:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1961:                         $checked = '';
 1962:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1963:                         $checked = 'checked="checked" ';
 1964:                     }
 1965:                 }
 1966:             }
 1967:             $datatable .= '<span class="LC_nobreak"><label>'.
 1968:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1969:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1970:                           '</label></span>&nbsp; ';
 1971:         }
 1972:     }
 1973:     if ($context eq 'requestcourses') {
 1974:         $datatable .= '</tr><tr>';
 1975:         foreach my $item (@usertools) {
 1976:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1977:         }
 1978:         $datatable .= '</tr></table>';
 1979:     }
 1980:     $datatable .= '</td></tr>';
 1981:     $$rowtotal += $typecount;
 1982:     return $datatable;
 1983: }
 1984: 
 1985: sub print_requestmail {
 1986:     my ($dom,$action,$settings,$rowtotal) = @_;
 1987:     my ($now,$datatable,%currapp);
 1988:     $now = time;
 1989:     if (ref($settings) eq 'HASH') {
 1990:         if (ref($settings->{'notify'}) eq 'HASH') {
 1991:             if ($settings->{'notify'}{'approval'} ne '') {
 1992:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 1993:             }
 1994:         }
 1995:     }
 1996:     my $numinrow = 2;
 1997:     my $css_class;
 1998:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 1999:     my $text;
 2000:     if ($action eq 'requestcourses') {
 2001:         $text = &mt('Receive notification of course requests requiring approval');
 2002:     } elsif ($action eq 'requestauthor') {
 2003:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2004:     } else {
 2005:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2006:     }
 2007:     $datatable = '<tr'.$css_class.'>'.
 2008:                  ' <td>'.$text.'</td>'.
 2009:                  ' <td class="LC_left_item">';
 2010:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2011:                                                  $action.'notifyapproval',%currapp);
 2012:     if ($numdc > 0) {
 2013:         $datatable .= $table;
 2014:     } else {
 2015:         $datatable .= &mt('There are no active Domain Coordinators');
 2016:     }
 2017:     $datatable .='</td></tr>';
 2018:     $$rowtotal += $rows;
 2019:     return $datatable;
 2020: }
 2021: 
 2022: sub print_studentcode {
 2023:     my ($settings,$rowtotal) = @_;
 2024:     my $rownum = 0; 
 2025:     my ($output,%current);
 2026:     my @crstypes = ('official','unofficial','community','textbook');
 2027:     if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2028:         foreach my $type (@crstypes) {
 2029:             $current{$type} = $settings->{'uniquecode'}{$type};
 2030:         }
 2031:     }
 2032:     $output .= '<tr>'.
 2033:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2034:                '<td class="LC_left_item">';
 2035:     foreach my $type (@crstypes) {
 2036:         my $check = ' ';
 2037:         if ($current{$type}) {
 2038:             $check = ' checked="checked" ';
 2039:         }
 2040:         $output .= '<span class="LC_nobreak"><label>'.
 2041:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2042:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2043:     }
 2044:     $output .= '</td></tr>';
 2045:     $$rowtotal ++;
 2046:     return $output;
 2047: }
 2048: 
 2049: sub print_textbookcourses {
 2050:     my ($dom,$settings,$rowtotal) = @_;
 2051:     my $rownum = 0;
 2052:     my $css_class;
 2053:     my $itemcount = 1;
 2054:     my $maxnum = 0;
 2055:     my $bookshash;
 2056:     if (ref($settings) eq 'HASH') {
 2057:         $bookshash = $settings->{'textbooks'};
 2058:     }
 2059:     my %ordered;
 2060:     if (ref($bookshash) eq 'HASH') {
 2061:         foreach my $item (keys(%{$bookshash})) {
 2062:             if (ref($bookshash->{$item}) eq 'HASH') {
 2063:                 my $num = $bookshash->{$item}{'order'};
 2064:                 $ordered{$num} = $item;
 2065:             }
 2066:         }
 2067:     }
 2068:     my $confname = $dom.'-domainconfig';
 2069:     my $switchserver = &check_switchserver($dom,$confname);
 2070:     my $maxnum = scalar(keys(%ordered));
 2071:     my $datatable = &textbookcourses_javascript(\%ordered);
 2072:     if (keys(%ordered)) {
 2073:         my @items = sort { $a <=> $b } keys(%ordered);
 2074:         for (my $i=0; $i<@items; $i++) {
 2075:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2076:             my $key = $ordered{$items[$i]};
 2077:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2078:             my $coursetitle = $coursehash{'description'};
 2079:             my ($subject,$title,$author,$image,$imgsrc,$cdom,$cnum);
 2080:             if (ref($bookshash->{$key}) eq 'HASH') {
 2081:                 $subject = $bookshash->{$key}->{'subject'};
 2082:                 $title = $bookshash->{$key}->{'title'};
 2083:                 $author = $bookshash->{$key}->{'author'};
 2084:                 $image = $bookshash->{$key}->{'image'};
 2085:                 if ($image ne '') {
 2086:                     my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2087:                     my $imagethumb = "$path/tn-".$imagefile;
 2088:                     $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2089:                 }
 2090:             }
 2091:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$key'".');"';
 2092:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2093:                          .'<select name="'.$key.'"'.$chgstr.'>';
 2094:             for (my $k=0; $k<=$maxnum; $k++) {
 2095:                 my $vpos = $k+1;
 2096:                 my $selstr;
 2097:                 if ($k == $i) {
 2098:                     $selstr = ' selected="selected" ';
 2099:                 }
 2100:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2101:             }
 2102:             $datatable .= '</select>'.('&nbsp;'x2).
 2103:                 '<label><input type="checkbox" name="book_del" value="'.$key.'" />'.
 2104:                 &mt('Delete?').'</label></span></td>'.
 2105:                 '<td colspan="2">'.
 2106:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="book_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2107:                 ('&nbsp;'x2).
 2108:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="book_title_'.$i.'" value="'.$title.'" /></span> '.
 2109:                 ('&nbsp;'x2).
 2110:                 '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="book_author_'.$i.'" value="'.$author.'" /></span> '.
 2111:                 ('&nbsp;'x2).
 2112:                 '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2113:             if ($image) {
 2114:                 $datatable .= '<span class="LC_nobreak">'.
 2115:                               $imgsrc.
 2116:                               '<label><input type="checkbox" name="book_image_del"'.
 2117:                               ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2118:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2119:             }
 2120:             if ($switchserver) {
 2121:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2122:             } else {
 2123:                 $datatable .= '<input type="file" name="book_image_'.$i.'" value="" />';
 2124:             }
 2125:             $datatable .= '<input type="hidden" name="book_id_'.$i.'" value="'.$key.'" /></span> '.
 2126:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2127:                           $coursetitle.'</span></td></tr>'."\n";
 2128:             $itemcount ++;
 2129:         }
 2130:     }
 2131:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2132:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'addbook_pos'".');"';
 2133:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2134:                   '<input type="hidden" name="book_maxnum" value="'.$maxnum.'" />'."\n".
 2135:                   '<select name="addbook_pos"'.$chgstr.'>';
 2136:     for (my $k=0; $k<$maxnum+1; $k++) {
 2137:         my $vpos = $k+1;
 2138:         my $selstr;
 2139:         if ($k == $maxnum) {
 2140:             $selstr = ' selected="selected" ';
 2141:         }
 2142:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2143:     }
 2144:     $datatable .= '</select>&nbsp;'."\n".
 2145:                   '<input type="checkbox" name="addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2146:                   '<td colspan="2">'.
 2147:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="addbook_subject" value="" /></span> '."\n".
 2148:                   ('&nbsp;'x2).
 2149:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="addbook_title" value="" /></span> '."\n".
 2150:                   ('&nbsp;'x2).
 2151:                   '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="addbook_author" value="" /></span> '."\n".
 2152:                   ('&nbsp;'x2).
 2153:                   '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2154:     if ($switchserver) {
 2155:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2156:     } else {
 2157:         $datatable .= '<input type="file" name="addbook_image" value="" />';
 2158:     }
 2159:     $datatable .= '</span>'."\n".
 2160:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2161:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},'addbook_cdom').
 2162:                   '<input type="text" size="25" name="addbook_cnum" value="" />'.
 2163:                   &Apache::loncommon::selectcourse_link
 2164:                       ('display','addbook_cnum','addbook_cdom',undef,undef,undef,'Course');
 2165:                   '</span></td>'."\n".
 2166:                   '</tr>'."\n";
 2167:     $itemcount ++;
 2168:     return $datatable;
 2169: }
 2170: 
 2171: sub textbookcourses_javascript {
 2172:     my ($textbooks) = @_;
 2173:     return unless(ref($textbooks) eq 'HASH');
 2174:     my $num = scalar(keys(%{$textbooks}));
 2175:     my @jsarray;
 2176:     foreach my $item (sort {$a <=> $b } (keys(%{$textbooks}))) {
 2177:         push(@jsarray,$textbooks->{$item});
 2178:     }
 2179:     my $jstext = '    var textbooks = Array('."'".join("','",@jsarray)."'".');'."\n";
 2180:     return <<"ENDSCRIPT";
 2181: <script type="text/javascript">
 2182: // <![CDATA[
 2183: function reorderBooks(form,item) {
 2184:     var changedVal;
 2185: $jstext 
 2186:     var newpos = 'addbook_pos';
 2187:     var current = new Array;
 2188:     var maxh = 1 + $num;
 2189:     var current = new Array;
 2190:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2191:     if (item == newpos) {
 2192:         changedVal = newitemVal;
 2193:     } else {
 2194:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2195:         current[newitemVal] = newpos;
 2196:     }
 2197:     for (var i=0; i<textbooks.length; i++) {
 2198:         var elementName = textbooks[i];
 2199:         if (elementName != item) {
 2200:             if (form.elements[elementName]) {
 2201:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2202:                 current[currVal] = elementName;
 2203:             }
 2204:         }
 2205:     }
 2206:     var oldVal;
 2207:     for (var j=0; j<maxh; j++) {
 2208:         if (current[j] == undefined) {
 2209:             oldVal = j;
 2210:         }
 2211:     }
 2212:     if (oldVal < changedVal) {
 2213:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2214:            var elementName = current[k];
 2215:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2216:         }
 2217:     } else {
 2218:         for (var k=changedVal; k<oldVal; k++) {
 2219:             var elementName = current[k];
 2220:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2221:         }
 2222:     }
 2223:     return;
 2224: }
 2225: 
 2226: // ]]>
 2227: </script>
 2228: 
 2229: ENDSCRIPT
 2230: }
 2231: 
 2232: sub print_autoenroll {
 2233:     my ($dom,$settings,$rowtotal) = @_;
 2234:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2235:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2236:     if (ref($settings) eq 'HASH') {
 2237:         if (exists($settings->{'run'})) {
 2238:             if ($settings->{'run'} eq '0') {
 2239:                 $runoff = ' checked="checked" ';
 2240:                 $runon = ' ';
 2241:             } else {
 2242:                 $runon = ' checked="checked" ';
 2243:                 $runoff = ' ';
 2244:             }
 2245:         } else {
 2246:             if ($autorun) {
 2247:                 $runon = ' checked="checked" ';
 2248:                 $runoff = ' ';
 2249:             } else {
 2250:                 $runoff = ' checked="checked" ';
 2251:                 $runon = ' ';
 2252:             }
 2253:         }
 2254:         if (exists($settings->{'co-owners'})) {
 2255:             if ($settings->{'co-owners'} eq '0') {
 2256:                 $coownersoff = ' checked="checked" ';
 2257:                 $coownerson = ' ';
 2258:             } else {
 2259:                 $coownerson = ' checked="checked" ';
 2260:                 $coownersoff = ' ';
 2261:             }
 2262:         } else {
 2263:             $coownersoff = ' checked="checked" ';
 2264:             $coownerson = ' ';
 2265:         }
 2266:         if (exists($settings->{'sender_domain'})) {
 2267:             $defdom = $settings->{'sender_domain'};
 2268:         }
 2269:     } else {
 2270:         if ($autorun) {
 2271:             $runon = ' checked="checked" ';
 2272:             $runoff = ' ';
 2273:         } else {
 2274:             $runoff = ' checked="checked" ';
 2275:             $runon = ' ';
 2276:         }
 2277:     }
 2278:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2279:     my $notif_sender;
 2280:     if (ref($settings) eq 'HASH') {
 2281:         $notif_sender = $settings->{'sender_uname'};
 2282:     }
 2283:     my $datatable='<tr class="LC_odd_row">'.
 2284:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2285:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2286:                   '<input type="radio" name="autoenroll_run"'.
 2287:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2288:                   '<label><input type="radio" name="autoenroll_run"'.
 2289:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2290:                   '</tr><tr>'.
 2291:                   '<td>'.&mt('Notification messages - sender').
 2292:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2293:                   &mt('username').':&nbsp;'.
 2294:                   '<input type="text" name="sender_uname" value="'.
 2295:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2296:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2297:                   '<tr class="LC_odd_row">'.
 2298:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2299:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2300:                   '<input type="radio" name="autoassign_coowners"'.
 2301:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2302:                   '<label><input type="radio" name="autoassign_coowners"'.
 2303:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2304:                   '</tr>';
 2305:     $$rowtotal += 3;
 2306:     return $datatable;
 2307: }
 2308: 
 2309: sub print_autoupdate {
 2310:     my ($position,$dom,$settings,$rowtotal) = @_;
 2311:     my $datatable;
 2312:     if ($position eq 'top') {
 2313:         my $updateon = ' ';
 2314:         my $updateoff = ' checked="checked" ';
 2315:         my $classlistson = ' ';
 2316:         my $classlistsoff = ' checked="checked" ';
 2317:         if (ref($settings) eq 'HASH') {
 2318:             if ($settings->{'run'} eq '1') {
 2319:                 $updateon = $updateoff;
 2320:                 $updateoff = ' ';
 2321:             }
 2322:             if ($settings->{'classlists'} eq '1') {
 2323:                 $classlistson = $classlistsoff;
 2324:                 $classlistsoff = ' ';
 2325:             }
 2326:         }
 2327:         my %title = (
 2328:                    run => 'Auto-update active?',
 2329:                    classlists => 'Update information in classlists?',
 2330:                     );
 2331:         $datatable = '<tr class="LC_odd_row">'. 
 2332:                   '<td>'.&mt($title{'run'}).'</td>'.
 2333:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2334:                   '<input type="radio" name="autoupdate_run"'.
 2335:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2336:                   '<label><input type="radio" name="autoupdate_run"'.
 2337:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2338:                   '</tr><tr>'.
 2339:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2340:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2341:                   '<label><input type="radio" name="classlists"'.
 2342:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2343:                   '<label><input type="radio" name="classlists"'.
 2344:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2345:                   '</tr>';
 2346:         $$rowtotal += 2;
 2347:     } elsif ($position eq 'middle') {
 2348:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2349:         my $numinrow = 3;
 2350:         my $locknamesettings;
 2351:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2352:                                      $dom,$numinrow,$othertitle,
 2353:                                     'lockablenames');
 2354:         $$rowtotal ++;
 2355:     } else {
 2356:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2357:         my @fields = ('lastname','firstname','middlename','generation',
 2358:                       'permanentemail','id');
 2359:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2360:         my $numrows = 0;
 2361:         if (ref($types) eq 'ARRAY') {
 2362:             if (@{$types} > 0) {
 2363:                 $datatable = 
 2364:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2365:                                          \@fields,$types,\$numrows);
 2366:                     $$rowtotal += @{$types}; 
 2367:             }
 2368:         }
 2369:         $datatable .= 
 2370:             &usertype_update_row($settings,{'default' => $othertitle},
 2371:                                  \%fieldtitles,\@fields,['default'],
 2372:                                  \$numrows);
 2373:         $$rowtotal ++;     
 2374:     }
 2375:     return $datatable;
 2376: }
 2377: 
 2378: sub print_autocreate {
 2379:     my ($dom,$settings,$rowtotal) = @_;
 2380:     my (%createon,%createoff,%currhash);
 2381:     my @types = ('xml','req');
 2382:     if (ref($settings) eq 'HASH') {
 2383:         foreach my $item (@types) {
 2384:             $createoff{$item} = ' checked="checked" ';
 2385:             $createon{$item} = ' ';
 2386:             if (exists($settings->{$item})) {
 2387:                 if ($settings->{$item}) {
 2388:                     $createon{$item} = ' checked="checked" ';
 2389:                     $createoff{$item} = ' ';
 2390:                 }
 2391:             }
 2392:         }
 2393:         if ($settings->{'xmldc'} ne '') {
 2394:             $currhash{$settings->{'xmldc'}} = 1;
 2395:         }
 2396:     } else {
 2397:         foreach my $item (@types) {
 2398:             $createoff{$item} = ' checked="checked" ';
 2399:             $createon{$item} = ' ';
 2400:         }
 2401:     }
 2402:     $$rowtotal += 2;
 2403:     my $numinrow = 2;
 2404:     my $datatable='<tr class="LC_odd_row">'.
 2405:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2406:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2407:                   '<input type="radio" name="autocreate_xml"'.
 2408:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2409:                   '<label><input type="radio" name="autocreate_xml"'.
 2410:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2411:                   '</td></tr><tr>'.
 2412:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2413:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2414:                   '<input type="radio" name="autocreate_req"'.
 2415:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2416:                   '<label><input type="radio" name="autocreate_req"'.
 2417:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2418:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2419:                                                    'autocreate_xmldc',%currhash);
 2420:     if ($numdc > 1) {
 2421:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2422:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2423:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2424:     } else {
 2425:         $datatable .= $dctable.'</td></tr>';
 2426:     }
 2427:     $$rowtotal += $rows;
 2428:     return $datatable;
 2429: }
 2430: 
 2431: sub print_directorysrch {
 2432:     my ($dom,$settings,$rowtotal) = @_;
 2433:     my $srchon = ' ';
 2434:     my $srchoff = ' checked="checked" ';
 2435:     my ($exacton,$containson,$beginson);
 2436:     my $localon = ' ';
 2437:     my $localoff = ' checked="checked" ';
 2438:     if (ref($settings) eq 'HASH') {
 2439:         if ($settings->{'available'} eq '1') {
 2440:             $srchon = $srchoff;
 2441:             $srchoff = ' ';
 2442:         }
 2443:         if ($settings->{'localonly'} eq '1') {
 2444:             $localon = $localoff;
 2445:             $localoff = ' ';
 2446:         }
 2447:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2448:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2449:                 if ($type eq 'exact') {
 2450:                     $exacton = ' checked="checked" ';
 2451:                 } elsif ($type eq 'contains') {
 2452:                     $containson = ' checked="checked" ';
 2453:                 } elsif ($type eq 'begins') {
 2454:                     $beginson = ' checked="checked" ';
 2455:                 }
 2456:             }
 2457:         } else {
 2458:             if ($settings->{'searchtypes'} eq 'exact') {
 2459:                 $exacton = ' checked="checked" ';
 2460:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2461:                 $containson = ' checked="checked" ';
 2462:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2463:                 $exacton = ' checked="checked" ';
 2464:                 $containson = ' checked="checked" ';
 2465:             }
 2466:         }
 2467:     }
 2468:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2469:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2470: 
 2471:     my $numinrow = 4;
 2472:     my $cansrchrow = 0;
 2473:     my $datatable='<tr class="LC_odd_row">'.
 2474:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2475:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2476:                   '<input type="radio" name="dirsrch_available"'.
 2477:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2478:                   '<label><input type="radio" name="dirsrch_available"'.
 2479:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2480:                   '</tr><tr>'.
 2481:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2482:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2483:                   '<input type="radio" name="dirsrch_localonly"'.
 2484:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2485:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2486:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2487:                   '</tr>';
 2488:     $$rowtotal += 2;
 2489:     if (ref($usertypes) eq 'HASH') {
 2490:         if (keys(%{$usertypes}) > 0) {
 2491:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2492:                                          $numinrow,$othertitle,'cansearch');
 2493:             $cansrchrow = 1;
 2494:         }
 2495:     }
 2496:     if ($cansrchrow) {
 2497:         $$rowtotal ++;
 2498:         $datatable .= '<tr>';
 2499:     } else {
 2500:         $datatable .= '<tr class="LC_odd_row">';
 2501:     }
 2502:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2503:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2504:     foreach my $title (@{$titleorder}) {
 2505:         if (defined($searchtitles->{$title})) {
 2506:             my $check = ' ';
 2507:             if (ref($settings) eq 'HASH') {
 2508:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2509:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2510:                         $check = ' checked="checked" ';
 2511:                     }
 2512:                 }
 2513:             }
 2514:             $datatable .= '<td class="LC_left_item">'.
 2515:                           '<span class="LC_nobreak"><label>'.
 2516:                           '<input type="checkbox" name="searchby" '.
 2517:                           'value="'.$title.'"'.$check.'/>'.
 2518:                           $searchtitles->{$title}.'</label></span></td>';
 2519:         }
 2520:     }
 2521:     $datatable .= '</tr></table></td></tr>';
 2522:     $$rowtotal ++;
 2523:     if ($cansrchrow) {
 2524:         $datatable .= '<tr class="LC_odd_row">';
 2525:     } else {
 2526:         $datatable .= '<tr>';
 2527:     }
 2528:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2529:                   '<td class="LC_left_item" colspan="2">'.
 2530:                   '<span class="LC_nobreak"><label>'.
 2531:                   '<input type="checkbox" name="searchtypes" '.
 2532:                   $exacton.' value="exact" />'.&mt('Exact match').
 2533:                   '</label>&nbsp;'.
 2534:                   '<label><input type="checkbox" name="searchtypes" '.
 2535:                   $beginson.' value="begins" />'.&mt('Begins with').
 2536:                   '</label>&nbsp;'.
 2537:                   '<label><input type="checkbox" name="searchtypes" '.
 2538:                   $containson.' value="contains" />'.&mt('Contains').
 2539:                   '</label></span></td></tr>';
 2540:     $$rowtotal ++;
 2541:     return $datatable;
 2542: }
 2543: 
 2544: sub print_contacts {
 2545:     my ($dom,$settings,$rowtotal) = @_;
 2546:     my $datatable;
 2547:     my @contacts = ('adminemail','supportemail');
 2548:     my (%checked,%to,%otheremails,%bccemails);
 2549:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2550:                     'requestsmail','updatesmail','idconflictsmail');
 2551:     foreach my $type (@mailings) {
 2552:         $otheremails{$type} = '';
 2553:     }
 2554:     $bccemails{'helpdeskmail'} = '';
 2555:     if (ref($settings) eq 'HASH') {
 2556:         foreach my $item (@contacts) {
 2557:             if (exists($settings->{$item})) {
 2558:                 $to{$item} = $settings->{$item};
 2559:             }
 2560:         }
 2561:         foreach my $type (@mailings) {
 2562:             if (exists($settings->{$type})) {
 2563:                 if (ref($settings->{$type}) eq 'HASH') {
 2564:                     foreach my $item (@contacts) {
 2565:                         if ($settings->{$type}{$item}) {
 2566:                             $checked{$type}{$item} = ' checked="checked" ';
 2567:                         }
 2568:                     }
 2569:                     $otheremails{$type} = $settings->{$type}{'others'};
 2570:                     if ($type eq 'helpdeskmail') {
 2571:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2572:                     }
 2573:                 }
 2574:             } elsif ($type eq 'lonstatusmail') {
 2575:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2576:             }
 2577:         }
 2578:     } else {
 2579:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2580:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2581:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2582:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2583:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2584:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2585:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2586:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2587:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2588:     }
 2589:     my ($titles,$short_titles) = &contact_titles();
 2590:     my $rownum = 0;
 2591:     my $css_class;
 2592:     foreach my $item (@contacts) {
 2593:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2594:         $datatable .= '<tr'.$css_class.'>'. 
 2595:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2596:                   '</span></td><td class="LC_right_item">'.
 2597:                   '<input type="text" name="'.$item.'" value="'.
 2598:                   $to{$item}.'" /></td></tr>';
 2599:         $rownum ++;
 2600:     }
 2601:     foreach my $type (@mailings) {
 2602:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2603:         $datatable .= '<tr'.$css_class.'>'.
 2604:                       '<td><span class="LC_nobreak">'.
 2605:                       $titles->{$type}.': </span></td>'.
 2606:                       '<td class="LC_left_item">'.
 2607:                       '<span class="LC_nobreak">';
 2608:         foreach my $item (@contacts) {
 2609:             $datatable .= '<label>'.
 2610:                           '<input type="checkbox" name="'.$type.'"'.
 2611:                           $checked{$type}{$item}.
 2612:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2613:                           '</label>&nbsp;';
 2614:         }
 2615:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2616:                       '<input type="text" name="'.$type.'_others" '.
 2617:                       'value="'.$otheremails{$type}.'"  />';
 2618:         if ($type eq 'helpdeskmail') {
 2619:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2620:                           '<input type="text" name="'.$type.'_bcc" '.
 2621:                           'value="'.$bccemails{$type}.'"  />';
 2622:         }
 2623:         $datatable .= '</td></tr>'."\n";
 2624:         $rownum ++;
 2625:     }
 2626:     my %choices;
 2627:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2628:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2629:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2630:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2631:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2632:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2633:     my @toggles = ('reporterrors','reportupdates');
 2634:     my %defaultchecked = ('reporterrors'  => 'on',
 2635:                           'reportupdates' => 'on');
 2636:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2637:                                                \%choices,$rownum);
 2638:     $datatable .= $reports;
 2639:     $$rowtotal += $rownum;
 2640:     return $datatable;
 2641: }
 2642: 
 2643: sub print_helpsettings {
 2644:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2645:     my ($datatable,$itemcount);
 2646:     $itemcount = 1;
 2647:     my (%choices,%defaultchecked,@toggles);
 2648:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2649:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2650:                                  &mt('LON-CAPA bug tracker'),600,500));
 2651:     %defaultchecked = ('submitbugs' => 'on');
 2652:     @toggles = ('submitbugs',);
 2653: 
 2654:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2655:                                                  \%choices,$itemcount);
 2656:     return $datatable;
 2657: }
 2658: 
 2659: sub radiobutton_prefs {
 2660:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2661:         $additional) = @_;
 2662:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2663:                    (ref($choices) eq 'HASH'));
 2664: 
 2665:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2666: 
 2667:     foreach my $item (@{$toggles}) {
 2668:         if ($defaultchecked->{$item} eq 'on') {
 2669:             $checkedon{$item} = ' checked="checked" ';
 2670:             $checkedoff{$item} = ' ';
 2671:         } elsif ($defaultchecked->{$item} eq 'off') {
 2672:             $checkedoff{$item} = ' checked="checked" ';
 2673:             $checkedon{$item} = ' ';
 2674:         }
 2675:     }
 2676:     if (ref($settings) eq 'HASH') {
 2677:         foreach my $item (@{$toggles}) {
 2678:             if ($settings->{$item} eq '1') {
 2679:                 $checkedon{$item} =  ' checked="checked" ';
 2680:                 $checkedoff{$item} = ' ';
 2681:             } elsif ($settings->{$item} eq '0') {
 2682:                 $checkedoff{$item} =  ' checked="checked" ';
 2683:                 $checkedon{$item} = ' ';
 2684:             }
 2685:         }
 2686:     }
 2687:     if ($onclick) {
 2688:         $onclick = ' onclick="'.$onclick.'"';
 2689:     }
 2690:     foreach my $item (@{$toggles}) {
 2691:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2692:         $datatable .=
 2693:             '<tr'.$css_class.'><td valign="top">'.
 2694:             '<span class="LC_nobreak">'.$choices->{$item}.
 2695:             '</span></td>'.
 2696:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2697:             '<label><input type="radio" name="'.
 2698:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2699:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2700:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2701:             '</span>'.$additional.
 2702:             '</td>'.
 2703:             '</tr>';
 2704:         $itemcount ++;
 2705:     }
 2706:     return ($datatable,$itemcount);
 2707: }
 2708: 
 2709: sub print_coursedefaults {
 2710:     my ($position,$dom,$settings,$rowtotal) = @_;
 2711:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2712:     my $itemcount = 1;
 2713:     my %choices =  &Apache::lonlocal::texthash (
 2714:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2715:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2716:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2717:         coursecredits        => 'Credits can be specified for courses',
 2718:     );
 2719:     my %staticdefaults = (
 2720:                            anonsurvey_threshold => 10,
 2721:                            uploadquota          => 500,
 2722:                          );
 2723:     if ($position eq 'top') {
 2724:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2725:         @toggles = ('canuse_pdfforms');
 2726:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2727:                                                  \%choices,$itemcount);
 2728:     } else {
 2729:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2730:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2731:             %curruploadquota);
 2732:         my $currusecredits = 0;
 2733:         my @types = ('official','unofficial','community','textbook');
 2734:         if (ref($settings) eq 'HASH') {
 2735:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2736:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2737:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2738:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2739:                 }
 2740:             }
 2741:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2742:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2743:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2744:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2745:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2746:                     ($def_textbook_credits ne '')) {
 2747:                     $currusecredits = 1;
 2748:                 }
 2749:             }
 2750:         }
 2751:         if (!$currdefresponder) {
 2752:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2753:         } elsif ($currdefresponder < 1) {
 2754:             $currdefresponder = 1;
 2755:         }
 2756:         foreach my $type (@types) {
 2757:             if ($curruploadquota{$type} eq '') {
 2758:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2759:             }
 2760:         }
 2761:         $datatable .=
 2762:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2763:                 $choices{'anonsurvey_threshold'}.
 2764:                 '</span></td>'.
 2765:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2766:                 '<input type="text" name="anonsurvey_threshold"'.
 2767:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2768:                 '</td></tr>'."\n";
 2769:         $itemcount ++;
 2770:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2771:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2772:                       $choices{'uploadquota'}.
 2773:                       '</span></td>'.
 2774:                       '<td align="right" class="LC_right_item">'.
 2775:                       '<table><tr>';
 2776:         foreach my $type (@types) {
 2777:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2778:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2779:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2780:         }
 2781:         $datatable .= '</tr></table></td></tr>'."\n";
 2782:         $itemcount ++;
 2783:         my $onclick = 'toggleCredits(this.form);';
 2784:         my $display = 'none';
 2785:         if ($currusecredits) {
 2786:             $display = 'block';
 2787:         }
 2788:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2789:                          '<span class="LC_nobreak">'.
 2790:                          &mt('Default credits for official courses [_1]',
 2791:                          '<input type="text" name="official_credits" value="'.
 2792:                          $def_official_credits.'" size="3" />').
 2793:                          '</span><br />'.
 2794:                          '<span class="LC_nobreak">'.
 2795:                          &mt('Default credits for unofficial courses [_1]',
 2796:                          '<input type="text" name="unofficial_credits" value="'.
 2797:                          $def_unofficial_credits.'" size="3" />').
 2798:                          '</span><br />'.
 2799:                          '<span class="LC_nobreak">'.
 2800:                          &mt('Default credits for textbook courses [_1]',
 2801:                          '<input type="text" name="textbook_credits" value="'.
 2802:                          $def_textbook_credits.'" size="3" />').
 2803:                          '</span></div>'."\n";
 2804:         %defaultchecked = ('coursecredits' => 'off');
 2805:         @toggles = ('coursecredits');
 2806:         my $current = {
 2807:                         'coursecredits' => $currusecredits,
 2808:                       };
 2809:         (my $table,$itemcount) =
 2810:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2811:                                \%choices,$itemcount,$onclick,$additional);
 2812:         $datatable .= $table;
 2813:         $itemcount ++;
 2814:     }
 2815:     $$rowtotal += $itemcount;
 2816:     return $datatable;
 2817: }
 2818: 
 2819: sub print_selfenrollment {
 2820:     my ($position,$dom,$settings,$rowtotal) = @_;
 2821:     my ($css_class,$datatable);
 2822:     my $itemcount = 1;
 2823:     my @types = ('official','unofficial','community','textbook');
 2824:     if (($position eq 'top') || ($position eq 'middle')) {
 2825:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 2826:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 2827:         my @rows;
 2828:         my $key;
 2829:         if ($position eq 'top') {
 2830:             $key = 'admin'; 
 2831:             if (ref($rowsref) eq 'ARRAY') {
 2832:                 @rows = @{$rowsref};
 2833:             }
 2834:         } elsif ($position eq 'middle') {
 2835:             $key = 'default';
 2836:             @rows = ('types','registered','approval','limit');
 2837:         }
 2838:         foreach my $row (@rows) {
 2839:             if (defined($titlesref->{$row})) {
 2840:                 $itemcount ++;
 2841:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2842:                 $datatable .= '<tr'.$css_class.'>'.
 2843:                               '<td>'.$titlesref->{$row}.'</td>'.
 2844:                               '<td class="LC_left_item">'.
 2845:                               '<table><tr>';
 2846:                 my (%current,%currentcap);
 2847:                 if (ref($settings) eq 'HASH') {
 2848:                     if (ref($settings->{$key}) eq 'HASH') {
 2849:                         foreach my $type (@types) {
 2850:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2851:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 2852:                             }
 2853:                             if (($row eq 'limit') && ($key eq 'default')) {
 2854:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2855:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 2856:                                 }
 2857:                             }
 2858:                         }
 2859:                     }
 2860:                 }
 2861:                 my %roles = (
 2862:                              '0' => &Apache::lonnet::plaintext('dc'),
 2863:                             ); 
 2864:             
 2865:                 foreach my $type (@types) {
 2866:                     unless (($row eq 'registered') && ($key eq 'default')) {
 2867:                         $datatable .= '<th>'.&mt($type).'</th>';
 2868:                     }
 2869:                 }
 2870:                 unless (($row eq 'registered') && ($key eq 'default')) {
 2871:                     $datatable .= '</tr><tr>';
 2872:                 }
 2873:                 foreach my $type (@types) {
 2874:                     if ($type eq 'community') {
 2875:                         $roles{'1'} = &mt('Community personnel');
 2876:                     } else {
 2877:                         $roles{'1'} = &mt('Course personnel');
 2878:                     }
 2879:                     $datatable .= '<td style="vertical-align: top">';
 2880:                     if ($position eq 'top') {
 2881:                         my %checked;
 2882:                         if ($current{$type} eq '0') {
 2883:                             $checked{'0'} = ' checked="checked"';
 2884:                         } else {
 2885:                             $checked{'1'} = ' checked="checked"';
 2886:                         }
 2887:                         foreach my $role ('1','0') {
 2888:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2889:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 2890:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 2891:                                           $roles{$role}.'</label></span> ';
 2892:                         }
 2893:                     } else {
 2894:                         if ($row eq 'types') {
 2895:                             my %checked;
 2896:                             if ($current{$type} =~ /^(all|dom)$/) {
 2897:                                 $checked{$1} = ' checked="checked"';
 2898:                             } else {
 2899:                                 $checked{''} = ' checked="checked"';
 2900:                             }
 2901:                             foreach my $val ('','dom','all') {
 2902:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2903:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2904:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2905:                             }
 2906:                         } elsif ($row eq 'registered') {
 2907:                             my %checked;
 2908:                             if ($current{$type} eq '1') {
 2909:                                 $checked{'1'} = ' checked="checked"';
 2910:                             } else {
 2911:                                 $checked{'0'} = ' checked="checked"';
 2912:                             }
 2913:                             foreach my $val ('0','1') {
 2914:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2915:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2916:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2917:                             }
 2918:                         } elsif ($row eq 'approval') {
 2919:                             my %checked;
 2920:                             if ($current{$type} =~ /^([12])$/) {
 2921:                                 $checked{$1} = ' checked="checked"';
 2922:                             } else {
 2923:                                 $checked{'0'} = ' checked="checked"';
 2924:                             }
 2925:                             for my $val (0..2) {
 2926:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2927:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2928:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2929:                             }
 2930:                         } elsif ($row eq 'limit') {
 2931:                             my %checked;
 2932:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 2933:                                 $checked{$1} = ' checked="checked"';
 2934:                             } else {
 2935:                                 $checked{'none'} = ' checked="checked"';
 2936:                             }
 2937:                             my $cap;
 2938:                             if ($currentcap{$type} =~ /^\d+$/) {
 2939:                                 $cap = $currentcap{$type};
 2940:                             }
 2941:                             foreach my $val ('none','allstudents','selfenrolled') {
 2942:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2943:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2944:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2945:                             }
 2946:                             $datatable .= '<br />'.
 2947:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 2948:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 2949:                                           '</span>'; 
 2950:                         }
 2951:                     }
 2952:                     $datatable .= '</td>';
 2953:                 }
 2954:                 $datatable .= '</tr>';
 2955:             }
 2956:             $datatable .= '</table></td></tr>';
 2957:         }
 2958:     } elsif ($position eq 'bottom') {
 2959:         my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 2960:         my %currvalidation;
 2961:         if (ref($settings) eq 'HASH') {
 2962:             if (ref($settings->{'validation'}) eq 'HASH') {
 2963:                 %currvalidation = %{$settings->{'validation'}}; 
 2964:             }
 2965:         }
 2966:         foreach my $item (@{$itemsref}) {
 2967:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2968:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2969:                           $namesref->{$item}.
 2970:                           '</span></td>'.
 2971:                           '<td class="LC_left_item">';
 2972:             if (($item eq 'url') || ($item eq 'button')) {
 2973:                 $datatable .= '<span class="LC_nobreak">'.
 2974:                               '<input type="text" name="selfenroll_validation_'.$item.'"'.
 2975:                               ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 2976:             } elsif ($item eq 'fields') {
 2977:                 my @currfields;
 2978:                 if (ref($currvalidation{$item}) eq 'ARRAY') {
 2979:                     @currfields = @{$currvalidation{$item}};
 2980:                 }
 2981:                 foreach my $field (@{$fieldsref}) {
 2982:                     my $check = '';
 2983:                     if (grep(/^\Q$field\E$/,@currfields)) {
 2984:                         $check = ' checked="checked"';
 2985:                     }
 2986:                     $datatable .= '<span class="LC_nobreak"><label>'.
 2987:                                   '<input type="checkbox" name="selfenroll_validation_fields"'.
 2988:                                   ' value="'.$field.'"'.$check.' />'.$field.
 2989:                                   '</label></span> ';
 2990:                 }
 2991:             } elsif ($item eq 'markup') {
 2992:                 $datatable .= '<textarea name="selfenroll_validation_markup" cols="50" rows="5" wrap="soft">'.
 2993:                               $currvalidation{$item}. 
 2994:                               '</textarea>';
 2995:             }
 2996:             $datatable .= '</td></tr>'."\n";
 2997:             $itemcount ++;
 2998:         }
 2999:     }
 3000:     $$rowtotal += $itemcount;
 3001:     return $datatable;
 3002: }
 3003: 
 3004: sub print_usersessions {
 3005:     my ($position,$dom,$settings,$rowtotal) = @_;
 3006:     my ($css_class,$datatable,%checked,%choices);
 3007:     my (%by_ip,%by_location,@intdoms);
 3008:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3009: 
 3010:     my @alldoms = &Apache::lonnet::all_domains();
 3011:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3012:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3013:     my %altids = &id_for_thisdom(%servers);
 3014:     my $itemcount = 1;
 3015:     if ($position eq 'top') {
 3016:         if (keys(%serverhomes) > 1) {
 3017:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3018:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 3019:         } else {
 3020:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3021:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3022:         }
 3023:     } else {
 3024:         if (keys(%by_location) == 0) {
 3025:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3026:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3027:         } else {
 3028:             my %lt = &usersession_titles();
 3029:             my $numinrow = 5;
 3030:             my $prefix;
 3031:             my @types;
 3032:             if ($position eq 'bottom') {
 3033:                 $prefix = 'remote';
 3034:                 @types = ('version','excludedomain','includedomain');
 3035:             } else {
 3036:                 $prefix = 'hosted';
 3037:                 @types = ('excludedomain','includedomain');
 3038:             }
 3039:             my (%current,%checkedon,%checkedoff);
 3040:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3041:             my @locations = sort(keys(%by_location));
 3042:             foreach my $type (@types) {
 3043:                 $checkedon{$type} = '';
 3044:                 $checkedoff{$type} = ' checked="checked"';
 3045:             }
 3046:             if (ref($settings) eq 'HASH') {
 3047:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3048:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3049:                         $current{$key} = $settings->{$prefix}{$key};
 3050:                         if ($key eq 'version') {
 3051:                             if ($current{$key} ne '') {
 3052:                                 $checkedon{$key} = ' checked="checked"';
 3053:                                 $checkedoff{$key} = '';
 3054:                             }
 3055:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3056:                             $checkedon{$key} = ' checked="checked"';
 3057:                             $checkedoff{$key} = '';
 3058:                         }
 3059:                     }
 3060:                 }
 3061:             }
 3062:             foreach my $type (@types) {
 3063:                 next if ($type ne 'version' && !@locations);
 3064:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3065:                 $datatable .= '<tr'.$css_class.'>
 3066:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3067:                                <span class="LC_nobreak">&nbsp;
 3068:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3069:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3070:                 if ($type eq 'version') {
 3071:                     my $selector = '<select name="'.$prefix.'_version">';
 3072:                     foreach my $version (@lcversions) {
 3073:                         my $selected = '';
 3074:                         if ($current{'version'} eq $version) {
 3075:                             $selected = ' selected="selected"';
 3076:                         }
 3077:                         $selector .= ' <option value="'.$version.'"'.
 3078:                                      $selected.'>'.$version.'</option>';
 3079:                     }
 3080:                     $selector .= '</select> ';
 3081:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3082:                 } else {
 3083:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3084:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3085:                                  ' />'.('&nbsp;'x2).
 3086:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3087:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3088:                                  "\n".
 3089:                                  '</div><div><table>';
 3090:                     my $rem;
 3091:                     for (my $i=0; $i<@locations; $i++) {
 3092:                         my ($showloc,$value,$checkedtype);
 3093:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3094:                             my $ip = $by_location{$locations[$i]}->[0];
 3095:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3096:                                  $value = join(':',@{$by_ip{$ip}});
 3097:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3098:                                 if (ref($current{$type}) eq 'ARRAY') {
 3099:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3100:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3101:                                             $checkedtype = ' checked="checked"';
 3102:                                             last;
 3103:                                         }
 3104:                                     }
 3105:                                 }
 3106:                             }
 3107:                         }
 3108:                         $rem = $i%($numinrow);
 3109:                         if ($rem == 0) {
 3110:                             if ($i > 0) {
 3111:                                 $datatable .= '</tr>';
 3112:                             }
 3113:                             $datatable .= '<tr>';
 3114:                         }
 3115:                         $datatable .= '<td class="LC_left_item">'.
 3116:                                       '<span class="LC_nobreak"><label>'.
 3117:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3118:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3119:                                       '</label></span></td>';
 3120:                     }
 3121:                     $rem = @locations%($numinrow);
 3122:                     my $colsleft = $numinrow - $rem;
 3123:                     if ($colsleft > 1 ) {
 3124:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3125:                                       '&nbsp;</td>';
 3126:                     } elsif ($colsleft == 1) {
 3127:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3128:                     }
 3129:                     $datatable .= '</tr></table>';
 3130:                 }
 3131:                 $datatable .= '</td></tr>';
 3132:                 $itemcount ++;
 3133:             }
 3134:         }
 3135:     }
 3136:     $$rowtotal += $itemcount;
 3137:     return $datatable;
 3138: }
 3139: 
 3140: sub build_location_hashes {
 3141:     my ($intdoms,$by_ip,$by_location) = @_;
 3142:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3143:                   (ref($by_location) eq 'HASH')); 
 3144:     my %iphost = &Apache::lonnet::get_iphost();
 3145:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3146:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3147:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3148:         foreach my $id (@{$iphost{$primary_ip}}) {
 3149:             my $intdom = &Apache::lonnet::internet_dom($id);
 3150:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3151:                 push(@{$intdoms},$intdom);
 3152:             }
 3153:         }
 3154:     }
 3155:     foreach my $ip (keys(%iphost)) {
 3156:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3157:             foreach my $id (@{$iphost{$ip}}) {
 3158:                 my $location = &Apache::lonnet::internet_dom($id);
 3159:                 if ($location) {
 3160:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3161:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3162:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3163:                             push(@{$by_ip->{$ip}},$location);
 3164:                         }
 3165:                     } else {
 3166:                         $by_ip->{$ip} = [$location];
 3167:                     }
 3168:                 }
 3169:             }
 3170:         }
 3171:     }
 3172:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3173:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3174:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3175:             my $first = $by_ip->{$ip}->[0];
 3176:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3177:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3178:                     push(@{$by_location->{$first}},$ip);
 3179:                 }
 3180:             } else {
 3181:                 $by_location->{$first} = [$ip];
 3182:             }
 3183:         }
 3184:     }
 3185:     return;
 3186: }
 3187: 
 3188: sub current_offloads_to {
 3189:     my ($dom,$settings,$servers) = @_;
 3190:     my (%spareid,%otherdomconfigs);
 3191:     if (ref($servers) eq 'HASH') {
 3192:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3193:             my $gotspares;
 3194:             if (ref($settings) eq 'HASH') {
 3195:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3196:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3197:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3198:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3199:                         $gotspares = 1;
 3200:                     }
 3201:                 }
 3202:             }
 3203:             unless ($gotspares) {
 3204:                 my $gotspares;
 3205:                 my $serverhomeID =
 3206:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3207:                 my $serverhomedom =
 3208:                     &Apache::lonnet::host_domain($serverhomeID);
 3209:                 if ($serverhomedom ne $dom) {
 3210:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3211:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3212:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3213:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3214:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3215:                                 $gotspares = 1;
 3216:                             }
 3217:                         }
 3218:                     } else {
 3219:                         $otherdomconfigs{$serverhomedom} =
 3220:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3221:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3222:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3223:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3224:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3225:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3226:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3227:                                         $gotspares = 1;
 3228:                                     }
 3229:                                 }
 3230:                             }
 3231:                         }
 3232:                     }
 3233:                 }
 3234:             }
 3235:             unless ($gotspares) {
 3236:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3237:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3238:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3239:                } else {
 3240:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3241:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3242:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3243:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3244:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3245:                     } else {
 3246:                         my %what = (
 3247:                              spareid => 1,
 3248:                         );
 3249:                         my ($result,$returnhash) = 
 3250:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3251:                         if ($result eq 'ok') { 
 3252:                             if (ref($returnhash) eq 'HASH') {
 3253:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3254:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3255:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3256:                                 }
 3257:                             }
 3258:                         }
 3259:                     }
 3260:                 }
 3261:             }
 3262:         }
 3263:     }
 3264:     return %spareid;
 3265: }
 3266: 
 3267: sub spares_row {
 3268:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3269:     my $css_class;
 3270:     my $numinrow = 4;
 3271:     my $itemcount = 1;
 3272:     my $datatable;
 3273:     my %typetitles = &sparestype_titles();
 3274:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3275:         foreach my $server (sort(keys(%{$servers}))) {
 3276:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3277:             my ($othercontrol,$serverdom);
 3278:             if ($serverhome ne $server) {
 3279:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3280:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3281:             } else {
 3282:                 $serverdom = &Apache::lonnet::host_domain($server);
 3283:                 if ($serverdom ne $dom) {
 3284:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3285:                 }
 3286:             }
 3287:             next unless (ref($spareid->{$server}) eq 'HASH');
 3288:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3289:             $datatable .= '<tr'.$css_class.'>
 3290:                            <td rowspan="2">
 3291:                             <span class="LC_nobreak">'.
 3292:                           &mt('[_1] when busy, offloads to:'
 3293:                               ,'<b>'.$server.'</b>').
 3294:                           "\n";
 3295:             my (%current,%canselect);
 3296:             my @choices = 
 3297:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3298:             foreach my $type ('primary','default') {
 3299:                 if (ref($spareid->{$server}) eq 'HASH') {
 3300:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3301:                         my @spares = @{$spareid->{$server}{$type}};
 3302:                         if (@spares > 0) {
 3303:                             if ($othercontrol) {
 3304:                                 $current{$type} = join(', ',@spares);
 3305:                             } else {
 3306:                                 $current{$type} .= '<table>';
 3307:                                 my $numspares = scalar(@spares);
 3308:                                 for (my $i=0;  $i<@spares; $i++) {
 3309:                                     my $rem = $i%($numinrow);
 3310:                                     if ($rem == 0) {
 3311:                                         if ($i > 0) {
 3312:                                             $current{$type} .= '</tr>';
 3313:                                         }
 3314:                                         $current{$type} .= '<tr>';
 3315:                                     }
 3316:                                     $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;'.
 3317:                                                        $spareid->{$server}{$type}[$i].
 3318:                                                        '</label></td>'."\n";
 3319:                                 }
 3320:                                 my $rem = @spares%($numinrow);
 3321:                                 my $colsleft = $numinrow - $rem;
 3322:                                 if ($colsleft > 1 ) {
 3323:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3324:                                                        '" class="LC_left_item">'.
 3325:                                                        '&nbsp;</td>';
 3326:                                 } elsif ($colsleft == 1) {
 3327:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3328:                                 }
 3329:                                 $current{$type} .= '</tr></table>';
 3330:                             }
 3331:                         }
 3332:                     }
 3333:                     if ($current{$type} eq '') {
 3334:                         $current{$type} = &mt('None specified');
 3335:                     }
 3336:                     if ($othercontrol) {
 3337:                         if ($type eq 'primary') {
 3338:                             $canselect{$type} = $othercontrol;
 3339:                         }
 3340:                     } else {
 3341:                         $canselect{$type} = 
 3342:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3343:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3344:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3345:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3346:                         if (@choices > 0) {
 3347:                             foreach my $lonhost (@choices) {
 3348:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3349:                             }
 3350:                         }
 3351:                         $canselect{$type} .= '</select>'."\n";
 3352:                     }
 3353:                 } else {
 3354:                     $current{$type} = &mt('Could not be determined');
 3355:                     if ($type eq 'primary') {
 3356:                         $canselect{$type} =  $othercontrol;
 3357:                     }
 3358:                 }
 3359:                 if ($type eq 'default') {
 3360:                     $datatable .= '<tr'.$css_class.'>';
 3361:                 }
 3362:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3363:                               '<td>'.$current{$type}.'</td>'."\n".
 3364:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3365:             }
 3366:             $itemcount ++;
 3367:         }
 3368:     }
 3369:     $$rowtotal += $itemcount;
 3370:     return $datatable;
 3371: }
 3372: 
 3373: sub possible_newspares {
 3374:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3375:     my $serverhostname = &Apache::lonnet::hostname($server);
 3376:     my %excluded;
 3377:     if ($serverhostname ne '') {
 3378:         %excluded = (
 3379:                        $serverhostname => 1,
 3380:                     );
 3381:     }
 3382:     if (ref($currspares) eq 'HASH') {
 3383:         foreach my $type (keys(%{$currspares})) {
 3384:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3385:                 if (@{$currspares->{$type}} > 0) {
 3386:                     foreach my $curr (@{$currspares->{$type}}) {
 3387:                         my $hostname = &Apache::lonnet::hostname($curr);
 3388:                         $excluded{$hostname} = 1;
 3389:                     }
 3390:                 }
 3391:             }
 3392:         }
 3393:     }
 3394:     my @choices;
 3395:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3396:         if (keys(%{$serverhomes}) > 1) {
 3397:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3398:                 unless ($excluded{$name}) {
 3399:                     if (exists($altids->{$serverhomes->{$name}})) {
 3400:                         push(@choices,$altids->{$serverhomes->{$name}});
 3401:                     } else {
 3402:                         push(@choices,$serverhomes->{$name});
 3403:                     }
 3404:                 }
 3405:             }
 3406:         }
 3407:     }
 3408:     return sort(@choices);
 3409: }
 3410: 
 3411: sub print_loadbalancing {
 3412:     my ($dom,$settings,$rowtotal) = @_;
 3413:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3414:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3415:     my $numinrow = 1;
 3416:     my $datatable;
 3417:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3418:     my (%currbalancer,%currtargets,%currrules,%existing);
 3419:     if (ref($settings) eq 'HASH') {
 3420:         %existing = %{$settings};
 3421:     }
 3422:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3423:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3424:                                   \%currtargets,\%currrules);
 3425:     } else {
 3426:         return;
 3427:     }
 3428:     my ($othertitle,$usertypes,$types) =
 3429:         &Apache::loncommon::sorted_inst_types($dom);
 3430:     my $rownum = 8;
 3431:     if (ref($types) eq 'ARRAY') {
 3432:         $rownum += scalar(@{$types});
 3433:     }
 3434:     my @css_class = ('LC_odd_row','LC_even_row');
 3435:     my $balnum = 0;
 3436:     my $islast;
 3437:     my (@toshow,$disabledtext);
 3438:     if (keys(%currbalancer) > 0) {
 3439:         @toshow = sort(keys(%currbalancer));
 3440:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3441:             push(@toshow,'');
 3442:         }
 3443:     } else {
 3444:         @toshow = ('');
 3445:         $disabledtext = &mt('No existing load balancer');
 3446:     }
 3447:     foreach my $lonhost (@toshow) {
 3448:         if ($balnum == scalar(@toshow)-1) {
 3449:             $islast = 1;
 3450:         } else {
 3451:             $islast = 0;
 3452:         }
 3453:         my $cssidx = $balnum%2;
 3454:         my $targets_div_style = 'display: none';
 3455:         my $disabled_div_style = 'display: block';
 3456:         my $homedom_div_style = 'display: none';
 3457:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3458:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3459:                       '<p>';
 3460:         if ($lonhost eq '') {
 3461:             $datatable .= '<span class="LC_nobreak">';
 3462:             if (keys(%currbalancer) > 0) {
 3463:                 $datatable .= &mt('Add balancer:');
 3464:             } else {
 3465:                 $datatable .= &mt('Enable balancer:');
 3466:             }
 3467:             $datatable .= '&nbsp;'.
 3468:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3469:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3470:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3471:                           '<option value="" selected="selected">'.&mt('None').
 3472:                           '</option>'."\n";
 3473:             foreach my $server (sort(keys(%servers))) {
 3474:                 next if ($currbalancer{$server});
 3475:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3476:             }
 3477:             $datatable .=
 3478:                 '</select>'."\n".
 3479:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3480:         } else {
 3481:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3482:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3483:                            &mt('Stop balancing').'</label>'.
 3484:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3485:             $targets_div_style = 'display: block';
 3486:             $disabled_div_style = 'display: none';
 3487:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3488:                 $homedom_div_style = 'display: block';
 3489:             }
 3490:         }
 3491:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3492:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3493:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3494:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3495:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3496:         my @sparestypes = ('primary','default');
 3497:         my %typetitles = &sparestype_titles();
 3498:         foreach my $sparetype (@sparestypes) {
 3499:             my $targettable;
 3500:             for (my $i=0; $i<$numspares; $i++) {
 3501:                 my $checked;
 3502:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3503:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3504:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3505:                             $checked = ' checked="checked"';
 3506:                         }
 3507:                     }
 3508:                 }
 3509:                 my ($chkboxval,$disabled);
 3510:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3511:                     $chkboxval = $spares[$i];
 3512:                 }
 3513:                 if (exists($currbalancer{$spares[$i]})) {
 3514:                     $disabled = ' disabled="disabled"';
 3515:                 }
 3516:                 $targettable .=
 3517:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3518:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3519:                     '</span></label></td>';
 3520:                 my $rem = $i%($numinrow);
 3521:                 if ($rem == 0) {
 3522:                     if (($i > 0) && ($i < $numspares-1)) {
 3523:                         $targettable .= '</tr>';
 3524:                     }
 3525:                     if ($i < $numspares-1) {
 3526:                         $targettable .= '<tr>';
 3527:                     }
 3528:                 }
 3529:             }
 3530:             if ($targettable ne '') {
 3531:                 my $rem = $numspares%($numinrow);
 3532:                 my $colsleft = $numinrow - $rem;
 3533:                 if ($colsleft > 1 ) {
 3534:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3535:                                     '&nbsp;</td>';
 3536:                 } elsif ($colsleft == 1) {
 3537:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3538:                 }
 3539:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3540:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3541:             }
 3542:         }
 3543:         $datatable .= '</div></td></tr>'.
 3544:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3545:                                            $othertitle,$usertypes,$types,\%servers,
 3546:                                            \%currbalancer,$lonhost,
 3547:                                            $targets_div_style,$homedom_div_style,
 3548:                                            $css_class[$cssidx],$balnum,$islast);
 3549:         $$rowtotal += $rownum;
 3550:         $balnum ++;
 3551:     }
 3552:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3553:     return $datatable;
 3554: }
 3555: 
 3556: sub get_loadbalancers_config {
 3557:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3558:     return unless ((ref($servers) eq 'HASH') &&
 3559:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3560:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3561:     if (keys(%{$existing}) > 0) {
 3562:         my $oldlonhost;
 3563:         foreach my $key (sort(keys(%{$existing}))) {
 3564:             if ($key eq 'lonhost') {
 3565:                 $oldlonhost = $existing->{'lonhost'};
 3566:                 $currbalancer->{$oldlonhost} = 1;
 3567:             } elsif ($key eq 'targets') {
 3568:                 if ($oldlonhost) {
 3569:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3570:                 }
 3571:             } elsif ($key eq 'rules') {
 3572:                 if ($oldlonhost) {
 3573:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3574:                 }
 3575:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3576:                 $currbalancer->{$key} = 1;
 3577:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3578:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3579:             }
 3580:         }
 3581:     } else {
 3582:         my ($balancerref,$targetsref) =
 3583:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3584:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3585:             foreach my $server (sort(keys(%{$balancerref}))) {
 3586:                 $currbalancer->{$server} = 1;
 3587:                 $currtargets->{$server} = $targetsref->{$server};
 3588:             }
 3589:         }
 3590:     }
 3591:     return;
 3592: }
 3593: 
 3594: sub loadbalancing_rules {
 3595:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3596:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3597:         $css_class,$balnum,$islast) = @_;
 3598:     my $output;
 3599:     my $num = 0;
 3600:     my ($alltypes,$othertypes,$titles) =
 3601:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3602:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3603:         foreach my $type (@{$alltypes}) {
 3604:             $num ++;
 3605:             my $current;
 3606:             if (ref($currrules) eq 'HASH') {
 3607:                 $current = $currrules->{$type};
 3608:             }
 3609:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3610:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3611:                     $current = '';
 3612:                 }
 3613:             }
 3614:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3615:                                              $servers,$currbalancer,$lonhost,$dom,
 3616:                                              $targets_div_style,$homedom_div_style,
 3617:                                              $css_class,$balnum,$num,$islast);
 3618:         }
 3619:     }
 3620:     return $output;
 3621: }
 3622: 
 3623: sub loadbalancing_titles {
 3624:     my ($dom,$intdom,$usertypes,$types) = @_;
 3625:     my %othertypes = (
 3626:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3627:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3628:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3629:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3630:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3631:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3632:                      );
 3633:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3634:     if (ref($types) eq 'ARRAY') {
 3635:         unshift(@alltypes,@{$types},'default');
 3636:     }
 3637:     my %titles;
 3638:     foreach my $type (@alltypes) {
 3639:         if ($type =~ /^_LC_/) {
 3640:             $titles{$type} = $othertypes{$type};
 3641:         } elsif ($type eq 'default') {
 3642:             $titles{$type} = &mt('All users from [_1]',$dom);
 3643:             if (ref($types) eq 'ARRAY') {
 3644:                 if (@{$types} > 0) {
 3645:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3646:                 }
 3647:             }
 3648:         } elsif (ref($usertypes) eq 'HASH') {
 3649:             $titles{$type} = $usertypes->{$type};
 3650:         }
 3651:     }
 3652:     return (\@alltypes,\%othertypes,\%titles);
 3653: }
 3654: 
 3655: sub loadbalance_rule_row {
 3656:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3657:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3658:     my @rulenames;
 3659:     my %ruletitles = &offloadtype_text();
 3660:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3661:         @rulenames = ('balancer','offloadedto');
 3662:     } else {
 3663:         @rulenames = ('default','homeserver');
 3664:         if ($type eq '_LC_external') {
 3665:             push(@rulenames,'externalbalancer');
 3666:         } else {
 3667:             push(@rulenames,'specific');
 3668:         }
 3669:         push(@rulenames,'none');
 3670:     }
 3671:     my $style = $targets_div_style;
 3672:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3673:         $style = $homedom_div_style;
 3674:     }
 3675:     my $space;
 3676:     if ($islast && $num == 1) {
 3677:         $space = '<div display="inline-block">&nbsp;</div>';
 3678:     }
 3679:     my $output =
 3680:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3681:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3682:         '<td valaign="top">'.$space.
 3683:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3684:     for (my $i=0; $i<@rulenames; $i++) {
 3685:         my $rule = $rulenames[$i];
 3686:         my ($checked,$extra);
 3687:         if ($rulenames[$i] eq 'default') {
 3688:             $rule = '';
 3689:         }
 3690:         if ($rulenames[$i] eq 'specific') {
 3691:             if (ref($servers) eq 'HASH') {
 3692:                 my $default;
 3693:                 if (($current ne '') && (exists($servers->{$current}))) {
 3694:                     $checked = ' checked="checked"';
 3695:                 }
 3696:                 unless ($checked) {
 3697:                     $default = ' selected="selected"';
 3698:                 }
 3699:                 $extra =
 3700:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3701:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3702:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3703:                     '<option value=""'.$default.'></option>'."\n";
 3704:                 foreach my $server (sort(keys(%{$servers}))) {
 3705:                     if (ref($currbalancer) eq 'HASH') {
 3706:                         next if (exists($currbalancer->{$server}));
 3707:                     }
 3708:                     my $selected;
 3709:                     if ($server eq $current) {
 3710:                         $selected = ' selected="selected"';
 3711:                     }
 3712:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3713:                 }
 3714:                 $extra .= '</select>';
 3715:             }
 3716:         } elsif ($rule eq $current) {
 3717:             $checked = ' checked="checked"';
 3718:         }
 3719:         $output .= '<span class="LC_nobreak"><label>'.
 3720:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3721:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3722:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3723:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3724:                    '</label>'.$extra.'</span><br />'."\n";
 3725:     }
 3726:     $output .= '</div></td></tr>'."\n";
 3727:     return $output;
 3728: }
 3729: 
 3730: sub offloadtype_text {
 3731:     my %ruletitles = &Apache::lonlocal::texthash (
 3732:            'default'          => 'Offloads to default destinations',
 3733:            'homeserver'       => "Offloads to user's home server",
 3734:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3735:            'specific'         => 'Offloads to specific server',
 3736:            'none'             => 'No offload',
 3737:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3738:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3739:     );
 3740:     return %ruletitles;
 3741: }
 3742: 
 3743: sub sparestype_titles {
 3744:     my %typestitles = &Apache::lonlocal::texthash (
 3745:                           'primary' => 'primary',
 3746:                           'default' => 'default',
 3747:                       );
 3748:     return %typestitles;
 3749: }
 3750: 
 3751: sub contact_titles {
 3752:     my %titles = &Apache::lonlocal::texthash (
 3753:                    'supportemail' => 'Support E-mail address',
 3754:                    'adminemail'   => 'Default Server Admin E-mail address',
 3755:                    'errormail'    => 'Error reports to be e-mailed to',
 3756:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3757:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3758:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3759:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3760:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3761:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3762:                  );
 3763:     my %short_titles = &Apache::lonlocal::texthash (
 3764:                            adminemail   => 'Admin E-mail address',
 3765:                            supportemail => 'Support E-mail',
 3766:                        );   
 3767:     return (\%titles,\%short_titles);
 3768: }
 3769: 
 3770: sub tool_titles {
 3771:     my %titles = &Apache::lonlocal::texthash (
 3772:                      aboutme    => 'Personal web page',
 3773:                      blog       => 'Blog',
 3774:                      webdav     => 'WebDAV',
 3775:                      portfolio  => 'Portfolio',
 3776:                      official   => 'Official courses (with institutional codes)',
 3777:                      unofficial => 'Unofficial courses',
 3778:                      community  => 'Communities',
 3779:                      textbook   => 'Textbook courses',
 3780:                  );
 3781:     return %titles;
 3782: }
 3783: 
 3784: sub courserequest_titles {
 3785:     my %titles = &Apache::lonlocal::texthash (
 3786:                                    official   => 'Official',
 3787:                                    unofficial => 'Unofficial',
 3788:                                    community  => 'Communities',
 3789:                                    textbook   => 'Textbook',
 3790:                                    norequest  => 'Not allowed',
 3791:                                    approval   => 'Approval by Dom. Coord.',
 3792:                                    validate   => 'With validation',
 3793:                                    autolimit  => 'Numerical limit',
 3794:                                    unlimited  => '(blank for unlimited)',
 3795:                  );
 3796:     return %titles;
 3797: }
 3798: 
 3799: sub authorrequest_titles {
 3800:     my %titles = &Apache::lonlocal::texthash (
 3801:                                    norequest  => 'Not allowed',
 3802:                                    approval   => 'Approval by Dom. Coord.',
 3803:                                    automatic  => 'Automatic approval',
 3804:                  );
 3805:     return %titles;
 3806: }
 3807: 
 3808: sub courserequest_conditions {
 3809:     my %conditions = &Apache::lonlocal::texthash (
 3810:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3811:        validate   => '(Processing of request subject to institutional validation).',
 3812:                  );
 3813:     return %conditions;
 3814: }
 3815: 
 3816: 
 3817: sub print_usercreation {
 3818:     my ($position,$dom,$settings,$rowtotal) = @_;
 3819:     my $numinrow = 4;
 3820:     my $datatable;
 3821:     if ($position eq 'top') {
 3822:         $$rowtotal ++;
 3823:         my $rowcount = 0;
 3824:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3825:         if (ref($rules) eq 'HASH') {
 3826:             if (keys(%{$rules}) > 0) {
 3827:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3828:                                                 $ruleorder,$numinrow,$rowcount);
 3829:                 $$rowtotal ++;
 3830:                 $rowcount ++;
 3831:             }
 3832:         }
 3833:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3834:         if (ref($idrules) eq 'HASH') {
 3835:             if (keys(%{$idrules}) > 0) {
 3836:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3837:                                                 $idruleorder,$numinrow,$rowcount);
 3838:                 $$rowtotal ++;
 3839:                 $rowcount ++;
 3840:             }
 3841:         }
 3842:         if ($rowcount == 0) {
 3843:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3844:             $$rowtotal ++;
 3845:             $rowcount ++;
 3846:         }
 3847:     } elsif ($position eq 'middle') {
 3848:         my @creators = ('author','course','requestcrs');
 3849:         my ($rules,$ruleorder) =
 3850:             &Apache::lonnet::inst_userrules($dom,'username');
 3851:         my %lt = &usercreation_types();
 3852:         my %checked;
 3853:         if (ref($settings) eq 'HASH') {
 3854:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3855:                 foreach my $item (@creators) {
 3856:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3857:                 }
 3858:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3859:                 foreach my $item (@creators) {
 3860:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3861:                         $checked{$item} = 'none';
 3862:                     }
 3863:                 }
 3864:             }
 3865:         }
 3866:         my $rownum = 0;
 3867:         foreach my $item (@creators) {
 3868:             $rownum ++;
 3869:             if ($checked{$item} eq '') {
 3870:                 $checked{$item} = 'any';
 3871:             }
 3872:             my $css_class;
 3873:             if ($rownum%2) {
 3874:                 $css_class = '';
 3875:             } else {
 3876:                 $css_class = ' class="LC_odd_row" ';
 3877:             }
 3878:             $datatable .= '<tr'.$css_class.'>'.
 3879:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3880:                          '</span></td><td align="right">';
 3881:             my @options = ('any');
 3882:             if (ref($rules) eq 'HASH') {
 3883:                 if (keys(%{$rules}) > 0) {
 3884:                     push(@options,('official','unofficial'));
 3885:                 }
 3886:             }
 3887:             push(@options,'none');
 3888:             foreach my $option (@options) {
 3889:                 my $type = 'radio';
 3890:                 my $check = ' ';
 3891:                 if ($checked{$item} eq $option) {
 3892:                     $check = ' checked="checked" ';
 3893:                 } 
 3894:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3895:                               '<input type="'.$type.'" name="can_createuser_'.
 3896:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3897:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3898:             }
 3899:             $datatable .= '</td></tr>';
 3900:         }
 3901:     } else {
 3902:         my @contexts = ('author','course','domain');
 3903:         my @authtypes = ('int','krb4','krb5','loc');
 3904:         my %checked;
 3905:         if (ref($settings) eq 'HASH') {
 3906:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3907:                 foreach my $item (@contexts) {
 3908:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3909:                         foreach my $auth (@authtypes) {
 3910:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3911:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3912:                             }
 3913:                         }
 3914:                     }
 3915:                 }
 3916:             }
 3917:         } else {
 3918:             foreach my $item (@contexts) {
 3919:                 foreach my $auth (@authtypes) {
 3920:                     $checked{$item}{$auth} = ' checked="checked" ';
 3921:                 }
 3922:             }
 3923:         }
 3924:         my %title = &context_names();
 3925:         my %authname = &authtype_names();
 3926:         my $rownum = 0;
 3927:         my $css_class; 
 3928:         foreach my $item (@contexts) {
 3929:             if ($rownum%2) {
 3930:                 $css_class = '';
 3931:             } else {
 3932:                 $css_class = ' class="LC_odd_row" ';
 3933:             }
 3934:             $datatable .=   '<tr'.$css_class.'>'.
 3935:                             '<td>'.$title{$item}.
 3936:                             '</td><td class="LC_left_item">'.
 3937:                             '<span class="LC_nobreak">';
 3938:             foreach my $auth (@authtypes) {
 3939:                 $datatable .= '<label>'. 
 3940:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3941:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3942:                               $authname{$auth}.'</label>&nbsp;';
 3943:             }
 3944:             $datatable .= '</span></td></tr>';
 3945:             $rownum ++;
 3946:         }
 3947:         $$rowtotal += $rownum;
 3948:     }
 3949:     return $datatable;
 3950: }
 3951: 
 3952: sub print_selfcreation {
 3953:     my ($position,$dom,$settings,$rowtotal) = @_;
 3954:     my (@selfcreate,$createsettings,$datatable);
 3955:     if (ref($settings) eq 'HASH') {
 3956:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 3957:             $createsettings = $settings->{'cancreate'};
 3958:             if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3959:                 @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3960:             } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3961:                 if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3962:                     @selfcreate = ('email','login','sso');
 3963:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3964:                     @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3965:                 }
 3966:             }
 3967:         }
 3968:     }
 3969:     my %radiohash;
 3970:     my $numinrow = 4;
 3971:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 3972:     if ($position eq 'top') {
 3973:         my %choices = &Apache::lonlocal::texthash (
 3974:                                                       cancreate_login      => 'Institutional Login',
 3975:                                                       cancreate_sso        => 'Institutional Single Sign On',
 3976:                                                   );
 3977:         my @toggles = sort(keys(%choices));
 3978:         my %defaultchecked = (
 3979:                                'cancreate_login' => 'off',
 3980:                                'cancreate_sso'   => 'off',
 3981:                              );
 3982:         my ($onclick,$itemcount);
 3983:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 3984:                                                      \%choices,$itemcount,$onclick);
 3985:         $$rowtotal += $itemcount;
 3986: 
 3987:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3988: 
 3989:         if (ref($usertypes) eq 'HASH') {
 3990:             if (keys(%{$usertypes}) > 0) {
 3991:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3992:                                              $dom,$numinrow,$othertitle,
 3993:                                              'statustocreate',$$rowtotal);
 3994:                 $$rowtotal ++;
 3995:             }
 3996:         }
 3997:     } elsif ($position eq 'middle') {
 3998:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 3999:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4000:         $usertypes->{'default'} = $othertitle;
 4001:         if (ref($types) eq 'ARRAY') {
 4002:             push(@{$types},'default');
 4003:             $usertypes->{'default'} = $othertitle;
 4004:             foreach my $status (@{$types}) {
 4005:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4006:                                                        $numinrow,$$rowtotal,$usertypes);
 4007:                 $$rowtotal ++;
 4008:             }
 4009:         }
 4010:     } else {
 4011:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4012:         my %choices =
 4013:             &Apache::lonlocal::texthash(
 4014:                                           email         => 'Approved automatically',
 4015:                                           emailapproval => 'Queued for approval by DC',
 4016:                                           off           => 'Not enabled',
 4017:                                        );
 4018:         $datatable .= '<tr'.$css_class.'>'.
 4019:                       '<td>'.&mt('E-mail address as username').
 4020:                       '</td><td class="LC_left_item">'.
 4021:                       '<span class="LC_nobreak">';
 4022:         foreach my $option ('email','emailapproval','off') {
 4023:             my $checked;
 4024:             if ($option eq 'email') {
 4025:                 if ($radiohash{'cancreate_email'}) {
 4026:                     $checked = 'checked="checked"';
 4027:                 }
 4028:             } elsif ($option eq 'emailapproval') {
 4029:                 if ($radiohash{'cancreate_emailapproval'}) {
 4030:                     $checked = 'checked="checked"';
 4031:                 }
 4032:             } else {
 4033:                 if ((!$radiohash{'cancreate_email'}) && (!$radiohash{'cancreate_emailapproval'})) {
 4034:                     $checked = 'checked="checked"';  
 4035:                 }
 4036:             }
 4037:             $datatable .= '<label>'.
 4038:                           '<input type="radio" name="cancreate_email" '.
 4039:                           $checked.' value="'.$option.'" />'.
 4040:                           $choices{$option}.'</label>&nbsp;';
 4041:         }
 4042:         $$rowtotal ++;
 4043:         $datatable .= '</span></td></tr>'.
 4044:                       &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4045:         $$rowtotal ++;
 4046:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4047:         $numinrow = 1;
 4048:         my ($othertitle,$usertypes,$types) =  &Apache::loncommon::sorted_inst_types($dom);
 4049:         $usertypes->{'default'} = $othertitle;
 4050:         if (ref($types) eq 'ARRAY') {
 4051:             push(@{$types},'default');
 4052:             $usertypes->{'default'} = $othertitle;
 4053:             foreach my $status (@{$types}) {
 4054:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4055:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4056:                 $$rowtotal ++;
 4057:             }
 4058:         }
 4059:         my ($emailrules,$emailruleorder) =
 4060:             &Apache::lonnet::inst_userrules($dom,'email');
 4061:         if (ref($emailrules) eq 'HASH') {
 4062:             if (keys(%{$emailrules}) > 0) {
 4063:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4064:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4065:                 $$rowtotal ++;
 4066:             }
 4067:         }
 4068:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4069:     }
 4070:     return $datatable;
 4071: }
 4072: 
 4073: sub captcha_choice {
 4074:     my ($context,$settings,$itemcount) = @_;
 4075:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4076:     my %lt = &captcha_phrases();
 4077:     $keyentry = 'hidden';
 4078:     if ($context eq 'cancreate') {
 4079:         $rowname = &mt('CAPTCHA validation');
 4080:     } elsif ($context eq 'login') {
 4081:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4082:     }
 4083:     if (ref($settings) eq 'HASH') {
 4084:         if ($settings->{'captcha'}) {
 4085:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4086:         } else {
 4087:             $checked{'original'} = ' checked="checked"';
 4088:         }
 4089:         if ($settings->{'captcha'} eq 'recaptcha') {
 4090:             $pubtext = $lt{'pub'};
 4091:             $privtext = $lt{'priv'};
 4092:             $keyentry = 'text';
 4093:         }
 4094:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4095:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4096:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4097:         }
 4098:     } else {
 4099:         $checked{'original'} = ' checked="checked"';
 4100:     }
 4101:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4102:     my $output = '<tr'.$css_class.'>'.
 4103:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4104:                  '<table><tr><td>'."\n";
 4105:     foreach my $option ('original','recaptcha','notused') {
 4106:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4107:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4108:                    $lt{$option}.'</label></span>';
 4109:         unless ($option eq 'notused') {
 4110:             $output .= ('&nbsp;'x2)."\n";
 4111:         }
 4112:     }
 4113: #
 4114: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4115: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4116: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4117: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4118: #
 4119:     $output .= '</td></tr>'."\n".
 4120:                '<tr><td>'."\n".
 4121:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4122:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4123:                $currpub.'" size="40" /></span><br />'."\n".
 4124:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4125:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4126:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4127:                '</td></tr>';
 4128:     return $output;
 4129: }
 4130: 
 4131: sub user_formats_row {
 4132:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4133:     my $output;
 4134:     my %text = (
 4135:                    'username' => 'new usernames',
 4136:                    'id'       => 'IDs',
 4137:                    'email'    => 'self-created accounts (e-mail)',
 4138:                );
 4139:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4140:     $output = '<tr '.$css_class.'>'.
 4141:               '<td><span class="LC_nobreak">';
 4142:     if ($type eq 'email') {
 4143:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4144:     } else {
 4145:         $output .= &mt("Format rules to check for $text{$type}: ");
 4146:     }
 4147:     $output .= '</span></td>'.
 4148:                '<td class="LC_left_item" colspan="2"><table>';
 4149:     my $rem;
 4150:     if (ref($ruleorder) eq 'ARRAY') {
 4151:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4152:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4153:                 my $rem = $i%($numinrow);
 4154:                 if ($rem == 0) {
 4155:                     if ($i > 0) {
 4156:                         $output .= '</tr>';
 4157:                     }
 4158:                     $output .= '<tr>';
 4159:                 }
 4160:                 my $check = ' ';
 4161:                 if (ref($settings) eq 'HASH') {
 4162:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4163:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4164:                             $check = ' checked="checked" ';
 4165:                         }
 4166:                     }
 4167:                 }
 4168:                 $output .= '<td class="LC_left_item">'.
 4169:                            '<span class="LC_nobreak"><label>'.
 4170:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4171:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4172:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4173:             }
 4174:         }
 4175:         $rem = @{$ruleorder}%($numinrow);
 4176:     }
 4177:     my $colsleft = $numinrow - $rem;
 4178:     if ($colsleft > 1 ) {
 4179:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4180:                    '&nbsp;</td>';
 4181:     } elsif ($colsleft == 1) {
 4182:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4183:     }
 4184:     $output .= '</tr></table></td></tr>';
 4185:     return $output;
 4186: }
 4187: 
 4188: sub usercreation_types {
 4189:     my %lt = &Apache::lonlocal::texthash (
 4190:                     author     => 'When adding a co-author',
 4191:                     course     => 'When adding a user to a course',
 4192:                     requestcrs => 'When requesting a course',
 4193:                     any        => 'Any',
 4194:                     official   => 'Institutional only ',
 4195:                     unofficial => 'Non-institutional only',
 4196:                     none       => 'None',
 4197:     );
 4198:     return %lt;
 4199: }
 4200: 
 4201: sub selfcreation_types {
 4202:     my %lt = &Apache::lonlocal::texthash (
 4203:                     selfcreate => 'User creates own account',
 4204:                     any        => 'Any',
 4205:                     official   => 'Institutional only ',
 4206:                     unofficial => 'Non-institutional only',
 4207:                     email      => 'E-mail address',
 4208:                     login      => 'Institutional Login',
 4209:                     sso        => 'SSO',
 4210:              );
 4211: }
 4212: 
 4213: sub authtype_names {
 4214:     my %lt = &Apache::lonlocal::texthash(
 4215:                       int    => 'Internal',
 4216:                       krb4   => 'Kerberos 4',
 4217:                       krb5   => 'Kerberos 5',
 4218:                       loc    => 'Local',
 4219:                   );
 4220:     return %lt;
 4221: }
 4222: 
 4223: sub context_names {
 4224:     my %context_title = &Apache::lonlocal::texthash(
 4225:        author => 'Creating users when an Author',
 4226:        course => 'Creating users when in a course',
 4227:        domain => 'Creating users when a Domain Coordinator',
 4228:     );
 4229:     return %context_title;
 4230: }
 4231: 
 4232: sub print_usermodification {
 4233:     my ($position,$dom,$settings,$rowtotal) = @_;
 4234:     my $numinrow = 4;
 4235:     my ($context,$datatable,$rowcount);
 4236:     if ($position eq 'top') {
 4237:         $rowcount = 0;
 4238:         $context = 'author'; 
 4239:         foreach my $role ('ca','aa') {
 4240:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4241:                                                    $numinrow,$rowcount);
 4242:             $$rowtotal ++;
 4243:             $rowcount ++;
 4244:         }
 4245:     } elsif ($position eq 'bottom') {
 4246:         $context = 'course';
 4247:         $rowcount = 0;
 4248:         foreach my $role ('st','ep','ta','in','cr') {
 4249:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4250:                                                    $numinrow,$rowcount);
 4251:             $$rowtotal ++;
 4252:             $rowcount ++;
 4253:         }
 4254:     }
 4255:     return $datatable;
 4256: }
 4257: 
 4258: sub print_defaults {
 4259:     my ($dom,$settings,$rowtotal) = @_;
 4260:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4261:                  'datelocale_def','portal_def');
 4262:     my %defaults;
 4263:     if (ref($settings) eq 'HASH') {
 4264:         %defaults = %{$settings};
 4265:     } else {
 4266:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4267:         foreach my $item (@items) {
 4268:             $defaults{$item} = $domdefaults{$item};
 4269:         }
 4270:     }
 4271:     my $titles = &defaults_titles($dom);
 4272:     my $rownum = 0;
 4273:     my ($datatable,$css_class);
 4274:     foreach my $item (@items) {
 4275:         if ($rownum%2) {
 4276:             $css_class = '';
 4277:         } else {
 4278:             $css_class = ' class="LC_odd_row" ';
 4279:         }
 4280:         $datatable .= '<tr'.$css_class.'>'.
 4281:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 4282:                   '</span></td><td class="LC_right_item">';
 4283:         if ($item eq 'auth_def') {
 4284:             my @authtypes = ('internal','krb4','krb5','localauth');
 4285:             my %shortauth = (
 4286:                              internal => 'int',
 4287:                              krb4 => 'krb4',
 4288:                              krb5 => 'krb5',
 4289:                              localauth  => 'loc'
 4290:                            );
 4291:             my %authnames = &authtype_names();
 4292:             foreach my $auth (@authtypes) {
 4293:                 my $checked = ' ';
 4294:                 if ($defaults{$item} eq $auth) {
 4295:                     $checked = ' checked="checked" ';
 4296:                 }
 4297:                 $datatable .= '<label><input type="radio" name="'.$item.
 4298:                               '" value="'.$auth.'"'.$checked.'/>'.
 4299:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4300:             }
 4301:         } elsif ($item eq 'timezone_def') {
 4302:             my $includeempty = 1;
 4303:             $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4304:         } elsif ($item eq 'datelocale_def') {
 4305:             my $includeempty = 1;
 4306:             $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4307:         } elsif ($item eq 'lang_def') {
 4308:             my %langchoices = &get_languages_hash();
 4309:             $langchoices{''} = 'No language preference';
 4310:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4311:             $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4312:                                                           \%langchoices);
 4313:         } else {
 4314:             my $size;
 4315:             if ($item eq 'portal_def') {
 4316:                 $size = ' size="25"';
 4317:             }
 4318:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 4319:                           $defaults{$item}.'"'.$size.' />';
 4320:         }
 4321:         $datatable .= '</td></tr>';
 4322:         $rownum ++;
 4323:     }
 4324:     $$rowtotal += $rownum;
 4325:     return $datatable;
 4326: }
 4327: 
 4328: sub get_languages_hash {
 4329:     my %langchoices;
 4330:     foreach my $id (&Apache::loncommon::languageids()) {
 4331:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4332:         if ($code ne '') {
 4333:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4334:         }
 4335:     }
 4336:     return %langchoices;
 4337: }
 4338: 
 4339: sub defaults_titles {
 4340:     my ($dom) = @_;
 4341:     my %titles = &Apache::lonlocal::texthash (
 4342:                    'auth_def'      => 'Default authentication type',
 4343:                    'auth_arg_def'  => 'Default authentication argument',
 4344:                    'lang_def'      => 'Default language',
 4345:                    'timezone_def'  => 'Default timezone',
 4346:                    'datelocale_def' => 'Default locale for dates',
 4347:                    'portal_def'     => 'Portal/Default URL',
 4348:                  );
 4349:     if ($dom) {
 4350:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4351:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4352:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4353:         $protocol = 'http' if ($protocol ne 'https');
 4354:         if ($uint_dom) {
 4355:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4356:                                          $uint_dom);
 4357:         }
 4358:     }
 4359:     return (\%titles);
 4360: }
 4361: 
 4362: sub print_scantronformat {
 4363:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4364:     my $itemcount = 1;
 4365:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4366:         %confhash);
 4367:     my $switchserver = &check_switchserver($dom,$confname);
 4368:     my %lt = &Apache::lonlocal::texthash (
 4369:                 default => 'Default bubblesheet format file error',
 4370:                 custom  => 'Custom bubblesheet format file error',
 4371:              );
 4372:     my %scantronfiles = (
 4373:         default => 'default.tab',
 4374:         custom => 'custom.tab',
 4375:     );
 4376:     foreach my $key (keys(%scantronfiles)) {
 4377:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4378:                               .$scantronfiles{$key};
 4379:     }
 4380:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4381:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4382:         if (!$switchserver) {
 4383:             my $servadm = $r->dir_config('lonAdmEMail');
 4384:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4385:             if ($configuserok eq 'ok') {
 4386:                 if ($author_ok eq 'ok') {
 4387:                     my %legacyfile = (
 4388:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4389:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4390:                     );
 4391:                     my %md5chk;
 4392:                     foreach my $type (keys(%legacyfile)) {
 4393:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4394:                         chomp($md5chk{$type});
 4395:                     }
 4396:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4397:                         foreach my $type (keys(%legacyfile)) {
 4398:                             ($scantronurls{$type},my $error) = 
 4399:                                 &legacy_scantronformat($r,$dom,$confname,
 4400:                                                  $type,$legacyfile{$type},
 4401:                                                  $scantronurls{$type},
 4402:                                                  $scantronfiles{$type});
 4403:                             if ($error ne '') {
 4404:                                 $error{$type} = $error;
 4405:                             }
 4406:                         }
 4407:                         if (keys(%error) == 0) {
 4408:                             $is_custom = 1;
 4409:                             $confhash{'scantron'}{'scantronformat'} = 
 4410:                                 $scantronurls{'custom'};
 4411:                             my $putresult = 
 4412:                                 &Apache::lonnet::put_dom('configuration',
 4413:                                                          \%confhash,$dom);
 4414:                             if ($putresult ne 'ok') {
 4415:                                 $error{'custom'} = 
 4416:                                     '<span class="LC_error">'.
 4417:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4418:                             }
 4419:                         }
 4420:                     } else {
 4421:                         ($scantronurls{'default'},my $error) =
 4422:                             &legacy_scantronformat($r,$dom,$confname,
 4423:                                           'default',$legacyfile{'default'},
 4424:                                           $scantronurls{'default'},
 4425:                                           $scantronfiles{'default'});
 4426:                         if ($error eq '') {
 4427:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4428:                             my $putresult =
 4429:                                 &Apache::lonnet::put_dom('configuration',
 4430:                                                          \%confhash,$dom);
 4431:                             if ($putresult ne 'ok') {
 4432:                                 $error{'default'} =
 4433:                                     '<span class="LC_error">'.
 4434:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4435:                             }
 4436:                         } else {
 4437:                             $error{'default'} = $error;
 4438:                         }
 4439:                     }
 4440:                 }
 4441:             }
 4442:         } else {
 4443:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4444:         }
 4445:     }
 4446:     if (ref($settings) eq 'HASH') {
 4447:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4448:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4449:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4450:                 $scantronurl = '';
 4451:             } else {
 4452:                 $scantronurl = $settings->{'scantronformat'};
 4453:             }
 4454:             $is_custom = 1;
 4455:         } else {
 4456:             $scantronurl = $scantronurls{'default'};
 4457:         }
 4458:     } else {
 4459:         if ($is_custom) {
 4460:             $scantronurl = $scantronurls{'custom'};
 4461:         } else {
 4462:             $scantronurl = $scantronurls{'default'};
 4463:         }
 4464:     }
 4465:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4466:     $datatable .= '<tr'.$css_class.'>';
 4467:     if (!$is_custom) {
 4468:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4469:                       '<span class="LC_nobreak">';
 4470:         if ($scantronurl) {
 4471:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4472:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4473:         } else {
 4474:             $datatable = &mt('File unavailable for display');
 4475:         }
 4476:         $datatable .= '</span></td>';
 4477:         if (keys(%error) == 0) { 
 4478:             $datatable .= '<td valign="bottom">';
 4479:             if (!$switchserver) {
 4480:                 $datatable .= &mt('Upload:').'<br />';
 4481:             }
 4482:         } else {
 4483:             my $errorstr;
 4484:             foreach my $key (sort(keys(%error))) {
 4485:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4486:             }
 4487:             $datatable .= '<td>'.$errorstr;
 4488:         }
 4489:     } else {
 4490:         if (keys(%error) > 0) {
 4491:             my $errorstr;
 4492:             foreach my $key (sort(keys(%error))) {
 4493:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4494:             } 
 4495:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4496:         } elsif ($scantronurl) {
 4497:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4498:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4499:             $datatable .= '<td><span class="LC_nobreak">'.
 4500:                           $link.
 4501:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4502:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4503:                           '<td><span class="LC_nobreak">&nbsp;'.
 4504:                           &mt('Replace:').'</span><br />';
 4505:         }
 4506:     }
 4507:     if (keys(%error) == 0) {
 4508:         if ($switchserver) {
 4509:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4510:         } else {
 4511:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4512:                          '<input type="file" name="scantronformat" /></span>';
 4513:         }
 4514:     }
 4515:     $datatable .= '</td></tr>';
 4516:     $$rowtotal ++;
 4517:     return $datatable;
 4518: }
 4519: 
 4520: sub legacy_scantronformat {
 4521:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4522:     my ($url,$error);
 4523:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4524:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4525:         (my $result,$url) =
 4526:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4527:                          '','',$newfile);
 4528:         if ($result ne 'ok') {
 4529:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4530:         }
 4531:     }
 4532:     return ($url,$error);
 4533: }
 4534: 
 4535: sub print_coursecategories {
 4536:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4537:     my $datatable;
 4538:     if ($position eq 'top') {
 4539:         my $toggle_cats_crs = ' ';
 4540:         my $toggle_cats_dom = ' checked="checked" ';
 4541:         my $can_cat_crs = ' ';
 4542:         my $can_cat_dom = ' checked="checked" ';
 4543:         my $toggle_catscomm_comm = ' ';
 4544:         my $toggle_catscomm_dom = ' checked="checked" ';
 4545:         my $can_catcomm_comm = ' ';
 4546:         my $can_catcomm_dom = ' checked="checked" ';
 4547: 
 4548:         if (ref($settings) eq 'HASH') {
 4549:             if ($settings->{'togglecats'} eq 'crs') {
 4550:                 $toggle_cats_crs = $toggle_cats_dom;
 4551:                 $toggle_cats_dom = ' ';
 4552:             }
 4553:             if ($settings->{'categorize'} eq 'crs') {
 4554:                 $can_cat_crs = $can_cat_dom;
 4555:                 $can_cat_dom = ' ';
 4556:             }
 4557:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4558:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4559:                 $toggle_catscomm_dom = ' ';
 4560:             }
 4561:             if ($settings->{'categorizecomm'} eq 'comm') {
 4562:                 $can_catcomm_comm = $can_catcomm_dom;
 4563:                 $can_catcomm_dom = ' ';
 4564:             }
 4565:         }
 4566:         my %title = &Apache::lonlocal::texthash (
 4567:                      togglecats     => 'Show/Hide a course in catalog',
 4568:                      togglecatscomm => 'Show/Hide a community in catalog',
 4569:                      categorize     => 'Assign a category to a course',
 4570:                      categorizecomm => 'Assign a category to a community',
 4571:                     );
 4572:         my %level = &Apache::lonlocal::texthash (
 4573:                      dom  => 'Set in Domain',
 4574:                      crs  => 'Set in Course',
 4575:                      comm => 'Set in Community',
 4576:                     );
 4577:         $datatable = '<tr class="LC_odd_row">'.
 4578:                   '<td>'.$title{'togglecats'}.'</td>'.
 4579:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4580:                   '<input type="radio" name="togglecats"'.
 4581:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4582:                   '<label><input type="radio" name="togglecats"'.
 4583:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4584:                   '</tr><tr>'.
 4585:                   '<td>'.$title{'categorize'}.'</td>'.
 4586:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4587:                   '<label><input type="radio" name="categorize"'.
 4588:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4589:                   '<label><input type="radio" name="categorize"'.
 4590:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4591:                   '</tr><tr class="LC_odd_row">'.
 4592:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4593:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4594:                   '<input type="radio" name="togglecatscomm"'.
 4595:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4596:                   '<label><input type="radio" name="togglecatscomm"'.
 4597:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4598:                   '</tr><tr>'.
 4599:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4600:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4601:                   '<label><input type="radio" name="categorizecomm"'.
 4602:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4603:                   '<label><input type="radio" name="categorizecomm"'.
 4604:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4605:                   '</tr>';
 4606:         $$rowtotal += 4;
 4607:     } else {
 4608:         my $css_class;
 4609:         my $itemcount = 1;
 4610:         my $cathash; 
 4611:         if (ref($settings) eq 'HASH') {
 4612:             $cathash = $settings->{'cats'};
 4613:         }
 4614:         if (ref($cathash) eq 'HASH') {
 4615:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4616:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4617:                                                    \%allitems,\%idx,\@jsarray);
 4618:             my $maxdepth = scalar(@cats);
 4619:             my $colattrib = '';
 4620:             if ($maxdepth > 2) {
 4621:                 $colattrib = ' colspan="2" ';
 4622:             }
 4623:             my @path;
 4624:             if (@cats > 0) {
 4625:                 if (ref($cats[0]) eq 'ARRAY') {
 4626:                     my $numtop = @{$cats[0]};
 4627:                     my $maxnum = $numtop;
 4628:                     my %default_names = (
 4629:                           instcode    => &mt('Official courses'),
 4630:                           communities => &mt('Communities'),
 4631:                     );
 4632: 
 4633:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4634:                         ($cathash->{'instcode::0'} eq '') ||
 4635:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4636:                         ($cathash->{'communities::0'} eq '')) {
 4637:                         $maxnum ++;
 4638:                     }
 4639:                     my $lastidx;
 4640:                     for (my $i=0; $i<$numtop; $i++) {
 4641:                         my $parent = $cats[0][$i];
 4642:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4643:                         my $item = &escape($parent).'::0';
 4644:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4645:                         $lastidx = $idx{$item};
 4646:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4647:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4648:                         for (my $k=0; $k<=$maxnum; $k++) {
 4649:                             my $vpos = $k+1;
 4650:                             my $selstr;
 4651:                             if ($k == $i) {
 4652:                                 $selstr = ' selected="selected" ';
 4653:                             }
 4654:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4655:                         }
 4656:                         $datatable .= '</select></span></td><td>';
 4657:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4658:                             $datatable .=  '<span class="LC_nobreak">'
 4659:                                            .$default_names{$parent}.'</span>';
 4660:                             if ($parent eq 'instcode') {
 4661:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4662:                                               .&mt('with institutional codes')
 4663:                                               .')</span></td><td'.$colattrib.'>';
 4664:                             } else {
 4665:                                 $datatable .= '<table><tr><td>';
 4666:                             }
 4667:                             $datatable .= '<span class="LC_nobreak">'
 4668:                                           .'<label><input type="radio" name="'
 4669:                                           .$parent.'" value="1" checked="checked" />'
 4670:                                           .&mt('Display').'</label>';
 4671:                             if ($parent eq 'instcode') {
 4672:                                 $datatable .= '&nbsp;';
 4673:                             } else {
 4674:                                 $datatable .= '</span></td></tr><tr><td>'
 4675:                                               .'<span class="LC_nobreak">';
 4676:                             }
 4677:                             $datatable .= '<label><input type="radio" name="'
 4678:                                           .$parent.'" value="0" />'
 4679:                                           .&mt('Do not display').'</label></span>';
 4680:                             if ($parent eq 'communities') {
 4681:                                 $datatable .= '</td></tr></table>';
 4682:                             }
 4683:                             $datatable .= '</td>';
 4684:                         } else {
 4685:                             $datatable .= $parent
 4686:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 4687:                                           .'<input type="checkbox" name="deletecategory" '
 4688:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4689:                         }
 4690:                         my $depth = 1;
 4691:                         push(@path,$parent);
 4692:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4693:                         pop(@path);
 4694:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4695:                         $itemcount ++;
 4696:                     }
 4697:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4698:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4699:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4700:                     for (my $k=0; $k<=$maxnum; $k++) {
 4701:                         my $vpos = $k+1;
 4702:                         my $selstr;
 4703:                         if ($k == $numtop) {
 4704:                             $selstr = ' selected="selected" ';
 4705:                         }
 4706:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4707:                     }
 4708:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4709:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4710:                                   .'</tr>'."\n";
 4711:                     $itemcount ++;
 4712:                     foreach my $default ('instcode','communities') {
 4713:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4714:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4715:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4716:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4717:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4718:                             for (my $k=0; $k<=$maxnum; $k++) {
 4719:                                 my $vpos = $k+1;
 4720:                                 my $selstr;
 4721:                                 if ($k == $maxnum) {
 4722:                                     $selstr = ' selected="selected" ';
 4723:                                 }
 4724:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4725:                             }
 4726:                             $datatable .= '</select></span></td>'.
 4727:                                           '<td><span class="LC_nobreak">'.
 4728:                                           $default_names{$default}.'</span>';
 4729:                             if ($default eq 'instcode') {
 4730:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4731:                                               .&mt('with institutional codes').')</span>';
 4732:                             }
 4733:                             $datatable .= '</td>'
 4734:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4735:                                           .&mt('Display').'</label>&nbsp;'
 4736:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4737:                                           .&mt('Do not display').'</label></span></td></tr>';
 4738:                         }
 4739:                     }
 4740:                 }
 4741:             } else {
 4742:                 $datatable .= &initialize_categories($itemcount);
 4743:             }
 4744:         } else {
 4745:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4746:                           .&initialize_categories($itemcount);
 4747:         }
 4748:         $$rowtotal += $itemcount;
 4749:     }
 4750:     return $datatable;
 4751: }
 4752: 
 4753: sub print_serverstatuses {
 4754:     my ($dom,$settings,$rowtotal) = @_;
 4755:     my $datatable;
 4756:     my @pages = &serverstatus_pages();
 4757:     my (%namedaccess,%machineaccess);
 4758:     foreach my $type (@pages) {
 4759:         $namedaccess{$type} = '';
 4760:         $machineaccess{$type}= '';
 4761:     }
 4762:     if (ref($settings) eq 'HASH') {
 4763:         foreach my $type (@pages) {
 4764:             if (exists($settings->{$type})) {
 4765:                 if (ref($settings->{$type}) eq 'HASH') {
 4766:                     foreach my $key (keys(%{$settings->{$type}})) {
 4767:                         if ($key eq 'namedusers') {
 4768:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4769:                         } elsif ($key eq 'machines') {
 4770:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4771:                         }
 4772:                     }
 4773:                 }
 4774:             }
 4775:         }
 4776:     }
 4777:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4778:     my $rownum = 0;
 4779:     my $css_class;
 4780:     foreach my $type (@pages) {
 4781:         $rownum ++;
 4782:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4783:         $datatable .= '<tr'.$css_class.'>'.
 4784:                       '<td><span class="LC_nobreak">'.
 4785:                       $titles->{$type}.'</span></td>'.
 4786:                       '<td class="LC_left_item">'.
 4787:                       '<input type="text" name="'.$type.'_namedusers" '.
 4788:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4789:                       '<td class="LC_right_item">'.
 4790:                       '<span class="LC_nobreak">'.
 4791:                       '<input type="text" name="'.$type.'_machines" '.
 4792:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4793:                       '</td></tr>'."\n";
 4794:     }
 4795:     $$rowtotal += $rownum;
 4796:     return $datatable;
 4797: }
 4798: 
 4799: sub serverstatus_pages {
 4800:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4801:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4802:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 4803:             'uniquecodes','diskusage');
 4804: }
 4805: 
 4806: sub coursecategories_javascript {
 4807:     my ($settings) = @_;
 4808:     my ($output,$jstext,$cathash);
 4809:     if (ref($settings) eq 'HASH') {
 4810:         $cathash = $settings->{'cats'};
 4811:     }
 4812:     if (ref($cathash) eq 'HASH') {
 4813:         my (@cats,@jsarray,%idx);
 4814:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4815:         if (@jsarray > 0) {
 4816:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4817:             for (my $i=0; $i<@jsarray; $i++) {
 4818:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4819:                     my $catstr = join('","',@{$jsarray[$i]});
 4820:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4821:                 }
 4822:             }
 4823:         }
 4824:     } else {
 4825:         $jstext  = '    var categories = Array(1);'."\n".
 4826:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4827:     }
 4828:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4829:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4830:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4831:     $output = <<"ENDSCRIPT";
 4832: <script type="text/javascript">
 4833: // <![CDATA[
 4834: function reorderCats(form,parent,item,idx) {
 4835:     var changedVal;
 4836: $jstext
 4837:     var newpos = 'addcategory_pos';
 4838:     var current = new Array;
 4839:     if (parent == '') {
 4840:         var has_instcode = 0;
 4841:         var maxtop = categories[idx].length;
 4842:         for (var j=0; j<maxtop; j++) {
 4843:             if (categories[idx][j] == 'instcode::0') {
 4844:                 has_instcode == 1;
 4845:             }
 4846:         }
 4847:         if (has_instcode == 0) {
 4848:             categories[idx][maxtop] = 'instcode_pos';
 4849:         }
 4850:     } else {
 4851:         newpos += '_'+parent;
 4852:     }
 4853:     var maxh = 1 + categories[idx].length;
 4854:     var current = new Array;
 4855:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4856:     if (item == newpos) {
 4857:         changedVal = newitemVal;
 4858:     } else {
 4859:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4860:         current[newitemVal] = newpos;
 4861:     }
 4862:     for (var i=0; i<categories[idx].length; i++) {
 4863:         var elementName = categories[idx][i];
 4864:         if (elementName != item) {
 4865:             if (form.elements[elementName]) {
 4866:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4867:                 current[currVal] = elementName;
 4868:             }
 4869:         }
 4870:     }
 4871:     var oldVal;
 4872:     for (var j=0; j<maxh; j++) {
 4873:         if (current[j] == undefined) {
 4874:             oldVal = j;
 4875:         }
 4876:     }
 4877:     if (oldVal < changedVal) {
 4878:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4879:            var elementName = current[k];
 4880:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4881:         }
 4882:     } else {
 4883:         for (var k=changedVal; k<oldVal; k++) {
 4884:             var elementName = current[k];
 4885:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4886:         }
 4887:     }
 4888:     return;
 4889: }
 4890: 
 4891: function categoryCheck(form) {
 4892:     if (form.elements['addcategory_name'].value == 'instcode') {
 4893:         alert('$instcode_reserved\\n$choose_again');
 4894:         return false;
 4895:     }
 4896:     if (form.elements['addcategory_name'].value == 'communities') {
 4897:         alert('$communities_reserved\\n$choose_again');
 4898:         return false;
 4899:     }
 4900:     return true;
 4901: }
 4902: 
 4903: // ]]>
 4904: </script>
 4905: 
 4906: ENDSCRIPT
 4907:     return $output;
 4908: }
 4909: 
 4910: sub initialize_categories {
 4911:     my ($itemcount) = @_;
 4912:     my ($datatable,$css_class,$chgstr);
 4913:     my %default_names = (
 4914:                       instcode    => 'Official courses (with institutional codes)',
 4915:                       communities => 'Communities',
 4916:                         );
 4917:     my $select0 = ' selected="selected"';
 4918:     my $select1 = '';
 4919:     foreach my $default ('instcode','communities') {
 4920:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4921:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4922:         if ($default eq 'communities') {
 4923:             $select1 = $select0;
 4924:             $select0 = '';
 4925:         }
 4926:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4927:                      .'<select name="'.$default.'_pos">'
 4928:                      .'<option value="0"'.$select0.'>1</option>'
 4929:                      .'<option value="1"'.$select1.'>2</option>'
 4930:                      .'<option value="2">3</option></select>&nbsp;'
 4931:                      .$default_names{$default}
 4932:                      .'</span></td><td><span class="LC_nobreak">'
 4933:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4934:                      .&mt('Display').'</label>&nbsp;<label>'
 4935:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4936:                  .'</label></span></td></tr>';
 4937:         $itemcount ++;
 4938:     }
 4939:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4940:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4941:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4942:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4943:                   .'<option value="0">1</option>'
 4944:                   .'<option value="1">2</option>'
 4945:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4946:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4947:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4948:     return $datatable;
 4949: }
 4950: 
 4951: sub build_category_rows {
 4952:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4953:     my ($text,$name,$item,$chgstr);
 4954:     if (ref($cats) eq 'ARRAY') {
 4955:         my $maxdepth = scalar(@{$cats});
 4956:         if (ref($cats->[$depth]) eq 'HASH') {
 4957:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4958:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4959:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4960:                 $text .= '<td><table class="LC_data_table">';
 4961:                 my ($idxnum,$parent_name,$parent_item);
 4962:                 my $higher = $depth - 1;
 4963:                 if ($higher == 0) {
 4964:                     $parent_name = &escape($parent).'::'.$higher;
 4965:                 } else {
 4966:                     if (ref($path) eq 'ARRAY') {
 4967:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4968:                     }
 4969:                 }
 4970:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4971:                 for (my $j=0; $j<=$numchildren; $j++) {
 4972:                     if ($j < $numchildren) {
 4973:                         $name = $cats->[$depth]{$parent}[$j];
 4974:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4975:                         $idxnum = $idx->{$item};
 4976:                     } else {
 4977:                         $name = $parent_name;
 4978:                         $item = $parent_item;
 4979:                     }
 4980:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4981:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4982:                     for (my $i=0; $i<=$numchildren; $i++) {
 4983:                         my $vpos = $i+1;
 4984:                         my $selstr;
 4985:                         if ($j == $i) {
 4986:                             $selstr = ' selected="selected" ';
 4987:                         }
 4988:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4989:                     }
 4990:                     $text .= '</select>&nbsp;';
 4991:                     if ($j < $numchildren) {
 4992:                         my $deeper = $depth+1;
 4993:                         $text .= $name.'&nbsp;'
 4994:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4995:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4996:                         if(ref($path) eq 'ARRAY') {
 4997:                             push(@{$path},$name);
 4998:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4999:                             pop(@{$path});
 5000:                         }
 5001:                     } else {
 5002:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5003:                         if ($j == $numchildren) {
 5004:                             $text .= $name;
 5005:                         } else {
 5006:                             $text .= $item;
 5007:                         }
 5008:                         $text .= '" value="" />';
 5009:                     }
 5010:                     $text .= '</td></tr>';
 5011:                 }
 5012:                 $text .= '</table></td>';
 5013:             } else {
 5014:                 my $higher = $depth-1;
 5015:                 if ($higher == 0) {
 5016:                     $name = &escape($parent).'::'.$higher;
 5017:                 } else {
 5018:                     if (ref($path) eq 'ARRAY') {
 5019:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5020:                     }
 5021:                 }
 5022:                 my $colspan;
 5023:                 if ($parent ne 'instcode') {
 5024:                     $colspan = $maxdepth - $depth - 1;
 5025:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5026:                 }
 5027:             }
 5028:         }
 5029:     }
 5030:     return $text;
 5031: }
 5032: 
 5033: sub modifiable_userdata_row {
 5034:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5035:     my ($role,$rolename,$statustype);
 5036:     $role = $item;
 5037:     if ($context eq 'cancreate') {
 5038:         if ($item =~ /^emailusername_(.+)$/) {
 5039:             $statustype = $1;
 5040:             $role = 'emailusername';
 5041:             if (ref($usertypes) eq 'HASH') {
 5042:                 if ($usertypes->{$statustype}) {
 5043:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5044:                 } else {
 5045:                     $rolename = &mt('Data provided by user');
 5046:                 }
 5047:             }
 5048:         }
 5049:     } elsif ($context eq 'selfcreate') {
 5050:         if (ref($usertypes) eq 'HASH') {
 5051:             $rolename = $usertypes->{$role};
 5052:         } else {
 5053:             $rolename = $role;
 5054:         }
 5055:     } else {
 5056:         if ($role eq 'cr') {
 5057:             $rolename = &mt('Custom role');
 5058:         } else {
 5059:             $rolename = &Apache::lonnet::plaintext($role);
 5060:         }
 5061:     }
 5062:     my (@fields,%fieldtitles);
 5063:     if (ref($fieldsref) eq 'ARRAY') {
 5064:         @fields = @{$fieldsref};
 5065:     } else {
 5066:         @fields = ('lastname','firstname','middlename','generation',
 5067:                    'permanentemail','id');
 5068:     }
 5069:     if ((ref($titlesref) eq 'HASH')) {
 5070:         %fieldtitles = %{$titlesref};
 5071:     } else {
 5072:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5073:     }
 5074:     my $output;
 5075:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5076:     $output = '<tr '.$css_class.'>'.
 5077:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5078:               '<td class="LC_left_item" colspan="2"><table>';
 5079:     my $rem;
 5080:     my %checks;
 5081:     if (ref($settings) eq 'HASH') {
 5082:         if (ref($settings->{$context}) eq 'HASH') {
 5083:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5084:                 my $hashref = $settings->{$context}->{$role};
 5085:                 if ($role eq 'emailusername') {
 5086:                     if ($statustype) {
 5087:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5088:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5089:                             if (ref($hashref) eq 'HASH') {
 5090:                                 foreach my $field (@fields) {
 5091:                                     if ($hashref->{$field}) {
 5092:                                         $checks{$field} = $hashref->{$field};
 5093:                                     }
 5094:                                 }
 5095:                             }
 5096:                         }
 5097:                     }
 5098:                 } else {
 5099:                     if (ref($hashref) eq 'HASH') {
 5100:                         foreach my $field (@fields) {
 5101:                             if ($hashref->{$field}) {
 5102:                                 $checks{$field} = ' checked="checked" ';
 5103:                             }
 5104:                         }
 5105:                     }
 5106:                 }
 5107:             }
 5108:         }
 5109:     }
 5110: 
 5111:     for (my $i=0; $i<@fields; $i++) {
 5112:         my $rem = $i%($numinrow);
 5113:         if ($rem == 0) {
 5114:             if ($i > 0) {
 5115:                 $output .= '</tr>';
 5116:             }
 5117:             $output .= '<tr>';
 5118:         }
 5119:         my $check = ' ';
 5120:         unless ($role eq 'emailusername') {
 5121:             if (exists($checks{$fields[$i]})) {
 5122:                 $check = $checks{$fields[$i]}
 5123:             } else {
 5124:                 if ($role eq 'st') {
 5125:                     if (ref($settings) ne 'HASH') {
 5126:                         $check = ' checked="checked" ';
 5127:                     }
 5128:                 }
 5129:             }
 5130:         }
 5131:         $output .= '<td class="LC_left_item">'.
 5132:                    '<span class="LC_nobreak">';
 5133:         if ($role eq 'emailusername') {
 5134:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5135:                 $checks{$fields[$i]} = 'omit';
 5136:             }
 5137:             foreach my $option ('required','optional','omit') {
 5138:                 my $checked='';
 5139:                 if ($checks{$fields[$i]} eq $option) {
 5140:                     $checked='checked="checked" ';
 5141:                 }
 5142:                 $output .= '<label>'.
 5143:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5144:                            &mt($option).'</label>'.('&nbsp;' x2);
 5145:             }
 5146:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5147:         } else {
 5148:             $output .= '<label>'.
 5149:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5150:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5151:                        '</label>';
 5152:         }
 5153:         $output .= '</span></td>';
 5154:         $rem = @fields%($numinrow);
 5155:     }
 5156:     my $colsleft = $numinrow - $rem;
 5157:     if ($colsleft > 1 ) {
 5158:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5159:                    '&nbsp;</td>';
 5160:     } elsif ($colsleft == 1) {
 5161:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5162:     }
 5163:     $output .= '</tr></table></td></tr>';
 5164:     return $output;
 5165: }
 5166: 
 5167: sub insttypes_row {
 5168:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5169:     my %lt = &Apache::lonlocal::texthash (
 5170:                       cansearch => 'Users allowed to search',
 5171:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5172:                       lockablenames => 'User preference to lock name',
 5173:              );
 5174:     my $showdom;
 5175:     if ($context eq 'cansearch') {
 5176:         $showdom = ' ('.$dom.')';
 5177:     }
 5178:     my $class = 'LC_left_item';
 5179:     if ($context eq 'statustocreate') {
 5180:         $class = 'LC_right_item';
 5181:     }
 5182:     my $css_class = ' class="LC_odd_row"';
 5183:     if ($rownum ne '') { 
 5184:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5185:     }
 5186:     my $output = '<tr'.$css_class.'>'.
 5187:                  '<td>'.$lt{$context}.$showdom.
 5188:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5189:     my $rem;
 5190:     if (ref($types) eq 'ARRAY') {
 5191:         for (my $i=0; $i<@{$types}; $i++) {
 5192:             if (defined($usertypes->{$types->[$i]})) {
 5193:                 my $rem = $i%($numinrow);
 5194:                 if ($rem == 0) {
 5195:                     if ($i > 0) {
 5196:                         $output .= '</tr>';
 5197:                     }
 5198:                     $output .= '<tr>';
 5199:                 }
 5200:                 my $check = ' ';
 5201:                 if (ref($settings) eq 'HASH') {
 5202:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5203:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5204:                             $check = ' checked="checked" ';
 5205:                         }
 5206:                     } elsif ($context eq 'statustocreate') {
 5207:                         $check = ' checked="checked" ';
 5208:                     }
 5209:                 }
 5210:                 $output .= '<td class="LC_left_item">'.
 5211:                            '<span class="LC_nobreak"><label>'.
 5212:                            '<input type="checkbox" name="'.$context.'" '.
 5213:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5214:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5215:             }
 5216:         }
 5217:         $rem = @{$types}%($numinrow);
 5218:     }
 5219:     my $colsleft = $numinrow - $rem;
 5220:     if (($rem == 0) && (@{$types} > 0)) {
 5221:         $output .= '<tr>';
 5222:     }
 5223:     if ($colsleft > 1) {
 5224:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5225:     } else {
 5226:         $output .= '<td class="LC_left_item">';
 5227:     }
 5228:     my $defcheck = ' ';
 5229:     if (ref($settings) eq 'HASH') {  
 5230:         if (ref($settings->{$context}) eq 'ARRAY') {
 5231:             if (grep(/^default$/,@{$settings->{$context}})) {
 5232:                 $defcheck = ' checked="checked" ';
 5233:             }
 5234:         } elsif ($context eq 'statustocreate') {
 5235:             $defcheck = ' checked="checked" ';
 5236:         }
 5237:     }
 5238:     $output .= '<span class="LC_nobreak"><label>'.
 5239:                '<input type="checkbox" name="'.$context.'" '.
 5240:                'value="default"'.$defcheck.'/>'.
 5241:                $othertitle.'</label></span></td>'.
 5242:                '</tr></table></td></tr>';
 5243:     return $output;
 5244: }
 5245: 
 5246: sub sorted_searchtitles {
 5247:     my %searchtitles = &Apache::lonlocal::texthash(
 5248:                          'uname' => 'username',
 5249:                          'lastname' => 'last name',
 5250:                          'lastfirst' => 'last name, first name',
 5251:                      );
 5252:     my @titleorder = ('uname','lastname','lastfirst');
 5253:     return (\%searchtitles,\@titleorder);
 5254: }
 5255: 
 5256: sub sorted_searchtypes {
 5257:     my %srchtypes_desc = (
 5258:                            exact    => 'is exact match',
 5259:                            contains => 'contains ..',
 5260:                            begins   => 'begins with ..',
 5261:                          );
 5262:     my @srchtypeorder = ('exact','begins','contains');
 5263:     return (\%srchtypes_desc,\@srchtypeorder);
 5264: }
 5265: 
 5266: sub usertype_update_row {
 5267:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5268:     my $datatable;
 5269:     my $numinrow = 4;
 5270:     foreach my $type (@{$types}) {
 5271:         if (defined($usertypes->{$type})) {
 5272:             $$rownums ++;
 5273:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5274:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5275:                           '</td><td class="LC_left_item"><table>';
 5276:             for (my $i=0; $i<@{$fields}; $i++) {
 5277:                 my $rem = $i%($numinrow);
 5278:                 if ($rem == 0) {
 5279:                     if ($i > 0) {
 5280:                         $datatable .= '</tr>';
 5281:                     }
 5282:                     $datatable .= '<tr>';
 5283:                 }
 5284:                 my $check = ' ';
 5285:                 if (ref($settings) eq 'HASH') {
 5286:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5287:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5288:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5289:                                 $check = ' checked="checked" ';
 5290:                             }
 5291:                         }
 5292:                     }
 5293:                 }
 5294: 
 5295:                 if ($i == @{$fields}-1) {
 5296:                     my $colsleft = $numinrow - $rem;
 5297:                     if ($colsleft > 1) {
 5298:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5299:                     } else {
 5300:                         $datatable .= '<td>';
 5301:                     }
 5302:                 } else {
 5303:                     $datatable .= '<td>';
 5304:                 }
 5305:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5306:                               '<input type="checkbox" name="updateable_'.$type.
 5307:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5308:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5309:             }
 5310:             $datatable .= '</tr></table></td></tr>';
 5311:         }
 5312:     }
 5313:     return $datatable;
 5314: }
 5315: 
 5316: sub modify_login {
 5317:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5318:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5319:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5320:     %title = ( coursecatalog => 'Display course catalog',
 5321:                adminmail => 'Display administrator E-mail address',
 5322:                helpdesk  => 'Display "Contact Helpdesk" link',
 5323:                newuser => 'Link for visitors to create a user account',
 5324:                loginheader => 'Log-in box header');
 5325:     @offon = ('off','on');
 5326:     if (ref($domconfig{login}) eq 'HASH') {
 5327:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5328:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5329:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5330:             }
 5331:         }
 5332:     }
 5333:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5334:                                            \%domconfig,\%loginhash);
 5335:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5336:     foreach my $item (@toggles) {
 5337:         $loginhash{login}{$item} = $env{'form.'.$item};
 5338:     }
 5339:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5340:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5341:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5342:                                          \%loginhash);
 5343:     }
 5344: 
 5345:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5346:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5347:     if (keys(%servers) > 1) {
 5348:         foreach my $lonhost (keys(%servers)) {
 5349:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5350:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5351:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5352:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5353:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5354:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5355:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5356:                         $changes{'loginvia'}{$lonhost} = 1;
 5357:                     } else {
 5358:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5359:                         $changes{'loginvia'}{$lonhost} = 1;
 5360:                     }
 5361:                 } else {
 5362:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5363:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5364:                         $changes{'loginvia'}{$lonhost} = 1;
 5365:                     }
 5366:                 }
 5367:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5368:                     foreach my $item (@loginvia_attribs) {
 5369:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5370:                     }
 5371:                 } else {
 5372:                     foreach my $item (@loginvia_attribs) {
 5373:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5374:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5375:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5376:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5377:                                 $new = '/';
 5378:                             }
 5379:                         }
 5380:                         if (($item eq 'custompath') && 
 5381:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5382:                             $new = '';
 5383:                         }
 5384:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5385:                             $changes{'loginvia'}{$lonhost} = 1;
 5386:                         }
 5387:                         if ($item eq 'exempt') {
 5388:                             $new =~ s/^\s+//;
 5389:                             $new =~ s/\s+$//;
 5390:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5391:                             my @okips;
 5392:                             foreach my $ip (@poss_ips) {
 5393:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5394:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5395:                                         push(@okips,$ip); 
 5396:                                     }
 5397:                                 }
 5398:                             }
 5399:                             if (@okips > 0) {
 5400:                                 $new = join(',',@okips); 
 5401:                             } else {
 5402:                                 $new = ''; 
 5403:                             }
 5404:                         }
 5405:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5406:                     }
 5407:                 }
 5408:             } else {
 5409:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5410:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5411:                     $changes{'loginvia'}{$lonhost} = 1;
 5412:                     foreach my $item (@loginvia_attribs) {
 5413:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5414:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5415:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5416:                                 $new = '/';
 5417:                             }
 5418:                         }
 5419:                         if (($item eq 'custompath') && 
 5420:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5421:                             $new = '';
 5422:                         }
 5423:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5424:                     }
 5425:                 }
 5426:             }
 5427:         }
 5428:     }
 5429: 
 5430:     my $servadm = $r->dir_config('lonAdmEMail');
 5431:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5432:     if (ref($domconfig{'login'}) eq 'HASH') {
 5433:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5434:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5435:                 if ($lang eq 'nolang') {
 5436:                     push(@currlangs,$lang);
 5437:                 } elsif (defined($langchoices{$lang})) {
 5438:                     push(@currlangs,$lang);
 5439:                 } else {
 5440:                     next;
 5441:                 }
 5442:             }
 5443:         }
 5444:     }
 5445:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5446:     if (@currlangs > 0) {
 5447:         foreach my $lang (@currlangs) {
 5448:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5449:                 $changes{'helpurl'}{$lang} = 1;
 5450:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5451:                 $changes{'helpurl'}{$lang} = 1;
 5452:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5453:                 push(@newlangs,$lang);
 5454:             } else {
 5455:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5456:             }
 5457:         }
 5458:     }
 5459:     unless (grep(/^nolang$/,@currlangs)) {
 5460:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5461:             $changes{'helpurl'}{'nolang'} = 1;
 5462:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5463:             push(@newlangs,'nolang');
 5464:         }
 5465:     }
 5466:     if ($env{'form.loginhelpurl_add_lang'}) {
 5467:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5468:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5469:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5470:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5471:         }
 5472:     }
 5473:     if ((@newlangs > 0) || ($addedfile)) {
 5474:         my $error;
 5475:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5476:         if ($configuserok eq 'ok') {
 5477:             if ($switchserver) {
 5478:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5479:             } elsif ($author_ok eq 'ok') {
 5480:                 my @allnew = @newlangs;
 5481:                 if ($addedfile ne '') {
 5482:                     push(@allnew,$addedfile);
 5483:                 }
 5484:                 foreach my $lang (@allnew) {
 5485:                     my $formelem = 'loginhelpurl_'.$lang;
 5486:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5487:                         $formelem = 'loginhelpurl_add_file';
 5488:                     }
 5489:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5490:                                                                "help/$lang",'','',$newfile{$lang});
 5491:                     if ($result eq 'ok') {
 5492:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5493:                         $changes{'helpurl'}{$lang} = 1;
 5494:                     } else {
 5495:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5496:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5497:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5498:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5499: 
 5500:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5501:                         }
 5502:                     }
 5503:                 }
 5504:             } else {
 5505:                 $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);
 5506:             }
 5507:         } else {
 5508:             $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);
 5509:         }
 5510:         if ($error) {
 5511:             &Apache::lonnet::logthis($error);
 5512:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5513:         }
 5514:     }
 5515:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5516: 
 5517:     my $defaulthelpfile = '/adm/loginproblems.html';
 5518:     my $defaulttext = &mt('Default in use');
 5519: 
 5520:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5521:                                              $dom);
 5522:     if ($putresult eq 'ok') {
 5523:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5524:         my %defaultchecked = (
 5525:                     'coursecatalog' => 'on',
 5526:                     'helpdesk'      => 'on',
 5527:                     'adminmail'     => 'off',
 5528:                     'newuser'       => 'off',
 5529:         );
 5530:         if (ref($domconfig{'login'}) eq 'HASH') {
 5531:             foreach my $item (@toggles) {
 5532:                 if ($defaultchecked{$item} eq 'on') { 
 5533:                     if (($domconfig{'login'}{$item} eq '0') &&
 5534:                         ($env{'form.'.$item} eq '1')) {
 5535:                         $changes{$item} = 1;
 5536:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5537:                               $domconfig{'login'}{$item} eq '1') &&
 5538:                              ($env{'form.'.$item} eq '0')) {
 5539:                         $changes{$item} = 1;
 5540:                     }
 5541:                 } elsif ($defaultchecked{$item} eq 'off') {
 5542:                     if (($domconfig{'login'}{$item} eq '1') &&
 5543:                         ($env{'form.'.$item} eq '0')) {
 5544:                         $changes{$item} = 1;
 5545:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5546:                               $domconfig{'login'}{$item} eq '0') &&
 5547:                              ($env{'form.'.$item} eq '1')) {
 5548:                         $changes{$item} = 1;
 5549:                     }
 5550:                 }
 5551:             }
 5552:         }
 5553:         if (keys(%changes) > 0 || $colchgtext) {
 5554:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5555:             if (ref($lastactref) eq 'HASH') {
 5556:                 $lastactref->{'domainconfig'} = 1;
 5557:             }
 5558:             $resulttext = &mt('Changes made:').'<ul>';
 5559:             foreach my $item (sort(keys(%changes))) {
 5560:                 if ($item eq 'loginvia') {
 5561:                     if (ref($changes{$item}) eq 'HASH') {
 5562:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5563:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5564:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5565:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5566:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5567:                                     $protocol = 'http' if ($protocol ne 'https');
 5568:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5569: 
 5570:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5571:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5572:                                     } else {
 5573:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5574:                                     }
 5575:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5576:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5577:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5578:                                     }
 5579:                                     $resulttext .= '</li>';
 5580:                                 } else {
 5581:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5582:                                 }
 5583:                             } else {
 5584:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5585:                             }
 5586:                         }
 5587:                         $resulttext .= '</ul></li>';
 5588:                     }
 5589:                 } elsif ($item eq 'helpurl') {
 5590:                     if (ref($changes{$item}) eq 'HASH') {
 5591:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5592:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5593:                                 my ($chg,$link);
 5594:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5595:                                 if ($lang eq 'nolang') {
 5596:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5597:                                 } else {
 5598:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5599:                                 }
 5600:                                 $resulttext .= '<li>'.$chg.'</li>';
 5601:                             } else {
 5602:                                 my $chg;
 5603:                                 if ($lang eq 'nolang') {
 5604:                                     $chg = &mt('custom log-in help file for no preferred language');
 5605:                                 } else {
 5606:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 5607:                                 }
 5608:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 5609:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 5610:                                                       '?inhibitmenu=yes',$chg,600,500).
 5611:                                                '</li>';
 5612:                             }
 5613:                         }
 5614:                     }
 5615:                 } elsif ($item eq 'captcha') {
 5616:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5617:                         my $chgtxt;
 5618:                         if ($loginhash{'login'}{$item} eq 'notused') {
 5619:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 5620:                         } else {
 5621:                             my %captchas = &captcha_phrases();
 5622:                             if ($captchas{$loginhash{'login'}{$item}}) {
 5623:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 5624:                             } else {
 5625:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 5626:                             }
 5627:                         }
 5628:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5629:                     }
 5630:                 } elsif ($item eq 'recaptchakeys') {
 5631:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5632:                         my ($privkey,$pubkey);
 5633:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 5634:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 5635:                             $privkey = $loginhash{'login'}{$item}{'private'};
 5636:                         }
 5637:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 5638:                         if (!$pubkey) {
 5639:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 5640:                         } else {
 5641:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 5642:                         }
 5643:                         if (!$privkey) {
 5644:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 5645:                         } else {
 5646:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 5647:                         }
 5648:                         $chgtxt .= '</ul>';
 5649:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5650:                     }
 5651:                 } else {
 5652:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 5653:                 }
 5654:             }
 5655:             $resulttext .= $colchgtext.'</ul>';
 5656:         } else {
 5657:             $resulttext = &mt('No changes made to log-in page settings');
 5658:         }
 5659:     } else {
 5660:         $resulttext = '<span class="LC_error">'.
 5661: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5662:     }
 5663:     if ($errors) {
 5664:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 5665:                        $errors.'</ul>';
 5666:     }
 5667:     return $resulttext;
 5668: }
 5669: 
 5670: sub color_font_choices {
 5671:     my %choices =
 5672:         &Apache::lonlocal::texthash (
 5673:             img => "Header",
 5674:             bgs => "Background colors",
 5675:             links => "Link colors",
 5676:             images => "Images",
 5677:             font => "Font color",
 5678:             fontmenu => "Font menu",
 5679:             pgbg => "Page",
 5680:             tabbg => "Header",
 5681:             sidebg => "Border",
 5682:             link => "Link",
 5683:             alink => "Active link",
 5684:             vlink => "Visited link",
 5685:         );
 5686:     return %choices;
 5687: }
 5688: 
 5689: sub modify_rolecolors {
 5690:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 5691:     my ($resulttext,%rolehash);
 5692:     $rolehash{'rolecolors'} = {};
 5693:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5694:         if ($domconfig{'rolecolors'} eq '') {
 5695:             $domconfig{'rolecolors'} = {};
 5696:         }
 5697:     }
 5698:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5699:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5700:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5701:                                              $dom);
 5702:     if ($putresult eq 'ok') {
 5703:         if (keys(%changes) > 0) {
 5704:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5705:             if (ref($lastactref) eq 'HASH') {
 5706:                 $lastactref->{'domainconfig'} = 1;
 5707:             }
 5708:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5709:                                              $rolehash{'rolecolors'});
 5710:         } else {
 5711:             $resulttext = &mt('No changes made to default color schemes');
 5712:         }
 5713:     } else {
 5714:         $resulttext = '<span class="LC_error">'.
 5715: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5716:     }
 5717:     if ($errors) {
 5718:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5719:                        $errors.'</ul>';
 5720:     }
 5721:     return $resulttext;
 5722: }
 5723: 
 5724: sub modify_colors {
 5725:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5726:     my (%changes,%choices);
 5727:     my @bgs;
 5728:     my @links = ('link','alink','vlink');
 5729:     my @logintext;
 5730:     my @images;
 5731:     my $servadm = $r->dir_config('lonAdmEMail');
 5732:     my $errors;
 5733:     my %defaults;
 5734:     foreach my $role (@{$roles}) {
 5735:         if ($role eq 'login') {
 5736:             %choices = &login_choices();
 5737:             @logintext = ('textcol','bgcol');
 5738:         } else {
 5739:             %choices = &color_font_choices();
 5740:         }
 5741:         if ($role eq 'login') {
 5742:             @images = ('img','logo','domlogo','login');
 5743:             @bgs = ('pgbg','mainbg','sidebg');
 5744:         } else {
 5745:             @images = ('img');
 5746:             @bgs = ('pgbg','tabbg','sidebg');
 5747:         }
 5748:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 5749:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 5750:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5751:         }
 5752:         if ($role eq 'login') {
 5753:             foreach my $item (@logintext) {
 5754:                 unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'logintext'}{$item}) {
 5755:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5756:                 }
 5757:             }
 5758:         } else {
 5759:             unless($env{'form.'.$role.'_fontmenu'} eq $defaults{'fontmenu'}) {
 5760:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5761:             }
 5762:         }
 5763:         foreach my $item (@bgs) {
 5764:             unless ($env{'form.'.$role.'_'.$item} eq $defaults{'bgs'}{$item} ) {
 5765:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5766:             }
 5767:         }
 5768:         foreach my $item (@links) {
 5769:             unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'links'}{$item}) {
 5770:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5771:             }
 5772:         }
 5773:         my ($configuserok,$author_ok,$switchserver) = 
 5774:             &config_check($dom,$confname,$servadm);
 5775:         my ($width,$height) = &thumb_dimensions();
 5776:         if (ref($domconfig->{$role}) ne 'HASH') {
 5777:             $domconfig->{$role} = {};
 5778:         }
 5779:         foreach my $img (@images) {
 5780:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5781:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5782:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5783:                 } else { 
 5784:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5785:                 }
 5786:             } 
 5787: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5788: 		 && !defined($domconfig->{$role}{$img})
 5789: 		 && !$env{'form.'.$role.'_del_'.$img}
 5790: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5791: 		# import the old configured image from the .tab setting
 5792: 		# if they haven't provided a new one 
 5793: 		$domconfig->{$role}{$img} = 
 5794: 		    $env{'form.'.$role.'_import_'.$img};
 5795: 	    }
 5796:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5797:                 my $error;
 5798:                 if ($configuserok eq 'ok') {
 5799:                     if ($switchserver) {
 5800:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5801:                     } else {
 5802:                         if ($author_ok eq 'ok') {
 5803:                             my ($result,$logourl) = 
 5804:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5805:                                            $dom,$confname,$img,$width,$height);
 5806:                             if ($result eq 'ok') {
 5807:                                 $confhash->{$role}{$img} = $logourl;
 5808:                                 $changes{$role}{'images'}{$img} = 1;
 5809:                             } else {
 5810:                                 $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);
 5811:                             }
 5812:                         } else {
 5813:                             $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);
 5814:                         }
 5815:                     }
 5816:                 } else {
 5817:                     $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);
 5818:                 }
 5819:                 if ($error) {
 5820:                     &Apache::lonnet::logthis($error);
 5821:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5822:                 }
 5823:             } elsif ($domconfig->{$role}{$img} ne '') {
 5824:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5825:                     my $error;
 5826:                     if ($configuserok eq 'ok') {
 5827: # is confname an author?
 5828:                         if ($switchserver eq '') {
 5829:                             if ($author_ok eq 'ok') {
 5830:                                 my ($result,$logourl) = 
 5831:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5832:                                             $dom,$confname,$img,$width,$height);
 5833:                                 if ($result eq 'ok') {
 5834:                                     $confhash->{$role}{$img} = $logourl;
 5835: 				    $changes{$role}{'images'}{$img} = 1;
 5836:                                 }
 5837:                             }
 5838:                         }
 5839:                     }
 5840:                 }
 5841:             }
 5842:         }
 5843:         if (ref($domconfig) eq 'HASH') {
 5844:             if (ref($domconfig->{$role}) eq 'HASH') {
 5845:                 foreach my $img (@images) {
 5846:                     if ($domconfig->{$role}{$img} ne '') {
 5847:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5848:                             $confhash->{$role}{$img} = '';
 5849:                             $changes{$role}{'images'}{$img} = 1;
 5850:                         } else {
 5851:                             if ($confhash->{$role}{$img} eq '') {
 5852:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5853:                             }
 5854:                         }
 5855:                     } else {
 5856:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5857:                             $confhash->{$role}{$img} = '';
 5858:                             $changes{$role}{'images'}{$img} = 1;
 5859:                         } 
 5860:                     }
 5861:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5862:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5863:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5864:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5865:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5866:                             }
 5867:                         } else {
 5868:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5869:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5870:                             }
 5871:                         }
 5872:                     }
 5873:                 }
 5874:                 if ($domconfig->{$role}{'font'} ne '') {
 5875:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5876:                         $changes{$role}{'font'} = 1;
 5877:                     }
 5878:                 } else {
 5879:                     if ($confhash->{$role}{'font'}) {
 5880:                         $changes{$role}{'font'} = 1;
 5881:                     }
 5882:                 }
 5883:                 if ($role ne 'login') {
 5884:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5885:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5886:                             $changes{$role}{'fontmenu'} = 1;
 5887:                         }
 5888:                     } else {
 5889:                         if ($confhash->{$role}{'fontmenu'}) {
 5890:                             $changes{$role}{'fontmenu'} = 1;
 5891:                         }
 5892:                     }
 5893:                 }
 5894:                 foreach my $item (@bgs) {
 5895:                     if ($domconfig->{$role}{$item} ne '') {
 5896:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5897:                             $changes{$role}{'bgs'}{$item} = 1;
 5898:                         } 
 5899:                     } else {
 5900:                         if ($confhash->{$role}{$item}) {
 5901:                             $changes{$role}{'bgs'}{$item} = 1;
 5902:                         }
 5903:                     }
 5904:                 }
 5905:                 foreach my $item (@links) {
 5906:                     if ($domconfig->{$role}{$item} ne '') {
 5907:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5908:                             $changes{$role}{'links'}{$item} = 1;
 5909:                         }
 5910:                     } else {
 5911:                         if ($confhash->{$role}{$item}) {
 5912:                             $changes{$role}{'links'}{$item} = 1;
 5913:                         }
 5914:                     }
 5915:                 }
 5916:                 foreach my $item (@logintext) {
 5917:                     if ($domconfig->{$role}{$item} ne '') {
 5918:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5919:                             $changes{$role}{'logintext'}{$item} = 1;
 5920:                         }
 5921:                     } else {
 5922:                         if ($confhash->{$role}{$item}) {
 5923:                             $changes{$role}{'logintext'}{$item} = 1;
 5924:                         }
 5925:                     }
 5926:                 }
 5927:             } else {
 5928:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5929:                                         \@logintext,$confhash,\%changes); 
 5930:             }
 5931:         } else {
 5932:             &default_change_checker($role,\@images,\@links,\@bgs,
 5933:                                     \@logintext,$confhash,\%changes); 
 5934:         }
 5935:     }
 5936:     return ($errors,%changes);
 5937: }
 5938: 
 5939: sub config_check {
 5940:     my ($dom,$confname,$servadm) = @_;
 5941:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5942:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5943:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5944:                                                    $confname,$servadm);
 5945:     if ($configuserok eq 'ok') {
 5946:         $switchserver = &check_switchserver($dom,$confname);
 5947:         if ($switchserver eq '') {
 5948:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5949:         }
 5950:     }
 5951:     return ($configuserok,$author_ok,$switchserver);
 5952: }
 5953: 
 5954: sub default_change_checker {
 5955:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5956:     foreach my $item (@{$links}) {
 5957:         if ($confhash->{$role}{$item}) {
 5958:             $changes->{$role}{'links'}{$item} = 1;
 5959:         }
 5960:     }
 5961:     foreach my $item (@{$bgs}) {
 5962:         if ($confhash->{$role}{$item}) {
 5963:             $changes->{$role}{'bgs'}{$item} = 1;
 5964:         }
 5965:     }
 5966:     foreach my $item (@{$logintext}) {
 5967:         if ($confhash->{$role}{$item}) {
 5968:             $changes->{$role}{'logintext'}{$item} = 1;
 5969:         }
 5970:     }
 5971:     foreach my $img (@{$images}) {
 5972:         if ($env{'form.'.$role.'_del_'.$img}) {
 5973:             $confhash->{$role}{$img} = '';
 5974:             $changes->{$role}{'images'}{$img} = 1;
 5975:         }
 5976:         if ($role eq 'login') {
 5977:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5978:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5979:             }
 5980:         }
 5981:     }
 5982:     if ($confhash->{$role}{'font'}) {
 5983:         $changes->{$role}{'font'} = 1;
 5984:     }
 5985: }
 5986: 
 5987: sub display_colorchgs {
 5988:     my ($dom,$changes,$roles,$confhash) = @_;
 5989:     my (%choices,$resulttext);
 5990:     if (!grep(/^login$/,@{$roles})) {
 5991:         $resulttext = &mt('Changes made:').'<br />';
 5992:     }
 5993:     foreach my $role (@{$roles}) {
 5994:         if ($role eq 'login') {
 5995:             %choices = &login_choices();
 5996:         } else {
 5997:             %choices = &color_font_choices();
 5998:         }
 5999:         if (ref($changes->{$role}) eq 'HASH') {
 6000:             if ($role ne 'login') {
 6001:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6002:             }
 6003:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6004:                 if ($role ne 'login') {
 6005:                     $resulttext .= '<ul>';
 6006:                 }
 6007:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6008:                     if ($role ne 'login') {
 6009:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6010:                     }
 6011:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6012:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6013:                             if ($confhash->{$role}{$key}{$item}) {
 6014:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6015:                             } else {
 6016:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6017:                             }
 6018:                         } elsif ($confhash->{$role}{$item} eq '') {
 6019:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6020:                         } else {
 6021:                             my $newitem = $confhash->{$role}{$item};
 6022:                             if ($key eq 'images') {
 6023:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6024:                             }
 6025:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6026:                         }
 6027:                     }
 6028:                     if ($role ne 'login') {
 6029:                         $resulttext .= '</ul></li>';
 6030:                     }
 6031:                 } else {
 6032:                     if ($confhash->{$role}{$key} eq '') {
 6033:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6034:                     } else {
 6035:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6036:                     }
 6037:                 }
 6038:                 if ($role ne 'login') {
 6039:                     $resulttext .= '</ul>';
 6040:                 }
 6041:             }
 6042:         }
 6043:     }
 6044:     return $resulttext;
 6045: }
 6046: 
 6047: sub thumb_dimensions {
 6048:     return ('200','50');
 6049: }
 6050: 
 6051: sub check_dimensions {
 6052:     my ($inputfile) = @_;
 6053:     my ($fullwidth,$fullheight);
 6054:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6055:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6056:             my $imageinfo = <PIPE>;
 6057:             if (!close(PIPE)) {
 6058:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6059:             }
 6060:             chomp($imageinfo);
 6061:             my ($fullsize) = 
 6062:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6063:             if ($fullsize) {
 6064:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6065:             }
 6066:         }
 6067:     }
 6068:     return ($fullwidth,$fullheight);
 6069: }
 6070: 
 6071: sub check_configuser {
 6072:     my ($uhome,$dom,$confname,$servadm) = @_;
 6073:     my ($configuserok,%currroles);
 6074:     if ($uhome eq 'no_host') {
 6075:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6076:         my $configpass = &LONCAPA::Enrollment::create_password();
 6077:         $configuserok = 
 6078:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6079:                              $configpass,'','','','','',undef,$servadm);
 6080:     } else {
 6081:         $configuserok = 'ok';
 6082:         %currroles = 
 6083:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6084:     }
 6085:     return ($configuserok,%currroles);
 6086: }
 6087: 
 6088: sub check_authorstatus {
 6089:     my ($dom,$confname,%currroles) = @_;
 6090:     my $author_ok;
 6091:     if (!$currroles{':'.$dom.':au'}) {
 6092:         my $start = time;
 6093:         my $end = 0;
 6094:         $author_ok = 
 6095:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6096:                                         'au',$end,$start,'','','domconfig');
 6097:     } else {
 6098:         $author_ok = 'ok';
 6099:     }
 6100:     return $author_ok;
 6101: }
 6102: 
 6103: sub publishlogo {
 6104:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6105:     my ($output,$fname,$logourl);
 6106:     if ($action eq 'upload') {
 6107:         $fname=$env{'form.'.$formname.'.filename'};
 6108:         chop($env{'form.'.$formname});
 6109:     } else {
 6110:         ($fname) = ($formname =~ /([^\/]+)$/);
 6111:     }
 6112:     if ($savefileas ne '') {
 6113:         $fname = $savefileas;
 6114:     }
 6115:     $fname=&Apache::lonnet::clean_filename($fname);
 6116: # See if there is anything left
 6117:     unless ($fname) { return ('error: no uploaded file'); }
 6118:     $fname="$subdir/$fname";
 6119:     my $docroot=$r->dir_config('lonDocRoot');
 6120:     my $filepath="$docroot/priv";
 6121:     my $relpath = "$dom/$confname";
 6122:     my ($fnamepath,$file,$fetchthumb);
 6123:     $file=$fname;
 6124:     if ($fname=~m|/|) {
 6125:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6126:     }
 6127:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6128:     my $count;
 6129:     for ($count=5;$count<=$#parts;$count++) {
 6130:         $filepath.="/$parts[$count]";
 6131:         if ((-e $filepath)!=1) {
 6132:             mkdir($filepath,02770);
 6133:         }
 6134:     }
 6135:     # Check for bad extension and disallow upload
 6136:     if ($file=~/\.(\w+)$/ &&
 6137:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6138:         $output = 
 6139:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6140:     } elsif ($file=~/\.(\w+)$/ &&
 6141:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6142:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6143:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6144:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6145:     } elsif (-d "$filepath/$file") {
 6146:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6147:     } else {
 6148:         my $source = $filepath.'/'.$file;
 6149:         my $logfile;
 6150:         if (!open($logfile,">>$source".'.log')) {
 6151:             return (&mt('No write permission to Authoring Space'));
 6152:         }
 6153:         print $logfile
 6154: "\n================= Publish ".localtime()." ================\n".
 6155: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6156: # Save the file
 6157:         if (!open(FH,'>'.$source)) {
 6158:             &Apache::lonnet::logthis('Failed to create '.$source);
 6159:             return (&mt('Failed to create file'));
 6160:         }
 6161:         if ($action eq 'upload') {
 6162:             if (!print FH ($env{'form.'.$formname})) {
 6163:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6164:                 return (&mt('Failed to write file'));
 6165:             }
 6166:         } else {
 6167:             my $original = &Apache::lonnet::filelocation('',$formname);
 6168:             if(!copy($original,$source)) {
 6169:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6170:                 return (&mt('Failed to write file'));
 6171:             }
 6172:         }
 6173:         close(FH);
 6174:         chmod(0660, $source); # Permissions to rw-rw---.
 6175: 
 6176:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6177:         my $copyfile=$targetdir.'/'.$file;
 6178: 
 6179:         my @parts=split(/\//,$targetdir);
 6180:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6181:         for (my $count=5;$count<=$#parts;$count++) {
 6182:             $path.="/$parts[$count]";
 6183:             if (!-e $path) {
 6184:                 print $logfile "\nCreating directory ".$path;
 6185:                 mkdir($path,02770);
 6186:             }
 6187:         }
 6188:         my $versionresult;
 6189:         if (-e $copyfile) {
 6190:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6191:         } else {
 6192:             $versionresult = 'ok';
 6193:         }
 6194:         if ($versionresult eq 'ok') {
 6195:             if (copy($source,$copyfile)) {
 6196:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6197:                 $output = 'ok';
 6198:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6199:                 push(@{$modified_urls},[$copyfile,$source]);
 6200:                 my $metaoutput = 
 6201:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6202:                 unless ($registered_cleanup) {
 6203:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6204:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6205:                     $registered_cleanup=1;
 6206:                 }
 6207:             } else {
 6208:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6209:                 $output = &mt('Failed to copy file to RES space').", $!";
 6210:             }
 6211:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6212:                 my $inputfile = $filepath.'/'.$file;
 6213:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6214:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6215:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6216:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6217:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6218:                         system("convert -sample $thumbsize $inputfile $outfile");
 6219:                         chmod(0660, $filepath.'/tn-'.$file);
 6220:                         if (-e $outfile) {
 6221:                             my $copyfile=$targetdir.'/tn-'.$file;
 6222:                             if (copy($outfile,$copyfile)) {
 6223:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6224:                                 my $thumb_metaoutput = 
 6225:                                     &write_metadata($dom,$confname,$formname,
 6226:                                                     $targetdir,'tn-'.$file,$logfile);
 6227:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6228:                                 unless ($registered_cleanup) {
 6229:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6230:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6231:                                     $registered_cleanup=1;
 6232:                                 }
 6233:                             } else {
 6234:                                 print $logfile "\nUnable to write ".$copyfile.
 6235:                                                ':'.$!."\n";
 6236:                             }
 6237:                         }
 6238:                     }
 6239:                 }
 6240:             }
 6241:         } else {
 6242:             $output = $versionresult;
 6243:         }
 6244:     }
 6245:     return ($output,$logourl);
 6246: }
 6247: 
 6248: sub logo_versioning {
 6249:     my ($targetdir,$file,$logfile) = @_;
 6250:     my $target = $targetdir.'/'.$file;
 6251:     my ($maxversion,$fn,$extn,$output);
 6252:     $maxversion = 0;
 6253:     if ($file =~ /^(.+)\.(\w+)$/) {
 6254:         $fn=$1;
 6255:         $extn=$2;
 6256:     }
 6257:     opendir(DIR,$targetdir);
 6258:     while (my $filename=readdir(DIR)) {
 6259:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6260:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6261:         }
 6262:     }
 6263:     $maxversion++;
 6264:     print $logfile "\nCreating old version ".$maxversion."\n";
 6265:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6266:     if (copy($target,$copyfile)) {
 6267:         print $logfile "Copied old target to ".$copyfile."\n";
 6268:         $copyfile=$copyfile.'.meta';
 6269:         if (copy($target.'.meta',$copyfile)) {
 6270:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6271:             $output = 'ok';
 6272:         } else {
 6273:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6274:             $output = &mt('Failed to copy old meta').", $!, ";
 6275:         }
 6276:     } else {
 6277:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6278:         $output = &mt('Failed to copy old target').", $!, ";
 6279:     }
 6280:     return $output;
 6281: }
 6282: 
 6283: sub write_metadata {
 6284:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6285:     my (%metadatafields,%metadatakeys,$output);
 6286:     $metadatafields{'title'}=$formname;
 6287:     $metadatafields{'creationdate'}=time;
 6288:     $metadatafields{'lastrevisiondate'}=time;
 6289:     $metadatafields{'copyright'}='public';
 6290:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6291:                                          $env{'user.domain'};
 6292:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6293:     $metadatafields{'domain'}=$dom;
 6294:     {
 6295:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6296:         my $mfh;
 6297:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6298:             foreach (sort(keys(%metadatafields))) {
 6299:                 unless ($_=~/\./) {
 6300:                     my $unikey=$_;
 6301:                     $unikey=~/^([A-Za-z]+)/;
 6302:                     my $tag=$1;
 6303:                     $tag=~tr/A-Z/a-z/;
 6304:                     print $mfh "\n\<$tag";
 6305:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6306:                         my $value=$metadatafields{$unikey.'.'.$_};
 6307:                         $value=~s/\"/\'\'/g;
 6308:                         print $mfh ' '.$_.'="'.$value.'"';
 6309:                     }
 6310:                     print $mfh '>'.
 6311:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6312:                             .'</'.$tag.'>';
 6313:                 }
 6314:             }
 6315:             $output = 'ok';
 6316:             print $logfile "\nWrote metadata";
 6317:             close($mfh);
 6318:         } else {
 6319:             print $logfile "\nFailed to open metadata file";
 6320:             $output = &mt('Could not write metadata');
 6321:         }
 6322:     }
 6323:     return $output;
 6324: }
 6325: 
 6326: sub notifysubscribed {
 6327:     foreach my $targetsource (@{$modified_urls}){
 6328:         next unless (ref($targetsource) eq 'ARRAY');
 6329:         my ($target,$source)=@{$targetsource};
 6330:         if ($source ne '') {
 6331:             if (open(my $logfh,'>>'.$source.'.log')) {
 6332:                 print $logfh "\nCleanup phase: Notifications\n";
 6333:                 my @subscribed=&subscribed_hosts($target);
 6334:                 foreach my $subhost (@subscribed) {
 6335:                     print $logfh "\nNotifying host ".$subhost.':';
 6336:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6337:                     print $logfh $reply;
 6338:                 }
 6339:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6340:                 foreach my $subhost (@subscribedmeta) {
 6341:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6342:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6343:                                                         $subhost);
 6344:                     print $logfh $reply;
 6345:                 }
 6346:                 print $logfh "\n============ Done ============\n";
 6347:                 close($logfh);
 6348:             }
 6349:         }
 6350:     }
 6351:     return OK;
 6352: }
 6353: 
 6354: sub subscribed_hosts {
 6355:     my ($target) = @_;
 6356:     my @subscribed;
 6357:     if (open(my $fh,"<$target.subscription")) {
 6358:         while (my $subline=<$fh>) {
 6359:             if ($subline =~ /^($match_lonid):/) {
 6360:                 my $host = $1;
 6361:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6362:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6363:                         push(@subscribed,$host);
 6364:                     }
 6365:                 }
 6366:             }
 6367:         }
 6368:     }
 6369:     return @subscribed;
 6370: }
 6371: 
 6372: sub check_switchserver {
 6373:     my ($dom,$confname) = @_;
 6374:     my ($allowed,$switchserver);
 6375:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6376:     if ($home eq 'no_host') {
 6377:         $home = &Apache::lonnet::domain($dom,'primary');
 6378:     }
 6379:     my @ids=&Apache::lonnet::current_machine_ids();
 6380:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6381:     if (!$allowed) {
 6382: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6383:     }
 6384:     return $switchserver;
 6385: }
 6386: 
 6387: sub modify_quotas {
 6388:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6389:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6390:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6391:         $author_ok,$switchserver,$errors);
 6392:     if ($action eq 'quotas') {
 6393:         $context = 'tools'; 
 6394:     } else {
 6395:         $context = $action;
 6396:     }
 6397:     if ($context eq 'requestcourses') {
 6398:         @usertools = ('official','unofficial','community','textbook');
 6399:         @options =('norequest','approval','validate','autolimit');
 6400:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6401:         %titles = &courserequest_titles();
 6402:         $toolregexp = join('|',@usertools);
 6403:         %conditions = &courserequest_conditions();
 6404:         $confname = $dom.'-domainconfig';
 6405:         my $servadm = $r->dir_config('lonAdmEMail');
 6406:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6407:     } elsif ($context eq 'requestauthor') {
 6408:         @usertools = ('author');
 6409:         %titles = &authorrequest_titles();
 6410:     } else {
 6411:         @usertools = ('aboutme','blog','webdav','portfolio');
 6412:         %titles = &tool_titles();
 6413:     }
 6414:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6415:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6416:     foreach my $key (keys(%env)) {
 6417:         if ($context eq 'requestcourses') {
 6418:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6419:                 my $item = $1;
 6420:                 my $type = $2;
 6421:                 if ($type =~ /^limit_(.+)/) {
 6422:                     $limithash{$item}{$1} = $env{$key};
 6423:                 } else {
 6424:                     $confhash{$item}{$type} = $env{$key};
 6425:                 }
 6426:             }
 6427:         } elsif ($context eq 'requestauthor') {
 6428:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6429:                 $confhash{$1} = $env{$key};
 6430:             }
 6431:         } else {
 6432:             if ($key =~ /^form\.quota_(.+)$/) {
 6433:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6434:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6435:                 $confhash{'authorquota'}{$1} = $env{$key};
 6436:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6437:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6438:             }
 6439:         }
 6440:     }
 6441:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6442:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6443:         @approvalnotify = sort(@approvalnotify);
 6444:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6445:         my @crstypes = ('official','unofficial','community','textbook');
 6446:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6447:         foreach my $type (@hasuniquecode) {
 6448:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6449:                 $confhash{'uniquecode'}{$type} = 1;
 6450:             }
 6451:         }
 6452:         my ($newbook,@allpos);
 6453:         if ($context eq 'requestcourses') {
 6454:             if ($env{'form.addbook'}) {
 6455:                 if (($env{'form.addbook_cnum'} =~ /^$match_courseid$/) &&
 6456:                     ($env{'form.addbook_cdom'} =~ /^$match_domain$/)) {
 6457:                     if (&Apache::lonnet::homeserver($env{'form.addbook_cnum'},
 6458:                                                     $env{'form.addbook_cdom'}) eq 'no_host') {
 6459:                         $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6460:                                    '</span></li>';
 6461:                     } else {
 6462:                         $newbook = $env{'form.addbook_cdom'}.'_'.$env{'form.addbook_cnum'};
 6463:                         my $position = $env{'form.addbook_pos'};
 6464:                         $position =~ s/\D+//g;
 6465:                         if ($position ne '') {
 6466:                             $allpos[$position] = $newbook;
 6467:                         }
 6468:                     }
 6469:                 } else {
 6470:                     $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6471:                                '</span></li>';
 6472:                 }
 6473:             }
 6474:         }
 6475:         if (ref($domconfig{$action}) eq 'HASH') {
 6476:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6477:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6478:                     $changes{'notify'}{'approval'} = 1;
 6479:                 }
 6480:             } else {
 6481:                 if ($confhash{'notify'}{'approval'}) {
 6482:                     $changes{'notify'}{'approval'} = 1;
 6483:                 }
 6484:             }
 6485:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6486:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6487:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6488:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6489:                             $changes{'uniquecode'} = 1;
 6490:                         }
 6491:                     }
 6492:                     unless ($changes{'uniquecode'}) {
 6493:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6494:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6495:                                 $changes{'uniquecode'} = 1;
 6496:                             }
 6497:                         }
 6498:                     }
 6499:                } else {
 6500:                    $changes{'uniquecode'} = 1;
 6501:                }
 6502:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6503:                 $changes{'uniquecode'} = 1;
 6504:             }
 6505:             if ($context eq 'requestcourses') {
 6506:                 if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6507:                     my %deletions;
 6508:                     my @todelete = &Apache::loncommon::get_env_multiple('form.book_del');
 6509:                     if (@todelete) {
 6510:                         map { $deletions{$_} = 1; } @todelete;
 6511:                     }
 6512:                     my %imgdeletions;
 6513:                     my @todeleteimages = &Apache::loncommon::get_env_multiple('form.book_image_del');
 6514:                     if (@todeleteimages) {
 6515:                         map { $imgdeletions{$_} = 1; } @todeleteimages;
 6516:                     }
 6517:                     my $maxnum = $env{'form.book_maxnum'};
 6518:                     for (my $i=0; $i<=$maxnum; $i++) {
 6519:                         my $key = $env{'form.book_id_'.$i};
 6520:                         if (ref($domconfig{$action}{'textbooks'}{$key}) eq 'HASH') {
 6521:                             if ($deletions{$key}) {
 6522:                                 if ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6523:                                     #FIXME need to obsolete item in RES space
 6524:                                 }
 6525:                                 next;
 6526:                             } else {
 6527:                                 my $newpos = $env{'form.'.$key};
 6528:                                 $newpos =~ s/\D+//g;
 6529:                                 foreach my $item ('subject','title','author') {
 6530:                                     $confhash{'textbooks'}{$key}{$item} = $env{'form.book_'.$item.'_'.$i};
 6531:                                     if ($domconfig{$action}{'textbooks'}{$key}{$item} ne $confhash{'textbooks'}{$key}{$item}) {
 6532:                                         $changes{'textbooks'}{$key} = 1;
 6533:                                     }
 6534:                                 }
 6535:                                 $allpos[$newpos] = $key;
 6536:                             }
 6537:                             if ($imgdeletions{$key}) {
 6538:                                 $changes{'textbooks'}{$key} = 1;
 6539:                                 #FIXME need to obsolete item in RES space
 6540:                             } elsif ($env{'form.book_image_'.$i.'.filename'}) {
 6541:                                 my ($cdom,$cnum) = split(/_/,$key);
 6542:                                 my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,'book_image_'.$i,
 6543:                                                                               $cdom,$cnum,$configuserok,
 6544:                                                                               $switchserver,$author_ok);
 6545:                                 if ($imgurl) {
 6546:                                     $confhash{'textbooks'}{$key}{'image'} = $imgurl;
 6547:                                     $changes{'textbooks'}{$key} = 1; 
 6548:                                 }
 6549:                                 if ($error) {
 6550:                                     &Apache::lonnet::logthis($error);
 6551:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6552:                                 } 
 6553:                             } elsif ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6554:                                 $confhash{'textbooks'}{$key}{'image'} = 
 6555:                                     $domconfig{$action}{'textbooks'}{$key}{'image'};
 6556:                             }
 6557:                         }
 6558:                     }
 6559:                 }
 6560:             }
 6561:         } else {
 6562:             if ($confhash{'notify'}{'approval'}) {
 6563:                 $changes{'notify'}{'approval'} = 1;
 6564:             }
 6565:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 6566:                 $changes{'uniquecode'} = 1;
 6567:             }
 6568:         }
 6569:         if ($context eq 'requestcourses') {
 6570:             if ($newbook) {
 6571:                 $changes{'textbooks'}{$newbook} = 1;
 6572:                 foreach my $item ('subject','title','author') {
 6573:                     $env{'form.addbook_'.$item} =~ s/(`)/'/g;
 6574:                     if ($env{'form.addbook_'.$item}) {
 6575:                         $confhash{'textbooks'}{$newbook}{$item} = $env{'form.addbook_'.$item};
 6576:                     }
 6577:                 }
 6578:                 if ($env{'form.addbook_image.filename'} ne '') {
 6579:                     my ($cdom,$cnum) = split(/_/,$newbook);
 6580:                     my ($imageurl,$error) = 
 6581:                         &process_textbook_image($r,$dom,$confname,'addbook_image',$cdom,$cnum,$configuserok,
 6582:                                                 $switchserver,$author_ok);
 6583:                     if ($imageurl) {
 6584:                         $confhash{'textbooks'}{$newbook}{'image'} = $imageurl;
 6585:                     }
 6586:                     if ($error) {
 6587:                         &Apache::lonnet::logthis($error);
 6588:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6589:                     }
 6590:                 }
 6591:             }
 6592:             if (@allpos > 0) {
 6593:                 my $idx = 0;
 6594:                 foreach my $item (@allpos) {
 6595:                     if ($item ne '') {
 6596:                         $confhash{'textbooks'}{$item}{'order'} = $idx;
 6597:                         if (ref($domconfig{$action}) eq 'HASH') {
 6598:                             if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6599:                                 if (ref($domconfig{$action}{'textbooks'}{$item}) eq 'HASH') {
 6600:                                     if ($domconfig{$action}{'textbooks'}{$item}{'order'} ne $idx) {
 6601:                                         $changes{'textbooks'}{$item} = 1;
 6602:                                     }
 6603:                                 }
 6604:                             }
 6605:                         }
 6606:                         $idx ++;
 6607:                     }
 6608:                 }
 6609:             }
 6610:         }
 6611:     } else {
 6612:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 6613:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 6614:     }
 6615:     foreach my $item (@usertools) {
 6616:         foreach my $type (@{$types},'default','_LC_adv') {
 6617:             my $unset; 
 6618:             if ($context eq 'requestcourses') {
 6619:                 $unset = '0';
 6620:                 if ($type eq '_LC_adv') {
 6621:                     $unset = '';
 6622:                 }
 6623:                 if ($confhash{$item}{$type} eq 'autolimit') {
 6624:                     $confhash{$item}{$type} .= '=';
 6625:                     unless ($limithash{$item}{$type} =~ /\D/) {
 6626:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 6627:                     }
 6628:                 }
 6629:             } elsif ($context eq 'requestauthor') {
 6630:                 $unset = '0';
 6631:                 if ($type eq '_LC_adv') {
 6632:                     $unset = '';
 6633:                 }
 6634:             } else {
 6635:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 6636:                     $confhash{$item}{$type} = 1;
 6637:                 } else {
 6638:                     $confhash{$item}{$type} = 0;
 6639:                 }
 6640:             }
 6641:             if (ref($domconfig{$action}) eq 'HASH') {
 6642:                 if ($action eq 'requestauthor') {
 6643:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 6644:                         $changes{$type} = 1;
 6645:                     }
 6646:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 6647:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 6648:                         $changes{$item}{$type} = 1;
 6649:                     }
 6650:                 } else {
 6651:                     if ($context eq 'requestcourses') {
 6652:                         if ($confhash{$item}{$type} ne $unset) {
 6653:                             $changes{$item}{$type} = 1;
 6654:                         }
 6655:                     } else {
 6656:                         if (!$confhash{$item}{$type}) {
 6657:                             $changes{$item}{$type} = 1;
 6658:                         }
 6659:                     }
 6660:                 }
 6661:             } else {
 6662:                 if ($context eq 'requestcourses') {
 6663:                     if ($confhash{$item}{$type} ne $unset) {
 6664:                         $changes{$item}{$type} = 1;
 6665:                     }
 6666:                 } elsif ($context eq 'requestauthor') {
 6667:                     if ($confhash{$type} ne $unset) {
 6668:                         $changes{$type} = 1;
 6669:                     }
 6670:                 } else {
 6671:                     if (!$confhash{$item}{$type}) {
 6672:                         $changes{$item}{$type} = 1;
 6673:                     }
 6674:                 }
 6675:             }
 6676:         }
 6677:     }
 6678:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6679:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 6680:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6681:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 6682:                     if (exists($confhash{'defaultquota'}{$key})) {
 6683:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 6684:                             $changes{'defaultquota'}{$key} = 1;
 6685:                         }
 6686:                     } else {
 6687:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 6688:                     }
 6689:                 }
 6690:             } else {
 6691:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 6692:                     if (exists($confhash{'defaultquota'}{$key})) {
 6693:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 6694:                             $changes{'defaultquota'}{$key} = 1;
 6695:                         }
 6696:                     } else {
 6697:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 6698:                     }
 6699:                 }
 6700:             }
 6701:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6702:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 6703:                     if (exists($confhash{'authorquota'}{$key})) {
 6704:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 6705:                             $changes{'authorquota'}{$key} = 1;
 6706:                         }
 6707:                     } else {
 6708:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 6709:                     }
 6710:                 }
 6711:             }
 6712:         }
 6713:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 6714:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 6715:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6716:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6717:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 6718:                             $changes{'defaultquota'}{$key} = 1;
 6719:                         }
 6720:                     } else {
 6721:                         if (!exists($domconfig{'quotas'}{$key})) {
 6722:                             $changes{'defaultquota'}{$key} = 1;
 6723:                         }
 6724:                     }
 6725:                 } else {
 6726:                     $changes{'defaultquota'}{$key} = 1;
 6727:                 }
 6728:             }
 6729:         }
 6730:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 6731:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 6732:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6733:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6734:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 6735:                             $changes{'authorquota'}{$key} = 1;
 6736:                         }
 6737:                     } else {
 6738:                         $changes{'authorquota'}{$key} = 1;
 6739:                     }
 6740:                 } else {
 6741:                     $changes{'authorquota'}{$key} = 1;
 6742:                 }
 6743:             }
 6744:         }
 6745:     }
 6746: 
 6747:     if ($context eq 'requestauthor') {
 6748:         $domdefaults{'requestauthor'} = \%confhash;
 6749:     } else {
 6750:         foreach my $key (keys(%confhash)) {
 6751:             unless (($context eq 'requestcourses') && ($key eq 'textbooks')) {
 6752:                 $domdefaults{$key} = $confhash{$key};
 6753:             }
 6754:         }
 6755:     }
 6756: 
 6757:     my %quotahash = (
 6758:                       $action => { %confhash }
 6759:                     );
 6760:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 6761:                                              $dom);
 6762:     if ($putresult eq 'ok') {
 6763:         if (keys(%changes) > 0) {
 6764:             my $cachetime = 24*60*60;
 6765:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6766:             if (ref($lastactref) eq 'HASH') {
 6767:                 $lastactref->{'domdefaults'} = 1;
 6768:             }
 6769:             $resulttext = &mt('Changes made:').'<ul>';
 6770:             unless (($context eq 'requestcourses') ||
 6771:                     ($context eq 'requestauthor')) {
 6772:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 6773:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 6774:                     foreach my $type (@{$types},'default') {
 6775:                         if (defined($changes{'defaultquota'}{$type})) {
 6776:                             my $typetitle = $usertypes->{$type};
 6777:                             if ($type eq 'default') {
 6778:                                 $typetitle = $othertitle;
 6779:                             }
 6780:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 6781:                         }
 6782:                     }
 6783:                     $resulttext .= '</ul></li>';
 6784:                 }
 6785:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 6786:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 6787:                     foreach my $type (@{$types},'default') {
 6788:                         if (defined($changes{'authorquota'}{$type})) {
 6789:                             my $typetitle = $usertypes->{$type};
 6790:                             if ($type eq 'default') {
 6791:                                 $typetitle = $othertitle;
 6792:                             }
 6793:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 6794:                         }
 6795:                     }
 6796:                     $resulttext .= '</ul></li>';
 6797:                 }
 6798:             }
 6799:             my %newenv;
 6800:             foreach my $item (@usertools) {
 6801:                 my (%haschgs,%inconf);
 6802:                 if ($context eq 'requestauthor') {
 6803:                     %haschgs = %changes;
 6804:                     %inconf = %confhash;
 6805:                 } else {
 6806:                     if (ref($changes{$item}) eq 'HASH') {
 6807:                         %haschgs = %{$changes{$item}};
 6808:                     }
 6809:                     if (ref($confhash{$item}) eq 'HASH') {
 6810:                         %inconf = %{$confhash{$item}};
 6811:                     }
 6812:                 }
 6813:                 if (keys(%haschgs) > 0) {
 6814:                     my $newacc = 
 6815:                         &Apache::lonnet::usertools_access($env{'user.name'},
 6816:                                                           $env{'user.domain'},
 6817:                                                           $item,'reload',$context);
 6818:                     if (($context eq 'requestcourses') ||
 6819:                         ($context eq 'requestauthor')) {
 6820:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 6821:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 6822:                         }
 6823:                     } else {
 6824:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 6825:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 6826:                         }
 6827:                     }
 6828:                     unless ($context eq 'requestauthor') {
 6829:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 6830:                     }
 6831:                     foreach my $type (@{$types},'default','_LC_adv') {
 6832:                         if ($haschgs{$type}) {
 6833:                             my $typetitle = $usertypes->{$type};
 6834:                             if ($type eq 'default') {
 6835:                                 $typetitle = $othertitle;
 6836:                             } elsif ($type eq '_LC_adv') {
 6837:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 6838:                             }
 6839:                             if ($inconf{$type}) {
 6840:                                 if ($context eq 'requestcourses') {
 6841:                                     my $cond;
 6842:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 6843:                                         if ($1 eq '') {
 6844:                                             $cond = &mt('(Automatic processing of any request).');
 6845:                                         } else {
 6846:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 6847:                                         }
 6848:                                     } else { 
 6849:                                         $cond = $conditions{$inconf{$type}};
 6850:                                     }
 6851:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 6852:                                 } elsif ($context eq 'requestauthor') {
 6853:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 6854:                                                              $titles{$inconf{$type}},$typetitle);
 6855: 
 6856:                                 } else {
 6857:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 6858:                                 }
 6859:                             } else {
 6860:                                 if ($type eq '_LC_adv') {
 6861:                                     if ($inconf{$type} eq '0') {
 6862:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6863:                                     } else { 
 6864:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 6865:                                     }
 6866:                                 } else {
 6867:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6868:                                 }
 6869:                             }
 6870:                         }
 6871:                     }
 6872:                     unless ($context eq 'requestauthor') {
 6873:                         $resulttext .= '</ul></li>';
 6874:                     }
 6875:                 }
 6876:             }
 6877:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 6878:                 if (ref($changes{'notify'}) eq 'HASH') {
 6879:                     if ($changes{'notify'}{'approval'}) {
 6880:                         if (ref($confhash{'notify'}) eq 'HASH') {
 6881:                             if ($confhash{'notify'}{'approval'}) {
 6882:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 6883:                             } else {
 6884:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 6885:                             }
 6886:                         }
 6887:                     }
 6888:                 }
 6889:             }
 6890:             if ($action eq 'requestcourses') {
 6891:                 my @offon = ('off','on');
 6892:                 if ($changes{'uniquecode'}) {
 6893:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6894:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 6895:                         $resulttext .= '<li>'.
 6896:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 6897:                                        '</li>';
 6898:                     } else {
 6899:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 6900:                                        '</li>';
 6901:                     }
 6902:                 }
 6903:                 if (ref($changes{'textbooks'}) eq 'HASH') {
 6904:                     $resulttext .= '<li>'.&mt('Available textbooks updated').'<ul>';
 6905:                     foreach my $key (sort(keys(%{$changes{'textbooks'}}))) {
 6906:                         my %coursehash = &Apache::lonnet::coursedescription($key);
 6907:                         my $coursetitle = $coursehash{'description'};
 6908:                         my $position = $confhash{'textbooks'}{$key}{'order'} + 1;
 6909:                         $resulttext .= '<li>';
 6910:                         foreach my $item ('subject','title','author') {
 6911:                             my $name = $item.':';
 6912:                             $name =~ s/^(\w)/\U$1/;
 6913:                             $resulttext .= &mt($name).' '.$confhash{'textbooks'}{$key}{$item}.'<br />';
 6914:                         }
 6915:                         $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 6916:                         if ($confhash{'textbooks'}{$key}{'image'}) {
 6917:                             $resulttext .= ' '.&mt('Image: [_1]',
 6918:                                                '<img src="'.$confhash{'textbooks'}{$key}{'image'}.'"'.
 6919:                                                ' alt="Textbook cover" />').'<br />';
 6920:                         }
 6921:                         $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 6922:                     }
 6923:                     $resulttext .= '</ul></li>';
 6924:                 }
 6925:             }
 6926:             $resulttext .= '</ul>';
 6927:             if (keys(%newenv)) {
 6928:                 &Apache::lonnet::appenv(\%newenv);
 6929:             }
 6930:         } else {
 6931:             if ($context eq 'requestcourses') {
 6932:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 6933:             } elsif ($context eq 'requestauthor') {
 6934:                 $resulttext = &mt('No changes made to rights to request author space.');
 6935:             } else {
 6936:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 6937:             }
 6938:         }
 6939:     } else {
 6940:         $resulttext = '<span class="LC_error">'.
 6941: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6942:     }
 6943:     if ($errors) {
 6944:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 6945:                        '<ul>'.$errors.'</ul></p>';
 6946:     }
 6947:     return $resulttext;
 6948: }
 6949: 
 6950: sub process_textbook_image {
 6951:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$configuserok,$switchserver,$author_ok) = @_;
 6952:     my $filename = $env{'form.'.$caller.'.filename'};
 6953:     my ($error,$url);
 6954:     my ($width,$height) = (50,50);
 6955:     if ($configuserok eq 'ok') {
 6956:         if ($switchserver) {
 6957:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 6958:                          $switchserver);
 6959:         } elsif ($author_ok eq 'ok') {
 6960:             my ($result,$imageurl) =
 6961:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 6962:                              "textbooks/$dom/$cnum/cover",$width,$height);
 6963:             if ($result eq 'ok') {
 6964:                 $url = $imageurl;
 6965:             } else {
 6966:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 6967:             }
 6968:         } else {
 6969:             $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);
 6970:         }
 6971:     } else {
 6972:         $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);
 6973:     }
 6974:     return ($url,$error);
 6975: }
 6976: 
 6977: sub modify_autoenroll {
 6978:     my ($dom,$lastactref,%domconfig) = @_;
 6979:     my ($resulttext,%changes);
 6980:     my %currautoenroll;
 6981:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 6982:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 6983:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 6984:         }
 6985:     }
 6986:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6987:     my %title = ( run => 'Auto-enrollment active',
 6988:                   sender => 'Sender for notification messages',
 6989:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6990:     my @offon = ('off','on');
 6991:     my $sender_uname = $env{'form.sender_uname'};
 6992:     my $sender_domain = $env{'form.sender_domain'};
 6993:     if ($sender_domain eq '') {
 6994:         $sender_uname = '';
 6995:     } elsif ($sender_uname eq '') {
 6996:         $sender_domain = '';
 6997:     }
 6998:     my $coowners = $env{'form.autoassign_coowners'};
 6999:     my %autoenrollhash =  (
 7000:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7001:                                        'sender_uname' => $sender_uname,
 7002:                                        'sender_domain' => $sender_domain,
 7003:                                        'co-owners' => $coowners,
 7004:                                 }
 7005:                      );
 7006:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7007:                                              $dom);
 7008:     if ($putresult eq 'ok') {
 7009:         if (exists($currautoenroll{'run'})) {
 7010:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7011:                  $changes{'run'} = 1;
 7012:              }
 7013:         } elsif ($autorun) {
 7014:             if ($env{'form.autoenroll_run'} ne '1') {
 7015:                  $changes{'run'} = 1;
 7016:             }
 7017:         }
 7018:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7019:             $changes{'sender'} = 1;
 7020:         }
 7021:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7022:             $changes{'sender'} = 1;
 7023:         }
 7024:         if ($currautoenroll{'co-owners'} ne '') {
 7025:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7026:                 $changes{'coowners'} = 1;
 7027:             }
 7028:         } elsif ($coowners) {
 7029:             $changes{'coowners'} = 1;
 7030:         }      
 7031:         if (keys(%changes) > 0) {
 7032:             $resulttext = &mt('Changes made:').'<ul>';
 7033:             if ($changes{'run'}) {
 7034:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7035:             }
 7036:             if ($changes{'sender'}) {
 7037:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7038:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7039:                 } else {
 7040:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7041:                 }
 7042:             }
 7043:             if ($changes{'coowners'}) {
 7044:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7045:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7046:                 if (ref($lastactref) eq 'HASH') {
 7047:                     $lastactref->{'domainconfig'} = 1;
 7048:                 }
 7049:             }
 7050:             $resulttext .= '</ul>';
 7051:         } else {
 7052:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7053:         }
 7054:     } else {
 7055:         $resulttext = '<span class="LC_error">'.
 7056: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7057:     }
 7058:     return $resulttext;
 7059: }
 7060: 
 7061: sub modify_autoupdate {
 7062:     my ($dom,%domconfig) = @_;
 7063:     my ($resulttext,%currautoupdate,%fields,%changes);
 7064:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7065:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7066:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7067:         }
 7068:     }
 7069:     my @offon = ('off','on');
 7070:     my %title = &Apache::lonlocal::texthash (
 7071:                    run => 'Auto-update:',
 7072:                    classlists => 'Updates to user information in classlists?'
 7073:                 );
 7074:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7075:     my %fieldtitles = &Apache::lonlocal::texthash (
 7076:                         id => 'Student/Employee ID',
 7077:                         permanentemail => 'E-mail address',
 7078:                         lastname => 'Last Name',
 7079:                         firstname => 'First Name',
 7080:                         middlename => 'Middle Name',
 7081:                         generation => 'Generation',
 7082:                       );
 7083:     $othertitle = &mt('All users');
 7084:     if (keys(%{$usertypes}) >  0) {
 7085:         $othertitle = &mt('Other users');
 7086:     }
 7087:     foreach my $key (keys(%env)) {
 7088:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7089:             my ($usertype,$item) = ($1,$2);
 7090:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7091:                 if ($usertype eq 'default') {   
 7092:                     push(@{$fields{$1}},$2);
 7093:                 } elsif (ref($types) eq 'ARRAY') {
 7094:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7095:                         push(@{$fields{$1}},$2);
 7096:                     }
 7097:                 }
 7098:             }
 7099:         }
 7100:     }
 7101:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 7102:     @lockablenames = sort(@lockablenames);
 7103:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 7104:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7105:         if (@changed) {
 7106:             $changes{'lockablenames'} = 1;
 7107:         }
 7108:     } else {
 7109:         if (@lockablenames) {
 7110:             $changes{'lockablenames'} = 1;
 7111:         }
 7112:     }
 7113:     my %updatehash = (
 7114:                       autoupdate => { run => $env{'form.autoupdate_run'},
 7115:                                       classlists => $env{'form.classlists'},
 7116:                                       fields => {%fields},
 7117:                                       lockablenames => \@lockablenames,
 7118:                                     }
 7119:                      );
 7120:     foreach my $key (keys(%currautoupdate)) {
 7121:         if (($key eq 'run') || ($key eq 'classlists')) {
 7122:             if (exists($updatehash{autoupdate}{$key})) {
 7123:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 7124:                     $changes{$key} = 1;
 7125:                 }
 7126:             }
 7127:         } elsif ($key eq 'fields') {
 7128:             if (ref($currautoupdate{$key}) eq 'HASH') {
 7129:                 foreach my $item (@{$types},'default') {
 7130:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 7131:                         my $change = 0;
 7132:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 7133:                             if (!exists($fields{$item})) {
 7134:                                 $change = 1;
 7135:                                 last;
 7136:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 7137:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 7138:                                     $change = 1;
 7139:                                     last;
 7140:                                 }
 7141:                             }
 7142:                         }
 7143:                         if ($change) {
 7144:                             push(@{$changes{$key}},$item);
 7145:                         }
 7146:                     } 
 7147:                 }
 7148:             }
 7149:         } elsif ($key eq 'lockablenames') {
 7150:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 7151:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7152:                 if (@changed) {
 7153:                     $changes{'lockablenames'} = 1;
 7154:                 }
 7155:             } else {
 7156:                 if (@lockablenames) {
 7157:                     $changes{'lockablenames'} = 1;
 7158:                 }
 7159:             }
 7160:         }
 7161:     }
 7162:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 7163:         if (@lockablenames) {
 7164:             $changes{'lockablenames'} = 1;
 7165:         }
 7166:     }
 7167:     foreach my $item (@{$types},'default') {
 7168:         if (defined($fields{$item})) {
 7169:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 7170:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 7171:                     my $change = 0;
 7172:                     if (ref($fields{$item}) eq 'ARRAY') {
 7173:                         foreach my $type (@{$fields{$item}}) {
 7174:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 7175:                                 $change = 1;
 7176:                                 last;
 7177:                             }
 7178:                         }
 7179:                     }
 7180:                     if ($change) {
 7181:                         push(@{$changes{'fields'}},$item);
 7182:                     }
 7183:                 } else {
 7184:                     push(@{$changes{'fields'}},$item);
 7185:                 }
 7186:             } else {
 7187:                 push(@{$changes{'fields'}},$item);
 7188:             }
 7189:         }
 7190:     }
 7191:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7192:                                              $dom);
 7193:     if ($putresult eq 'ok') {
 7194:         if (keys(%changes) > 0) {
 7195:             $resulttext = &mt('Changes made:').'<ul>';
 7196:             foreach my $key (sort(keys(%changes))) {
 7197:                 if ($key eq 'lockablenames') {
 7198:                     $resulttext .= '<li>';
 7199:                     if (@lockablenames) {
 7200:                         $usertypes->{'default'} = $othertitle;
 7201:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7202:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7203:                     } else {
 7204:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7205:                     }
 7206:                     $resulttext .= '</li>';
 7207:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7208:                     foreach my $item (@{$changes{$key}}) {
 7209:                         my @newvalues;
 7210:                         foreach my $type (@{$fields{$item}}) {
 7211:                             push(@newvalues,$fieldtitles{$type});
 7212:                         }
 7213:                         my $newvaluestr;
 7214:                         if (@newvalues > 0) {
 7215:                             $newvaluestr = join(', ',@newvalues);
 7216:                         } else {
 7217:                             $newvaluestr = &mt('none');
 7218:                         }
 7219:                         if ($item eq 'default') {
 7220:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7221:                         } else {
 7222:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7223:                         }
 7224:                     }
 7225:                 } else {
 7226:                     my $newvalue;
 7227:                     if ($key eq 'run') {
 7228:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7229:                     } else {
 7230:                         $newvalue = $offon[$env{'form.'.$key}];
 7231:                     }
 7232:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7233:                 }
 7234:             }
 7235:             $resulttext .= '</ul>';
 7236:         } else {
 7237:             $resulttext = &mt('No changes made to autoupdates');
 7238:         }
 7239:     } else {
 7240:         $resulttext = '<span class="LC_error">'.
 7241: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7242:     }
 7243:     return $resulttext;
 7244: }
 7245: 
 7246: sub modify_autocreate {
 7247:     my ($dom,%domconfig) = @_;
 7248:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7249:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7250:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7251:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7252:         }
 7253:     }
 7254:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7255:                  req => 'Auto-creation of validated requests for official courses',
 7256:                  xmldc => 'Identity of course creator of courses from XML files',
 7257:                );
 7258:     my @types = ('xml','req');
 7259:     foreach my $item (@types) {
 7260:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7261:         $newvals{$item} =~ s/\D//g;
 7262:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7263:     }
 7264:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7265:     my %domcoords = &get_active_dcs($dom);
 7266:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7267:         $newvals{'xmldc'} = '';
 7268:     } 
 7269:     %autocreatehash =  (
 7270:                         autocreate => { xml => $newvals{'xml'},
 7271:                                         req => $newvals{'req'},
 7272:                                       }
 7273:                        );
 7274:     if ($newvals{'xmldc'} ne '') {
 7275:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7276:     }
 7277:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7278:                                              $dom);
 7279:     if ($putresult eq 'ok') {
 7280:         my @items = @types;
 7281:         if ($newvals{'xml'}) {
 7282:             push(@items,'xmldc');
 7283:         }
 7284:         foreach my $item (@items) {
 7285:             if (exists($currautocreate{$item})) {
 7286:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7287:                     $changes{$item} = 1;
 7288:                 }
 7289:             } elsif ($newvals{$item}) {
 7290:                 $changes{$item} = 1;
 7291:             }
 7292:         }
 7293:         if (keys(%changes) > 0) {
 7294:             my @offon = ('off','on'); 
 7295:             $resulttext = &mt('Changes made:').'<ul>';
 7296:             foreach my $item (@types) {
 7297:                 if ($changes{$item}) {
 7298:                     my $newtxt = $offon[$newvals{$item}];
 7299:                     $resulttext .= '<li>'.
 7300:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7301:                                        '<b>','</b>').
 7302:                                    '</li>';
 7303:                 }
 7304:             }
 7305:             if ($changes{'xmldc'}) {
 7306:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7307:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7308:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7309:             }
 7310:             $resulttext .= '</ul>';
 7311:         } else {
 7312:             $resulttext = &mt('No changes made to auto-creation settings');
 7313:         }
 7314:     } else {
 7315:         $resulttext = '<span class="LC_error">'.
 7316:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7317:     }
 7318:     return $resulttext;
 7319: }
 7320: 
 7321: sub modify_directorysrch {
 7322:     my ($dom,%domconfig) = @_;
 7323:     my ($resulttext,%changes);
 7324:     my %currdirsrch;
 7325:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7326:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7327:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7328:         }
 7329:     }
 7330:     my %title = ( available => 'Directory search available',
 7331:                   localonly => 'Other domains can search',
 7332:                   searchby => 'Search types',
 7333:                   searchtypes => 'Search latitude');
 7334:     my @offon = ('off','on');
 7335:     my @otherdoms = ('Yes','No');
 7336: 
 7337:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7338:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7339:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7340: 
 7341:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7342:     if (keys(%{$usertypes}) == 0) {
 7343:         @cansearch = ('default');
 7344:     } else {
 7345:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7346:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7347:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7348:                     push(@{$changes{'cansearch'}},$type);
 7349:                 }
 7350:             }
 7351:             foreach my $type (@cansearch) {
 7352:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7353:                     push(@{$changes{'cansearch'}},$type);
 7354:                 }
 7355:             }
 7356:         } else {
 7357:             push(@{$changes{'cansearch'}},@cansearch);
 7358:         }
 7359:     }
 7360: 
 7361:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7362:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7363:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7364:                 push(@{$changes{'searchby'}},$by);
 7365:             }
 7366:         }
 7367:         foreach my $by (@searchby) {
 7368:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7369:                 push(@{$changes{'searchby'}},$by);
 7370:             }
 7371:         }
 7372:     } else {
 7373:         push(@{$changes{'searchby'}},@searchby);
 7374:     }
 7375: 
 7376:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7377:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7378:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7379:                 push(@{$changes{'searchtypes'}},$type);
 7380:             }
 7381:         }
 7382:         foreach my $type (@searchtypes) {
 7383:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7384:                 push(@{$changes{'searchtypes'}},$type);
 7385:             }
 7386:         }
 7387:     } else {
 7388:         if (exists($currdirsrch{'searchtypes'})) {
 7389:             foreach my $type (@searchtypes) {  
 7390:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7391:                     push(@{$changes{'searchtypes'}},$type);
 7392:                 }
 7393:             }
 7394:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7395:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7396:             }   
 7397:         } else {
 7398:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7399:         }
 7400:     }
 7401: 
 7402:     my %dirsrch_hash =  (
 7403:             directorysrch => { available => $env{'form.dirsrch_available'},
 7404:                                cansearch => \@cansearch,
 7405:                                localonly => $env{'form.dirsrch_localonly'},
 7406:                                searchby => \@searchby,
 7407:                                searchtypes => \@searchtypes,
 7408:                              }
 7409:             );
 7410:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7411:                                              $dom);
 7412:     if ($putresult eq 'ok') {
 7413:         if (exists($currdirsrch{'available'})) {
 7414:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7415:                  $changes{'available'} = 1;
 7416:              }
 7417:         } else {
 7418:             if ($env{'form.dirsrch_available'} eq '1') {
 7419:                 $changes{'available'} = 1;
 7420:             }
 7421:         }
 7422:         if (exists($currdirsrch{'localonly'})) {
 7423:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7424:                  $changes{'localonly'} = 1;
 7425:              }
 7426:         } else {
 7427:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7428:                 $changes{'localonly'} = 1;
 7429:             }
 7430:         }
 7431:         if (keys(%changes) > 0) {
 7432:             $resulttext = &mt('Changes made:').'<ul>';
 7433:             if ($changes{'available'}) {
 7434:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7435:             }
 7436:             if ($changes{'localonly'}) {
 7437:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7438:             }
 7439: 
 7440:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7441:                 my $chgtext;
 7442:                 if (ref($usertypes) eq 'HASH') {
 7443:                     if (keys(%{$usertypes}) > 0) {
 7444:                         foreach my $type (@{$types}) {
 7445:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7446:                                 $chgtext .= $usertypes->{$type}.'; ';
 7447:                             }
 7448:                         }
 7449:                         if (grep(/^default$/,@cansearch)) {
 7450:                             $chgtext .= $othertitle;
 7451:                         } else {
 7452:                             $chgtext =~ s/\; $//;
 7453:                         }
 7454:                         $resulttext .=
 7455:                             '<li>'.
 7456:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 7457:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 7458:                             '</li>';
 7459:                     }
 7460:                 }
 7461:             }
 7462:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 7463:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 7464:                 my $chgtext;
 7465:                 foreach my $type (@{$titleorder}) {
 7466:                     if (grep(/^\Q$type\E$/,@searchby)) {
 7467:                         if (defined($searchtitles->{$type})) {
 7468:                             $chgtext .= $searchtitles->{$type}.'; ';
 7469:                         }
 7470:                     }
 7471:                 }
 7472:                 $chgtext =~ s/\; $//;
 7473:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 7474:             }
 7475:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 7476:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 7477:                 my $chgtext;
 7478:                 foreach my $type (@{$srchtypeorder}) {
 7479:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 7480:                         if (defined($srchtypes_desc->{$type})) {
 7481:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 7482:                         }
 7483:                     }
 7484:                 }
 7485:                 $chgtext =~ s/\; $//;
 7486:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 7487:             }
 7488:             $resulttext .= '</ul>';
 7489:         } else {
 7490:             $resulttext = &mt('No changes made to institution directory search settings');
 7491:         }
 7492:     } else {
 7493:         $resulttext = '<span class="LC_error">'.
 7494:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 7495:     }
 7496:     return $resulttext;
 7497: }
 7498: 
 7499: sub modify_contacts {
 7500:     my ($dom,$lastactref,%domconfig) = @_;
 7501:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 7502:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 7503:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 7504:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 7505:         }
 7506:     }
 7507:     my (%others,%to,%bcc);
 7508:     my @contacts = ('supportemail','adminemail');
 7509:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 7510:                     'requestsmail','updatesmail','idconflictsmail');
 7511:     my @toggles = ('reporterrors','reportupdates');
 7512:     foreach my $type (@mailings) {
 7513:         @{$newsetting{$type}} = 
 7514:             &Apache::loncommon::get_env_multiple('form.'.$type);
 7515:         foreach my $item (@contacts) {
 7516:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 7517:                 $contacts_hash{contacts}{$type}{$item} = 1;
 7518:             } else {
 7519:                 $contacts_hash{contacts}{$type}{$item} = 0;
 7520:             }
 7521:         }  
 7522:         $others{$type} = $env{'form.'.$type.'_others'};
 7523:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 7524:         if ($type eq 'helpdeskmail') {
 7525:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 7526:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 7527:         }
 7528:     }
 7529:     foreach my $item (@contacts) {
 7530:         $to{$item} = $env{'form.'.$item};
 7531:         $contacts_hash{'contacts'}{$item} = $to{$item};
 7532:     }
 7533:     foreach my $item (@toggles) {
 7534:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 7535:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 7536:         }
 7537:     }
 7538:     if (keys(%currsetting) > 0) {
 7539:         foreach my $item (@contacts) {
 7540:             if ($to{$item} ne $currsetting{$item}) {
 7541:                 $changes{$item} = 1;
 7542:             }
 7543:         }
 7544:         foreach my $type (@mailings) {
 7545:             foreach my $item (@contacts) {
 7546:                 if (ref($currsetting{$type}) eq 'HASH') {
 7547:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 7548:                         push(@{$changes{$type}},$item);
 7549:                     }
 7550:                 } else {
 7551:                     push(@{$changes{$type}},@{$newsetting{$type}});
 7552:                 }
 7553:             }
 7554:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 7555:                 push(@{$changes{$type}},'others');
 7556:             }
 7557:             if ($type eq 'helpdeskmail') {   
 7558:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 7559:                     push(@{$changes{$type}},'bcc'); 
 7560:                 }
 7561:             }
 7562:         }
 7563:     } else {
 7564:         my %default;
 7565:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 7566:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 7567:         $default{'errormail'} = 'adminemail';
 7568:         $default{'packagesmail'} = 'adminemail';
 7569:         $default{'helpdeskmail'} = 'supportemail';
 7570:         $default{'lonstatusmail'} = 'adminemail';
 7571:         $default{'requestsmail'} = 'adminemail';
 7572:         $default{'updatesmail'} = 'adminemail';
 7573:         foreach my $item (@contacts) {
 7574:            if ($to{$item} ne $default{$item}) {
 7575:               $changes{$item} = 1;
 7576:            }
 7577:         }
 7578:         foreach my $type (@mailings) {
 7579:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 7580:                
 7581:                 push(@{$changes{$type}},@{$newsetting{$type}});
 7582:             }
 7583:             if ($others{$type} ne '') {
 7584:                 push(@{$changes{$type}},'others');
 7585:             }
 7586:             if ($type eq 'helpdeskmail') {
 7587:                 if ($bcc{$type} ne '') {
 7588:                     push(@{$changes{$type}},'bcc');
 7589:                 }
 7590:             }
 7591:         }
 7592:     }
 7593:     foreach my $item (@toggles) {
 7594:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 7595:             $changes{$item} = 1;
 7596:         } elsif ((!$env{'form.'.$item}) &&
 7597:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 7598:             $changes{$item} = 1;
 7599:         }
 7600:     }
 7601:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 7602:                                              $dom);
 7603:     if ($putresult eq 'ok') {
 7604:         if (keys(%changes) > 0) {
 7605:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7606:             if (ref($lastactref) eq 'HASH') {
 7607:                 $lastactref->{'domainconfig'} = 1;
 7608:             }
 7609:             my ($titles,$short_titles)  = &contact_titles();
 7610:             $resulttext = &mt('Changes made:').'<ul>';
 7611:             foreach my $item (@contacts) {
 7612:                 if ($changes{$item}) {
 7613:                     $resulttext .= '<li>'.$titles->{$item}.
 7614:                                     &mt(' set to: ').
 7615:                                     '<span class="LC_cusr_emph">'.
 7616:                                     $to{$item}.'</span></li>';
 7617:                 }
 7618:             }
 7619:             foreach my $type (@mailings) {
 7620:                 if (ref($changes{$type}) eq 'ARRAY') {
 7621:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 7622:                     my @text;
 7623:                     foreach my $item (@{$newsetting{$type}}) {
 7624:                         push(@text,$short_titles->{$item});
 7625:                     }
 7626:                     if ($others{$type} ne '') {
 7627:                         push(@text,$others{$type});
 7628:                     }
 7629:                     $resulttext .= '<span class="LC_cusr_emph">'.
 7630:                                    join(', ',@text).'</span>';
 7631:                     if ($type eq 'helpdeskmail') {
 7632:                         if ($bcc{$type} ne '') {
 7633:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 7634:                         }
 7635:                     }
 7636:                     $resulttext .= '</li>';
 7637:                 }
 7638:             }
 7639:             my @offon = ('off','on');
 7640:             if ($changes{'reporterrors'}) {
 7641:                 $resulttext .= '<li>'.
 7642:                                &mt('E-mail error reports to [_1] set to "'.
 7643:                                    $offon[$env{'form.reporterrors'}].'".',
 7644:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7645:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 7646:                                '</li>';
 7647:             }
 7648:             if ($changes{'reportupdates'}) {
 7649:                 $resulttext .= '<li>'.
 7650:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 7651:                                     $offon[$env{'form.reportupdates'}].'".',
 7652:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7653:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 7654:                                 '</li>';
 7655:             }
 7656:             $resulttext .= '</ul>';
 7657:         } else {
 7658:             $resulttext = &mt('No changes made to contact information');
 7659:         }
 7660:     } else {
 7661:         $resulttext = '<span class="LC_error">'.
 7662:             &mt('An error occurred: [_1].',$putresult).'</span>';
 7663:     }
 7664:     return $resulttext;
 7665: }
 7666: 
 7667: sub modify_usercreation {
 7668:     my ($dom,%domconfig) = @_;
 7669:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 7670:     my $warningmsg;
 7671:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 7672:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 7673:             if ($key eq 'cancreate') {
 7674:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 7675:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 7676:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 7677:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 7678:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7679:                         } else {
 7680:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7681:                         }
 7682:                     }
 7683:                 }
 7684:             } elsif ($key eq 'email_rule') {
 7685:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 7686:             } else {
 7687:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 7688:             }
 7689:         }
 7690:     }
 7691:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 7692:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 7693:     my @contexts = ('author','course','requestcrs');
 7694:     foreach my $item(@contexts) {
 7695:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 7696:     }
 7697:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 7698:         foreach my $item (@contexts) {
 7699:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 7700:                 push(@{$changes{'cancreate'}},$item);
 7701:             }
 7702:         }
 7703:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 7704:         foreach my $item (@contexts) {
 7705:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 7706:                 if ($cancreate{$item} ne 'any') {
 7707:                     push(@{$changes{'cancreate'}},$item);
 7708:                 }
 7709:             } else {
 7710:                 if ($cancreate{$item} ne 'none') {
 7711:                     push(@{$changes{'cancreate'}},$item);
 7712:                 }
 7713:             }
 7714:         }
 7715:     } else {
 7716:         foreach my $item (@contexts)  {
 7717:             push(@{$changes{'cancreate'}},$item);
 7718:         }
 7719:     }
 7720: 
 7721:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 7722:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 7723:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 7724:                 push(@{$changes{'username_rule'}},$type);
 7725:             }
 7726:         }
 7727:         foreach my $type (@username_rule) {
 7728:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 7729:                 push(@{$changes{'username_rule'}},$type);
 7730:             }
 7731:         }
 7732:     } else {
 7733:         push(@{$changes{'username_rule'}},@username_rule);
 7734:     }
 7735: 
 7736:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 7737:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 7738:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 7739:                 push(@{$changes{'id_rule'}},$type);
 7740:             }
 7741:         }
 7742:         foreach my $type (@id_rule) {
 7743:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 7744:                 push(@{$changes{'id_rule'}},$type);
 7745:             }
 7746:         }
 7747:     } else {
 7748:         push(@{$changes{'id_rule'}},@id_rule);
 7749:     }
 7750: 
 7751:     my @authen_contexts = ('author','course','domain');
 7752:     my @authtypes = ('int','krb4','krb5','loc');
 7753:     my %authhash;
 7754:     foreach my $item (@authen_contexts) {
 7755:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 7756:         foreach my $auth (@authtypes) {
 7757:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 7758:                 $authhash{$item}{$auth} = 1;
 7759:             } else {
 7760:                 $authhash{$item}{$auth} = 0;
 7761:             }
 7762:         }
 7763:     }
 7764:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 7765:         foreach my $item (@authen_contexts) {
 7766:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 7767:                 foreach my $auth (@authtypes) {
 7768:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 7769:                         push(@{$changes{'authtypes'}},$item);
 7770:                         last;
 7771:                     }
 7772:                 }
 7773:             }
 7774:         }
 7775:     } else {
 7776:         foreach my $item (@authen_contexts) {
 7777:             push(@{$changes{'authtypes'}},$item);
 7778:         }
 7779:     }
 7780: 
 7781:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 7782:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 7783:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 7784:     $save_usercreate{'id_rule'} = \@id_rule;
 7785:     $save_usercreate{'username_rule'} = \@username_rule,
 7786:     $save_usercreate{'authtypes'} = \%authhash;
 7787: 
 7788:     my %usercreation_hash =  (
 7789:         usercreation     => \%save_usercreate,
 7790:     );
 7791: 
 7792:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 7793:                                              $dom);
 7794: 
 7795:     if ($putresult eq 'ok') {
 7796:         if (keys(%changes) > 0) {
 7797:             $resulttext = &mt('Changes made:').'<ul>';
 7798:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 7799:                 my %lt = &usercreation_types();
 7800:                 foreach my $type (@{$changes{'cancreate'}}) {
 7801:                     my $chgtext = $lt{$type}.', ';
 7802:                     if ($cancreate{$type} eq 'none') {
 7803:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 7804:                     } elsif ($cancreate{$type} eq 'any') {
 7805:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 7806:                     } elsif ($cancreate{$type} eq 'official') {
 7807:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 7808:                     } elsif ($cancreate{$type} eq 'unofficial') {
 7809:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 7810:                     }
 7811:                     $resulttext .= '<li>'.$chgtext.'</li>';
 7812:                 }
 7813:             }
 7814:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 7815:                 my ($rules,$ruleorder) = 
 7816:                     &Apache::lonnet::inst_userrules($dom,'username');
 7817:                 my $chgtext = '<ul>';
 7818:                 foreach my $type (@username_rule) {
 7819:                     if (ref($rules->{$type}) eq 'HASH') {
 7820:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 7821:                     }
 7822:                 }
 7823:                 $chgtext .= '</ul>';
 7824:                 if (@username_rule > 0) {
 7825:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 7826:                 } else {
 7827:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 7828:                 }
 7829:             }
 7830:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 7831:                 my ($idrules,$idruleorder) = 
 7832:                     &Apache::lonnet::inst_userrules($dom,'id');
 7833:                 my $chgtext = '<ul>';
 7834:                 foreach my $type (@id_rule) {
 7835:                     if (ref($idrules->{$type}) eq 'HASH') {
 7836:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 7837:                     }
 7838:                 }
 7839:                 $chgtext .= '</ul>';
 7840:                 if (@id_rule > 0) {
 7841:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 7842:                 } else {
 7843:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 7844:                 }
 7845:             }
 7846:             my %authname = &authtype_names();
 7847:             my %context_title = &context_names();
 7848:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7849:                 my $chgtext = '<ul>';
 7850:                 foreach my $type (@{$changes{'authtypes'}}) {
 7851:                     my @allowed;
 7852:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7853:                     foreach my $auth (@authtypes) {
 7854:                         if ($authhash{$type}{$auth}) {
 7855:                             push(@allowed,$authname{$auth});
 7856:                         }
 7857:                     }
 7858:                     if (@allowed > 0) {
 7859:                         $chgtext .= join(', ',@allowed).'</li>';
 7860:                     } else {
 7861:                         $chgtext .= &mt('none').'</li>';
 7862:                     }
 7863:                 }
 7864:                 $chgtext .= '</ul>';
 7865:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7866:                 $resulttext .= '</li>';
 7867:             }
 7868:             $resulttext .= '</ul>';
 7869:         } else {
 7870:             $resulttext = &mt('No changes made to user creation settings');
 7871:         }
 7872:     } else {
 7873:         $resulttext = '<span class="LC_error">'.
 7874:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7875:     }
 7876:     if ($warningmsg ne '') {
 7877:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7878:     }
 7879:     return $resulttext;
 7880: }
 7881: 
 7882: sub modify_selfcreation {
 7883:     my ($dom,%domconfig) = @_;
 7884:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 7885:     my (%save_usercreate,%save_usermodify);
 7886:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7887:     if (ref($types) eq 'ARRAY') {
 7888:         $usertypes->{'default'} = $othertitle;
 7889:         push(@{$types},'default');
 7890:     }
 7891: #
 7892: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 7893: #
 7894:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 7895:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 7896:             if ($key eq 'cancreate') {
 7897:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 7898:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 7899:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 7900:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 7901:                             ($item eq 'emailusername') || ($item eq 'notify')) {
 7902:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7903:                         } else {
 7904:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7905:                         }
 7906:                     }
 7907:                 }
 7908:             } elsif ($key eq 'email_rule') {
 7909:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 7910:             } else {
 7911:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 7912:             }
 7913:         }
 7914:     }
 7915: #
 7916: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 7917: #
 7918:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7919:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7920:             if ($key eq 'selfcreate') {
 7921:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 7922:             } else {
 7923:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 7924:             }
 7925:         }
 7926:     }
 7927: 
 7928:     my @contexts = ('selfcreate');
 7929:     @{$cancreate{'selfcreate'}} = ();
 7930:     %{$cancreate{'emailusername'}} = ();
 7931:     @{$cancreate{'statustocreate'}} = ();
 7932:     my %selfcreatetypes = (
 7933:                              sso   => 'users authenticated by institutional single sign on',
 7934:                              login => 'users authenticated by institutional log-in',
 7935:                              email => 'users who provide a valid e-mail address for use as username (automatic creation)',
 7936:                              emailapproval => 'users who provide a valid e-mail address for use as username (queued for Domain Coordinator review)',
 7937:                           );
 7938: #
 7939: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 7940: # is permitted.
 7941: #
 7942:     foreach my $item ('login','sso','email') {
 7943:         if ($item eq 'email') {
 7944:             if ($env{'form.cancreate_email'} eq 'email') {
 7945:                 push(@{$cancreate{'selfcreate'}},'email');
 7946:             } elsif ($env{'form.cancreate_email'} eq 'emailapproval') {
 7947:                 push(@{$cancreate{'selfcreate'}},'emailapproval');
 7948:             }
 7949:         } else {
 7950:             if ($env{'form.cancreate_'.$item}) {
 7951:                 push(@{$cancreate{'selfcreate'}},$item);
 7952:             }
 7953:         }
 7954:     }
 7955:     my (@email_rule,%userinfo,%savecaptcha);
 7956:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 7957: #
 7958: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 7959: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 7960: #
 7961:     if (($env{'form.cancreate_email'} eq 'email') || ($env{'form.cancreate_email'} eq 'emailapproval')) {
 7962:         push(@contexts,'emailusername');
 7963:         if (ref($types) eq 'ARRAY') {
 7964:             foreach my $type (@{$types}) {
 7965:                 if (ref($infofields) eq 'ARRAY') {
 7966:                     foreach my $field (@{$infofields}) {
 7967:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 7968:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 7969:                         }
 7970:                     }
 7971:                 }
 7972:             }
 7973:         }
 7974: #
 7975: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 7976: # queued requests for self-creation of account using e-mail address as username
 7977: #
 7978: 
 7979:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 7980:         @approvalnotify = sort(@approvalnotify);
 7981:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 7982:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 7983:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 7984:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 7985:                     push(@{$changes{'cancreate'}},'notify');
 7986:                 }
 7987:             } else {
 7988:                 if ($cancreate{'notify'}{'approval'}) {
 7989:                     push(@{$changes{'cancreate'}},'notify');
 7990:                 }
 7991:             }
 7992:         } elsif ($cancreate{'notify'}{'approval'}) {
 7993:             push(@{$changes{'cancreate'}},'notify');
 7994:         }
 7995: 
 7996: #
 7997: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 7998: #
 7999:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8000:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8001:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8002:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8003:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8004:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8005:                         push(@{$changes{'email_rule'}},$type);
 8006:                     }
 8007:                 }
 8008:             }
 8009:             if (@email_rule > 0) {
 8010:                 foreach my $type (@email_rule) {
 8011:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8012:                         push(@{$changes{'email_rule'}},$type);
 8013:                     }
 8014:                 }
 8015:             }
 8016:         } elsif (@email_rule > 0) {
 8017:             push(@{$changes{'email_rule'}},@email_rule);
 8018:         }
 8019:     }
 8020: #  
 8021: # Check if domain default is set appropriately, if selef-creation of accounts is to be available for
 8022: # institutional log-in.
 8023: #
 8024:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8025:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8026:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8027:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8028:             $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.').' '.
 8029:                           &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.');
 8030:         }
 8031:     }
 8032:     my @fields = ('lastname','firstname','middlename','generation',
 8033:                   'permanentemail','id');
 8034:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8035: #
 8036: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8037: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8038: # may self-create accounts 
 8039: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8040: # which the user may supply, if institutional data is unavailable.
 8041: #
 8042:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8043:         if (ref($types) eq 'ARRAY') {
 8044:             if (@{$types} > 1) {
 8045:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8046:                 push(@contexts,'statustocreate');
 8047:             } else {
 8048:                 undef($cancreate{'statustocreate'});
 8049:             } 
 8050:             foreach my $type (@{$types}) {
 8051:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8052:                 foreach my $field (@fields) {
 8053:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8054:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8055:                     } else {
 8056:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8057:                     }
 8058:                 }
 8059:             }
 8060:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8061:                 foreach my $type (@{$types}) {
 8062:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8063:                         foreach my $field (@fields) {
 8064:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8065:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8066:                                 push(@{$changes{'selfcreate'}},$type);
 8067:                                 last;
 8068:                             }
 8069:                         }
 8070:                     }
 8071:                 }
 8072:             } else {
 8073:                 foreach my $type (@{$types}) {
 8074:                     push(@{$changes{'selfcreate'}},$type);
 8075:                 }
 8076:             }
 8077:         }
 8078:     }
 8079:     foreach my $item (@contexts) {
 8080:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 8081:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 8082:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 8083:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 8084:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8085:                             push(@{$changes{'cancreate'}},$item);
 8086:                         }
 8087:                     }
 8088:                 }
 8089:             }
 8090:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8091:                 foreach my $type (@{$cancreate{$item}}) {
 8092:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 8093:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8094:                             push(@{$changes{'cancreate'}},$item);
 8095:                         }
 8096:                     }
 8097:                 }
 8098:             }
 8099:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 8100:             if (ref($cancreate{$item}) eq 'HASH') {
 8101:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 8102:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 8103:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 8104:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 8105:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8106:                                     push(@{$changes{'cancreate'}},$item);
 8107:                                 }
 8108:                             }
 8109:                         }
 8110:                     } else {
 8111:                         if (!$cancreate{$item}{$curr}) {
 8112:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8113:                                 push(@{$changes{'cancreate'}},$item);
 8114:                             }
 8115:                         }
 8116:                     }
 8117:                 }
 8118:                 foreach my $field (keys(%{$cancreate{$item}})) {
 8119:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 8120:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 8121:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 8122:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 8123:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8124:                                         push(@{$changes{'cancreate'}},$item);
 8125:                                     }
 8126:                                 }
 8127:                             } else {
 8128:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8129:                                     push(@{$changes{'cancreate'}},$item);
 8130:                                 }
 8131:                             }
 8132:                         }
 8133:                     } else {
 8134:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 8135:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8136:                                 push(@{$changes{'cancreate'}},$item);
 8137:                             }
 8138:                         }
 8139:                     }
 8140:                 }
 8141:             }
 8142:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 8143:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8144:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 8145:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8146:                         push(@{$changes{'cancreate'}},$item);
 8147:                     }
 8148:                 }
 8149:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 8150:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 8151:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8152:                         push(@{$changes{'cancreate'}},$item);
 8153:                     }
 8154:                 }
 8155:             }
 8156:         } elsif ($item eq 'emailusername') {
 8157:             if (ref($cancreate{$item}) eq 'HASH') {
 8158:                 foreach my $type (keys(%{$cancreate{$item}})) {
 8159:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 8160:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 8161:                             if ($cancreate{$item}{$type}{$field}) {
 8162:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8163:                                     push(@{$changes{'cancreate'}},$item);
 8164:                                 }
 8165:                                 last;
 8166:                             }
 8167:                         }
 8168:                     }
 8169:                 }
 8170:             }
 8171:         }
 8172:     }
 8173: #
 8174: # Populate %save_usercreate hash with updates to self-creation configuration.
 8175: #
 8176:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 8177:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 8178:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 8179:     if (ref($cancreate{'notify'}) eq 'HASH') {
 8180:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 8181:     }
 8182:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8183:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 8184:     }
 8185:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 8186:     $save_usercreate{'emailrule'} = \@email_rule;
 8187: 
 8188:     my %userconfig_hash = (
 8189:             usercreation     => \%save_usercreate,
 8190:             usermodification => \%save_usermodify,
 8191:     );
 8192:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8193:                                              $dom);
 8194: #
 8195: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8196: #
 8197:     if ($putresult eq 'ok') {
 8198:         if (keys(%changes) > 0) {
 8199:             $resulttext = &mt('Changes made:').'<ul>';
 8200:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8201:                 my %lt = &selfcreation_types();
 8202:                 foreach my $type (@{$changes{'cancreate'}}) {
 8203:                     my $chgtext;
 8204:                     if ($type eq 'selfcreate') {
 8205:                         if (@{$cancreate{$type}} == 0) {
 8206:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 8207:                         } else {
 8208:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 8209:                                         '<ul>';
 8210:                             foreach my $case (@{$cancreate{$type}}) {
 8211:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 8212:                             }
 8213:                             $chgtext .= '</ul>';
 8214:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 8215:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 8216:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8217:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 8218:                                             $chgtext .= '<br />'.
 8219:                                                         '<span class="LC_warning">'.
 8220:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8221:                                                         '</span>';
 8222:                                         }
 8223:                                     }
 8224:                                 }
 8225:                             }
 8226:                         }
 8227:                     } elsif ($type eq 'statustocreate') {
 8228:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 8229:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 8230:                             if (@{$cancreate{'selfcreate'}} > 0) {
 8231:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 8232:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 8233:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 8234:                                         $chgtext .= '<br />'.
 8235:                                                     '<span class="LC_warning">'.
 8236:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8237:                                                     '</span>';
 8238:                                     }
 8239:                                 } elsif (ref($usertypes) eq 'HASH') {
 8240:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8241:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 8242:                                     } else {
 8243:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 8244:                                     }
 8245:                                     $chgtext .= '<ul>';
 8246:                                     foreach my $case (@{$cancreate{$type}}) {
 8247:                                         if ($case eq 'default') {
 8248:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 8249:                                         } else {
 8250:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 8251:                                         }
 8252:                                     }
 8253:                                     $chgtext .= '</ul>';
 8254:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8255:                                         $chgtext .= '<br /><span class="LC_warning">'.
 8256:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 8257:                                                     '</span>';
 8258:                                     }
 8259:                                 }
 8260:                             } else {
 8261:                                 if (@{$cancreate{$type}} == 0) {
 8262:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8263:                                 } else {
 8264:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8265:                                 }
 8266:                             }
 8267:                         }
 8268:                     } elsif ($type eq 'captcha') {
 8269:                         if ($savecaptcha{$type} eq 'notused') {
 8270:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8271:                         } else {
 8272:                             my %captchas = &captcha_phrases();
 8273:                             if ($captchas{$savecaptcha{$type}}) {
 8274:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8275:                             } else {
 8276:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8277:                             }
 8278:                         }
 8279:                     } elsif ($type eq 'recaptchakeys') {
 8280:                         my ($privkey,$pubkey);
 8281:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8282:                             $pubkey = $savecaptcha{$type}{'public'};
 8283:                             $privkey = $savecaptcha{$type}{'private'};
 8284:                         }
 8285:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8286:                         if (!$pubkey) {
 8287:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8288:                         } else {
 8289:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8290:                         }
 8291:                         if (!$privkey) {
 8292:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8293:                         } else {
 8294:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8295:                         }
 8296:                         $chgtext .= '</ul>';
 8297:                     } elsif ($type eq 'emailusername') {
 8298:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8299:                             if (ref($types) eq 'ARRAY') {
 8300:                                 foreach my $type (@{$types}) {
 8301:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 8302:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 8303:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',$usertypes->{$type}).
 8304:                                                     '<ul>';
 8305:                                             foreach my $field (@{$infofields}) {
 8306:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 8307:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8308:                                                 }
 8309:                                             }
 8310:                                         }
 8311:                                         $chgtext .= '</ul>';
 8312:                                     } else {
 8313:                                         $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 />';
 8314:                                     }
 8315:                                 }
 8316:                             }
 8317:                         }
 8318:                     } elsif ($type eq 'notify') {
 8319:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8320:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8321:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8322:                                 if ($cancreate{'notify'}{'approval'}) {
 8323:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8324:                                 }
 8325:                             }
 8326:                         }
 8327:                     }
 8328:                     if ($chgtext) {
 8329:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8330:                     }
 8331:                 }
 8332:             }
 8333:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8334:                 my ($emailrules,$emailruleorder) =
 8335:                     &Apache::lonnet::inst_userrules($dom,'email');
 8336:                 my $chgtext = '<ul>';
 8337:                 foreach my $type (@email_rule) {
 8338:                     if (ref($emailrules->{$type}) eq 'HASH') {
 8339:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 8340:                     }
 8341:                 }
 8342:                 $chgtext .= '</ul>';
 8343:                 if (@email_rule > 0) {
 8344:                     $resulttext .= '<li>'.
 8345:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 8346:                                        $chgtext.
 8347:                                    '</li>';
 8348:                 } else {
 8349:                     $resulttext .= '<li>'.
 8350:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 8351:                                    '</li>';
 8352:                 }
 8353:             }
 8354:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 8355:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 8356:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8357:                 foreach my $type (@{$changes{'selfcreate'}}) {
 8358:                     my $typename = $type;
 8359:                     if (ref($usertypes) eq 'HASH') {
 8360:                         if ($usertypes->{$type} ne '') {
 8361:                             $typename = $usertypes->{$type};
 8362:                         }
 8363:                     }
 8364:                     my @modifiable;
 8365:                     $resulttext .= '<li>'.
 8366:                                     &mt('Self-creation of account by users with status: [_1]',
 8367:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 8368:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 8369:                     foreach my $field (@fields) {
 8370:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 8371:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 8372:                         }
 8373:                     }
 8374:                     if (@modifiable > 0) {
 8375:                         $resulttext .= join(', ',@modifiable);
 8376:                     } else {
 8377:                         $resulttext .= &mt('none');
 8378:                     }
 8379:                     $resulttext .= '</li>';
 8380:                 }
 8381:                 $resulttext .= '</ul></li>';
 8382:             }
 8383:             $resulttext .= '</ul>';
 8384:         } else {
 8385:             $resulttext = &mt('No changes made to self-creation settings');
 8386:         }
 8387:     } else {
 8388:         $resulttext = '<span class="LC_error">'.
 8389:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8390:     }
 8391:     if ($warningmsg ne '') {
 8392:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8393:     }
 8394:     return $resulttext;
 8395: }
 8396: 
 8397: sub process_captcha {
 8398:     my ($container,$changes,$newsettings,$current) = @_;
 8399:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 8400:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 8401:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 8402:         $newsettings->{'captcha'} = 'original';
 8403:     }
 8404:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 8405:         if ($container eq 'cancreate') {
 8406:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8407:                 push(@{$changes->{'cancreate'}},'captcha');
 8408:             } elsif (!defined($changes->{'cancreate'})) {
 8409:                 $changes->{'cancreate'} = ['captcha'];
 8410:             }
 8411:         } else {
 8412:             $changes->{'captcha'} = 1;
 8413:         }
 8414:     }
 8415:     my ($newpub,$newpriv,$currpub,$currpriv);
 8416:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 8417:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 8418:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 8419:         $newpub =~ s/\W//g;
 8420:         $newpriv =~ s/\W//g;
 8421:         $newsettings->{'recaptchakeys'} = {
 8422:                                              public  => $newpub,
 8423:                                              private => $newpriv,
 8424:                                           };
 8425:     }
 8426:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 8427:         $currpub = $current->{'recaptchakeys'}{'public'};
 8428:         $currpriv = $current->{'recaptchakeys'}{'private'};
 8429:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 8430:             $newsettings->{'recaptchakeys'} = {
 8431:                                                  public  => '',
 8432:                                                  private => '',
 8433:                                               }
 8434:         }
 8435:     }
 8436:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 8437:         if ($container eq 'cancreate') {
 8438:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8439:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 8440:             } elsif (!defined($changes->{'cancreate'})) {
 8441:                 $changes->{'cancreate'} = ['recaptchakeys'];
 8442:             }
 8443:         } else {
 8444:             $changes->{'recaptchakeys'} = 1;
 8445:         }
 8446:     }
 8447:     return;
 8448: }
 8449: 
 8450: sub modify_usermodification {
 8451:     my ($dom,%domconfig) = @_;
 8452:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 8453:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8454:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8455:             if ($key eq 'selfcreate') {
 8456:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 8457:             } else {  
 8458:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 8459:             }
 8460:         }
 8461:     }
 8462:     my @contexts = ('author','course');
 8463:     my %context_title = (
 8464:                            author => 'In author context',
 8465:                            course => 'In course context',
 8466:                         );
 8467:     my @fields = ('lastname','firstname','middlename','generation',
 8468:                   'permanentemail','id');
 8469:     my %roles = (
 8470:                   author => ['ca','aa'],
 8471:                   course => ['st','ep','ta','in','cr'],
 8472:                 );
 8473:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8474:     foreach my $context (@contexts) {
 8475:         foreach my $role (@{$roles{$context}}) {
 8476:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 8477:             foreach my $item (@fields) {
 8478:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 8479:                     $modifyhash{$context}{$role}{$item} = 1;
 8480:                 } else {
 8481:                     $modifyhash{$context}{$role}{$item} = 0;
 8482:                 }
 8483:             }
 8484:         }
 8485:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 8486:             foreach my $role (@{$roles{$context}}) {
 8487:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 8488:                     foreach my $field (@fields) {
 8489:                         if ($modifyhash{$context}{$role}{$field} ne 
 8490:                                 $curr_usermodification{$context}{$role}{$field}) {
 8491:                             push(@{$changes{$context}},$role);
 8492:                             last;
 8493:                         }
 8494:                     }
 8495:                 }
 8496:             }
 8497:         } else {
 8498:             foreach my $context (@contexts) {
 8499:                 foreach my $role (@{$roles{$context}}) {
 8500:                     push(@{$changes{$context}},$role);
 8501:                 }
 8502:             }
 8503:         }
 8504:     }
 8505:     my %usermodification_hash =  (
 8506:                                    usermodification => \%modifyhash,
 8507:                                  );
 8508:     my $putresult = &Apache::lonnet::put_dom('configuration',
 8509:                                              \%usermodification_hash,$dom);
 8510:     if ($putresult eq 'ok') {
 8511:         if (keys(%changes) > 0) {
 8512:             $resulttext = &mt('Changes made: ').'<ul>';
 8513:             foreach my $context (@contexts) {
 8514:                 if (ref($changes{$context}) eq 'ARRAY') {
 8515:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 8516:                     if (ref($changes{$context}) eq 'ARRAY') {
 8517:                         foreach my $role (@{$changes{$context}}) {
 8518:                             my $rolename;
 8519:                             if ($role eq 'cr') {
 8520:                                 $rolename = &mt('Custom');
 8521:                             } else {
 8522:                                 $rolename = &Apache::lonnet::plaintext($role);
 8523:                             }
 8524:                             my @modifiable;
 8525:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 8526:                             foreach my $field (@fields) {
 8527:                                 if ($modifyhash{$context}{$role}{$field}) {
 8528:                                     push(@modifiable,$fieldtitles{$field});
 8529:                                 }
 8530:                             }
 8531:                             if (@modifiable > 0) {
 8532:                                 $resulttext .= join(', ',@modifiable);
 8533:                             } else {
 8534:                                 $resulttext .= &mt('none'); 
 8535:                             }
 8536:                             $resulttext .= '</li>';
 8537:                         }
 8538:                         $resulttext .= '</ul></li>';
 8539:                     }
 8540:                 }
 8541:             }
 8542:             $resulttext .= '</ul>';
 8543:         } else {
 8544:             $resulttext = &mt('No changes made to user modification settings');
 8545:         }
 8546:     } else {
 8547:         $resulttext = '<span class="LC_error">'.
 8548:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8549:     }
 8550:     return $resulttext;
 8551: }
 8552: 
 8553: sub modify_defaults {
 8554:     my ($dom,$lastactref,%domconfig) = @_;
 8555:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 8556:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8557:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 8558:     my @authtypes = ('internal','krb4','krb5','localauth');
 8559:     foreach my $item (@items) {
 8560:         $newvalues{$item} = $env{'form.'.$item};
 8561:         if ($item eq 'auth_def') {
 8562:             if ($newvalues{$item} ne '') {
 8563:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 8564:                     push(@errors,$item);
 8565:                 }
 8566:             }
 8567:         } elsif ($item eq 'lang_def') {
 8568:             if ($newvalues{$item} ne '') {
 8569:                 if ($newvalues{$item} =~ /^(\w+)/) {
 8570:                     my $langcode = $1;
 8571:                     if ($langcode ne 'x_chef') {
 8572:                         if (code2language($langcode) eq '') {
 8573:                             push(@errors,$item);
 8574:                         }
 8575:                     }
 8576:                 } else {
 8577:                     push(@errors,$item);
 8578:                 }
 8579:             }
 8580:         } elsif ($item eq 'timezone_def') {
 8581:             if ($newvalues{$item} ne '') {
 8582:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 8583:                     push(@errors,$item);   
 8584:                 }
 8585:             }
 8586:         } elsif ($item eq 'datelocale_def') {
 8587:             if ($newvalues{$item} ne '') {
 8588:                 my @datelocale_ids = DateTime::Locale->ids();
 8589:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 8590:                     push(@errors,$item);
 8591:                 }
 8592:             }
 8593:         } elsif ($item eq 'portal_def') {
 8594:             if ($newvalues{$item} ne '') {
 8595:                 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])\/?$/) {
 8596:                     push(@errors,$item);
 8597:                 }
 8598:             }
 8599:         }
 8600:         if (grep(/^\Q$item\E$/,@errors)) {
 8601:             $newvalues{$item} = $domdefaults{$item};
 8602:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 8603:             $changes{$item} = 1;
 8604:         }
 8605:         $domdefaults{$item} = $newvalues{$item};
 8606:     }
 8607:     my %defaults_hash = (
 8608:                          defaults => \%newvalues,
 8609:                         );
 8610:     my $title = &defaults_titles();
 8611:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 8612:                                              $dom);
 8613:     if ($putresult eq 'ok') {
 8614:         if (keys(%changes) > 0) {
 8615:             $resulttext = &mt('Changes made:').'<ul>';
 8616:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 8617:             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";
 8618:             foreach my $item (sort(keys(%changes))) {
 8619:                 my $value = $env{'form.'.$item};
 8620:                 if ($value eq '') {
 8621:                     $value = &mt('none');
 8622:                 } elsif ($item eq 'auth_def') {
 8623:                     my %authnames = &authtype_names();
 8624:                     my %shortauth = (
 8625:                              internal => 'int',
 8626:                              krb4 => 'krb4',
 8627:                              krb5 => 'krb5',
 8628:                              localauth  => 'loc',
 8629:                     );
 8630:                     $value = $authnames{$shortauth{$value}};
 8631:                 }
 8632:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 8633:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 8634:             }
 8635:             $resulttext .= '</ul>';
 8636:             $mailmsgtext .= "\n";
 8637:             my $cachetime = 24*60*60;
 8638:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8639:             if (ref($lastactref) eq 'HASH') {
 8640:                 $lastactref->{'domdefaults'} = 1;
 8641:             }
 8642:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 8643:                 my $notify = 1;
 8644:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 8645:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 8646:                         $notify = 0;
 8647:                     }
 8648:                 }
 8649:                 if ($notify) {
 8650:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 8651:                                                "LON-CAPA Domain Settings Change - $dom",
 8652:                                                $mailmsgtext);
 8653:                 }
 8654:             }
 8655:         } else {
 8656:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 8657:         }
 8658:     } else {
 8659:         $resulttext = '<span class="LC_error">'.
 8660:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8661:     }
 8662:     if (@errors > 0) {
 8663:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 8664:         foreach my $item (@errors) {
 8665:             $resulttext .= ' "'.$title->{$item}.'",';
 8666:         }
 8667:         $resulttext =~ s/,$//;
 8668:     }
 8669:     return $resulttext;
 8670: }
 8671: 
 8672: sub modify_scantron {
 8673:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 8674:     my ($resulttext,%confhash,%changes,$errors);
 8675:     my $custom = 'custom.tab';
 8676:     my $default = 'default.tab';
 8677:     my $servadm = $r->dir_config('lonAdmEMail');
 8678:     my ($configuserok,$author_ok,$switchserver) = 
 8679:         &config_check($dom,$confname,$servadm);
 8680:     if ($env{'form.scantronformat.filename'} ne '') {
 8681:         my $error;
 8682:         if ($configuserok eq 'ok') {
 8683:             if ($switchserver) {
 8684:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 8685:             } else {
 8686:                 if ($author_ok eq 'ok') {
 8687:                     my ($result,$scantronurl) =
 8688:                         &publishlogo($r,'upload','scantronformat',$dom,
 8689:                                      $confname,'scantron','','',$custom);
 8690:                     if ($result eq 'ok') {
 8691:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 8692:                         $changes{'scantronformat'} = 1;
 8693:                     } else {
 8694:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 8695:                     }
 8696:                 } else {
 8697:                     $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);
 8698:                 }
 8699:             }
 8700:         } else {
 8701:             $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);
 8702:         }
 8703:         if ($error) {
 8704:             &Apache::lonnet::logthis($error);
 8705:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8706:         }
 8707:     }
 8708:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 8709:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 8710:             if ($env{'form.scantronformat_del'}) {
 8711:                 $confhash{'scantron'}{'scantronformat'} = '';
 8712:                 $changes{'scantronformat'} = 1;
 8713:             }
 8714:         }
 8715:     }
 8716:     if (keys(%confhash) > 0) {
 8717:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 8718:                                                  $dom);
 8719:         if ($putresult eq 'ok') {
 8720:             if (keys(%changes) > 0) {
 8721:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 8722:                     $resulttext = &mt('Changes made:').'<ul>';
 8723:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 8724:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 8725:                     } else {
 8726:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 8727:                     }
 8728:                     $resulttext .= '</ul>';
 8729:                 } else {
 8730:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 8731:                 }
 8732:                 $resulttext .= '</ul>';
 8733:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 8734:                 if (ref($lastactref) eq 'HASH') {
 8735:                     $lastactref->{'domainconfig'} = 1;
 8736:                 }
 8737:             } else {
 8738:                 $resulttext = &mt('No changes made to bubblesheet format file');
 8739:             }
 8740:         } else {
 8741:             $resulttext = '<span class="LC_error">'.
 8742:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 8743:         }
 8744:     } else {
 8745:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 8746:     }
 8747:     if ($errors) {
 8748:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8749:                        $errors.'</ul>';
 8750:     }
 8751:     return $resulttext;
 8752: }
 8753: 
 8754: sub modify_coursecategories {
 8755:     my ($dom,%domconfig) = @_;
 8756:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 8757:         $cathash);
 8758:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 8759:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8760:         $cathash = $domconfig{'coursecategories'}{'cats'};
 8761:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 8762:             $changes{'togglecats'} = 1;
 8763:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 8764:         }
 8765:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 8766:             $changes{'categorize'} = 1;
 8767:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 8768:         }
 8769:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 8770:             $changes{'togglecatscomm'} = 1;
 8771:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 8772:         }
 8773:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 8774:             $changes{'categorizecomm'} = 1;
 8775:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 8776:         }
 8777:     } else {
 8778:         $changes{'togglecats'} = 1;
 8779:         $changes{'categorize'} = 1;
 8780:         $changes{'togglecatscomm'} = 1;
 8781:         $changes{'categorizecomm'} = 1;
 8782:         $domconfig{'coursecategories'} = {
 8783:                                              togglecats => $env{'form.togglecats'},
 8784:                                              categorize => $env{'form.categorize'},
 8785:                                              togglecatscomm => $env{'form.togglecatscomm'},
 8786:                                              categorizecomm => $env{'form.categorizecomm'},
 8787:                                          };
 8788:     }
 8789:     if (ref($cathash) eq 'HASH') {
 8790:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 8791:             push (@deletecategory,'instcode::0');
 8792:         }
 8793:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 8794:             push(@deletecategory,'communities::0');
 8795:         }
 8796:     }
 8797:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 8798:     if (ref($cathash) eq 'HASH') {
 8799:         if (@deletecategory > 0) {
 8800:             #FIXME Need to remove category from all courses using a deleted category 
 8801:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 8802:             foreach my $item (@deletecategory) {
 8803:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 8804:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 8805:                     $deletions{$item} = 1;
 8806:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 8807:                 }
 8808:             }
 8809:         }
 8810:         foreach my $item (keys(%{$cathash})) {
 8811:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8812:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 8813:                 $reorderings{$item} = 1;
 8814:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 8815:             }
 8816:             if ($env{'form.addcategory_name_'.$item} ne '') {
 8817:                 my $newcat = $env{'form.addcategory_name_'.$item};
 8818:                 my $newdepth = $depth+1;
 8819:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8820:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 8821:                 $adds{$newitem} = 1; 
 8822:             }
 8823:             if ($env{'form.subcat_'.$item} ne '') {
 8824:                 my $newcat = $env{'form.subcat_'.$item};
 8825:                 my $newdepth = $depth+1;
 8826:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8827:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 8828:                 $adds{$newitem} = 1;
 8829:             }
 8830:         }
 8831:     }
 8832:     if ($env{'form.instcode'} eq '1') {
 8833:         if (ref($cathash) eq 'HASH') {
 8834:             my $newitem = 'instcode::0';
 8835:             if ($cathash->{$newitem} eq '') {  
 8836:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 8837:                 $adds{$newitem} = 1;
 8838:             }
 8839:         } else {
 8840:             my $newitem = 'instcode::0';
 8841:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 8842:             $adds{$newitem} = 1;
 8843:         }
 8844:     }
 8845:     if ($env{'form.communities'} eq '1') {
 8846:         if (ref($cathash) eq 'HASH') {
 8847:             my $newitem = 'communities::0';
 8848:             if ($cathash->{$newitem} eq '') {
 8849:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 8850:                 $adds{$newitem} = 1;
 8851:             }
 8852:         } else {
 8853:             my $newitem = 'communities::0';
 8854:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 8855:             $adds{$newitem} = 1;
 8856:         }
 8857:     }
 8858:     if ($env{'form.addcategory_name'} ne '') {
 8859:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 8860:             ($env{'form.addcategory_name'} ne 'communities')) {
 8861:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 8862:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 8863:             $adds{$newitem} = 1;
 8864:         }
 8865:     }
 8866:     my $putresult;
 8867:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8868:         if (keys(%deletions) > 0) {
 8869:             foreach my $key (keys(%deletions)) {
 8870:                 if ($predelallitems{$key} ne '') {
 8871:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 8872:                 }
 8873:             }
 8874:         }
 8875:         my (@chkcats,@chktrails,%chkallitems);
 8876:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 8877:         if (ref($chkcats[0]) eq 'ARRAY') {
 8878:             my $depth = 0;
 8879:             my $chg = 0;
 8880:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 8881:                 my $name = $chkcats[0][$i];
 8882:                 my $item;
 8883:                 if ($name eq '') {
 8884:                     $chg ++;
 8885:                 } else {
 8886:                     $item = &escape($name).'::0';
 8887:                     if ($chg) {
 8888:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 8889:                     }
 8890:                     $depth ++; 
 8891:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 8892:                     $depth --;
 8893:                 }
 8894:             }
 8895:         }
 8896:     }
 8897:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8898:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 8899:         if ($putresult eq 'ok') {
 8900:             my %title = (
 8901:                          togglecats     => 'Show/Hide a course in catalog',
 8902:                          categorize     => 'Assign a category to a course',
 8903:                          togglecatscomm => 'Show/Hide a community in catalog',
 8904:                          categorizecomm => 'Assign a category to a community',
 8905:                         );
 8906:             my %level = (
 8907:                          dom  => 'set in Domain ("Modify Course/Community")',
 8908:                          crs  => 'set in Course ("Course Configuration")',
 8909:                          comm => 'set in Community ("Community Configuration")',
 8910:                         );
 8911:             $resulttext = &mt('Changes made:').'<ul>';
 8912:             if ($changes{'togglecats'}) {
 8913:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 8914:             }
 8915:             if ($changes{'categorize'}) {
 8916:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 8917:             }
 8918:             if ($changes{'togglecatscomm'}) {
 8919:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 8920:             }
 8921:             if ($changes{'categorizecomm'}) {
 8922:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 8923:             }
 8924:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8925:                 my $cathash;
 8926:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8927:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 8928:                 } else {
 8929:                     $cathash = {};
 8930:                 } 
 8931:                 my (@cats,@trails,%allitems);
 8932:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 8933:                 if (keys(%deletions) > 0) {
 8934:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 8935:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 8936:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 8937:                     }
 8938:                     $resulttext .= '</ul></li>';
 8939:                 }
 8940:                 if (keys(%reorderings) > 0) {
 8941:                     my %sort_by_trail;
 8942:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 8943:                     foreach my $key (keys(%reorderings)) {
 8944:                         if ($allitems{$key} ne '') {
 8945:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 8946:                         }
 8947:                     }
 8948:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 8949:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 8950:                     }
 8951:                     $resulttext .= '</ul></li>';
 8952:                 }
 8953:                 if (keys(%adds) > 0) {
 8954:                     my %sort_by_trail;
 8955:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 8956:                     foreach my $key (keys(%adds)) {
 8957:                         if ($allitems{$key} ne '') {
 8958:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 8959:                         }
 8960:                     }
 8961:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 8962:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 8963:                     }
 8964:                     $resulttext .= '</ul></li>';
 8965:                 }
 8966:             }
 8967:             $resulttext .= '</ul>';
 8968:         } else {
 8969:             $resulttext = '<span class="LC_error">'.
 8970:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8971:         }
 8972:     } else {
 8973:         $resulttext = &mt('No changes made to course and community categories');
 8974:     }
 8975:     return $resulttext;
 8976: }
 8977: 
 8978: sub modify_serverstatuses {
 8979:     my ($dom,%domconfig) = @_;
 8980:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 8981:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 8982:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 8983:     }
 8984:     my @pages = &serverstatus_pages();
 8985:     foreach my $type (@pages) {
 8986:         $newserverstatus{$type}{'namedusers'} = '';
 8987:         $newserverstatus{$type}{'machines'} = '';
 8988:         if (defined($env{'form.'.$type.'_namedusers'})) {
 8989:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 8990:             my @okusers;
 8991:             foreach my $user (@users) {
 8992:                 my ($uname,$udom) = split(/:/,$user);
 8993:                 if (($udom =~ /^$match_domain$/) &&   
 8994:                     (&Apache::lonnet::domain($udom)) &&
 8995:                     ($uname =~ /^$match_username$/)) {
 8996:                     if (!grep(/^\Q$user\E/,@okusers)) {
 8997:                         push(@okusers,$user);
 8998:                     }
 8999:                 }
 9000:             }
 9001:             if (@okusers > 0) {
 9002:                  @okusers = sort(@okusers);
 9003:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 9004:             }
 9005:         }
 9006:         if (defined($env{'form.'.$type.'_machines'})) {
 9007:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 9008:             my @okmachines;
 9009:             foreach my $ip (@machines) {
 9010:                 my @parts = split(/\./,$ip);
 9011:                 next if (@parts < 4);
 9012:                 my $badip = 0;
 9013:                 for (my $i=0; $i<4; $i++) {
 9014:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 9015:                         $badip = 1;
 9016:                         last;
 9017:                     }
 9018:                 }
 9019:                 if (!$badip) {
 9020:                     push(@okmachines,$ip);     
 9021:                 }
 9022:             }
 9023:             @okmachines = sort(@okmachines);
 9024:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 9025:         }
 9026:     }
 9027:     my %serverstatushash =  (
 9028:                                 serverstatuses => \%newserverstatus,
 9029:                             );
 9030:     foreach my $type (@pages) {
 9031:         foreach my $setting ('namedusers','machines') {
 9032:             my (@current,@new);
 9033:             if (ref($currserverstatus{$type}) eq 'HASH') {
 9034:                 if ($currserverstatus{$type}{$setting} ne '') { 
 9035:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 9036:                 }
 9037:             }
 9038:             if ($newserverstatus{$type}{$setting} ne '') {
 9039:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 9040:             }
 9041:             if (@current > 0) {
 9042:                 if (@new > 0) {
 9043:                     foreach my $item (@current) {
 9044:                         if (!grep(/^\Q$item\E$/,@new)) {
 9045:                             $changes{$type}{$setting} = 1;
 9046:                             last;
 9047:                         }
 9048:                     }
 9049:                     foreach my $item (@new) {
 9050:                         if (!grep(/^\Q$item\E$/,@current)) {
 9051:                             $changes{$type}{$setting} = 1;
 9052:                             last;
 9053:                         }
 9054:                     }
 9055:                 } else {
 9056:                     $changes{$type}{$setting} = 1;
 9057:                 }
 9058:             } elsif (@new > 0) {
 9059:                 $changes{$type}{$setting} = 1;
 9060:             }
 9061:         }
 9062:     }
 9063:     if (keys(%changes) > 0) {
 9064:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9065:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9066:                                                  \%serverstatushash,$dom);
 9067:         if ($putresult eq 'ok') {
 9068:             $resulttext .= &mt('Changes made:').'<ul>';
 9069:             foreach my $type (@pages) {
 9070:                 if (ref($changes{$type}) eq 'HASH') {
 9071:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 9072:                     if ($changes{$type}{'namedusers'}) {
 9073:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 9074:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 9075:                         } else {
 9076:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 9077:                         }
 9078:                     }
 9079:                     if ($changes{$type}{'machines'}) {
 9080:                         if ($newserverstatus{$type}{'machines'} eq '') {
 9081:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 9082:                         } else {
 9083:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 9084:                         }
 9085: 
 9086:                     }
 9087:                     $resulttext .= '</ul></li>';
 9088:                 }
 9089:             }
 9090:             $resulttext .= '</ul>';
 9091:         } else {
 9092:             $resulttext = '<span class="LC_error">'.
 9093:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 9094: 
 9095:         }
 9096:     } else {
 9097:         $resulttext = &mt('No changes made to access to server status pages');
 9098:     }
 9099:     return $resulttext;
 9100: }
 9101: 
 9102: sub modify_helpsettings {
 9103:     my ($r,$dom,$confname,%domconfig) = @_;
 9104:     my ($resulttext,$errors,%changes,%helphash);
 9105:     my %defaultchecked = ('submitbugs' => 'on');
 9106:     my @offon = ('off','on');
 9107:     my @toggles = ('submitbugs');
 9108:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 9109:         foreach my $item (@toggles) {
 9110:             if ($defaultchecked{$item} eq 'on') { 
 9111:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9112:                     if ($env{'form.'.$item} eq '0') {
 9113:                         $changes{$item} = 1;
 9114:                     }
 9115:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9116:                     $changes{$item} = 1;
 9117:                 }
 9118:             } elsif ($defaultchecked{$item} eq 'off') {
 9119:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9120:                     if ($env{'form.'.$item} eq '1') {
 9121:                         $changes{$item} = 1;
 9122:                     }
 9123:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9124:                     $changes{$item} = 1;
 9125:                 }
 9126:             }
 9127:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 9128:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 9129:             }
 9130:         }
 9131:     }
 9132:     my $putresult;
 9133:     if (keys(%changes) > 0) {
 9134:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 9135:         if ($putresult eq 'ok') {
 9136:             $resulttext = &mt('Changes made:').'<ul>';
 9137:             foreach my $item (sort(keys(%changes))) {
 9138:                 if ($item eq 'submitbugs') {
 9139:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 9140:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 9141:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 9142:                 }
 9143:             }
 9144:             $resulttext .= '</ul>';
 9145:         } else {
 9146:             $resulttext = &mt('No changes made to help settings');
 9147:             $errors .= '<li><span class="LC_error">'.
 9148:                        &mt('An error occurred storing the settings: [_1]',
 9149:                            $putresult).'</span></li>';
 9150:         }
 9151:     }
 9152:     if ($errors) {
 9153:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9154:                        $errors.'</ul>';
 9155:     }
 9156:     return $resulttext;
 9157: }
 9158: 
 9159: sub modify_coursedefaults {
 9160:     my ($dom,$lastactref,%domconfig) = @_;
 9161:     my ($resulttext,$errors,%changes,%defaultshash);
 9162:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 9163:     my @toggles = ('canuse_pdfforms');
 9164:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 9165:                    'uploadquota_community','uploadquota_textbook');
 9166:     my @types = ('official','unofficial','community','textbook');
 9167:     my %staticdefaults = (
 9168:                            anonsurvey_threshold => 10,
 9169:                            uploadquota          => 500,
 9170:                          );
 9171: 
 9172:     $defaultshash{'coursedefaults'} = {};
 9173: 
 9174:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 9175:         if ($domconfig{'coursedefaults'} eq '') {
 9176:             $domconfig{'coursedefaults'} = {};
 9177:         }
 9178:     }
 9179: 
 9180:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 9181:         foreach my $item (@toggles) {
 9182:             if ($defaultchecked{$item} eq 'on') {
 9183:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9184:                     ($env{'form.'.$item} eq '0')) {
 9185:                     $changes{$item} = 1;
 9186:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9187:                     $changes{$item} = 1;
 9188:                 }
 9189:             } elsif ($defaultchecked{$item} eq 'off') {
 9190:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9191:                     ($env{'form.'.$item} eq '1')) {
 9192:                     $changes{$item} = 1;
 9193:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9194:                     $changes{$item} = 1;
 9195:                 }
 9196:             }
 9197:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 9198:         }
 9199:         foreach my $item (@numbers) {
 9200:             my ($currdef,$newdef);
 9201:             $newdef = $env{'form.'.$item};
 9202:             if ($item eq 'anonsurvey_threshold') {
 9203:                 $currdef = $domconfig{'coursedefaults'}{$item};
 9204:                 $newdef =~ s/\D//g;
 9205:                 if ($newdef eq '' || $newdef < 1) {
 9206:                     $newdef = 1;
 9207:                 }
 9208:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 9209:             } else {
 9210:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 9211:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9212:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 9213:                 }
 9214:                 $newdef =~ s/[^\w.\-]//g;
 9215:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 9216:             }
 9217:             if ($currdef ne $newdef) {
 9218:                 my $staticdef;
 9219:                 if ($item eq 'anonsurvey_threshold') {
 9220:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
 9221:                         $changes{$item} = 1;
 9222:                     }
 9223:                 } else {
 9224:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
 9225:                         $changes{'uploadquota'} = 1;
 9226:                     }
 9227:                 }
 9228:             }
 9229:         }
 9230: 
 9231:         my $officialcreds = $env{'form.official_credits'};
 9232:         $officialcreds =~ s/[^\d.]+//g;
 9233:         my $unofficialcreds = $env{'form.unofficial_credits'};
 9234:         $unofficialcreds =~ s/[^\d.]+//g;
 9235:         my $textbookcreds = $env{'form.textbook_credits'};
 9236:         $textbookcreds =~ s/[^\d.]+//g;
 9237:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 9238:                 ($env{'form.coursecredits'} eq '1')) {
 9239:                 $changes{'coursecredits'} = 1;
 9240:         } else {
 9241:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 9242:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
 9243:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
 9244:                 $changes{'coursecredits'} = 1;
 9245:             }
 9246:         }
 9247:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 9248:             official   => $officialcreds,
 9249:             unofficial => $unofficialcreds,
 9250:             textbook   => $textbookcreds,
 9251:         }
 9252:     }
 9253:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9254:                                              $dom);
 9255:     if ($putresult eq 'ok') {
 9256:         if (keys(%changes) > 0) {
 9257:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9258:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || 
 9259:                 ($changes{'uploadquota'})) { 
 9260:                 if ($changes{'canuse_pdfforms'}) {
 9261:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 9262:                 }
 9263:                 if ($changes{'coursecredits'}) {
 9264:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 9265:                         $domdefaults{'officialcredits'} =
 9266:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 9267:                         $domdefaults{'unofficialcredits'} =
 9268:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 9269:                         $domdefaults{'textbookcredits'} =
 9270:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
 9271:                     }
 9272:                 }
 9273:                 if ($changes{'uploadquota'}) {
 9274:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9275:                         foreach my $type (@types) {
 9276:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
 9277:                         }
 9278:                     }
 9279:                 }
 9280:                 my $cachetime = 24*60*60;
 9281:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9282:                 if (ref($lastactref) eq 'HASH') {
 9283:                     $lastactref->{'domdefaults'} = 1;
 9284:                 }
 9285:             }
 9286:             $resulttext = &mt('Changes made:').'<ul>';
 9287:             foreach my $item (sort(keys(%changes))) {
 9288:                 if ($item eq 'canuse_pdfforms') {
 9289:                     if ($env{'form.'.$item} eq '1') {
 9290:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 9291:                     } else {
 9292:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 9293:                     }
 9294:                 } elsif ($item eq 'anonsurvey_threshold') {
 9295:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 9296:                 } elsif ($item eq 'uploadquota') {
 9297:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9298:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
 9299:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
 9300:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
 9301:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
 9302: 
 9303:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
 9304:                                        '</ul>'.
 9305:                                        '</li>';
 9306:                     } else {
 9307:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
 9308:                     }
 9309:                 } elsif ($item eq 'coursecredits') {
 9310:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 9311:                         if (($domdefaults{'officialcredits'} eq '') &&
 9312:                             ($domdefaults{'unofficialcredits'} eq '') &&
 9313:                             ($domdefaults{'textbookcredits'} eq '')) {
 9314:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 9315:                         } else {
 9316:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
 9317:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
 9318:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
 9319:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
 9320:                                            '</ul>'.
 9321:                                            '</li>';
 9322:                         }
 9323:                     } else {
 9324:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 9325:                     }
 9326:                 }
 9327:             }
 9328:             $resulttext .= '</ul>';
 9329:         } else {
 9330:             $resulttext = &mt('No changes made to course defaults');
 9331:         }
 9332:     } else {
 9333:         $resulttext = '<span class="LC_error">'.
 9334:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9335:     }
 9336:     return $resulttext;
 9337: }
 9338: 
 9339: sub modify_selfenrollment {
 9340:     my ($dom,$lastactref,%domconfig) = @_;
 9341:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
 9342:     my @types = ('official','unofficial','community','textbook');
 9343:     my %titles = &tool_titles();
 9344:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 9345:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 9346:     $ordered{'default'} = ['types','registered','approval','limit'];
 9347: 
 9348:     my (%roles,%shown,%toplevel);
 9349:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
 9350: 
 9351:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
 9352:         if ($domconfig{'selfenrollment'} eq '') {
 9353:             $domconfig{'selfenrollment'} = {};
 9354:         }
 9355:     }
 9356:     %toplevel = (
 9357:                   admin      => 'Configuration Rights',
 9358:                   default    => 'Default settings',
 9359:                   validation => 'Validation of self-enrollment requests',
 9360:                 );
 9361:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 9362: 
 9363:     if (ref($ordered{'admin'}) eq 'ARRAY') {
 9364:         foreach my $item (@{$ordered{'admin'}}) {
 9365:             foreach my $type (@types) {
 9366:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
 9367:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
 9368:                 } else {
 9369:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
 9370:                 }
 9371:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 9372:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 9373:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
 9374:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
 9375:                             push(@{$changes{'admin'}{$type}},$item);
 9376:                         }
 9377:                     } else {
 9378:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
 9379:                             push(@{$changes{'admin'}{$type}},$item);
 9380:                         }
 9381:                     }
 9382:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
 9383:                     push(@{$changes{'admin'}{$type}},$item);
 9384:                 }
 9385:             }
 9386:         }
 9387:     }
 9388: 
 9389:     foreach my $item (@{$ordered{'default'}}) {
 9390:         foreach my $type (@types) {
 9391:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
 9392:             if ($item eq 'types') {
 9393:                 unless (($value eq 'all') || ($value eq 'dom')) {
 9394:                     $value = '';
 9395:                 }
 9396:             } elsif ($item eq 'registered') {
 9397:                 unless ($value eq '1') {
 9398:                     $value = 0;
 9399:                 }
 9400:             } elsif ($item eq 'approval') {
 9401:                 unless ($value =~ /^[012]$/) {
 9402:                     $value = 0;
 9403:                 }
 9404:             } else {
 9405:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
 9406:                     $value = 'none';
 9407:                 }
 9408:             }
 9409:             $selfenrollhash{'default'}{$type}{$item} = $value;
 9410:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 9411:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 9412:                     if ($selfenrollhash{'default'}{$type}{$item} ne
 9413:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
 9414:                          push(@{$changes{'default'}{$type}},$item);
 9415:                     }
 9416:                 } else {
 9417:                     push(@{$changes{'default'}{$type}},$item);
 9418:                 }
 9419:             } else {
 9420:                 push(@{$changes{'default'}{$type}},$item);
 9421:             }
 9422:             if ($item eq 'limit') {
 9423:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
 9424:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
 9425:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
 9426:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
 9427:                     }
 9428:                 } else {
 9429:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
 9430:                 }
 9431:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 9432:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
 9433:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
 9434:                          push(@{$changes{'default'}{$type}},'cap');
 9435:                     }
 9436:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
 9437:                     push(@{$changes{'default'}{$type}},'cap');
 9438:                 }
 9439:             }
 9440:         }
 9441:     }
 9442: 
 9443:     foreach my $item (@{$itemsref}) {
 9444:         if ($item eq 'fields') {
 9445:             my @changed;
 9446:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
 9447:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
 9448:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
 9449:             }
 9450:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
 9451:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
 9452:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
 9453:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
 9454:                 } else {
 9455:                     @changed = @{$selfenrollhash{'validation'}{$item}};
 9456:                 }
 9457:             } else {
 9458:                 @changed = @{$selfenrollhash{'validation'}{$item}};
 9459:             }
 9460:             if (@changed) {
 9461:                 if ($selfenrollhash{'validation'}{$item}) { 
 9462:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
 9463:                 } else {
 9464:                     $changes{'validation'}{$item} = &mt('None');
 9465:                 }
 9466:             }
 9467:         } else {
 9468:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
 9469:             if ($item eq 'markup') {
 9470:                if ($env{'form.selfenroll_validation_'.$item}) {
 9471:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9472:                }
 9473:             }
 9474:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
 9475:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
 9476:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
 9477:                 }
 9478:             }
 9479:         }
 9480:     }
 9481: 
 9482:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
 9483:                                              $dom);
 9484:     if ($putresult eq 'ok') {
 9485:         if (keys(%changes) > 0) {
 9486:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9487:             $resulttext = &mt('Changes made:').'<ul>';
 9488:             foreach my $key ('admin','default','validation') {
 9489:                 if (ref($changes{$key}) eq 'HASH') {
 9490:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
 9491:                     if ($key eq 'validation') {
 9492:                         foreach my $item (@{$itemsref}) {
 9493:                             if (exists($changes{$key}{$item})) {
 9494:                                 if ($item eq 'markup') {
 9495:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
 9496:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
 9497:                                 } else {  
 9498:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
 9499:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
 9500:                                 }
 9501:                             }
 9502:                         }
 9503:                     } else {
 9504:                         foreach my $type (@types) {
 9505:                             if ($type eq 'community') {
 9506:                                 $roles{'1'} = &mt('Community personnel');
 9507:                             } else {
 9508:                                 $roles{'1'} = &mt('Course personnel');
 9509:                             }
 9510:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
 9511:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
 9512:                                     if ($key eq 'admin') {
 9513:                                         my @mgrdc = ();
 9514:                                         if (ref($ordered{$key}) eq 'ARRAY') {
 9515:                                             foreach my $item (@{$ordered{'admin'}}) {
 9516:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
 9517:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
 9518:                                                         push(@mgrdc,$item);
 9519:                                                     }
 9520:                                                 }
 9521:                                             }
 9522:                                             if (@mgrdc) {
 9523:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 9524:                                             } else {
 9525:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
 9526:                                             }
 9527:                                         }
 9528:                                     } else {
 9529:                                         if (ref($ordered{$key}) eq 'ARRAY') {
 9530:                                             foreach my $item (@{$ordered{$key}}) {
 9531:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
 9532:                                                     $domdefaults{$type.'selfenroll'.$item} =
 9533:                                                         $selfenrollhash{$key}{$type}{$item};
 9534:                                                 }
 9535:                                             }
 9536:                                         }
 9537:                                     }
 9538:                                 }
 9539:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
 9540:                                 foreach my $item (@{$ordered{$key}}) {
 9541:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
 9542:                                         $resulttext .= '<li>';
 9543:                                         if ($key eq 'admin') {
 9544:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
 9545:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
 9546:                                         } else {
 9547:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
 9548:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
 9549:                                         }
 9550:                                         $resulttext .= '</li>';
 9551:                                     }
 9552:                                 }
 9553:                                 $resulttext .= '</ul></li>';
 9554:                             }
 9555:                         }
 9556:                         $resulttext .= '</ul></li>'; 
 9557:                     }
 9558:                 }
 9559:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
 9560:                     my $cachetime = 24*60*60;
 9561:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9562:                     if (ref($lastactref) eq 'HASH') {
 9563:                         $lastactref->{'domdefaults'} = 1;
 9564:                     }
 9565:                 }
 9566:             }
 9567:             $resulttext .= '</ul>';
 9568:         } else {
 9569:             $resulttext = &mt('No changes made to self-enrollment settings');
 9570:         }
 9571:     } else {
 9572:         $resulttext = '<span class="LC_error">'.
 9573:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9574:     }
 9575:     return $resulttext;
 9576: }
 9577: 
 9578: sub modify_usersessions {
 9579:     my ($dom,$lastactref,%domconfig) = @_;
 9580:     my @hostingtypes = ('version','excludedomain','includedomain');
 9581:     my @offloadtypes = ('primary','default');
 9582:     my %types = (
 9583:                   remote => \@hostingtypes,
 9584:                   hosted => \@hostingtypes,
 9585:                   spares => \@offloadtypes,
 9586:                 );
 9587:     my @prefixes = ('remote','hosted','spares');
 9588:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 9589:     my (%by_ip,%by_location,@intdoms);
 9590:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 9591:     my @locations = sort(keys(%by_location));
 9592:     my (%defaultshash,%changes);
 9593:     foreach my $prefix (@prefixes) {
 9594:         $defaultshash{'usersessions'}{$prefix} = {};
 9595:     }
 9596:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9597:     my $resulttext;
 9598:     my %iphost = &Apache::lonnet::get_iphost();
 9599:     foreach my $prefix (@prefixes) {
 9600:         next if ($prefix eq 'spares');
 9601:         foreach my $type (@{$types{$prefix}}) {
 9602:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 9603:             if ($type eq 'version') {
 9604:                 my $value = $env{'form.'.$prefix.'_'.$type};
 9605:                 my $okvalue;
 9606:                 if ($value ne '') {
 9607:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 9608:                         $okvalue = $value;
 9609:                     }
 9610:                 }
 9611:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9612:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 9613:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 9614:                             if ($inuse == 0) {
 9615:                                 $changes{$prefix}{$type} = 1;
 9616:                             } else {
 9617:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 9618:                                     $changes{$prefix}{$type} = 1;
 9619:                                 }
 9620:                                 if ($okvalue ne '') {
 9621:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9622:                                 } 
 9623:                             }
 9624:                         } else {
 9625:                             if (($inuse == 1) && ($okvalue ne '')) {
 9626:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9627:                                 $changes{$prefix}{$type} = 1;
 9628:                             }
 9629:                         }
 9630:                     } else {
 9631:                         if (($inuse == 1) && ($okvalue ne '')) {
 9632:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9633:                             $changes{$prefix}{$type} = 1;
 9634:                         }
 9635:                     }
 9636:                 } else {
 9637:                     if (($inuse == 1) && ($okvalue ne '')) {
 9638:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9639:                         $changes{$prefix}{$type} = 1;
 9640:                     }
 9641:                 }
 9642:             } else {
 9643:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 9644:                 my @okvals;
 9645:                 foreach my $val (@vals) {
 9646:                     if ($val =~ /:/) {
 9647:                         my @items = split(/:/,$val);
 9648:                         foreach my $item (@items) {
 9649:                             if (ref($by_location{$item}) eq 'ARRAY') {
 9650:                                 push(@okvals,$item);
 9651:                             }
 9652:                         }
 9653:                     } else {
 9654:                         if (ref($by_location{$val}) eq 'ARRAY') {
 9655:                             push(@okvals,$val);
 9656:                         }
 9657:                     }
 9658:                 }
 9659:                 @okvals = sort(@okvals);
 9660:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9661:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 9662:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 9663:                             if ($inuse == 0) {
 9664:                                 $changes{$prefix}{$type} = 1; 
 9665:                             } else {
 9666:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9667:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 9668:                                 if (@changed > 0) {
 9669:                                     $changes{$prefix}{$type} = 1;
 9670:                                 }
 9671:                             }
 9672:                         } else {
 9673:                             if ($inuse == 1) {
 9674:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9675:                                 $changes{$prefix}{$type} = 1;
 9676:                             }
 9677:                         } 
 9678:                     } else {
 9679:                         if ($inuse == 1) {
 9680:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9681:                             $changes{$prefix}{$type} = 1;
 9682:                         }
 9683:                     }
 9684:                 } else {
 9685:                     if ($inuse == 1) {
 9686:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9687:                         $changes{$prefix}{$type} = 1;
 9688:                     }
 9689:                 }
 9690:             }
 9691:         }
 9692:     }
 9693: 
 9694:     my @alldoms = &Apache::lonnet::all_domains();
 9695:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9696:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 9697:     my $savespares;
 9698: 
 9699:     foreach my $lonhost (sort(keys(%servers))) {
 9700:         my $serverhomeID =
 9701:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 9702:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 9703:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 9704:         my %spareschg;
 9705:         foreach my $type (@{$types{'spares'}}) {
 9706:             my @okspares;
 9707:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 9708:             foreach my $server (@checked) {
 9709:                 if (&Apache::lonnet::hostname($server) ne '') {
 9710:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 9711:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 9712:                             push(@okspares,$server);
 9713:                         }
 9714:                     }
 9715:                 }
 9716:             }
 9717:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 9718:             my $newspare;
 9719:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 9720:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 9721:                     $newspare = $new;
 9722:                 }
 9723:             }
 9724:             my @spares;
 9725:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 9726:                 @spares = sort(@okspares,$newspare);
 9727:             } else {
 9728:                 @spares = sort(@okspares);
 9729:             }
 9730:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 9731:             if (ref($spareid{$lonhost}) eq 'HASH') {
 9732:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 9733:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 9734:                     if (@diffs > 0) {
 9735:                         $spareschg{$type} = 1;
 9736:                     }
 9737:                 }
 9738:             }
 9739:         }
 9740:         if (keys(%spareschg) > 0) {
 9741:             $changes{'spares'}{$lonhost} = \%spareschg;
 9742:         }
 9743:     }
 9744: 
 9745:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9746:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 9747:             if (ref($changes{'spares'}) eq 'HASH') {
 9748:                 if (keys(%{$changes{'spares'}}) > 0) {
 9749:                     $savespares = 1;
 9750:                 }
 9751:             }
 9752:         } else {
 9753:             $savespares = 1;
 9754:         }
 9755:     }
 9756: 
 9757:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 9758:     if ((keys(%changes) > 0) || ($savespares)) {
 9759:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9760:                                                  $dom);
 9761:         if ($putresult eq 'ok') {
 9762:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 9763:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 9764:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 9765:                 }
 9766:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 9767:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 9768:                 }
 9769:             }
 9770:             my $cachetime = 24*60*60;
 9771:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9772:             if (ref($lastactref) eq 'HASH') {
 9773:                 $lastactref->{'domdefaults'} = 1;
 9774:             }
 9775:             if (keys(%changes) > 0) {
 9776:                 my %lt = &usersession_titles();
 9777:                 $resulttext = &mt('Changes made:').'<ul>';
 9778:                 foreach my $prefix (@prefixes) {
 9779:                     if (ref($changes{$prefix}) eq 'HASH') {
 9780:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 9781:                         if ($prefix eq 'spares') {
 9782:                             if (ref($changes{$prefix}) eq 'HASH') {
 9783:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 9784:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 9785:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 9786:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
 9787:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
 9788:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 9789:                                         foreach my $type (@{$types{$prefix}}) {
 9790:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 9791:                                                 my $offloadto = &mt('None');
 9792:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 9793:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 9794:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 9795:                                                     }
 9796:                                                 }
 9797:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 9798:                                             }
 9799:                                         }
 9800:                                     }
 9801:                                     $resulttext .= '</li>';
 9802:                                 }
 9803:                             }
 9804:                         } else {
 9805:                             foreach my $type (@{$types{$prefix}}) {
 9806:                                 if (defined($changes{$prefix}{$type})) {
 9807:                                     my $newvalue;
 9808:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 9809:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 9810:                                             if ($type eq 'version') {
 9811:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 9812:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 9813:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 9814:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 9815:                                                 }
 9816:                                             }
 9817:                                         }
 9818:                                     }
 9819:                                     if ($newvalue eq '') {
 9820:                                         if ($type eq 'version') {
 9821:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 9822:                                         } else {
 9823:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 9824:                                         }
 9825:                                     } else {
 9826:                                         if ($type eq 'version') {
 9827:                                             $newvalue .= ' '.&mt('(or later)'); 
 9828:                                         }
 9829:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 9830:                                     }
 9831:                                 }
 9832:                             }
 9833:                         }
 9834:                         $resulttext .= '</ul>';
 9835:                     }
 9836:                 }
 9837:                 $resulttext .= '</ul>';
 9838:             } else {
 9839:                 $resulttext = $nochgmsg;
 9840:             }
 9841:         } else {
 9842:             $resulttext = '<span class="LC_error">'.
 9843:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9844:         }
 9845:     } else {
 9846:         $resulttext = $nochgmsg;
 9847:     }
 9848:     return $resulttext;
 9849: }
 9850: 
 9851: sub modify_loadbalancing {
 9852:     my ($dom,%domconfig) = @_;
 9853:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9854:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9855:     my ($othertitle,$usertypes,$types) =
 9856:         &Apache::loncommon::sorted_inst_types($dom);
 9857:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9858:     my @sparestypes = ('primary','default');
 9859:     my %typetitles = &sparestype_titles();
 9860:     my $resulttext;
 9861:     my (%currbalancer,%currtargets,%currrules,%existing);
 9862:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 9863:         %existing = %{$domconfig{'loadbalancing'}};
 9864:     }
 9865:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 9866:                               \%currtargets,\%currrules);
 9867:     my ($saveloadbalancing,%defaultshash,%changes);
 9868:     my ($alltypes,$othertypes,$titles) =
 9869:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 9870:     my %ruletitles = &offloadtype_text();
 9871:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 9872:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 9873:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 9874:         if ($balancer eq '') {
 9875:             next;
 9876:         }
 9877:         if (!exists($servers{$balancer})) {
 9878:             if (exists($currbalancer{$balancer})) {
 9879:                 push(@{$changes{'delete'}},$balancer);
 9880:             }
 9881:             next;
 9882:         }
 9883:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 9884:             push(@{$changes{'delete'}},$balancer);
 9885:             next;
 9886:         }
 9887:         if (!exists($currbalancer{$balancer})) {
 9888:             push(@{$changes{'add'}},$balancer);
 9889:         }
 9890:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 9891:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 9892:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 9893:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 9894:             $saveloadbalancing = 1;
 9895:         }
 9896:         foreach my $sparetype (@sparestypes) {
 9897:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 9898:             my @offloadto;
 9899:             foreach my $target (@targets) {
 9900:                 if (($servers{$target}) && ($target ne $balancer)) {
 9901:                     if ($sparetype eq 'default') {
 9902:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 9903:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 9904:                         }
 9905:                     }
 9906:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 9907:                         push(@offloadto,$target);
 9908:                     }
 9909:                 }
 9910:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 9911:             }
 9912:         }
 9913:         if (ref($currtargets{$balancer}) eq 'HASH') {
 9914:             foreach my $sparetype (@sparestypes) {
 9915:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 9916:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 9917:                     if (@targetdiffs > 0) {
 9918:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 9919:                     }
 9920:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9921:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9922:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 9923:                     }
 9924:                 }
 9925:             }
 9926:         } else {
 9927:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 9928:                 foreach my $sparetype (@sparestypes) {
 9929:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9930:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9931:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 9932:                         }
 9933:                     }
 9934:                 }
 9935:             }
 9936:         }
 9937:         my $ishomedom;
 9938:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 9939:             $ishomedom = 1;
 9940:         }
 9941:         if (ref($alltypes) eq 'ARRAY') {
 9942:             foreach my $type (@{$alltypes}) {
 9943:                 my $rule;
 9944:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
 9945:                          (!$ishomedom)) {
 9946:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 9947:                 }
 9948:                 if ($rule eq 'specific') {
 9949:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 9950:                 }
 9951:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 9952:                 if (ref($currrules{$balancer}) eq 'HASH') {
 9953:                     if ($rule ne $currrules{$balancer}{$type}) {
 9954:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 9955:                     }
 9956:                 } elsif ($rule ne '') {
 9957:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 9958:                 }
 9959:             }
 9960:         }
 9961:     }
 9962:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 9963:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 9964:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 9965:             $defaultshash{'loadbalancing'} = {};
 9966:         }
 9967:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9968:                                                  \%defaultshash,$dom);
 9969:         if ($putresult eq 'ok') {
 9970:             if (keys(%changes) > 0) {
 9971:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 9972:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 9973:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 9974:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
 9975:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
 9976:                     }
 9977:                 }
 9978:                 if (ref($changes{'add'}) eq 'ARRAY') {
 9979:                     foreach my $balancer (sort(@{$changes{'add'}})) {
 9980:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 9981:                     }
 9982:                 }
 9983:                 if (ref($changes{'curr'}) eq 'HASH') {
 9984:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 9985:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 9986:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 9987:                                 my %offloadstr;
 9988:                                 foreach my $sparetype (@sparestypes) {
 9989:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9990:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9991:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 9992:                                         }
 9993:                                     }
 9994:                                 }
 9995:                                 if (keys(%offloadstr) == 0) {
 9996:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 9997:                                 } else {
 9998:                                     my $showoffload;
 9999:                                     foreach my $sparetype (@sparestypes) {
10000:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
10001:                                         if (defined($offloadstr{$sparetype})) {
10002:                                             $showoffload .= $offloadstr{$sparetype};
10003:                                         } else {
10004:                                             $showoffload .= &mt('None');
10005:                                         }
10006:                                         $showoffload .= ('&nbsp;'x3);
10007:                                     }
10008:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
10009:                                 }
10010:                             }
10011:                         }
10012:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
10013:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
10014:                                 foreach my $type (@{$alltypes}) {
10015:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
10016:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
10017:                                         my $balancetext;
10018:                                         if ($rule eq '') {
10019:                                             $balancetext =  $ruletitles{'default'};
10020:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
10021:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
10022:                                             $balancetext =  $ruletitles{$rule};
10023:                                         } else {
10024:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
10025:                                         }
10026:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
10027:                                     }
10028:                                 }
10029:                             }
10030:                         }
10031:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10032:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10033:                     }
10034:                 }
10035:                 if ($resulttext ne '') {
10036:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
10037:                 } else {
10038:                     $resulttext = $nochgmsg;
10039:                 }
10040:             } else {
10041:                 $resulttext = $nochgmsg;
10042:             }
10043:         } else {
10044:             $resulttext = '<span class="LC_error">'.
10045:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10046:         }
10047:     } else {
10048:         $resulttext = $nochgmsg;
10049:     }
10050:     return $resulttext;
10051: }
10052: 
10053: sub recurse_check {
10054:     my ($chkcats,$categories,$depth,$name) = @_;
10055:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
10056:         my $chg = 0;
10057:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
10058:             my $category = $chkcats->[$depth]{$name}[$j];
10059:             my $item;
10060:             if ($category eq '') {
10061:                 $chg ++;
10062:             } else {
10063:                 my $deeper = $depth + 1;
10064:                 $item = &escape($category).':'.&escape($name).':'.$depth;
10065:                 if ($chg) {
10066:                     $categories->{$item} -= $chg;
10067:                 }
10068:                 &recurse_check($chkcats,$categories,$deeper,$category);
10069:                 $deeper --;
10070:             }
10071:         }
10072:     }
10073:     return;
10074: }
10075: 
10076: sub recurse_cat_deletes {
10077:     my ($item,$coursecategories,$deletions) = @_;
10078:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10079:     my $subdepth = $depth + 1;
10080:     if (ref($coursecategories) eq 'HASH') {
10081:         foreach my $subitem (keys(%{$coursecategories})) {
10082:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
10083:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
10084:                 delete($coursecategories->{$subitem});
10085:                 $deletions->{$subitem} = 1;
10086:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
10087:             }
10088:         }
10089:     }
10090:     return;
10091: }
10092: 
10093: sub get_active_dcs {
10094:     my ($dom) = @_;
10095:     my $now = time;
10096:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
10097:     my %domcoords;
10098:     my $numdcs = 0;
10099:     foreach my $server (keys(%dompersonnel)) {
10100:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
10101:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
10102:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
10103:         }
10104:     }
10105:     return %domcoords;
10106: }
10107: 
10108: sub active_dc_picker {
10109:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
10110:     my %domcoords = &get_active_dcs($dom); 
10111:     my @domcoord = keys(%domcoords);
10112:     if (keys(%currhash)) {
10113:         foreach my $dc (keys(%currhash)) {
10114:             unless (exists($domcoords{$dc})) {
10115:                 push(@domcoord,$dc);
10116:             }
10117:         }
10118:     }
10119:     @domcoord = sort(@domcoord);
10120:     my $numdcs = scalar(@domcoord);
10121:     my $rows = 0;
10122:     my $table;
10123:     if ($numdcs > 1) {
10124:         $table = '<table>';
10125:         for (my $i=0; $i<@domcoord; $i++) {
10126:             my $rem = $i%($numinrow);
10127:             if ($rem == 0) {
10128:                 if ($i > 0) {
10129:                     $table .= '</tr>';
10130:                 }
10131:                 $table .= '<tr>';
10132:                 $rows ++;
10133:             }
10134:             my $check = '';
10135:             if ($inputtype eq 'radio') {
10136:                 if (keys(%currhash) == 0) {
10137:                     if (!$i) {
10138:                         $check = ' checked="checked"';
10139:                     }
10140:                 } elsif (exists($currhash{$domcoord[$i]})) {
10141:                     $check = ' checked="checked"';
10142:                 }
10143:             } else {
10144:                 if (exists($currhash{$domcoord[$i]})) {
10145:                     $check = ' checked="checked"';
10146:                 }
10147:             }
10148:             if ($i == @domcoord - 1) {
10149:                 my $colsleft = $numinrow - $rem;
10150:                 if ($colsleft > 1) {
10151:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
10152:                 } else {
10153:                     $table .= '<td class="LC_left_item">';
10154:                 }
10155:             } else {
10156:                 $table .= '<td class="LC_left_item">';
10157:             }
10158:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
10159:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10160:             $table .= '<span class="LC_nobreak"><label>'.
10161:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
10162:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
10163:             if ($user ne $dcname.':'.$dcdom) {
10164:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10165:             }
10166:             $table .= '</label></span></td>';
10167:         }
10168:         $table .= '</tr></table>';
10169:     } elsif ($numdcs == 1) {
10170:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
10171:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10172:         if ($inputtype eq 'radio') {
10173:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
10174:             if ($user ne $dcname.':'.$dcdom) {
10175:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10176:             }
10177:         } else {
10178:             my $check;
10179:             if (exists($currhash{$domcoord[0]})) {
10180:                 $check = ' checked="checked"';
10181:             }
10182:             $table .= '<span class="LC_nobreak"><label>'.
10183:                       '<input type="checkbox" name="'.$name.'" '.
10184:                       'value="'.$domcoord[0].'"'.$check.' />'.$user;
10185:             if ($user ne $dcname.':'.$dcdom) {
10186:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10187:             }
10188:             $table .= '</label></span>';
10189:             $rows ++;
10190:         }
10191:     }
10192:     return ($numdcs,$table,$rows);
10193: }
10194: 
10195: sub usersession_titles {
10196:     return &Apache::lonlocal::texthash(
10197:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
10198:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
10199:                spares => 'Servers offloaded to, when busy',
10200:                version => 'LON-CAPA version requirement',
10201:                excludedomain => 'Allow all, but exclude specific domains',
10202:                includedomain => 'Deny all, but include specific domains',
10203:                primary => 'Primary (checked first)',
10204:                default => 'Default',
10205:            );
10206: }
10207: 
10208: sub id_for_thisdom {
10209:     my (%servers) = @_;
10210:     my %altids;
10211:     foreach my $server (keys(%servers)) {
10212:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
10213:         if ($serverhome ne $server) {
10214:             $altids{$serverhome} = $server;
10215:         }
10216:     }
10217:     return %altids;
10218: }
10219: 
10220: sub count_servers {
10221:     my ($currbalancer,%servers) = @_;
10222:     my (@spares,$numspares);
10223:     foreach my $lonhost (sort(keys(%servers))) {
10224:         next if ($currbalancer eq $lonhost);
10225:         push(@spares,$lonhost);
10226:     }
10227:     if ($currbalancer) {
10228:         $numspares = scalar(@spares);
10229:     } else {
10230:         $numspares = scalar(@spares) - 1;
10231:     }
10232:     return ($numspares,@spares);
10233: }
10234: 
10235: sub lonbalance_targets_js {
10236:     my ($dom,$types,$servers,$settings) = @_;
10237:     my $select = &mt('Select');
10238:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
10239:     if (ref($servers) eq 'HASH') {
10240:         $alltargets = join("','",sort(keys(%{$servers})));
10241:         my @homedoms;
10242:         foreach my $server (sort(keys(%{$servers}))) {
10243:             if (&Apache::lonnet::host_domain($server) eq $dom) {
10244:                 push(@homedoms,'1');
10245:             } else {
10246:                 push(@homedoms,'0');
10247:             }
10248:         }
10249:         $allishome = join("','",@homedoms);
10250:     }
10251:     if (ref($types) eq 'ARRAY') {
10252:         if (@{$types} > 0) {
10253:             @alltypes = @{$types};
10254:         }
10255:     }
10256:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
10257:     $allinsttypes = join("','",@alltypes);
10258:     my (%currbalancer,%currtargets,%currrules,%existing);
10259:     if (ref($settings) eq 'HASH') {
10260:         %existing = %{$settings};
10261:     }
10262:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
10263:                               \%currtargets,\%currrules);
10264:     my $balancers = join("','",sort(keys(%currbalancer)));
10265:     return <<"END";
10266: 
10267: <script type="text/javascript">
10268: // <![CDATA[
10269: 
10270: currBalancers = new Array('$balancers');
10271: 
10272: function toggleTargets(balnum) {
10273:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
10274:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
10275:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
10276:     var prevbalancer = prevhostitem.value;
10277:     var baltotal = document.getElementById('loadbalancing_total').value;
10278:     prevhostitem.value = balancer;
10279:     if (prevbalancer != '') {
10280:         var prevIdx = currBalancers.indexOf(prevbalancer);
10281:         if (prevIdx != -1) {
10282:             currBalancers.splice(prevIdx,1);
10283:         }
10284:     }
10285:     if (balancer == '') {
10286:         hideSpares(balnum);
10287:     } else {
10288:         var currIdx = currBalancers.indexOf(balancer);
10289:         if (currIdx == -1) {
10290:             currBalancers.push(balancer);
10291:         }
10292:         var homedoms = new Array('$allishome');
10293:         var ishomedom = homedoms[lonhostitem.selectedIndex];
10294:         showSpares(balancer,ishomedom,balnum);
10295:     }
10296:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
10297:     return;
10298: }
10299: 
10300: function showSpares(balancer,ishomedom,balnum) {
10301:     var alltargets = new Array('$alltargets');
10302:     var insttypes = new Array('$allinsttypes');
10303:     var offloadtypes = new Array('primary','default');
10304: 
10305:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
10306:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
10307:  
10308:     for (var i=0; i<offloadtypes.length; i++) {
10309:         var count = 0;
10310:         for (var j=0; j<alltargets.length; j++) {
10311:             if (alltargets[j] != balancer) {
10312:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
10313:                 item.value = alltargets[j];
10314:                 item.style.textAlign='left';
10315:                 item.style.textFace='normal';
10316:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
10317:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
10318:                     item.disabled = '';
10319:                 } else {
10320:                     item.disabled = 'disabled';
10321:                     item.checked = false;
10322:                 }
10323:                 count ++;
10324:             }
10325:         }
10326:     }
10327:     for (var k=0; k<insttypes.length; k++) {
10328:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
10329:             if (ishomedom == 1) {
10330:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
10331:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
10332:             } else {
10333:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
10334:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
10335:             }
10336:         } else {
10337:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
10338:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
10339:         }
10340:         if ((insttypes[k] != '_LC_external') && 
10341:             ((insttypes[k] != '_LC_internetdom') ||
10342:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
10343:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
10344:             item.options.length = 0;
10345:             item.options[0] = new Option("","",true,true);
10346:             var idx = 0;
10347:             for (var m=0; m<alltargets.length; m++) {
10348:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
10349:                     idx ++;
10350:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
10351:                 }
10352:             }
10353:         }
10354:     }
10355:     return;
10356: }
10357: 
10358: function hideSpares(balnum) {
10359:     var alltargets = new Array('$alltargets');
10360:     var insttypes = new Array('$allinsttypes');
10361:     var offloadtypes = new Array('primary','default');
10362: 
10363:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
10364:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
10365: 
10366:     var total = alltargets.length - 1;
10367:     for (var i=0; i<offloadtypes; i++) {
10368:         for (var j=0; j<total; j++) {
10369:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
10370:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
10371:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
10372:         }
10373:     }
10374:     for (var k=0; k<insttypes.length; k++) {
10375:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
10376:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
10377:         if (insttypes[k] != '_LC_external') {
10378:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
10379:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
10380:         }
10381:     }
10382:     return;
10383: }
10384: 
10385: function checkOffloads(item,balnum,type) {
10386:     var alltargets = new Array('$alltargets');
10387:     var offloadtypes = new Array('primary','default');
10388:     if (item.checked) {
10389:         var total = alltargets.length - 1;
10390:         var other;
10391:         if (type == offloadtypes[0]) {
10392:             other = offloadtypes[1];
10393:         } else {
10394:             other = offloadtypes[0];
10395:         }
10396:         for (var i=0; i<total; i++) {
10397:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
10398:             if (server == item.value) {
10399:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
10400:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
10401:                 }
10402:             }
10403:         }
10404:     }
10405:     return;
10406: }
10407: 
10408: function singleServerToggle(balnum,type) {
10409:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
10410:     if (offloadtoSelIdx == 0) {
10411:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
10412:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
10413: 
10414:     } else {
10415:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
10416:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
10417:     }
10418:     return;
10419: }
10420: 
10421: function balanceruleChange(formname,balnum,type) {
10422:     if (type == '_LC_external') {
10423:         return;
10424:     }
10425:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
10426:     for (var i=0; i<typesRules.length; i++) {
10427:         if (formname.elements[typesRules[i]].checked) {
10428:             if (formname.elements[typesRules[i]].value != 'specific') {
10429:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
10430:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
10431:             } else {
10432:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
10433:             }
10434:         }
10435:     }
10436:     return;
10437: }
10438: 
10439: function balancerDeleteChange(balnum) {
10440:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
10441:     var baltotal = document.getElementById('loadbalancing_total').value;
10442:     var addtarget;
10443:     var removetarget;
10444:     var action = 'delete';
10445:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
10446:         var lonhost = hostitem.value;
10447:         var currIdx = currBalancers.indexOf(lonhost);
10448:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
10449:             if (currIdx != -1) {
10450:                 currBalancers.splice(currIdx,1);
10451:             }
10452:             addtarget = lonhost;
10453:         } else {
10454:             if (currIdx == -1) {
10455:                 currBalancers.push(lonhost);
10456:             }
10457:             removetarget = lonhost;
10458:             action = 'undelete';
10459:         }
10460:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
10461:     }
10462:     return;
10463: }
10464: 
10465: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
10466:     if (baltotal > 1) {
10467:         var offloadtypes = new Array('primary','default');
10468:         var alltargets = new Array('$alltargets');
10469:         var insttypes = new Array('$allinsttypes');
10470:         for (var i=0; i<baltotal; i++) {
10471:             if (i != balnum) {
10472:                 for (var j=0; j<offloadtypes.length; j++) {
10473:                     var total = alltargets.length - 1;
10474:                     for (var k=0; k<total; k++) {
10475:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
10476:                         var server = serveritem.value;
10477:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
10478:                             if (server == addtarget) {
10479:                                 serveritem.disabled = '';
10480:                             }
10481:                         }
10482:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
10483:                             if (server == removetarget) {
10484:                                 serveritem.disabled = 'disabled';
10485:                                 serveritem.checked = false;
10486:                             }
10487:                         }
10488:                     }
10489:                 }
10490:                 for (var j=0; j<insttypes.length; j++) {
10491:                     if (insttypes[j] != '_LC_external') {
10492:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
10493:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
10494:                             var currSel = singleserver.selectedIndex;
10495:                             var currVal = singleserver.options[currSel].value;
10496:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
10497:                                 var numoptions = singleserver.options.length;
10498:                                 var needsnew = 1;
10499:                                 for (var k=0; k<numoptions; k++) {
10500:                                     if (singleserver.options[k] == addtarget) {
10501:                                         needsnew = 0;
10502:                                         break;
10503:                                     }
10504:                                 }
10505:                                 if (needsnew == 1) {
10506:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
10507:                                 }
10508:                             }
10509:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
10510:                                 singleserver.options.length = 0;
10511:                                 if ((currVal) && (currVal != removetarget)) {
10512:                                     singleserver.options[0] = new Option("","",false,false);
10513:                                 } else {
10514:                                     singleserver.options[0] = new Option("","",true,true);
10515:                                 }
10516:                                 var idx = 0;
10517:                                 for (var m=0; m<alltargets.length; m++) {
10518:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
10519:                                         idx ++;
10520:                                         if (currVal == alltargets[m]) {
10521:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
10522:                                         } else {
10523:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
10524:                                         }
10525:                                     }
10526:                                 }
10527:                             }
10528:                         }
10529:                     }
10530:                 }
10531:             }
10532:         }
10533:     }
10534:     return;
10535: }
10536: 
10537: // ]]>
10538: </script>
10539: 
10540: END
10541: }
10542: 
10543: sub new_spares_js {
10544:     my @sparestypes = ('primary','default');
10545:     my $types = join("','",@sparestypes);
10546:     my $select = &mt('Select');
10547:     return <<"END";
10548: 
10549: <script type="text/javascript">
10550: // <![CDATA[
10551: 
10552: function updateNewSpares(formname,lonhost) {
10553:     var types = new Array('$types');
10554:     var include = new Array();
10555:     var exclude = new Array();
10556:     for (var i=0; i<types.length; i++) {
10557:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
10558:         for (var j=0; j<spareboxes.length; j++) {
10559:             if (formname.elements[spareboxes[j]].checked) {
10560:                 exclude.push(formname.elements[spareboxes[j]].value);
10561:             } else {
10562:                 include.push(formname.elements[spareboxes[j]].value);
10563:             }
10564:         }
10565:     }
10566:     for (var i=0; i<types.length; i++) {
10567:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
10568:         var selIdx = newSpare.selectedIndex;
10569:         var currnew = newSpare.options[selIdx].value;
10570:         var okSpares = new Array();
10571:         for (var j=0; j<newSpare.options.length; j++) {
10572:             var possible = newSpare.options[j].value;
10573:             if (possible != '') {
10574:                 if (exclude.indexOf(possible) == -1) {
10575:                     okSpares.push(possible);
10576:                 } else {
10577:                     if (currnew == possible) {
10578:                         selIdx = 0;
10579:                     }
10580:                 }
10581:             }
10582:         }
10583:         for (var k=0; k<include.length; k++) {
10584:             if (okSpares.indexOf(include[k]) == -1) {
10585:                 okSpares.push(include[k]);
10586:             }
10587:         }
10588:         okSpares.sort();
10589:         newSpare.options.length = 0;
10590:         if (selIdx == 0) {
10591:             newSpare.options[0] = new Option("$select","",true,true);
10592:         } else {
10593:             newSpare.options[0] = new Option("$select","",false,false);
10594:         }
10595:         for (var m=0; m<okSpares.length; m++) {
10596:             var idx = m+1;
10597:             var selThis = 0;
10598:             if (selIdx != 0) {
10599:                 if (okSpares[m] == currnew) {
10600:                     selThis = 1;
10601:                 }
10602:             }
10603:             if (selThis == 1) {
10604:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
10605:             } else {
10606:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
10607:             }
10608:         }
10609:     }
10610:     return;
10611: }
10612: 
10613: function checkNewSpares(lonhost,type) {
10614:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
10615:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
10616:     if (chosen != '') { 
10617:         var othertype;
10618:         var othernewSpare;
10619:         if (type == 'primary') {
10620:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
10621:         }
10622:         if (type == 'default') {
10623:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
10624:         }
10625:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
10626:             othernewSpare.selectedIndex = 0;
10627:         }
10628:     }
10629:     return;
10630: }
10631: 
10632: // ]]>
10633: </script>
10634: 
10635: END
10636: 
10637: }
10638: 
10639: sub common_domprefs_js {
10640:     return <<"END";
10641: 
10642: <script type="text/javascript">
10643: // <![CDATA[
10644: 
10645: function getIndicesByName(formname,item) {
10646:     var group = new Array();
10647:     for (var i=0;i<formname.elements.length;i++) {
10648:         if (formname.elements[i].name == item) {
10649:             group.push(formname.elements[i].id);
10650:         }
10651:     }
10652:     return group;
10653: }
10654: 
10655: // ]]>
10656: </script>
10657: 
10658: END
10659: 
10660: }
10661: 
10662: sub recaptcha_js {
10663:     my %lt = &captcha_phrases();
10664:     return <<"END";
10665: 
10666: <script type="text/javascript">
10667: // <![CDATA[
10668: 
10669: function updateCaptcha(caller,context) {
10670:     var privitem;
10671:     var pubitem;
10672:     var privtext;
10673:     var pubtext;
10674:     if (document.getElementById(context+'_recaptchapub')) {
10675:         pubitem = document.getElementById(context+'_recaptchapub');
10676:     } else {
10677:         return;
10678:     }
10679:     if (document.getElementById(context+'_recaptchapriv')) {
10680:         privitem = document.getElementById(context+'_recaptchapriv');
10681:     } else {
10682:         return;
10683:     }
10684:     if (document.getElementById(context+'_recaptchapubtxt')) {
10685:         pubtext = document.getElementById(context+'_recaptchapubtxt');
10686:     } else {
10687:         return;
10688:     }
10689:     if (document.getElementById(context+'_recaptchaprivtxt')) {
10690:         privtext = document.getElementById(context+'_recaptchaprivtxt');
10691:     } else {
10692:         return;
10693:     }
10694:     if (caller.checked) {
10695:         if (caller.value == 'recaptcha') {
10696:             pubitem.type = 'text';
10697:             privitem.type = 'text';
10698:             pubitem.size = '40';
10699:             privitem.size = '40';
10700:             pubtext.innerHTML = "$lt{'pub'}";
10701:             privtext.innerHTML = "$lt{'priv'}";
10702:         } else {
10703:             pubitem.type = 'hidden';
10704:             privitem.type = 'hidden';
10705:             pubtext.innerHTML = '';
10706:             privtext.innerHTML = '';
10707:         }
10708:     }
10709:     return;
10710: }
10711: 
10712: // ]]>
10713: </script>
10714: 
10715: END
10716: 
10717: }
10718: 
10719: sub credits_js {
10720:     return <<"END";
10721: 
10722: <script type="text/javascript">
10723: // <![CDATA[
10724: 
10725: function toggleCredits(domForm) {
10726:     if (document.getElementById('credits')) {
10727:         creditsitem = document.getElementById('credits');
10728:         var creditsLength = domForm.coursecredits.length;
10729:         if (creditsLength) {
10730:             var currval;
10731:             for (var i=0; i<creditsLength; i++) {
10732:                 if (domForm.coursecredits[i].checked) {
10733:                    currval = domForm.coursecredits[i].value;
10734:                 }
10735:             }
10736:             if (currval == 1) {
10737:                 creditsitem.style.display = 'block';
10738:             } else {
10739:                 creditsitem.style.display = 'none';
10740:             }
10741:         }
10742:     }
10743:     return;
10744: }
10745: 
10746: // ]]>
10747: </script>
10748: 
10749: END
10750: 
10751: }
10752: 
10753: sub captcha_phrases {
10754:     return &Apache::lonlocal::texthash (
10755:                  priv => 'Private key',
10756:                  pub  => 'Public key',
10757:                  original  => 'original (CAPTCHA)',
10758:                  recaptcha => 'successor (ReCAPTCHA)',
10759:                  notused   => 'unused',
10760:     );
10761: }
10762: 
10763: sub devalidate_remote_domconfs {
10764:     my ($dom,$cachekeys) = @_;
10765:     return unless (ref($cachekeys) eq 'HASH');
10766:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10767:     my %thismachine;
10768:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
10769:     my @posscached = ('domainconfig','domdefaults');
10770:     if (keys(%servers) > 1) {
10771:         foreach my $server (keys(%servers)) {
10772:             next if ($thismachine{$server});
10773:             my @cached;
10774:             foreach my $name (@posscached) {
10775:                 if ($cachekeys->{$name}) {
10776:                     push(@cached,&escape($name).':'.&escape($dom));
10777:                 }
10778:             }
10779:             if (@cached) {
10780:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
10781:             }
10782:         }
10783:     }
10784:     return;
10785: }
10786: 
10787: 1;

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